mov.c 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367
  1. /*
  2. * MOV demuxer
  3. * Copyright (c) 2001 Fabrice Bellard
  4. * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <limits.h>
  23. //#define DEBUG
  24. //#define MOV_EXPORT_ALL_METADATA
  25. #include "libavutil/attributes.h"
  26. #include "libavutil/audioconvert.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/intfloat.h"
  29. #include "libavutil/mathematics.h"
  30. #include "libavutil/avstring.h"
  31. #include "libavutil/dict.h"
  32. #include "libavutil/opt.h"
  33. #include "libavutil/timecode.h"
  34. #include "libavcodec/ac3tab.h"
  35. #include "avformat.h"
  36. #include "internal.h"
  37. #include "avio_internal.h"
  38. #include "riff.h"
  39. #include "isom.h"
  40. #include "libavcodec/get_bits.h"
  41. #include "id3v1.h"
  42. #include "mov_chan.h"
  43. #if CONFIG_ZLIB
  44. #include <zlib.h>
  45. #endif
  46. /*
  47. * First version by Francois Revol revol@free.fr
  48. * Seek function by Gael Chardon gael.dev@4now.net
  49. */
  50. #include "qtpalette.h"
  51. #undef NDEBUG
  52. #include <assert.h>
  53. /* those functions parse an atom */
  54. /* links atom IDs to parse functions */
  55. typedef struct MOVParseTableEntry {
  56. uint32_t type;
  57. int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
  58. } MOVParseTableEntry;
  59. static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom);
  60. static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
  61. unsigned len, const char *key)
  62. {
  63. char buf[16];
  64. short current, total = 0;
  65. avio_rb16(pb); // unknown
  66. current = avio_rb16(pb);
  67. if (len >= 6)
  68. total = avio_rb16(pb);
  69. if (!total)
  70. snprintf(buf, sizeof(buf), "%d", current);
  71. else
  72. snprintf(buf, sizeof(buf), "%d/%d", current, total);
  73. av_dict_set(&c->fc->metadata, key, buf, 0);
  74. return 0;
  75. }
  76. static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
  77. unsigned len, const char *key)
  78. {
  79. char buf[16];
  80. /* bypass padding bytes */
  81. avio_r8(pb);
  82. avio_r8(pb);
  83. avio_r8(pb);
  84. snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
  85. av_dict_set(&c->fc->metadata, key, buf, 0);
  86. return 0;
  87. }
  88. static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
  89. unsigned len, const char *key)
  90. {
  91. char buf[16];
  92. snprintf(buf, sizeof(buf), "%d", avio_r8(pb));
  93. av_dict_set(&c->fc->metadata, key, buf, 0);
  94. return 0;
  95. }
  96. static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb,
  97. unsigned len, const char *key)
  98. {
  99. short genre;
  100. char buf[20];
  101. avio_r8(pb); // unknown
  102. genre = avio_r8(pb);
  103. if (genre < 1 || genre > ID3v1_GENRE_MAX)
  104. return 0;
  105. snprintf(buf, sizeof(buf), "%s", ff_id3v1_genre_str[genre-1]);
  106. av_dict_set(&c->fc->metadata, key, buf, 0);
  107. return 0;
  108. }
  109. static int mov_read_custom_metadata(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  110. {
  111. char key[1024]={0}, data[1024]={0};
  112. int i;
  113. AVStream *st;
  114. MOVStreamContext *sc;
  115. if (c->fc->nb_streams < 1)
  116. return 0;
  117. st = c->fc->streams[c->fc->nb_streams-1];
  118. sc = st->priv_data;
  119. if (atom.size <= 8) return 0;
  120. for (i = 0; i < 3; i++) { // Parse up to three sub-atoms looking for name and data.
  121. int data_size = avio_rb32(pb);
  122. int tag = avio_rl32(pb);
  123. int str_size = 0, skip_size = 0;
  124. char *target = NULL;
  125. switch (tag) {
  126. case MKTAG('n','a','m','e'):
  127. avio_rb32(pb); // version/flags
  128. str_size = skip_size = data_size - 12;
  129. atom.size -= 12;
  130. target = key;
  131. break;
  132. case MKTAG('d','a','t','a'):
  133. avio_rb32(pb); // version/flags
  134. avio_rb32(pb); // reserved (zero)
  135. str_size = skip_size = data_size - 16;
  136. atom.size -= 16;
  137. target = data;
  138. break;
  139. default:
  140. skip_size = data_size - 8;
  141. str_size = 0;
  142. break;
  143. }
  144. if (target) {
  145. str_size = FFMIN3(sizeof(data)-1, str_size, atom.size);
  146. avio_read(pb, target, str_size);
  147. target[str_size] = 0;
  148. }
  149. atom.size -= skip_size;
  150. // If we didn't read the full data chunk for the sub-atom, skip to the end of it.
  151. if (skip_size > str_size) avio_skip(pb, skip_size - str_size);
  152. }
  153. if (*key && *data) {
  154. if (strcmp(key, "iTunSMPB") == 0) {
  155. int priming, remainder, samples;
  156. if(sscanf(data, "%*X %X %X %X", &priming, &remainder, &samples) == 3){
  157. if(priming>0 && priming<16384)
  158. sc->start_pad = priming;
  159. return 1;
  160. }
  161. }
  162. if (strcmp(key, "cdec") == 0) {
  163. // av_dict_set(&st->metadata, key, data, 0);
  164. return 1;
  165. }
  166. }
  167. return 0;
  168. }
  169. static const uint32_t mac_to_unicode[128] = {
  170. 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
  171. 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
  172. 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
  173. 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
  174. 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
  175. 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
  176. 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
  177. 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
  178. 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
  179. 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
  180. 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
  181. 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
  182. 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
  183. 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
  184. 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
  185. 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
  186. };
  187. static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
  188. char *dst, int dstlen)
  189. {
  190. char *p = dst;
  191. char *end = dst+dstlen-1;
  192. int i;
  193. for (i = 0; i < len; i++) {
  194. uint8_t t, c = avio_r8(pb);
  195. if (c < 0x80 && p < end)
  196. *p++ = c;
  197. else if (p < end)
  198. PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
  199. }
  200. *p = 0;
  201. return p - dst;
  202. }
  203. static int mov_read_covr(MOVContext *c, AVIOContext *pb, int type, int len)
  204. {
  205. AVPacket pkt;
  206. AVStream *st;
  207. MOVStreamContext *sc;
  208. enum AVCodecID id;
  209. int ret;
  210. switch (type) {
  211. case 0xd: id = AV_CODEC_ID_MJPEG; break;
  212. case 0xe: id = AV_CODEC_ID_PNG; break;
  213. case 0x1b: id = AV_CODEC_ID_BMP; break;
  214. default:
  215. av_log(c->fc, AV_LOG_WARNING, "Unknown cover type: 0x%x.\n", type);
  216. avio_skip(pb, len);
  217. return 0;
  218. }
  219. st = avformat_new_stream(c->fc, NULL);
  220. if (!st)
  221. return AVERROR(ENOMEM);
  222. sc = av_mallocz(sizeof(*sc));
  223. if (!sc)
  224. return AVERROR(ENOMEM);
  225. st->priv_data = sc;
  226. ret = av_get_packet(pb, &pkt, len);
  227. if (ret < 0)
  228. return ret;
  229. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  230. st->attached_pic = pkt;
  231. st->attached_pic.stream_index = st->index;
  232. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  233. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  234. st->codec->codec_id = id;
  235. return 0;
  236. }
  237. static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  238. {
  239. #ifdef MOV_EXPORT_ALL_METADATA
  240. char tmp_key[5];
  241. #endif
  242. char str[1024], key2[16], language[4] = {0};
  243. const char *key = NULL;
  244. uint16_t langcode = 0;
  245. uint32_t data_type = 0, str_size;
  246. int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
  247. if (c->itunes_metadata && atom.type == MKTAG('-','-','-','-'))
  248. return mov_read_custom_metadata(c, pb, atom);
  249. switch (atom.type) {
  250. case MKTAG(0xa9,'n','a','m'): key = "title"; break;
  251. case MKTAG(0xa9,'a','u','t'):
  252. case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
  253. case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
  254. case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
  255. case MKTAG( 'c','p','r','t'):
  256. case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
  257. case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
  258. case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
  259. case MKTAG(0xa9,'c','m','t'):
  260. case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
  261. case MKTAG(0xa9,'a','l','b'): key = "album"; break;
  262. case MKTAG(0xa9,'d','a','y'): key = "date"; break;
  263. case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
  264. case MKTAG( 'g','n','r','e'): key = "genre";
  265. parse = mov_metadata_gnre; break;
  266. case MKTAG(0xa9,'t','o','o'):
  267. case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
  268. case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
  269. case MKTAG( 'd','e','s','c'): key = "description";break;
  270. case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
  271. case MKTAG( 't','v','s','h'): key = "show"; break;
  272. case MKTAG( 't','v','e','n'): key = "episode_id";break;
  273. case MKTAG( 't','v','n','n'): key = "network"; break;
  274. case MKTAG( 't','r','k','n'): key = "track";
  275. parse = mov_metadata_track_or_disc_number; break;
  276. case MKTAG( 'd','i','s','k'): key = "disc";
  277. parse = mov_metadata_track_or_disc_number; break;
  278. case MKTAG( 't','v','e','s'): key = "episode_sort";
  279. parse = mov_metadata_int8_bypass_padding; break;
  280. case MKTAG( 't','v','s','n'): key = "season_number";
  281. parse = mov_metadata_int8_bypass_padding; break;
  282. case MKTAG( 's','t','i','k'): key = "media_type";
  283. parse = mov_metadata_int8_no_padding; break;
  284. case MKTAG( 'h','d','v','d'): key = "hd_video";
  285. parse = mov_metadata_int8_no_padding; break;
  286. case MKTAG( 'p','g','a','p'): key = "gapless_playback";
  287. parse = mov_metadata_int8_no_padding; break;
  288. }
  289. if (c->itunes_metadata && atom.size > 8) {
  290. int data_size = avio_rb32(pb);
  291. int tag = avio_rl32(pb);
  292. if (tag == MKTAG('d','a','t','a')) {
  293. data_type = avio_rb32(pb); // type
  294. avio_rb32(pb); // unknown
  295. str_size = data_size - 16;
  296. atom.size -= 16;
  297. if (atom.type == MKTAG('c', 'o', 'v', 'r')) {
  298. int ret = mov_read_covr(c, pb, data_type, str_size);
  299. if (ret < 0) {
  300. av_log(c->fc, AV_LOG_ERROR, "Error parsing cover art.\n");
  301. return ret;
  302. }
  303. }
  304. } else return 0;
  305. } else if (atom.size > 4 && key && !c->itunes_metadata) {
  306. str_size = avio_rb16(pb); // string length
  307. langcode = avio_rb16(pb);
  308. ff_mov_lang_to_iso639(langcode, language);
  309. atom.size -= 4;
  310. } else
  311. str_size = atom.size;
  312. #ifdef MOV_EXPORT_ALL_METADATA
  313. if (!key) {
  314. snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
  315. key = tmp_key;
  316. }
  317. #endif
  318. if (!key)
  319. return 0;
  320. if (atom.size < 0)
  321. return AVERROR_INVALIDDATA;
  322. str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
  323. if (parse)
  324. parse(c, pb, str_size, key);
  325. else {
  326. if (data_type == 3 || (data_type == 0 && (langcode < 0x400 || langcode == 0x7fff))) { // MAC Encoded
  327. mov_read_mac_string(c, pb, str_size, str, sizeof(str));
  328. } else {
  329. avio_read(pb, str, str_size);
  330. str[str_size] = 0;
  331. }
  332. av_dict_set(&c->fc->metadata, key, str, 0);
  333. if (*language && strcmp(language, "und")) {
  334. snprintf(key2, sizeof(key2), "%s-%s", key, language);
  335. av_dict_set(&c->fc->metadata, key2, str, 0);
  336. }
  337. }
  338. av_dlog(c->fc, "lang \"%3s\" ", language);
  339. av_dlog(c->fc, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
  340. key, str, (char*)&atom.type, str_size, atom.size);
  341. return 0;
  342. }
  343. static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  344. {
  345. int64_t start;
  346. int i, nb_chapters, str_len, version;
  347. char str[256+1];
  348. if ((atom.size -= 5) < 0)
  349. return 0;
  350. version = avio_r8(pb);
  351. avio_rb24(pb);
  352. if (version)
  353. avio_rb32(pb); // ???
  354. nb_chapters = avio_r8(pb);
  355. for (i = 0; i < nb_chapters; i++) {
  356. if (atom.size < 9)
  357. return 0;
  358. start = avio_rb64(pb);
  359. str_len = avio_r8(pb);
  360. if ((atom.size -= 9+str_len) < 0)
  361. return 0;
  362. avio_read(pb, str, str_len);
  363. str[str_len] = 0;
  364. avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
  365. }
  366. return 0;
  367. }
  368. static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  369. {
  370. AVStream *st;
  371. MOVStreamContext *sc;
  372. int entries, i, j;
  373. if (c->fc->nb_streams < 1)
  374. return 0;
  375. st = c->fc->streams[c->fc->nb_streams-1];
  376. sc = st->priv_data;
  377. avio_rb32(pb); // version + flags
  378. entries = avio_rb32(pb);
  379. if (entries >= UINT_MAX / sizeof(*sc->drefs))
  380. return AVERROR_INVALIDDATA;
  381. av_free(sc->drefs);
  382. sc->drefs_count = 0;
  383. sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
  384. if (!sc->drefs)
  385. return AVERROR(ENOMEM);
  386. sc->drefs_count = entries;
  387. for (i = 0; i < sc->drefs_count; i++) {
  388. MOVDref *dref = &sc->drefs[i];
  389. uint32_t size = avio_rb32(pb);
  390. int64_t next = avio_tell(pb) + size - 4;
  391. if (size < 12)
  392. return AVERROR_INVALIDDATA;
  393. dref->type = avio_rl32(pb);
  394. avio_rb32(pb); // version + flags
  395. av_dlog(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
  396. if (dref->type == MKTAG('a','l','i','s') && size > 150) {
  397. /* macintosh alias record */
  398. uint16_t volume_len, len;
  399. int16_t type;
  400. avio_skip(pb, 10);
  401. volume_len = avio_r8(pb);
  402. volume_len = FFMIN(volume_len, 27);
  403. avio_read(pb, dref->volume, 27);
  404. dref->volume[volume_len] = 0;
  405. av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
  406. avio_skip(pb, 12);
  407. len = avio_r8(pb);
  408. len = FFMIN(len, 63);
  409. avio_read(pb, dref->filename, 63);
  410. dref->filename[len] = 0;
  411. av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
  412. avio_skip(pb, 16);
  413. /* read next level up_from_alias/down_to_target */
  414. dref->nlvl_from = avio_rb16(pb);
  415. dref->nlvl_to = avio_rb16(pb);
  416. av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
  417. dref->nlvl_from, dref->nlvl_to);
  418. avio_skip(pb, 16);
  419. for (type = 0; type != -1 && avio_tell(pb) < next; ) {
  420. if(url_feof(pb))
  421. return AVERROR_EOF;
  422. type = avio_rb16(pb);
  423. len = avio_rb16(pb);
  424. av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
  425. if (len&1)
  426. len += 1;
  427. if (type == 2) { // absolute path
  428. av_free(dref->path);
  429. dref->path = av_mallocz(len+1);
  430. if (!dref->path)
  431. return AVERROR(ENOMEM);
  432. avio_read(pb, dref->path, len);
  433. if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
  434. len -= volume_len;
  435. memmove(dref->path, dref->path+volume_len, len);
  436. dref->path[len] = 0;
  437. }
  438. for (j = 0; j < len; j++)
  439. if (dref->path[j] == ':')
  440. dref->path[j] = '/';
  441. av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
  442. } else if (type == 0) { // directory name
  443. av_free(dref->dir);
  444. dref->dir = av_malloc(len+1);
  445. if (!dref->dir)
  446. return AVERROR(ENOMEM);
  447. avio_read(pb, dref->dir, len);
  448. dref->dir[len] = 0;
  449. for (j = 0; j < len; j++)
  450. if (dref->dir[j] == ':')
  451. dref->dir[j] = '/';
  452. av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
  453. } else
  454. avio_skip(pb, len);
  455. }
  456. }
  457. avio_seek(pb, next, SEEK_SET);
  458. }
  459. return 0;
  460. }
  461. static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  462. {
  463. AVStream *st;
  464. uint32_t type;
  465. uint32_t av_unused ctype;
  466. int title_size;
  467. char *title_str;
  468. if (c->fc->nb_streams < 1) // meta before first trak
  469. return 0;
  470. st = c->fc->streams[c->fc->nb_streams-1];
  471. avio_r8(pb); /* version */
  472. avio_rb24(pb); /* flags */
  473. /* component type */
  474. ctype = avio_rl32(pb);
  475. type = avio_rl32(pb); /* component subtype */
  476. av_dlog(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
  477. av_dlog(c->fc, "stype= %.4s\n", (char*)&type);
  478. if (type == MKTAG('v','i','d','e'))
  479. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  480. else if (type == MKTAG('s','o','u','n'))
  481. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  482. else if (type == MKTAG('m','1','a',' '))
  483. st->codec->codec_id = AV_CODEC_ID_MP2;
  484. else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
  485. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  486. avio_rb32(pb); /* component manufacture */
  487. avio_rb32(pb); /* component flags */
  488. avio_rb32(pb); /* component flags mask */
  489. title_size = atom.size - 24;
  490. if (title_size > 0) {
  491. title_str = av_malloc(title_size + 1); /* Add null terminator */
  492. if (!title_str)
  493. return AVERROR(ENOMEM);
  494. avio_read(pb, title_str, title_size);
  495. title_str[title_size] = 0;
  496. if (title_str[0])
  497. av_dict_set(&st->metadata, "handler_name", title_str +
  498. (!c->isom && title_str[0] == title_size - 1), 0);
  499. av_freep(&title_str);
  500. }
  501. return 0;
  502. }
  503. int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb, MOVAtom atom)
  504. {
  505. AVStream *st;
  506. int tag;
  507. if (fc->nb_streams < 1)
  508. return 0;
  509. st = fc->streams[fc->nb_streams-1];
  510. avio_rb32(pb); /* version + flags */
  511. ff_mp4_read_descr(fc, pb, &tag);
  512. if (tag == MP4ESDescrTag) {
  513. ff_mp4_parse_es_descr(pb, NULL);
  514. } else
  515. avio_rb16(pb); /* ID */
  516. ff_mp4_read_descr(fc, pb, &tag);
  517. if (tag == MP4DecConfigDescrTag)
  518. ff_mp4_read_dec_config_descr(fc, st, pb);
  519. return 0;
  520. }
  521. static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  522. {
  523. return ff_mov_read_esds(c->fc, pb, atom);
  524. }
  525. static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  526. {
  527. AVStream *st;
  528. int ac3info, acmod, lfeon, bsmod;
  529. if (c->fc->nb_streams < 1)
  530. return 0;
  531. st = c->fc->streams[c->fc->nb_streams-1];
  532. ac3info = avio_rb24(pb);
  533. bsmod = (ac3info >> 14) & 0x7;
  534. acmod = (ac3info >> 11) & 0x7;
  535. lfeon = (ac3info >> 10) & 0x1;
  536. st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
  537. st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
  538. if (lfeon)
  539. st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
  540. st->codec->audio_service_type = bsmod;
  541. if (st->codec->channels > 1 && bsmod == 0x7)
  542. st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
  543. return 0;
  544. }
  545. static int mov_read_dec3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  546. {
  547. AVStream *st;
  548. int eac3info, acmod, lfeon, bsmod;
  549. if (c->fc->nb_streams < 1)
  550. return 0;
  551. st = c->fc->streams[c->fc->nb_streams-1];
  552. /* No need to parse fields for additional independent substreams and its
  553. * associated dependent substreams since libavcodec's E-AC-3 decoder
  554. * does not support them yet. */
  555. avio_rb16(pb); /* data_rate and num_ind_sub */
  556. eac3info = avio_rb24(pb);
  557. bsmod = (eac3info >> 12) & 0x1f;
  558. acmod = (eac3info >> 9) & 0x7;
  559. lfeon = (eac3info >> 8) & 0x1;
  560. st->codec->channel_layout = avpriv_ac3_channel_layout_tab[acmod];
  561. if (lfeon)
  562. st->codec->channel_layout |= AV_CH_LOW_FREQUENCY;
  563. st->codec->channels = av_get_channel_layout_nb_channels(st->codec->channel_layout);
  564. st->codec->audio_service_type = bsmod;
  565. if (st->codec->channels > 1 && bsmod == 0x7)
  566. st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
  567. return 0;
  568. }
  569. static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  570. {
  571. AVStream *st;
  572. if (c->fc->nb_streams < 1)
  573. return 0;
  574. st = c->fc->streams[c->fc->nb_streams-1];
  575. if (atom.size < 16)
  576. return 0;
  577. /* skip version and flags */
  578. avio_skip(pb, 4);
  579. ff_mov_read_chan(c->fc, pb, st, atom.size - 4);
  580. return 0;
  581. }
  582. static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  583. {
  584. AVStream *st;
  585. if (c->fc->nb_streams < 1)
  586. return 0;
  587. st = c->fc->streams[c->fc->nb_streams-1];
  588. ff_get_wav_header(pb, st->codec, atom.size);
  589. return 0;
  590. }
  591. static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  592. {
  593. const int num = avio_rb32(pb);
  594. const int den = avio_rb32(pb);
  595. AVStream *st;
  596. if (c->fc->nb_streams < 1)
  597. return 0;
  598. st = c->fc->streams[c->fc->nb_streams-1];
  599. if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
  600. (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
  601. av_log(c->fc, AV_LOG_WARNING,
  602. "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
  603. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  604. num, den);
  605. } else if (den != 0) {
  606. st->sample_aspect_ratio.num = num;
  607. st->sample_aspect_ratio.den = den;
  608. }
  609. return 0;
  610. }
  611. /* this atom contains actual media data */
  612. static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  613. {
  614. if (atom.size == 0) /* wrong one (MP4) */
  615. return 0;
  616. c->found_mdat=1;
  617. return 0; /* now go for moov */
  618. }
  619. /* read major brand, minor version and compatible brands and store them as metadata */
  620. static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  621. {
  622. uint32_t minor_ver;
  623. int comp_brand_size;
  624. char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */
  625. char* comp_brands_str;
  626. uint8_t type[5] = {0};
  627. avio_read(pb, type, 4);
  628. if (strcmp(type, "qt "))
  629. c->isom = 1;
  630. av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
  631. av_dict_set(&c->fc->metadata, "major_brand", type, 0);
  632. minor_ver = avio_rb32(pb); /* minor version */
  633. snprintf(minor_ver_str, sizeof(minor_ver_str), "%d", minor_ver);
  634. av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0);
  635. comp_brand_size = atom.size - 8;
  636. if (comp_brand_size < 0)
  637. return AVERROR_INVALIDDATA;
  638. comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
  639. if (!comp_brands_str)
  640. return AVERROR(ENOMEM);
  641. avio_read(pb, comp_brands_str, comp_brand_size);
  642. comp_brands_str[comp_brand_size] = 0;
  643. av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
  644. av_freep(&comp_brands_str);
  645. return 0;
  646. }
  647. /* this atom should contain all header atoms */
  648. static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  649. {
  650. int ret;
  651. if ((ret = mov_read_default(c, pb, atom)) < 0)
  652. return ret;
  653. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  654. /* so we don't parse the whole file if over a network */
  655. c->found_moov=1;
  656. return 0; /* now go for mdat */
  657. }
  658. static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  659. {
  660. c->fragment.moof_offset = avio_tell(pb) - 8;
  661. av_dlog(c->fc, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
  662. return mov_read_default(c, pb, atom);
  663. }
  664. static void mov_metadata_creation_time(AVDictionary **metadata, int64_t time)
  665. {
  666. char buffer[32];
  667. if (time) {
  668. struct tm *ptm;
  669. time_t timet;
  670. if(time >= 2082844800)
  671. time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
  672. timet = time;
  673. ptm = gmtime(&timet);
  674. if (!ptm) return;
  675. strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm);
  676. av_dict_set(metadata, "creation_time", buffer, 0);
  677. }
  678. }
  679. static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  680. {
  681. AVStream *st;
  682. MOVStreamContext *sc;
  683. int version;
  684. char language[4] = {0};
  685. unsigned lang;
  686. int64_t creation_time;
  687. if (c->fc->nb_streams < 1)
  688. return 0;
  689. st = c->fc->streams[c->fc->nb_streams-1];
  690. sc = st->priv_data;
  691. version = avio_r8(pb);
  692. if (version > 1) {
  693. av_log_ask_for_sample(c, "unsupported version %d\n", version);
  694. return AVERROR_PATCHWELCOME;
  695. }
  696. avio_rb24(pb); /* flags */
  697. if (version == 1) {
  698. creation_time = avio_rb64(pb);
  699. avio_rb64(pb);
  700. } else {
  701. creation_time = avio_rb32(pb);
  702. avio_rb32(pb); /* modification time */
  703. }
  704. mov_metadata_creation_time(&st->metadata, creation_time);
  705. sc->time_scale = avio_rb32(pb);
  706. st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  707. lang = avio_rb16(pb); /* language */
  708. if (ff_mov_lang_to_iso639(lang, language))
  709. av_dict_set(&st->metadata, "language", language, 0);
  710. avio_rb16(pb); /* quality */
  711. return 0;
  712. }
  713. static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  714. {
  715. int64_t creation_time;
  716. int version = avio_r8(pb); /* version */
  717. avio_rb24(pb); /* flags */
  718. if (version == 1) {
  719. creation_time = avio_rb64(pb);
  720. avio_rb64(pb);
  721. } else {
  722. creation_time = avio_rb32(pb);
  723. avio_rb32(pb); /* modification time */
  724. }
  725. mov_metadata_creation_time(&c->fc->metadata, creation_time);
  726. c->time_scale = avio_rb32(pb); /* time scale */
  727. av_dlog(c->fc, "time scale = %i\n", c->time_scale);
  728. c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  729. // set the AVCodecContext duration because the duration of individual tracks
  730. // may be inaccurate
  731. if (c->time_scale > 0 && !c->trex_data)
  732. c->fc->duration = av_rescale(c->duration, AV_TIME_BASE, c->time_scale);
  733. avio_rb32(pb); /* preferred scale */
  734. avio_rb16(pb); /* preferred volume */
  735. avio_skip(pb, 10); /* reserved */
  736. avio_skip(pb, 36); /* display matrix */
  737. avio_rb32(pb); /* preview time */
  738. avio_rb32(pb); /* preview duration */
  739. avio_rb32(pb); /* poster time */
  740. avio_rb32(pb); /* selection time */
  741. avio_rb32(pb); /* selection duration */
  742. avio_rb32(pb); /* current time */
  743. avio_rb32(pb); /* next track ID */
  744. return 0;
  745. }
  746. static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  747. {
  748. AVStream *st;
  749. int little_endian;
  750. if (c->fc->nb_streams < 1)
  751. return 0;
  752. st = c->fc->streams[c->fc->nb_streams-1];
  753. little_endian = avio_rb16(pb) & 0xFF;
  754. av_dlog(c->fc, "enda %d\n", little_endian);
  755. if (little_endian == 1) {
  756. switch (st->codec->codec_id) {
  757. case AV_CODEC_ID_PCM_S24BE:
  758. st->codec->codec_id = AV_CODEC_ID_PCM_S24LE;
  759. break;
  760. case AV_CODEC_ID_PCM_S32BE:
  761. st->codec->codec_id = AV_CODEC_ID_PCM_S32LE;
  762. break;
  763. case AV_CODEC_ID_PCM_F32BE:
  764. st->codec->codec_id = AV_CODEC_ID_PCM_F32LE;
  765. break;
  766. case AV_CODEC_ID_PCM_F64BE:
  767. st->codec->codec_id = AV_CODEC_ID_PCM_F64LE;
  768. break;
  769. default:
  770. break;
  771. }
  772. }
  773. return 0;
  774. }
  775. static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  776. {
  777. AVStream *st;
  778. unsigned mov_field_order;
  779. enum AVFieldOrder decoded_field_order = AV_FIELD_UNKNOWN;
  780. if (c->fc->nb_streams < 1) // will happen with jp2 files
  781. return 0;
  782. st = c->fc->streams[c->fc->nb_streams-1];
  783. if (atom.size < 2)
  784. return AVERROR_INVALIDDATA;
  785. mov_field_order = avio_rb16(pb);
  786. if ((mov_field_order & 0xFF00) == 0x0100)
  787. decoded_field_order = AV_FIELD_PROGRESSIVE;
  788. else if ((mov_field_order & 0xFF00) == 0x0200) {
  789. switch (mov_field_order & 0xFF) {
  790. case 0x01: decoded_field_order = AV_FIELD_TT;
  791. break;
  792. case 0x06: decoded_field_order = AV_FIELD_BB;
  793. break;
  794. case 0x09: decoded_field_order = AV_FIELD_TB;
  795. break;
  796. case 0x0E: decoded_field_order = AV_FIELD_BT;
  797. break;
  798. }
  799. }
  800. if (decoded_field_order == AV_FIELD_UNKNOWN && mov_field_order) {
  801. av_log(NULL, AV_LOG_ERROR, "Unknown MOV field order 0x%04x\n", mov_field_order);
  802. }
  803. st->codec->field_order = decoded_field_order;
  804. return 0;
  805. }
  806. /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
  807. static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
  808. enum AVCodecID codec_id)
  809. {
  810. AVStream *st;
  811. uint64_t size;
  812. uint8_t *buf;
  813. if (c->fc->nb_streams < 1) // will happen with jp2 files
  814. return 0;
  815. st= c->fc->streams[c->fc->nb_streams-1];
  816. if (st->codec->codec_id != codec_id)
  817. return 0; /* unexpected codec_id - don't mess with extradata */
  818. size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
  819. if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
  820. return AVERROR_INVALIDDATA;
  821. buf= av_realloc(st->codec->extradata, size);
  822. if (!buf)
  823. return AVERROR(ENOMEM);
  824. st->codec->extradata= buf;
  825. buf+= st->codec->extradata_size;
  826. st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
  827. AV_WB32( buf , atom.size + 8);
  828. AV_WL32( buf + 4, atom.type);
  829. avio_read(pb, buf + 8, atom.size);
  830. return 0;
  831. }
  832. /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
  833. static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  834. {
  835. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_ALAC);
  836. }
  837. static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  838. {
  839. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVS);
  840. }
  841. static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  842. {
  843. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_JPEG2000);
  844. }
  845. static int mov_read_avid(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  846. {
  847. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_AVUI);
  848. }
  849. static int mov_read_svq3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  850. {
  851. return mov_read_extradata(c, pb, atom, AV_CODEC_ID_SVQ3);
  852. }
  853. static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  854. {
  855. AVStream *st;
  856. if (c->fc->nb_streams < 1)
  857. return 0;
  858. st = c->fc->streams[c->fc->nb_streams-1];
  859. if ((uint64_t)atom.size > (1<<30))
  860. return AVERROR_INVALIDDATA;
  861. if (st->codec->codec_id == AV_CODEC_ID_QDM2 || st->codec->codec_id == AV_CODEC_ID_QDMC) {
  862. // pass all frma atom to codec, needed at least for QDMC and QDM2
  863. av_free(st->codec->extradata);
  864. st->codec->extradata_size = 0;
  865. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  866. if (!st->codec->extradata)
  867. return AVERROR(ENOMEM);
  868. st->codec->extradata_size = atom.size;
  869. avio_read(pb, st->codec->extradata, atom.size);
  870. } else if (atom.size > 8) { /* to read frma, esds atoms */
  871. int ret;
  872. if ((ret = mov_read_default(c, pb, atom)) < 0)
  873. return ret;
  874. } else
  875. avio_skip(pb, atom.size);
  876. return 0;
  877. }
  878. /**
  879. * This function reads atom content and puts data in extradata without tag
  880. * nor size unlike mov_read_extradata.
  881. */
  882. static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  883. {
  884. AVStream *st;
  885. if (c->fc->nb_streams < 1)
  886. return 0;
  887. st = c->fc->streams[c->fc->nb_streams-1];
  888. if ((uint64_t)atom.size > (1<<30))
  889. return AVERROR_INVALIDDATA;
  890. if (atom.size >= 10) {
  891. // Broken files created by legacy versions of libavformat will
  892. // wrap a whole fiel atom inside of a glbl atom.
  893. unsigned size = avio_rb32(pb);
  894. unsigned type = avio_rl32(pb);
  895. avio_seek(pb, -8, SEEK_CUR);
  896. if (type == MKTAG('f','i','e','l') && size == atom.size)
  897. return mov_read_default(c, pb, atom);
  898. }
  899. av_free(st->codec->extradata);
  900. st->codec->extradata_size = 0;
  901. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  902. if (!st->codec->extradata)
  903. return AVERROR(ENOMEM);
  904. st->codec->extradata_size = atom.size;
  905. avio_read(pb, st->codec->extradata, atom.size);
  906. return 0;
  907. }
  908. static int mov_read_dvc1(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  909. {
  910. AVStream *st;
  911. uint8_t profile_level;
  912. if (c->fc->nb_streams < 1)
  913. return 0;
  914. st = c->fc->streams[c->fc->nb_streams-1];
  915. if (atom.size >= (1<<28) || atom.size < 7)
  916. return AVERROR_INVALIDDATA;
  917. profile_level = avio_r8(pb);
  918. if ((profile_level & 0xf0) != 0xc0)
  919. return 0;
  920. av_free(st->codec->extradata);
  921. st->codec->extradata_size = 0;
  922. st->codec->extradata = av_mallocz(atom.size - 7 + FF_INPUT_BUFFER_PADDING_SIZE);
  923. if (!st->codec->extradata)
  924. return AVERROR(ENOMEM);
  925. st->codec->extradata_size = atom.size - 7;
  926. avio_seek(pb, 6, SEEK_CUR);
  927. avio_read(pb, st->codec->extradata, st->codec->extradata_size);
  928. return 0;
  929. }
  930. /**
  931. * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
  932. * but can have extradata appended at the end after the 40 bytes belonging
  933. * to the struct.
  934. */
  935. static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  936. {
  937. AVStream *st;
  938. if (c->fc->nb_streams < 1)
  939. return 0;
  940. if (atom.size <= 40)
  941. return 0;
  942. st = c->fc->streams[c->fc->nb_streams-1];
  943. if ((uint64_t)atom.size > (1<<30))
  944. return AVERROR_INVALIDDATA;
  945. av_free(st->codec->extradata);
  946. st->codec->extradata_size = 0;
  947. st->codec->extradata = av_mallocz(atom.size - 40 + FF_INPUT_BUFFER_PADDING_SIZE);
  948. if (!st->codec->extradata)
  949. return AVERROR(ENOMEM);
  950. st->codec->extradata_size = atom.size - 40;
  951. avio_skip(pb, 40);
  952. avio_read(pb, st->codec->extradata, atom.size - 40);
  953. return 0;
  954. }
  955. static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  956. {
  957. AVStream *st;
  958. MOVStreamContext *sc;
  959. unsigned int i, entries;
  960. if (c->fc->nb_streams < 1)
  961. return 0;
  962. st = c->fc->streams[c->fc->nb_streams-1];
  963. sc = st->priv_data;
  964. avio_r8(pb); /* version */
  965. avio_rb24(pb); /* flags */
  966. entries = avio_rb32(pb);
  967. if (!entries)
  968. return 0;
  969. if (entries >= UINT_MAX/sizeof(int64_t))
  970. return AVERROR_INVALIDDATA;
  971. sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
  972. if (!sc->chunk_offsets)
  973. return AVERROR(ENOMEM);
  974. sc->chunk_count = entries;
  975. if (atom.type == MKTAG('s','t','c','o'))
  976. for (i = 0; i < entries && !pb->eof_reached; i++)
  977. sc->chunk_offsets[i] = avio_rb32(pb);
  978. else if (atom.type == MKTAG('c','o','6','4'))
  979. for (i = 0; i < entries && !pb->eof_reached; i++)
  980. sc->chunk_offsets[i] = avio_rb64(pb);
  981. else
  982. return AVERROR_INVALIDDATA;
  983. sc->chunk_count = i;
  984. if (pb->eof_reached)
  985. return AVERROR_EOF;
  986. return 0;
  987. }
  988. /**
  989. * Compute codec id for 'lpcm' tag.
  990. * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
  991. */
  992. enum AVCodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
  993. {
  994. if (flags & 1) { // floating point
  995. if (flags & 2) { // big endian
  996. if (bps == 32) return AV_CODEC_ID_PCM_F32BE;
  997. else if (bps == 64) return AV_CODEC_ID_PCM_F64BE;
  998. } else {
  999. if (bps == 32) return AV_CODEC_ID_PCM_F32LE;
  1000. else if (bps == 64) return AV_CODEC_ID_PCM_F64LE;
  1001. }
  1002. } else {
  1003. if (flags & 2) {
  1004. if (bps == 8)
  1005. // signed integer
  1006. if (flags & 4) return AV_CODEC_ID_PCM_S8;
  1007. else return AV_CODEC_ID_PCM_U8;
  1008. else if (bps == 16) return AV_CODEC_ID_PCM_S16BE;
  1009. else if (bps == 24) return AV_CODEC_ID_PCM_S24BE;
  1010. else if (bps == 32) return AV_CODEC_ID_PCM_S32BE;
  1011. } else {
  1012. if (bps == 8)
  1013. if (flags & 4) return AV_CODEC_ID_PCM_S8;
  1014. else return AV_CODEC_ID_PCM_U8;
  1015. else if (bps == 16) return AV_CODEC_ID_PCM_S16LE;
  1016. else if (bps == 24) return AV_CODEC_ID_PCM_S24LE;
  1017. else if (bps == 32) return AV_CODEC_ID_PCM_S32LE;
  1018. }
  1019. }
  1020. return AV_CODEC_ID_NONE;
  1021. }
  1022. int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
  1023. {
  1024. AVStream *st;
  1025. MOVStreamContext *sc;
  1026. int j, pseudo_stream_id;
  1027. if (c->fc->nb_streams < 1)
  1028. return 0;
  1029. st = c->fc->streams[c->fc->nb_streams-1];
  1030. sc = st->priv_data;
  1031. for (pseudo_stream_id = 0;
  1032. pseudo_stream_id < entries && !pb->eof_reached;
  1033. pseudo_stream_id++) {
  1034. //Parsing Sample description table
  1035. enum AVCodecID id;
  1036. int dref_id = 1;
  1037. MOVAtom a = { AV_RL32("stsd") };
  1038. int64_t start_pos = avio_tell(pb);
  1039. int64_t size = avio_rb32(pb); /* size */
  1040. uint32_t format = avio_rl32(pb); /* data format */
  1041. if (size >= 16) {
  1042. avio_rb32(pb); /* reserved */
  1043. avio_rb16(pb); /* reserved */
  1044. dref_id = avio_rb16(pb);
  1045. }else if (size <= 7){
  1046. av_log(c->fc, AV_LOG_ERROR, "invalid size %"PRId64" in stsd\n", size);
  1047. return AVERROR_INVALIDDATA;
  1048. }
  1049. if (st->codec->codec_tag &&
  1050. st->codec->codec_tag != format &&
  1051. (c->fc->video_codec_id ? ff_codec_get_id(ff_codec_movvideo_tags, format) != c->fc->video_codec_id
  1052. : st->codec->codec_tag != MKTAG('j','p','e','g'))
  1053. ){
  1054. /* Multiple fourcc, we skip JPEG. This is not correct, we should
  1055. * export it as a separate AVStream but this needs a few changes
  1056. * in the MOV demuxer, patch welcome. */
  1057. av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
  1058. avio_skip(pb, size - (avio_tell(pb) - start_pos));
  1059. continue;
  1060. }
  1061. /* we cannot demux concatenated h264 streams because of different extradata */
  1062. if (st->codec->codec_tag && st->codec->codec_tag == AV_RL32("avc1"))
  1063. av_log(c->fc, AV_LOG_WARNING, "Concatenated H.264 might not play corrently.\n");
  1064. sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
  1065. sc->dref_id= dref_id;
  1066. st->codec->codec_tag = format;
  1067. id = ff_codec_get_id(ff_codec_movaudio_tags, format);
  1068. if (id<=0 && ((format&0xFFFF) == 'm'+('s'<<8) || (format&0xFFFF) == 'T'+('S'<<8)))
  1069. id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format)&0xFFFF);
  1070. if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
  1071. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  1072. } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO && /* do not overwrite codec type */
  1073. format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */
  1074. id = ff_codec_get_id(ff_codec_movvideo_tags, format);
  1075. if (id <= 0)
  1076. id = ff_codec_get_id(ff_codec_bmp_tags, format);
  1077. if (id > 0)
  1078. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  1079. else if (st->codec->codec_type == AVMEDIA_TYPE_DATA ||
  1080. (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE &&
  1081. st->codec->codec_id == AV_CODEC_ID_NONE)){
  1082. id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
  1083. if (id > 0)
  1084. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  1085. }
  1086. }
  1087. av_dlog(c->fc, "size=%"PRId64" 4CC= %c%c%c%c codec_type=%d\n", size,
  1088. (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
  1089. (format >> 24) & 0xff, st->codec->codec_type);
  1090. if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
  1091. unsigned int color_depth, len;
  1092. int color_greyscale;
  1093. int color_table_id;
  1094. st->codec->codec_id = id;
  1095. avio_rb16(pb); /* version */
  1096. avio_rb16(pb); /* revision level */
  1097. avio_rb32(pb); /* vendor */
  1098. avio_rb32(pb); /* temporal quality */
  1099. avio_rb32(pb); /* spatial quality */
  1100. st->codec->width = avio_rb16(pb); /* width */
  1101. st->codec->height = avio_rb16(pb); /* height */
  1102. avio_rb32(pb); /* horiz resolution */
  1103. avio_rb32(pb); /* vert resolution */
  1104. avio_rb32(pb); /* data size, always 0 */
  1105. avio_rb16(pb); /* frames per samples */
  1106. len = avio_r8(pb); /* codec name, pascal string */
  1107. if (len > 31)
  1108. len = 31;
  1109. mov_read_mac_string(c, pb, len, st->codec->codec_name, 32);
  1110. if (len < 31)
  1111. avio_skip(pb, 31 - len);
  1112. /* codec_tag YV12 triggers an UV swap in rawdec.c */
  1113. if (!memcmp(st->codec->codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25))
  1114. st->codec->codec_tag=MKTAG('I', '4', '2', '0');
  1115. st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
  1116. color_table_id = avio_rb16(pb); /* colortable id */
  1117. av_dlog(c->fc, "depth %d, ctab id %d\n",
  1118. st->codec->bits_per_coded_sample, color_table_id);
  1119. /* figure out the palette situation */
  1120. color_depth = st->codec->bits_per_coded_sample & 0x1F;
  1121. color_greyscale = st->codec->bits_per_coded_sample & 0x20;
  1122. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  1123. if ((color_depth == 2) || (color_depth == 4) ||
  1124. (color_depth == 8)) {
  1125. /* for palette traversal */
  1126. unsigned int color_start, color_count, color_end;
  1127. unsigned char a, r, g, b;
  1128. if (color_greyscale) {
  1129. int color_index, color_dec;
  1130. /* compute the greyscale palette */
  1131. st->codec->bits_per_coded_sample = color_depth;
  1132. color_count = 1 << color_depth;
  1133. color_index = 255;
  1134. color_dec = 256 / (color_count - 1);
  1135. for (j = 0; j < color_count; j++) {
  1136. if (id == AV_CODEC_ID_CINEPAK){
  1137. r = g = b = color_count - 1 - color_index;
  1138. }else
  1139. r = g = b = color_index;
  1140. sc->palette[j] =
  1141. (0xFFU << 24) | (r << 16) | (g << 8) | (b);
  1142. color_index -= color_dec;
  1143. if (color_index < 0)
  1144. color_index = 0;
  1145. }
  1146. } else if (color_table_id) {
  1147. const uint8_t *color_table;
  1148. /* if flag bit 3 is set, use the default palette */
  1149. color_count = 1 << color_depth;
  1150. if (color_depth == 2)
  1151. color_table = ff_qt_default_palette_4;
  1152. else if (color_depth == 4)
  1153. color_table = ff_qt_default_palette_16;
  1154. else
  1155. color_table = ff_qt_default_palette_256;
  1156. for (j = 0; j < color_count; j++) {
  1157. r = color_table[j * 3 + 0];
  1158. g = color_table[j * 3 + 1];
  1159. b = color_table[j * 3 + 2];
  1160. sc->palette[j] =
  1161. (0xFFU << 24) | (r << 16) | (g << 8) | (b);
  1162. }
  1163. } else {
  1164. /* load the palette from the file */
  1165. color_start = avio_rb32(pb);
  1166. color_count = avio_rb16(pb);
  1167. color_end = avio_rb16(pb);
  1168. if ((color_start <= 255) &&
  1169. (color_end <= 255)) {
  1170. for (j = color_start; j <= color_end; j++) {
  1171. /* each A, R, G, or B component is 16 bits;
  1172. * only use the top 8 bits */
  1173. a = avio_r8(pb);
  1174. avio_r8(pb);
  1175. r = avio_r8(pb);
  1176. avio_r8(pb);
  1177. g = avio_r8(pb);
  1178. avio_r8(pb);
  1179. b = avio_r8(pb);
  1180. avio_r8(pb);
  1181. sc->palette[j] =
  1182. (a << 24 ) | (r << 16) | (g << 8) | (b);
  1183. }
  1184. }
  1185. }
  1186. sc->has_palette = 1;
  1187. }
  1188. } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
  1189. int bits_per_sample, flags;
  1190. uint16_t version = avio_rb16(pb);
  1191. st->codec->codec_id = id;
  1192. avio_rb16(pb); /* revision level */
  1193. avio_rb32(pb); /* vendor */
  1194. st->codec->channels = avio_rb16(pb); /* channel count */
  1195. av_dlog(c->fc, "audio channels %d\n", st->codec->channels);
  1196. st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */
  1197. sc->audio_cid = avio_rb16(pb);
  1198. avio_rb16(pb); /* packet size = 0 */
  1199. st->codec->sample_rate = ((avio_rb32(pb) >> 16));
  1200. //Read QT version 1 fields. In version 0 these do not exist.
  1201. av_dlog(c->fc, "version =%d, isom =%d\n",version,c->isom);
  1202. if (!c->isom) {
  1203. if (version==1) {
  1204. sc->samples_per_frame = avio_rb32(pb);
  1205. avio_rb32(pb); /* bytes per packet */
  1206. sc->bytes_per_frame = avio_rb32(pb);
  1207. avio_rb32(pb); /* bytes per sample */
  1208. } else if (version==2) {
  1209. avio_rb32(pb); /* sizeof struct only */
  1210. st->codec->sample_rate = av_int2double(avio_rb64(pb)); /* float 64 */
  1211. st->codec->channels = avio_rb32(pb);
  1212. avio_rb32(pb); /* always 0x7F000000 */
  1213. st->codec->bits_per_coded_sample = avio_rb32(pb); /* bits per channel if sound is uncompressed */
  1214. flags = avio_rb32(pb); /* lpcm format specific flag */
  1215. sc->bytes_per_frame = avio_rb32(pb); /* bytes per audio packet if constant */
  1216. sc->samples_per_frame = avio_rb32(pb); /* lpcm frames per audio packet if constant */
  1217. if (format == MKTAG('l','p','c','m'))
  1218. st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
  1219. }
  1220. }
  1221. switch (st->codec->codec_id) {
  1222. case AV_CODEC_ID_PCM_S8:
  1223. case AV_CODEC_ID_PCM_U8:
  1224. if (st->codec->bits_per_coded_sample == 16)
  1225. st->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
  1226. break;
  1227. case AV_CODEC_ID_PCM_S16LE:
  1228. case AV_CODEC_ID_PCM_S16BE:
  1229. if (st->codec->bits_per_coded_sample == 8)
  1230. st->codec->codec_id = AV_CODEC_ID_PCM_S8;
  1231. else if (st->codec->bits_per_coded_sample == 24)
  1232. st->codec->codec_id =
  1233. st->codec->codec_id == AV_CODEC_ID_PCM_S16BE ?
  1234. AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
  1235. break;
  1236. /* set values for old format before stsd version 1 appeared */
  1237. case AV_CODEC_ID_MACE3:
  1238. sc->samples_per_frame = 6;
  1239. sc->bytes_per_frame = 2*st->codec->channels;
  1240. break;
  1241. case AV_CODEC_ID_MACE6:
  1242. sc->samples_per_frame = 6;
  1243. sc->bytes_per_frame = 1*st->codec->channels;
  1244. break;
  1245. case AV_CODEC_ID_ADPCM_IMA_QT:
  1246. sc->samples_per_frame = 64;
  1247. sc->bytes_per_frame = 34*st->codec->channels;
  1248. break;
  1249. case AV_CODEC_ID_GSM:
  1250. sc->samples_per_frame = 160;
  1251. sc->bytes_per_frame = 33;
  1252. break;
  1253. default:
  1254. break;
  1255. }
  1256. bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
  1257. if (bits_per_sample) {
  1258. st->codec->bits_per_coded_sample = bits_per_sample;
  1259. sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
  1260. }
  1261. } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
  1262. // ttxt stsd contains display flags, justification, background
  1263. // color, fonts, and default styles, so fake an atom to read it
  1264. MOVAtom fake_atom = { .size = size - (avio_tell(pb) - start_pos) };
  1265. if (format != AV_RL32("mp4s")) // mp4s contains a regular esds atom
  1266. mov_read_glbl(c, pb, fake_atom);
  1267. st->codec->codec_id= id;
  1268. st->codec->width = sc->width;
  1269. st->codec->height = sc->height;
  1270. } else {
  1271. if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
  1272. MOVStreamContext *tmcd_ctx = st->priv_data;
  1273. int val;
  1274. avio_rb32(pb); /* reserved */
  1275. val = avio_rb32(pb); /* flags */
  1276. tmcd_ctx->tmcd_flags = val;
  1277. if (val & 1)
  1278. st->codec->flags2 |= CODEC_FLAG2_DROP_FRAME_TIMECODE;
  1279. avio_rb32(pb); /* time scale */
  1280. avio_rb32(pb); /* frame duration */
  1281. st->codec->time_base.den = avio_r8(pb); /* number of frame */
  1282. st->codec->time_base.num = 1;
  1283. }
  1284. /* other codec type, just skip (rtp, mp4s, ...) */
  1285. avio_skip(pb, size - (avio_tell(pb) - start_pos));
  1286. }
  1287. /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
  1288. a.size = size - (avio_tell(pb) - start_pos);
  1289. if (a.size > 8) {
  1290. int ret;
  1291. if ((ret = mov_read_default(c, pb, a)) < 0)
  1292. return ret;
  1293. } else if (a.size > 0)
  1294. avio_skip(pb, a.size);
  1295. }
  1296. if (pb->eof_reached)
  1297. return AVERROR_EOF;
  1298. if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
  1299. st->codec->sample_rate= sc->time_scale;
  1300. /* special codec parameters handling */
  1301. switch (st->codec->codec_id) {
  1302. #if CONFIG_DV_DEMUXER
  1303. case AV_CODEC_ID_DVAUDIO:
  1304. c->dv_fctx = avformat_alloc_context();
  1305. c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
  1306. if (!c->dv_demux) {
  1307. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  1308. return AVERROR(ENOMEM);
  1309. }
  1310. sc->dv_audio_container = 1;
  1311. st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  1312. break;
  1313. #endif
  1314. /* no ifdef since parameters are always those */
  1315. case AV_CODEC_ID_QCELP:
  1316. // force sample rate for qcelp when not stored in mov
  1317. if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
  1318. st->codec->sample_rate = 8000;
  1319. st->codec->channels= 1; /* really needed */
  1320. break;
  1321. case AV_CODEC_ID_AMR_NB:
  1322. st->codec->channels= 1; /* really needed */
  1323. /* force sample rate for amr, stsd in 3gp does not store sample rate */
  1324. st->codec->sample_rate = 8000;
  1325. break;
  1326. case AV_CODEC_ID_AMR_WB:
  1327. st->codec->channels = 1;
  1328. st->codec->sample_rate = 16000;
  1329. break;
  1330. case AV_CODEC_ID_MP2:
  1331. case AV_CODEC_ID_MP3:
  1332. st->codec->codec_type = AVMEDIA_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
  1333. st->need_parsing = AVSTREAM_PARSE_FULL;
  1334. break;
  1335. case AV_CODEC_ID_GSM:
  1336. case AV_CODEC_ID_ADPCM_MS:
  1337. case AV_CODEC_ID_ADPCM_IMA_WAV:
  1338. case AV_CODEC_ID_ILBC:
  1339. st->codec->block_align = sc->bytes_per_frame;
  1340. break;
  1341. case AV_CODEC_ID_ALAC:
  1342. if (st->codec->extradata_size == 36) {
  1343. st->codec->channels = AV_RB8 (st->codec->extradata+21);
  1344. st->codec->sample_rate = AV_RB32(st->codec->extradata+32);
  1345. }
  1346. break;
  1347. case AV_CODEC_ID_AC3:
  1348. st->need_parsing = AVSTREAM_PARSE_FULL;
  1349. break;
  1350. case AV_CODEC_ID_MPEG1VIDEO:
  1351. st->need_parsing = AVSTREAM_PARSE_FULL;
  1352. break;
  1353. case AV_CODEC_ID_VC1:
  1354. st->need_parsing = AVSTREAM_PARSE_FULL;
  1355. break;
  1356. default:
  1357. break;
  1358. }
  1359. return 0;
  1360. }
  1361. static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1362. {
  1363. int entries;
  1364. avio_r8(pb); /* version */
  1365. avio_rb24(pb); /* flags */
  1366. entries = avio_rb32(pb);
  1367. return ff_mov_read_stsd_entries(c, pb, entries);
  1368. }
  1369. static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1370. {
  1371. AVStream *st;
  1372. MOVStreamContext *sc;
  1373. unsigned int i, entries;
  1374. if (c->fc->nb_streams < 1)
  1375. return 0;
  1376. st = c->fc->streams[c->fc->nb_streams-1];
  1377. sc = st->priv_data;
  1378. avio_r8(pb); /* version */
  1379. avio_rb24(pb); /* flags */
  1380. entries = avio_rb32(pb);
  1381. av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  1382. if (!entries)
  1383. return 0;
  1384. if (entries >= UINT_MAX / sizeof(*sc->stsc_data))
  1385. return AVERROR_INVALIDDATA;
  1386. sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
  1387. if (!sc->stsc_data)
  1388. return AVERROR(ENOMEM);
  1389. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1390. sc->stsc_data[i].first = avio_rb32(pb);
  1391. sc->stsc_data[i].count = avio_rb32(pb);
  1392. sc->stsc_data[i].id = avio_rb32(pb);
  1393. }
  1394. sc->stsc_count = i;
  1395. if (pb->eof_reached)
  1396. return AVERROR_EOF;
  1397. return 0;
  1398. }
  1399. static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1400. {
  1401. AVStream *st;
  1402. MOVStreamContext *sc;
  1403. unsigned i, entries;
  1404. if (c->fc->nb_streams < 1)
  1405. return 0;
  1406. st = c->fc->streams[c->fc->nb_streams-1];
  1407. sc = st->priv_data;
  1408. avio_rb32(pb); // version + flags
  1409. entries = avio_rb32(pb);
  1410. if (entries >= UINT_MAX / sizeof(*sc->stps_data))
  1411. return AVERROR_INVALIDDATA;
  1412. sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
  1413. if (!sc->stps_data)
  1414. return AVERROR(ENOMEM);
  1415. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1416. sc->stps_data[i] = avio_rb32(pb);
  1417. //av_dlog(c->fc, "stps %d\n", sc->stps_data[i]);
  1418. }
  1419. sc->stps_count = i;
  1420. if (pb->eof_reached)
  1421. return AVERROR_EOF;
  1422. return 0;
  1423. }
  1424. static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1425. {
  1426. AVStream *st;
  1427. MOVStreamContext *sc;
  1428. unsigned int i, entries;
  1429. if (c->fc->nb_streams < 1)
  1430. return 0;
  1431. st = c->fc->streams[c->fc->nb_streams-1];
  1432. sc = st->priv_data;
  1433. avio_r8(pb); /* version */
  1434. avio_rb24(pb); /* flags */
  1435. entries = avio_rb32(pb);
  1436. av_dlog(c->fc, "keyframe_count = %d\n", entries);
  1437. if (!entries)
  1438. {
  1439. sc->keyframe_absent = 1;
  1440. if (!st->need_parsing && st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  1441. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  1442. return 0;
  1443. }
  1444. if (entries >= UINT_MAX / sizeof(int))
  1445. return AVERROR_INVALIDDATA;
  1446. sc->keyframes = av_malloc(entries * sizeof(int));
  1447. if (!sc->keyframes)
  1448. return AVERROR(ENOMEM);
  1449. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1450. sc->keyframes[i] = avio_rb32(pb);
  1451. //av_dlog(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
  1452. }
  1453. sc->keyframe_count = i;
  1454. if (pb->eof_reached)
  1455. return AVERROR_EOF;
  1456. return 0;
  1457. }
  1458. static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1459. {
  1460. AVStream *st;
  1461. MOVStreamContext *sc;
  1462. unsigned int i, entries, sample_size, field_size, num_bytes;
  1463. GetBitContext gb;
  1464. unsigned char* buf;
  1465. if (c->fc->nb_streams < 1)
  1466. return 0;
  1467. st = c->fc->streams[c->fc->nb_streams-1];
  1468. sc = st->priv_data;
  1469. avio_r8(pb); /* version */
  1470. avio_rb24(pb); /* flags */
  1471. if (atom.type == MKTAG('s','t','s','z')) {
  1472. sample_size = avio_rb32(pb);
  1473. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  1474. sc->sample_size = sample_size;
  1475. sc->alt_sample_size = sample_size;
  1476. field_size = 32;
  1477. } else {
  1478. sample_size = 0;
  1479. avio_rb24(pb); /* reserved */
  1480. field_size = avio_r8(pb);
  1481. }
  1482. entries = avio_rb32(pb);
  1483. av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
  1484. sc->sample_count = entries;
  1485. if (sample_size)
  1486. return 0;
  1487. if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
  1488. av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
  1489. return AVERROR_INVALIDDATA;
  1490. }
  1491. if (!entries)
  1492. return 0;
  1493. if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
  1494. return AVERROR_INVALIDDATA;
  1495. sc->sample_sizes = av_malloc(entries * sizeof(int));
  1496. if (!sc->sample_sizes)
  1497. return AVERROR(ENOMEM);
  1498. num_bytes = (entries*field_size+4)>>3;
  1499. buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
  1500. if (!buf) {
  1501. av_freep(&sc->sample_sizes);
  1502. return AVERROR(ENOMEM);
  1503. }
  1504. if (avio_read(pb, buf, num_bytes) < num_bytes) {
  1505. av_freep(&sc->sample_sizes);
  1506. av_free(buf);
  1507. return AVERROR_INVALIDDATA;
  1508. }
  1509. init_get_bits(&gb, buf, 8*num_bytes);
  1510. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1511. sc->sample_sizes[i] = get_bits_long(&gb, field_size);
  1512. sc->data_size += sc->sample_sizes[i];
  1513. }
  1514. sc->sample_count = i;
  1515. if (pb->eof_reached)
  1516. return AVERROR_EOF;
  1517. av_free(buf);
  1518. return 0;
  1519. }
  1520. static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1521. {
  1522. AVStream *st;
  1523. MOVStreamContext *sc;
  1524. unsigned int i, entries;
  1525. int64_t duration=0;
  1526. int64_t total_sample_count=0;
  1527. if (c->fc->nb_streams < 1)
  1528. return 0;
  1529. st = c->fc->streams[c->fc->nb_streams-1];
  1530. sc = st->priv_data;
  1531. avio_r8(pb); /* version */
  1532. avio_rb24(pb); /* flags */
  1533. entries = avio_rb32(pb);
  1534. av_dlog(c->fc, "track[%i].stts.entries = %i\n",
  1535. c->fc->nb_streams-1, entries);
  1536. if (entries >= UINT_MAX / sizeof(*sc->stts_data))
  1537. return -1;
  1538. sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
  1539. if (!sc->stts_data)
  1540. return AVERROR(ENOMEM);
  1541. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1542. int sample_duration;
  1543. int sample_count;
  1544. sample_count=avio_rb32(pb);
  1545. sample_duration = avio_rb32(pb);
  1546. /* sample_duration < 0 is invalid based on the spec */
  1547. if (sample_duration < 0) {
  1548. av_log(c->fc, AV_LOG_ERROR, "Invalid SampleDelta in STTS %d\n", sample_duration);
  1549. sample_duration = 1;
  1550. }
  1551. sc->stts_data[i].count= sample_count;
  1552. sc->stts_data[i].duration= sample_duration;
  1553. av_dlog(c->fc, "sample_count=%d, sample_duration=%d\n",
  1554. sample_count, sample_duration);
  1555. duration+=(int64_t)sample_duration*sample_count;
  1556. total_sample_count+=sample_count;
  1557. }
  1558. sc->stts_count = i;
  1559. if (pb->eof_reached)
  1560. return AVERROR_EOF;
  1561. st->nb_frames= total_sample_count;
  1562. if (duration)
  1563. st->duration= duration;
  1564. sc->track_end = duration;
  1565. return 0;
  1566. }
  1567. static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1568. {
  1569. AVStream *st;
  1570. MOVStreamContext *sc;
  1571. unsigned int i, entries;
  1572. if (c->fc->nb_streams < 1)
  1573. return 0;
  1574. st = c->fc->streams[c->fc->nb_streams-1];
  1575. sc = st->priv_data;
  1576. avio_r8(pb); /* version */
  1577. avio_rb24(pb); /* flags */
  1578. entries = avio_rb32(pb);
  1579. av_dlog(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  1580. if (!entries)
  1581. return 0;
  1582. if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
  1583. return AVERROR_INVALIDDATA;
  1584. sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
  1585. if (!sc->ctts_data)
  1586. return AVERROR(ENOMEM);
  1587. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1588. int count =avio_rb32(pb);
  1589. int duration =avio_rb32(pb);
  1590. sc->ctts_data[i].count = count;
  1591. sc->ctts_data[i].duration= duration;
  1592. av_dlog(c->fc, "count=%d, duration=%d\n",
  1593. count, duration);
  1594. if (FFABS(duration) > (1<<28) && i+2<entries) {
  1595. av_log(c->fc, AV_LOG_WARNING, "CTTS invalid\n");
  1596. av_freep(&sc->ctts_data);
  1597. sc->ctts_count = 0;
  1598. return 0;
  1599. }
  1600. if (duration < 0 && i+2<entries)
  1601. sc->dts_shift = FFMAX(sc->dts_shift, -duration);
  1602. }
  1603. sc->ctts_count = i;
  1604. if (pb->eof_reached)
  1605. return AVERROR_EOF;
  1606. av_dlog(c->fc, "dts shift %d\n", sc->dts_shift);
  1607. return 0;
  1608. }
  1609. static int mov_read_sbgp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1610. {
  1611. AVStream *st;
  1612. MOVStreamContext *sc;
  1613. unsigned int i, entries;
  1614. uint8_t version;
  1615. uint32_t grouping_type;
  1616. if (c->fc->nb_streams < 1)
  1617. return 0;
  1618. st = c->fc->streams[c->fc->nb_streams-1];
  1619. sc = st->priv_data;
  1620. version = avio_r8(pb); /* version */
  1621. avio_rb24(pb); /* flags */
  1622. grouping_type = avio_rl32(pb);
  1623. if (grouping_type != MKTAG( 'r','a','p',' '))
  1624. return 0; /* only support 'rap ' grouping */
  1625. if (version == 1)
  1626. avio_rb32(pb); /* grouping_type_parameter */
  1627. entries = avio_rb32(pb);
  1628. if (!entries)
  1629. return 0;
  1630. if (entries >= UINT_MAX / sizeof(*sc->rap_group))
  1631. return AVERROR_INVALIDDATA;
  1632. sc->rap_group = av_malloc(entries * sizeof(*sc->rap_group));
  1633. if (!sc->rap_group)
  1634. return AVERROR(ENOMEM);
  1635. for (i = 0; i < entries && !pb->eof_reached; i++) {
  1636. sc->rap_group[i].count = avio_rb32(pb); /* sample_count */
  1637. sc->rap_group[i].index = avio_rb32(pb); /* group_description_index */
  1638. }
  1639. sc->rap_group_count = i;
  1640. return pb->eof_reached ? AVERROR_EOF : 0;
  1641. }
  1642. static void mov_build_index(MOVContext *mov, AVStream *st)
  1643. {
  1644. MOVStreamContext *sc = st->priv_data;
  1645. int64_t current_offset;
  1646. int64_t current_dts = 0;
  1647. unsigned int stts_index = 0;
  1648. unsigned int stsc_index = 0;
  1649. unsigned int stss_index = 0;
  1650. unsigned int stps_index = 0;
  1651. unsigned int i, j;
  1652. uint64_t stream_size = 0;
  1653. AVIndexEntry *mem;
  1654. /* adjust first dts according to edit list */
  1655. if ((sc->empty_duration || sc->start_time) && mov->time_scale > 0) {
  1656. if (sc->empty_duration)
  1657. sc->empty_duration = av_rescale(sc->empty_duration, sc->time_scale, mov->time_scale);
  1658. sc->time_offset = sc->start_time - sc->empty_duration;
  1659. current_dts = -sc->time_offset;
  1660. if (sc->ctts_count>0 && sc->stts_count>0 &&
  1661. sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) {
  1662. /* more than 16 frames delay, dts are likely wrong
  1663. this happens with files created by iMovie */
  1664. sc->wrong_dts = 1;
  1665. st->codec->has_b_frames = 1;
  1666. }
  1667. }
  1668. /* only use old uncompressed audio chunk demuxing when stts specifies it */
  1669. if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  1670. sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
  1671. unsigned int current_sample = 0;
  1672. unsigned int stts_sample = 0;
  1673. unsigned int sample_size;
  1674. unsigned int distance = 0;
  1675. unsigned int rap_group_index = 0;
  1676. unsigned int rap_group_sample = 0;
  1677. int rap_group_present = sc->rap_group_count && sc->rap_group;
  1678. int key_off = (sc->keyframe_count && sc->keyframes[0] > 0) || (sc->stps_data && sc->stps_data[0] > 0);
  1679. current_dts -= sc->dts_shift;
  1680. if (!sc->sample_count || st->nb_index_entries)
  1681. return;
  1682. if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
  1683. return;
  1684. mem = av_realloc(st->index_entries, (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries));
  1685. if (!mem)
  1686. return;
  1687. st->index_entries = mem;
  1688. st->index_entries_allocated_size = (st->nb_index_entries + sc->sample_count) * sizeof(*st->index_entries);
  1689. for (i = 0; i < sc->chunk_count; i++) {
  1690. current_offset = sc->chunk_offsets[i];
  1691. while (stsc_index + 1 < sc->stsc_count &&
  1692. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1693. stsc_index++;
  1694. for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
  1695. int keyframe = 0;
  1696. if (current_sample >= sc->sample_count) {
  1697. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  1698. return;
  1699. }
  1700. if (!sc->keyframe_absent && (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index])) {
  1701. keyframe = 1;
  1702. if (stss_index + 1 < sc->keyframe_count)
  1703. stss_index++;
  1704. } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
  1705. keyframe = 1;
  1706. if (stps_index + 1 < sc->stps_count)
  1707. stps_index++;
  1708. }
  1709. if (rap_group_present && rap_group_index < sc->rap_group_count) {
  1710. if (sc->rap_group[rap_group_index].index > 0)
  1711. keyframe = 1;
  1712. if (++rap_group_sample == sc->rap_group[rap_group_index].count) {
  1713. rap_group_sample = 0;
  1714. rap_group_index++;
  1715. }
  1716. }
  1717. if (sc->keyframe_absent
  1718. && !sc->stps_count
  1719. && !rap_group_present
  1720. && st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  1721. keyframe = 1;
  1722. if (keyframe)
  1723. distance = 0;
  1724. sample_size = sc->alt_sample_size > 0 ? sc->alt_sample_size : sc->sample_sizes[current_sample];
  1725. if (sc->pseudo_stream_id == -1 ||
  1726. sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
  1727. AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
  1728. e->pos = current_offset;
  1729. e->timestamp = current_dts;
  1730. e->size = sample_size;
  1731. e->min_distance = distance;
  1732. e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
  1733. av_dlog(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1734. "size %d, distance %d, keyframe %d\n", st->index, current_sample,
  1735. current_offset, current_dts, sample_size, distance, keyframe);
  1736. }
  1737. current_offset += sample_size;
  1738. stream_size += sample_size;
  1739. current_dts += sc->stts_data[stts_index].duration;
  1740. distance++;
  1741. stts_sample++;
  1742. current_sample++;
  1743. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  1744. stts_sample = 0;
  1745. stts_index++;
  1746. }
  1747. }
  1748. }
  1749. if (st->duration > 0)
  1750. st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
  1751. } else {
  1752. unsigned chunk_samples, total = 0;
  1753. // compute total chunk count
  1754. for (i = 0; i < sc->stsc_count; i++) {
  1755. unsigned count, chunk_count;
  1756. chunk_samples = sc->stsc_data[i].count;
  1757. if (i != sc->stsc_count - 1 &&
  1758. sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
  1759. av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
  1760. return;
  1761. }
  1762. if (sc->samples_per_frame >= 160) { // gsm
  1763. count = chunk_samples / sc->samples_per_frame;
  1764. } else if (sc->samples_per_frame > 1) {
  1765. unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
  1766. count = (chunk_samples+samples-1) / samples;
  1767. } else {
  1768. count = (chunk_samples+1023) / 1024;
  1769. }
  1770. if (i < sc->stsc_count - 1)
  1771. chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
  1772. else
  1773. chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
  1774. total += chunk_count * count;
  1775. }
  1776. av_dlog(mov->fc, "chunk count %d\n", total);
  1777. if (total >= UINT_MAX / sizeof(*st->index_entries) - st->nb_index_entries)
  1778. return;
  1779. mem = av_realloc(st->index_entries, (st->nb_index_entries + total) * sizeof(*st->index_entries));
  1780. if (!mem)
  1781. return;
  1782. st->index_entries = mem;
  1783. st->index_entries_allocated_size = (st->nb_index_entries + total) * sizeof(*st->index_entries);
  1784. // populate index
  1785. for (i = 0; i < sc->chunk_count; i++) {
  1786. current_offset = sc->chunk_offsets[i];
  1787. if (stsc_index + 1 < sc->stsc_count &&
  1788. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1789. stsc_index++;
  1790. chunk_samples = sc->stsc_data[stsc_index].count;
  1791. while (chunk_samples > 0) {
  1792. AVIndexEntry *e;
  1793. unsigned size, samples;
  1794. if (sc->samples_per_frame >= 160) { // gsm
  1795. samples = sc->samples_per_frame;
  1796. size = sc->bytes_per_frame;
  1797. } else {
  1798. if (sc->samples_per_frame > 1) {
  1799. samples = FFMIN((1024 / sc->samples_per_frame)*
  1800. sc->samples_per_frame, chunk_samples);
  1801. size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
  1802. } else {
  1803. samples = FFMIN(1024, chunk_samples);
  1804. size = samples * sc->sample_size;
  1805. }
  1806. }
  1807. if (st->nb_index_entries >= total) {
  1808. av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
  1809. return;
  1810. }
  1811. e = &st->index_entries[st->nb_index_entries++];
  1812. e->pos = current_offset;
  1813. e->timestamp = current_dts;
  1814. e->size = size;
  1815. e->min_distance = 0;
  1816. e->flags = AVINDEX_KEYFRAME;
  1817. av_dlog(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
  1818. "size %d, duration %d\n", st->index, i, current_offset, current_dts,
  1819. size, samples);
  1820. current_offset += size;
  1821. current_dts += samples;
  1822. chunk_samples -= samples;
  1823. }
  1824. }
  1825. }
  1826. }
  1827. static int mov_open_dref(AVIOContext **pb, const char *src, MOVDref *ref,
  1828. AVIOInterruptCB *int_cb, int use_absolute_path, AVFormatContext *fc)
  1829. {
  1830. /* try relative path, we do not try the absolute because it can leak information about our
  1831. system to an attacker */
  1832. if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
  1833. char filename[1024];
  1834. const char *src_path;
  1835. int i, l;
  1836. /* find a source dir */
  1837. src_path = strrchr(src, '/');
  1838. if (src_path)
  1839. src_path++;
  1840. else
  1841. src_path = src;
  1842. /* find a next level down to target */
  1843. for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
  1844. if (ref->path[l] == '/') {
  1845. if (i == ref->nlvl_to - 1)
  1846. break;
  1847. else
  1848. i++;
  1849. }
  1850. /* compose filename if next level down to target was found */
  1851. if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
  1852. memcpy(filename, src, src_path - src);
  1853. filename[src_path - src] = 0;
  1854. for (i = 1; i < ref->nlvl_from; i++)
  1855. av_strlcat(filename, "../", 1024);
  1856. av_strlcat(filename, ref->path + l + 1, 1024);
  1857. if (!avio_open2(pb, filename, AVIO_FLAG_READ, int_cb, NULL))
  1858. return 0;
  1859. }
  1860. } else if (use_absolute_path) {
  1861. av_log(fc, AV_LOG_WARNING, "Using absolute path on user request, "
  1862. "this is a possible security issue\n");
  1863. if (!avio_open2(pb, ref->path, AVIO_FLAG_READ, int_cb, NULL))
  1864. return 0;
  1865. }
  1866. return AVERROR(ENOENT);
  1867. }
  1868. static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1869. {
  1870. AVStream *st;
  1871. MOVStreamContext *sc;
  1872. int ret;
  1873. st = avformat_new_stream(c->fc, NULL);
  1874. if (!st) return AVERROR(ENOMEM);
  1875. st->id = c->fc->nb_streams;
  1876. sc = av_mallocz(sizeof(MOVStreamContext));
  1877. if (!sc) return AVERROR(ENOMEM);
  1878. st->priv_data = sc;
  1879. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  1880. sc->ffindex = st->index;
  1881. if ((ret = mov_read_default(c, pb, atom)) < 0)
  1882. return ret;
  1883. /* sanity checks */
  1884. if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
  1885. (!sc->sample_size && !sc->sample_count))) {
  1886. av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
  1887. st->index);
  1888. return 0;
  1889. }
  1890. if (sc->time_scale <= 0) {
  1891. av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index);
  1892. sc->time_scale = c->time_scale;
  1893. if (sc->time_scale <= 0)
  1894. sc->time_scale = 1;
  1895. }
  1896. avpriv_set_pts_info(st, 64, 1, sc->time_scale);
  1897. mov_build_index(c, st);
  1898. if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
  1899. MOVDref *dref = &sc->drefs[sc->dref_id - 1];
  1900. if (mov_open_dref(&sc->pb, c->fc->filename, dref, &c->fc->interrupt_callback,
  1901. c->use_absolute_path, c->fc) < 0)
  1902. av_log(c->fc, AV_LOG_ERROR,
  1903. "stream %d, error opening alias: path='%s', dir='%s', "
  1904. "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
  1905. st->index, dref->path, dref->dir, dref->filename,
  1906. dref->volume, dref->nlvl_from, dref->nlvl_to);
  1907. } else
  1908. sc->pb = c->fc->pb;
  1909. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  1910. if (!st->sample_aspect_ratio.num &&
  1911. (st->codec->width != sc->width || st->codec->height != sc->height)) {
  1912. st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
  1913. ((double)st->codec->width * sc->height), INT_MAX);
  1914. }
  1915. av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
  1916. sc->time_scale*st->nb_frames, st->duration, INT_MAX);
  1917. #if FF_API_R_FRAME_RATE
  1918. if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
  1919. av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
  1920. sc->time_scale, sc->stts_data[0].duration, INT_MAX);
  1921. #endif
  1922. }
  1923. switch (st->codec->codec_id) {
  1924. #if CONFIG_H261_DECODER
  1925. case AV_CODEC_ID_H261:
  1926. #endif
  1927. #if CONFIG_H263_DECODER
  1928. case AV_CODEC_ID_H263:
  1929. #endif
  1930. #if CONFIG_MPEG4_DECODER
  1931. case AV_CODEC_ID_MPEG4:
  1932. #endif
  1933. st->codec->width = 0; /* let decoder init width/height */
  1934. st->codec->height= 0;
  1935. break;
  1936. }
  1937. /* Do not need those anymore. */
  1938. av_freep(&sc->chunk_offsets);
  1939. av_freep(&sc->stsc_data);
  1940. av_freep(&sc->sample_sizes);
  1941. av_freep(&sc->keyframes);
  1942. av_freep(&sc->stts_data);
  1943. av_freep(&sc->stps_data);
  1944. av_freep(&sc->rap_group);
  1945. return 0;
  1946. }
  1947. static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1948. {
  1949. int ret;
  1950. c->itunes_metadata = 1;
  1951. ret = mov_read_default(c, pb, atom);
  1952. c->itunes_metadata = 0;
  1953. return ret;
  1954. }
  1955. static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1956. {
  1957. while (atom.size > 8) {
  1958. uint32_t tag = avio_rl32(pb);
  1959. atom.size -= 4;
  1960. if (tag == MKTAG('h','d','l','r')) {
  1961. avio_seek(pb, -8, SEEK_CUR);
  1962. atom.size += 8;
  1963. return mov_read_default(c, pb, atom);
  1964. }
  1965. }
  1966. return 0;
  1967. }
  1968. static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1969. {
  1970. int i;
  1971. int width;
  1972. int height;
  1973. int64_t disp_transform[2];
  1974. int display_matrix[3][2];
  1975. AVStream *st;
  1976. MOVStreamContext *sc;
  1977. int version;
  1978. if (c->fc->nb_streams < 1)
  1979. return 0;
  1980. st = c->fc->streams[c->fc->nb_streams-1];
  1981. sc = st->priv_data;
  1982. version = avio_r8(pb);
  1983. avio_rb24(pb); /* flags */
  1984. /*
  1985. MOV_TRACK_ENABLED 0x0001
  1986. MOV_TRACK_IN_MOVIE 0x0002
  1987. MOV_TRACK_IN_PREVIEW 0x0004
  1988. MOV_TRACK_IN_POSTER 0x0008
  1989. */
  1990. if (version == 1) {
  1991. avio_rb64(pb);
  1992. avio_rb64(pb);
  1993. } else {
  1994. avio_rb32(pb); /* creation time */
  1995. avio_rb32(pb); /* modification time */
  1996. }
  1997. st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
  1998. avio_rb32(pb); /* reserved */
  1999. /* highlevel (considering edits) duration in movie timebase */
  2000. (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
  2001. avio_rb32(pb); /* reserved */
  2002. avio_rb32(pb); /* reserved */
  2003. avio_rb16(pb); /* layer */
  2004. avio_rb16(pb); /* alternate group */
  2005. avio_rb16(pb); /* volume */
  2006. avio_rb16(pb); /* reserved */
  2007. //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
  2008. // they're kept in fixed point format through all calculations
  2009. // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
  2010. for (i = 0; i < 3; i++) {
  2011. display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
  2012. display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
  2013. avio_rb32(pb); // 2.30 fixed point (not used)
  2014. }
  2015. width = avio_rb32(pb); // 16.16 fixed point track width
  2016. height = avio_rb32(pb); // 16.16 fixed point track height
  2017. sc->width = width >> 16;
  2018. sc->height = height >> 16;
  2019. //Assign clockwise rotate values based on transform matrix so that
  2020. //we can compensate for iPhone orientation during capture.
  2021. if (display_matrix[1][0] == -65536 && display_matrix[0][1] == 65536) {
  2022. av_dict_set(&st->metadata, "rotate", "90", 0);
  2023. }
  2024. if (display_matrix[0][0] == -65536 && display_matrix[1][1] == -65536) {
  2025. av_dict_set(&st->metadata, "rotate", "180", 0);
  2026. }
  2027. if (display_matrix[1][0] == 65536 && display_matrix[0][1] == -65536) {
  2028. av_dict_set(&st->metadata, "rotate", "270", 0);
  2029. }
  2030. // transform the display width/height according to the matrix
  2031. // skip this if the display matrix is the default identity matrix
  2032. // or if it is rotating the picture, ex iPhone 3GS
  2033. // to keep the same scale, use [width height 1<<16]
  2034. if (width && height &&
  2035. ((display_matrix[0][0] != 65536 ||
  2036. display_matrix[1][1] != 65536) &&
  2037. !display_matrix[0][1] &&
  2038. !display_matrix[1][0] &&
  2039. !display_matrix[2][0] && !display_matrix[2][1])) {
  2040. for (i = 0; i < 2; i++)
  2041. disp_transform[i] =
  2042. (int64_t) width * display_matrix[0][i] +
  2043. (int64_t) height * display_matrix[1][i] +
  2044. ((int64_t) display_matrix[2][i] << 16);
  2045. //sample aspect ratio is new width/height divided by old width/height
  2046. st->sample_aspect_ratio = av_d2q(
  2047. ((double) disp_transform[0] * height) /
  2048. ((double) disp_transform[1] * width), INT_MAX);
  2049. }
  2050. return 0;
  2051. }
  2052. static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2053. {
  2054. MOVFragment *frag = &c->fragment;
  2055. MOVTrackExt *trex = NULL;
  2056. int flags, track_id, i;
  2057. avio_r8(pb); /* version */
  2058. flags = avio_rb24(pb);
  2059. track_id = avio_rb32(pb);
  2060. if (!track_id)
  2061. return AVERROR_INVALIDDATA;
  2062. frag->track_id = track_id;
  2063. for (i = 0; i < c->trex_count; i++)
  2064. if (c->trex_data[i].track_id == frag->track_id) {
  2065. trex = &c->trex_data[i];
  2066. break;
  2067. }
  2068. if (!trex) {
  2069. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
  2070. return AVERROR_INVALIDDATA;
  2071. }
  2072. frag->base_data_offset = flags & MOV_TFHD_BASE_DATA_OFFSET ?
  2073. avio_rb64(pb) : frag->moof_offset;
  2074. frag->stsd_id = flags & MOV_TFHD_STSD_ID ? avio_rb32(pb) : trex->stsd_id;
  2075. frag->duration = flags & MOV_TFHD_DEFAULT_DURATION ?
  2076. avio_rb32(pb) : trex->duration;
  2077. frag->size = flags & MOV_TFHD_DEFAULT_SIZE ?
  2078. avio_rb32(pb) : trex->size;
  2079. frag->flags = flags & MOV_TFHD_DEFAULT_FLAGS ?
  2080. avio_rb32(pb) : trex->flags;
  2081. av_dlog(c->fc, "frag flags 0x%x\n", frag->flags);
  2082. return 0;
  2083. }
  2084. static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2085. {
  2086. c->chapter_track = avio_rb32(pb);
  2087. return 0;
  2088. }
  2089. static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2090. {
  2091. MOVTrackExt *trex;
  2092. if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
  2093. return AVERROR_INVALIDDATA;
  2094. trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
  2095. if (!trex)
  2096. return AVERROR(ENOMEM);
  2097. c->fc->duration = AV_NOPTS_VALUE; // the duration from mvhd is not representing the whole file when fragments are used.
  2098. c->trex_data = trex;
  2099. trex = &c->trex_data[c->trex_count++];
  2100. avio_r8(pb); /* version */
  2101. avio_rb24(pb); /* flags */
  2102. trex->track_id = avio_rb32(pb);
  2103. trex->stsd_id = avio_rb32(pb);
  2104. trex->duration = avio_rb32(pb);
  2105. trex->size = avio_rb32(pb);
  2106. trex->flags = avio_rb32(pb);
  2107. return 0;
  2108. }
  2109. static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2110. {
  2111. MOVFragment *frag = &c->fragment;
  2112. AVStream *st = NULL;
  2113. MOVStreamContext *sc;
  2114. MOVStts *ctts_data;
  2115. uint64_t offset;
  2116. int64_t dts;
  2117. int data_offset = 0;
  2118. unsigned entries, first_sample_flags = frag->flags;
  2119. int flags, distance, i, found_keyframe = 0;
  2120. for (i = 0; i < c->fc->nb_streams; i++) {
  2121. if (c->fc->streams[i]->id == frag->track_id) {
  2122. st = c->fc->streams[i];
  2123. break;
  2124. }
  2125. }
  2126. if (!st) {
  2127. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
  2128. return AVERROR_INVALIDDATA;
  2129. }
  2130. sc = st->priv_data;
  2131. if (sc->pseudo_stream_id+1 != frag->stsd_id)
  2132. return 0;
  2133. avio_r8(pb); /* version */
  2134. flags = avio_rb24(pb);
  2135. entries = avio_rb32(pb);
  2136. av_dlog(c->fc, "flags 0x%x entries %d\n", flags, entries);
  2137. /* Always assume the presence of composition time offsets.
  2138. * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
  2139. * 1) in the initial movie, there are no samples.
  2140. * 2) in the first movie fragment, there is only one sample without composition time offset.
  2141. * 3) in the subsequent movie fragments, there are samples with composition time offset. */
  2142. if (!sc->ctts_count && sc->sample_count)
  2143. {
  2144. /* Complement ctts table if moov atom doesn't have ctts atom. */
  2145. ctts_data = av_malloc(sizeof(*sc->ctts_data));
  2146. if (!ctts_data)
  2147. return AVERROR(ENOMEM);
  2148. sc->ctts_data = ctts_data;
  2149. sc->ctts_data[sc->ctts_count].count = sc->sample_count;
  2150. sc->ctts_data[sc->ctts_count].duration = 0;
  2151. sc->ctts_count++;
  2152. }
  2153. if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
  2154. return AVERROR_INVALIDDATA;
  2155. ctts_data = av_realloc(sc->ctts_data,
  2156. (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
  2157. if (!ctts_data)
  2158. return AVERROR(ENOMEM);
  2159. sc->ctts_data = ctts_data;
  2160. if (flags & MOV_TRUN_DATA_OFFSET) data_offset = avio_rb32(pb);
  2161. if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS) first_sample_flags = avio_rb32(pb);
  2162. dts = sc->track_end - sc->time_offset;
  2163. offset = frag->base_data_offset + data_offset;
  2164. distance = 0;
  2165. av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags);
  2166. for (i = 0; i < entries && !pb->eof_reached; i++) {
  2167. unsigned sample_size = frag->size;
  2168. int sample_flags = i ? frag->flags : first_sample_flags;
  2169. unsigned sample_duration = frag->duration;
  2170. int keyframe = 0;
  2171. if (flags & MOV_TRUN_SAMPLE_DURATION) sample_duration = avio_rb32(pb);
  2172. if (flags & MOV_TRUN_SAMPLE_SIZE) sample_size = avio_rb32(pb);
  2173. if (flags & MOV_TRUN_SAMPLE_FLAGS) sample_flags = avio_rb32(pb);
  2174. sc->ctts_data[sc->ctts_count].count = 1;
  2175. sc->ctts_data[sc->ctts_count].duration = (flags & MOV_TRUN_SAMPLE_CTS) ?
  2176. avio_rb32(pb) : 0;
  2177. sc->ctts_count++;
  2178. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  2179. keyframe = 1;
  2180. else if (!found_keyframe)
  2181. keyframe = found_keyframe =
  2182. !(sample_flags & (MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC |
  2183. MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES));
  2184. if (keyframe)
  2185. distance = 0;
  2186. av_add_index_entry(st, offset, dts, sample_size, distance,
  2187. keyframe ? AVINDEX_KEYFRAME : 0);
  2188. av_dlog(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  2189. "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
  2190. offset, dts, sample_size, distance, keyframe);
  2191. distance++;
  2192. dts += sample_duration;
  2193. offset += sample_size;
  2194. sc->data_size += sample_size;
  2195. }
  2196. if (pb->eof_reached)
  2197. return AVERROR_EOF;
  2198. frag->moof_offset = offset;
  2199. st->duration = sc->track_end = dts + sc->time_offset;
  2200. return 0;
  2201. }
  2202. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  2203. /* like the files created with Adobe Premiere 5.0, for samples see */
  2204. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  2205. static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2206. {
  2207. int err;
  2208. if (atom.size < 8)
  2209. return 0; /* continue */
  2210. if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  2211. avio_skip(pb, atom.size - 4);
  2212. return 0;
  2213. }
  2214. atom.type = avio_rl32(pb);
  2215. atom.size -= 8;
  2216. if (atom.type != MKTAG('m','d','a','t')) {
  2217. avio_skip(pb, atom.size);
  2218. return 0;
  2219. }
  2220. err = mov_read_mdat(c, pb, atom);
  2221. return err;
  2222. }
  2223. static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2224. {
  2225. #if CONFIG_ZLIB
  2226. AVIOContext ctx;
  2227. uint8_t *cmov_data;
  2228. uint8_t *moov_data; /* uncompressed data */
  2229. long cmov_len, moov_len;
  2230. int ret = -1;
  2231. avio_rb32(pb); /* dcom atom */
  2232. if (avio_rl32(pb) != MKTAG('d','c','o','m'))
  2233. return AVERROR_INVALIDDATA;
  2234. if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
  2235. av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !\n");
  2236. return AVERROR_INVALIDDATA;
  2237. }
  2238. avio_rb32(pb); /* cmvd atom */
  2239. if (avio_rl32(pb) != MKTAG('c','m','v','d'))
  2240. return AVERROR_INVALIDDATA;
  2241. moov_len = avio_rb32(pb); /* uncompressed size */
  2242. cmov_len = atom.size - 6 * 4;
  2243. cmov_data = av_malloc(cmov_len);
  2244. if (!cmov_data)
  2245. return AVERROR(ENOMEM);
  2246. moov_data = av_malloc(moov_len);
  2247. if (!moov_data) {
  2248. av_free(cmov_data);
  2249. return AVERROR(ENOMEM);
  2250. }
  2251. avio_read(pb, cmov_data, cmov_len);
  2252. if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  2253. goto free_and_return;
  2254. if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  2255. goto free_and_return;
  2256. atom.type = MKTAG('m','o','o','v');
  2257. atom.size = moov_len;
  2258. ret = mov_read_default(c, &ctx, atom);
  2259. free_and_return:
  2260. av_free(moov_data);
  2261. av_free(cmov_data);
  2262. return ret;
  2263. #else
  2264. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  2265. return AVERROR(ENOSYS);
  2266. #endif
  2267. }
  2268. /* edit list atom */
  2269. static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2270. {
  2271. MOVStreamContext *sc;
  2272. int i, edit_count, version, edit_start_index = 0;
  2273. if (c->fc->nb_streams < 1)
  2274. return 0;
  2275. sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
  2276. version = avio_r8(pb); /* version */
  2277. avio_rb24(pb); /* flags */
  2278. edit_count = avio_rb32(pb); /* entries */
  2279. if ((uint64_t)edit_count*12+8 > atom.size)
  2280. return AVERROR_INVALIDDATA;
  2281. for (i=0; i<edit_count; i++){
  2282. int64_t time;
  2283. int64_t duration;
  2284. if (version == 1) {
  2285. duration = avio_rb64(pb);
  2286. time = avio_rb64(pb);
  2287. } else {
  2288. duration = avio_rb32(pb); /* segment duration */
  2289. time = (int32_t)avio_rb32(pb); /* media time */
  2290. }
  2291. avio_rb32(pb); /* Media rate */
  2292. if (i == 0 && time == -1) {
  2293. sc->empty_duration = duration;
  2294. edit_start_index = 1;
  2295. } else if (i == edit_start_index && time >= 0)
  2296. sc->start_time = time;
  2297. }
  2298. if (edit_count > 1)
  2299. av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
  2300. "a/v desync might occur, patch welcome\n");
  2301. av_dlog(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
  2302. return 0;
  2303. }
  2304. static int mov_read_chan2(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2305. {
  2306. if (atom.size < 16)
  2307. return 0;
  2308. avio_skip(pb, 4);
  2309. ff_mov_read_chan(c->fc, pb, c->fc->streams[0], atom.size - 4);
  2310. return 0;
  2311. }
  2312. static int mov_read_tmcd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2313. {
  2314. uint32_t i;
  2315. MOVStreamContext *sc;
  2316. if (c->fc->nb_streams < 1)
  2317. return AVERROR_INVALIDDATA;
  2318. sc = c->fc->streams[c->fc->nb_streams - 1]->priv_data;
  2319. if (atom.size < 4)
  2320. return 0;
  2321. sc->trefs_count = atom.size / 4;
  2322. sc->trefs = av_malloc(sc->trefs_count * sizeof(*sc->trefs));
  2323. if (!sc->trefs)
  2324. return AVERROR(ENOMEM);
  2325. sc->tref_type = atom.type;
  2326. for (i = 0; i < sc->trefs_count; i++)
  2327. sc->trefs[i] = avio_rb32(pb);
  2328. return 0;
  2329. }
  2330. static const MOVParseTableEntry mov_default_parse_table[] = {
  2331. { MKTAG('A','C','L','R'), mov_read_avid },
  2332. { MKTAG('A','P','R','G'), mov_read_avid },
  2333. { MKTAG('A','A','L','P'), mov_read_avid },
  2334. { MKTAG('A','R','E','S'), mov_read_avid },
  2335. { MKTAG('a','v','s','s'), mov_read_avss },
  2336. { MKTAG('c','h','p','l'), mov_read_chpl },
  2337. { MKTAG('c','o','6','4'), mov_read_stco },
  2338. { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
  2339. { MKTAG('d','i','n','f'), mov_read_default },
  2340. { MKTAG('d','r','e','f'), mov_read_dref },
  2341. { MKTAG('e','d','t','s'), mov_read_default },
  2342. { MKTAG('e','l','s','t'), mov_read_elst },
  2343. { MKTAG('e','n','d','a'), mov_read_enda },
  2344. { MKTAG('f','i','e','l'), mov_read_fiel },
  2345. { MKTAG('f','t','y','p'), mov_read_ftyp },
  2346. { MKTAG('g','l','b','l'), mov_read_glbl },
  2347. { MKTAG('h','d','l','r'), mov_read_hdlr },
  2348. { MKTAG('i','l','s','t'), mov_read_ilst },
  2349. { MKTAG('j','p','2','h'), mov_read_jp2h },
  2350. { MKTAG('m','d','a','t'), mov_read_mdat },
  2351. { MKTAG('m','d','h','d'), mov_read_mdhd },
  2352. { MKTAG('m','d','i','a'), mov_read_default },
  2353. { MKTAG('m','e','t','a'), mov_read_meta },
  2354. { MKTAG('m','i','n','f'), mov_read_default },
  2355. { MKTAG('m','o','o','f'), mov_read_moof },
  2356. { MKTAG('m','o','o','v'), mov_read_moov },
  2357. { MKTAG('m','v','e','x'), mov_read_default },
  2358. { MKTAG('m','v','h','d'), mov_read_mvhd },
  2359. { MKTAG('S','M','I',' '), mov_read_svq3 },
  2360. { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
  2361. { MKTAG('a','v','c','C'), mov_read_glbl },
  2362. { MKTAG('p','a','s','p'), mov_read_pasp },
  2363. { MKTAG('s','t','b','l'), mov_read_default },
  2364. { MKTAG('s','t','c','o'), mov_read_stco },
  2365. { MKTAG('s','t','p','s'), mov_read_stps },
  2366. { MKTAG('s','t','r','f'), mov_read_strf },
  2367. { MKTAG('s','t','s','c'), mov_read_stsc },
  2368. { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
  2369. { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
  2370. { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
  2371. { MKTAG('s','t','t','s'), mov_read_stts },
  2372. { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
  2373. { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
  2374. { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
  2375. { MKTAG('t','r','a','k'), mov_read_trak },
  2376. { MKTAG('t','r','a','f'), mov_read_default },
  2377. { MKTAG('t','r','e','f'), mov_read_default },
  2378. { MKTAG('t','m','c','d'), mov_read_tmcd },
  2379. { MKTAG('c','h','a','p'), mov_read_chap },
  2380. { MKTAG('t','r','e','x'), mov_read_trex },
  2381. { MKTAG('t','r','u','n'), mov_read_trun },
  2382. { MKTAG('u','d','t','a'), mov_read_default },
  2383. { MKTAG('w','a','v','e'), mov_read_wave },
  2384. { MKTAG('e','s','d','s'), mov_read_esds },
  2385. { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
  2386. { MKTAG('d','e','c','3'), mov_read_dec3 }, /* EAC-3 info */
  2387. { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
  2388. { MKTAG('w','f','e','x'), mov_read_wfex },
  2389. { MKTAG('c','m','o','v'), mov_read_cmov },
  2390. { MKTAG('c','h','a','n'), mov_read_chan }, /* channel layout */
  2391. { MKTAG('d','v','c','1'), mov_read_dvc1 },
  2392. { MKTAG('s','b','g','p'), mov_read_sbgp },
  2393. { 0, NULL }
  2394. };
  2395. static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2396. {
  2397. int64_t total_size = 0;
  2398. MOVAtom a;
  2399. int i;
  2400. if (atom.size < 0)
  2401. atom.size = INT64_MAX;
  2402. while (total_size + 8 <= atom.size && !url_feof(pb)) {
  2403. int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
  2404. a.size = atom.size;
  2405. a.type=0;
  2406. if (atom.size >= 8) {
  2407. a.size = avio_rb32(pb);
  2408. a.type = avio_rl32(pb);
  2409. if (atom.type != MKTAG('r','o','o','t') &&
  2410. atom.type != MKTAG('m','o','o','v'))
  2411. {
  2412. if (a.type == MKTAG('t','r','a','k') || a.type == MKTAG('m','d','a','t'))
  2413. {
  2414. av_log(c->fc, AV_LOG_ERROR, "Broken file, trak/mdat not at top-level\n");
  2415. avio_skip(pb, -8);
  2416. return 0;
  2417. }
  2418. }
  2419. total_size += 8;
  2420. if (a.size == 1) { /* 64 bit extended size */
  2421. a.size = avio_rb64(pb) - 8;
  2422. total_size += 8;
  2423. }
  2424. }
  2425. av_dlog(c->fc, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
  2426. a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
  2427. if (a.size == 0) {
  2428. a.size = atom.size - total_size + 8;
  2429. }
  2430. a.size -= 8;
  2431. if (a.size < 0)
  2432. break;
  2433. a.size = FFMIN(a.size, atom.size - total_size);
  2434. for (i = 0; mov_default_parse_table[i].type; i++)
  2435. if (mov_default_parse_table[i].type == a.type) {
  2436. parse = mov_default_parse_table[i].parse;
  2437. break;
  2438. }
  2439. // container is user data
  2440. if (!parse && (atom.type == MKTAG('u','d','t','a') ||
  2441. atom.type == MKTAG('i','l','s','t')))
  2442. parse = mov_read_udta_string;
  2443. if (!parse) { /* skip leaf atoms data */
  2444. avio_skip(pb, a.size);
  2445. } else {
  2446. int64_t start_pos = avio_tell(pb);
  2447. int64_t left;
  2448. int err = parse(c, pb, a);
  2449. if (err < 0)
  2450. return err;
  2451. if (c->found_moov && c->found_mdat &&
  2452. ((!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX) ||
  2453. start_pos + a.size == avio_size(pb))) {
  2454. if (!pb->seekable || c->fc->flags & AVFMT_FLAG_IGNIDX)
  2455. c->next_root_atom = start_pos + a.size;
  2456. return 0;
  2457. }
  2458. left = a.size - avio_tell(pb) + start_pos;
  2459. if (left > 0) /* skip garbage at atom end */
  2460. avio_skip(pb, left);
  2461. else if(left < 0) {
  2462. av_log(c->fc, AV_LOG_DEBUG, "undoing overread of %"PRId64" in '%.4s'\n", -left, (char*)&a.type);
  2463. avio_seek(pb, left, SEEK_CUR);
  2464. }
  2465. }
  2466. total_size += a.size;
  2467. }
  2468. if (total_size < atom.size && atom.size < 0x7ffff)
  2469. avio_skip(pb, atom.size - total_size);
  2470. return 0;
  2471. }
  2472. static int mov_probe(AVProbeData *p)
  2473. {
  2474. unsigned int offset;
  2475. uint32_t tag;
  2476. int score = 0;
  2477. /* check file header */
  2478. offset = 0;
  2479. for (;;) {
  2480. /* ignore invalid offset */
  2481. if ((offset + 8) > (unsigned int)p->buf_size)
  2482. return score;
  2483. tag = AV_RL32(p->buf + offset + 4);
  2484. switch(tag) {
  2485. /* check for obvious tags */
  2486. case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
  2487. case MKTAG('m','o','o','v'):
  2488. case MKTAG('m','d','a','t'):
  2489. case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
  2490. case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
  2491. case MKTAG('f','t','y','p'):
  2492. return AVPROBE_SCORE_MAX;
  2493. /* those are more common words, so rate then a bit less */
  2494. case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
  2495. case MKTAG('w','i','d','e'):
  2496. case MKTAG('f','r','e','e'):
  2497. case MKTAG('j','u','n','k'):
  2498. case MKTAG('p','i','c','t'):
  2499. return AVPROBE_SCORE_MAX - 5;
  2500. case MKTAG(0x82,0x82,0x7f,0x7d):
  2501. case MKTAG('s','k','i','p'):
  2502. case MKTAG('u','u','i','d'):
  2503. case MKTAG('p','r','f','l'):
  2504. offset = AV_RB32(p->buf+offset) + offset;
  2505. /* if we only find those cause probedata is too small at least rate them */
  2506. score = AVPROBE_SCORE_MAX - 50;
  2507. break;
  2508. default:
  2509. /* unrecognized tag */
  2510. return score;
  2511. }
  2512. }
  2513. }
  2514. // must be done after parsing all trak because there's no order requirement
  2515. static void mov_read_chapters(AVFormatContext *s)
  2516. {
  2517. MOVContext *mov = s->priv_data;
  2518. AVStream *st = NULL;
  2519. MOVStreamContext *sc;
  2520. int64_t cur_pos;
  2521. int i;
  2522. for (i = 0; i < s->nb_streams; i++)
  2523. if (s->streams[i]->id == mov->chapter_track) {
  2524. st = s->streams[i];
  2525. break;
  2526. }
  2527. if (!st) {
  2528. av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
  2529. return;
  2530. }
  2531. st->discard = AVDISCARD_ALL;
  2532. sc = st->priv_data;
  2533. cur_pos = avio_tell(sc->pb);
  2534. for (i = 0; i < st->nb_index_entries; i++) {
  2535. AVIndexEntry *sample = &st->index_entries[i];
  2536. int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
  2537. uint8_t *title;
  2538. uint16_t ch;
  2539. int len, title_len;
  2540. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  2541. av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
  2542. goto finish;
  2543. }
  2544. // the first two bytes are the length of the title
  2545. len = avio_rb16(sc->pb);
  2546. if (len > sample->size-2)
  2547. continue;
  2548. title_len = 2*len + 1;
  2549. if (!(title = av_mallocz(title_len)))
  2550. goto finish;
  2551. // The samples could theoretically be in any encoding if there's an encd
  2552. // atom following, but in practice are only utf-8 or utf-16, distinguished
  2553. // instead by the presence of a BOM
  2554. if (!len) {
  2555. title[0] = 0;
  2556. } else {
  2557. ch = avio_rb16(sc->pb);
  2558. if (ch == 0xfeff)
  2559. avio_get_str16be(sc->pb, len, title, title_len);
  2560. else if (ch == 0xfffe)
  2561. avio_get_str16le(sc->pb, len, title, title_len);
  2562. else {
  2563. AV_WB16(title, ch);
  2564. if (len == 1 || len == 2)
  2565. title[len] = 0;
  2566. else
  2567. avio_get_str(sc->pb, INT_MAX, title + 2, len - 1);
  2568. }
  2569. }
  2570. avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
  2571. av_freep(&title);
  2572. }
  2573. finish:
  2574. avio_seek(sc->pb, cur_pos, SEEK_SET);
  2575. }
  2576. static int parse_timecode_in_framenum_format(AVFormatContext *s, AVStream *st,
  2577. uint32_t value, int flags)
  2578. {
  2579. AVTimecode tc;
  2580. char buf[AV_TIMECODE_STR_SIZE];
  2581. AVRational rate = {st->codec->time_base.den,
  2582. st->codec->time_base.num};
  2583. int ret = av_timecode_init(&tc, rate, flags, 0, s);
  2584. if (ret < 0)
  2585. return ret;
  2586. av_dict_set(&st->metadata, "timecode",
  2587. av_timecode_make_string(&tc, buf, value), 0);
  2588. return 0;
  2589. }
  2590. static int mov_read_timecode_track(AVFormatContext *s, AVStream *st)
  2591. {
  2592. MOVStreamContext *sc = st->priv_data;
  2593. int flags = 0;
  2594. int64_t cur_pos = avio_tell(sc->pb);
  2595. uint32_t value;
  2596. if (!st->nb_index_entries)
  2597. return -1;
  2598. avio_seek(sc->pb, st->index_entries->pos, SEEK_SET);
  2599. value = avio_rb32(s->pb);
  2600. if (sc->tmcd_flags & 0x0001) flags |= AV_TIMECODE_FLAG_DROPFRAME;
  2601. if (sc->tmcd_flags & 0x0002) flags |= AV_TIMECODE_FLAG_24HOURSMAX;
  2602. if (sc->tmcd_flags & 0x0004) flags |= AV_TIMECODE_FLAG_ALLOWNEGATIVE;
  2603. /* Assume Counter flag is set to 1 in tmcd track (even though it is likely
  2604. * not the case) and thus assume "frame number format" instead of QT one.
  2605. * No sample with tmcd track can be found with a QT timecode at the moment,
  2606. * despite what the tmcd track "suggests" (Counter flag set to 0 means QT
  2607. * format). */
  2608. parse_timecode_in_framenum_format(s, st, value, flags);
  2609. avio_seek(sc->pb, cur_pos, SEEK_SET);
  2610. return 0;
  2611. }
  2612. static int mov_read_close(AVFormatContext *s)
  2613. {
  2614. MOVContext *mov = s->priv_data;
  2615. int i, j;
  2616. for (i = 0; i < s->nb_streams; i++) {
  2617. AVStream *st = s->streams[i];
  2618. MOVStreamContext *sc = st->priv_data;
  2619. av_freep(&sc->ctts_data);
  2620. for (j = 0; j < sc->drefs_count; j++) {
  2621. av_freep(&sc->drefs[j].path);
  2622. av_freep(&sc->drefs[j].dir);
  2623. }
  2624. av_freep(&sc->drefs);
  2625. av_freep(&sc->trefs);
  2626. if (sc->pb && sc->pb != s->pb)
  2627. avio_close(sc->pb);
  2628. sc->pb = NULL;
  2629. av_freep(&sc->chunk_offsets);
  2630. av_freep(&sc->keyframes);
  2631. av_freep(&sc->sample_sizes);
  2632. av_freep(&sc->stps_data);
  2633. av_freep(&sc->stsc_data);
  2634. av_freep(&sc->stts_data);
  2635. }
  2636. if (mov->dv_demux) {
  2637. for (i = 0; i < mov->dv_fctx->nb_streams; i++) {
  2638. av_freep(&mov->dv_fctx->streams[i]->codec);
  2639. av_freep(&mov->dv_fctx->streams[i]);
  2640. }
  2641. av_freep(&mov->dv_fctx);
  2642. av_freep(&mov->dv_demux);
  2643. }
  2644. av_freep(&mov->trex_data);
  2645. return 0;
  2646. }
  2647. static int tmcd_is_referenced(AVFormatContext *s, int tmcd_id)
  2648. {
  2649. int i, j;
  2650. for (i = 0; i < s->nb_streams; i++) {
  2651. AVStream *st = s->streams[i];
  2652. MOVStreamContext *sc = st->priv_data;
  2653. if (s->streams[i]->codec->codec_type != AVMEDIA_TYPE_VIDEO)
  2654. continue;
  2655. for (j = 0; j < sc->trefs_count; j++)
  2656. if (tmcd_id == sc->trefs[j])
  2657. return 1;
  2658. }
  2659. return 0;
  2660. }
  2661. /* look for a tmcd track not referenced by any video track, and export it globally */
  2662. static void export_orphan_timecode(AVFormatContext *s)
  2663. {
  2664. int i;
  2665. for (i = 0; i < s->nb_streams; i++) {
  2666. AVStream *st = s->streams[i];
  2667. if (st->codec->codec_tag == MKTAG('t','m','c','d') &&
  2668. !tmcd_is_referenced(s, i + 1)) {
  2669. AVDictionaryEntry *tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
  2670. if (tcr) {
  2671. av_dict_set(&s->metadata, "timecode", tcr->value, 0);
  2672. break;
  2673. }
  2674. }
  2675. }
  2676. }
  2677. static int mov_read_header(AVFormatContext *s)
  2678. {
  2679. MOVContext *mov = s->priv_data;
  2680. AVIOContext *pb = s->pb;
  2681. int i, err;
  2682. MOVAtom atom = { AV_RL32("root") };
  2683. mov->fc = s;
  2684. /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  2685. if (pb->seekable)
  2686. atom.size = avio_size(pb);
  2687. else
  2688. atom.size = INT64_MAX;
  2689. /* check MOV header */
  2690. if ((err = mov_read_default(mov, pb, atom)) < 0) {
  2691. av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
  2692. mov_read_close(s);
  2693. return err;
  2694. }
  2695. if (!mov->found_moov) {
  2696. av_log(s, AV_LOG_ERROR, "moov atom not found\n");
  2697. mov_read_close(s);
  2698. return AVERROR_INVALIDDATA;
  2699. }
  2700. av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
  2701. if (pb->seekable) {
  2702. if (mov->chapter_track > 0)
  2703. mov_read_chapters(s);
  2704. for (i = 0; i < s->nb_streams; i++)
  2705. if (s->streams[i]->codec->codec_tag == AV_RL32("tmcd"))
  2706. mov_read_timecode_track(s, s->streams[i]);
  2707. }
  2708. /* copy timecode metadata from tmcd tracks to the related video streams */
  2709. for (i = 0; i < s->nb_streams; i++) {
  2710. AVStream *st = s->streams[i];
  2711. MOVStreamContext *sc = st->priv_data;
  2712. if (sc->tref_type == AV_RL32("tmcd") && sc->trefs_count) {
  2713. AVDictionaryEntry *tcr;
  2714. int tmcd_st_id = sc->trefs[0] - 1;
  2715. if (tmcd_st_id < 0 || tmcd_st_id >= s->nb_streams)
  2716. continue;
  2717. tcr = av_dict_get(s->streams[tmcd_st_id]->metadata, "timecode", NULL, 0);
  2718. if (tcr)
  2719. av_dict_set(&st->metadata, "timecode", tcr->value, 0);
  2720. }
  2721. }
  2722. export_orphan_timecode(s);
  2723. for (i = 0; i < s->nb_streams; i++) {
  2724. AVStream *st = s->streams[i];
  2725. MOVStreamContext *sc = st->priv_data;
  2726. if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->codec->codec_id == AV_CODEC_ID_AAC) {
  2727. if(!sc->start_pad)
  2728. sc->start_pad = 1024;
  2729. st->skip_samples = sc->start_pad;
  2730. }
  2731. }
  2732. if (mov->trex_data) {
  2733. for (i = 0; i < s->nb_streams; i++) {
  2734. AVStream *st = s->streams[i];
  2735. MOVStreamContext *sc = st->priv_data;
  2736. if (st->duration)
  2737. st->codec->bit_rate = sc->data_size * 8 * sc->time_scale / st->duration;
  2738. }
  2739. }
  2740. return 0;
  2741. }
  2742. static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
  2743. {
  2744. AVIndexEntry *sample = NULL;
  2745. int64_t best_dts = INT64_MAX;
  2746. int i;
  2747. for (i = 0; i < s->nb_streams; i++) {
  2748. AVStream *avst = s->streams[i];
  2749. MOVStreamContext *msc = avst->priv_data;
  2750. if (msc->pb && msc->current_sample < avst->nb_index_entries) {
  2751. AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
  2752. int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
  2753. av_dlog(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  2754. if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
  2755. (s->pb->seekable &&
  2756. ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
  2757. ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
  2758. (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
  2759. sample = current_sample;
  2760. best_dts = dts;
  2761. *st = avst;
  2762. }
  2763. }
  2764. }
  2765. return sample;
  2766. }
  2767. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  2768. {
  2769. MOVContext *mov = s->priv_data;
  2770. MOVStreamContext *sc;
  2771. AVIndexEntry *sample;
  2772. AVStream *st = NULL;
  2773. int ret;
  2774. mov->fc = s;
  2775. retry:
  2776. sample = mov_find_next_sample(s, &st);
  2777. if (!sample) {
  2778. mov->found_mdat = 0;
  2779. if (!mov->next_root_atom)
  2780. return AVERROR_EOF;
  2781. avio_seek(s->pb, mov->next_root_atom, SEEK_SET);
  2782. mov->next_root_atom = 0;
  2783. if (mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
  2784. url_feof(s->pb))
  2785. return AVERROR_EOF;
  2786. av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
  2787. goto retry;
  2788. }
  2789. sc = st->priv_data;
  2790. /* must be done just before reading, to avoid infinite loop on sample */
  2791. sc->current_sample++;
  2792. if (st->discard != AVDISCARD_ALL) {
  2793. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  2794. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
  2795. sc->ffindex, sample->pos);
  2796. return AVERROR_INVALIDDATA;
  2797. }
  2798. ret = av_get_packet(sc->pb, pkt, sample->size);
  2799. if (ret < 0)
  2800. return ret;
  2801. if (sc->has_palette) {
  2802. uint8_t *pal;
  2803. pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
  2804. if (!pal) {
  2805. av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
  2806. } else {
  2807. memcpy(pal, sc->palette, AVPALETTE_SIZE);
  2808. sc->has_palette = 0;
  2809. }
  2810. }
  2811. #if CONFIG_DV_DEMUXER
  2812. if (mov->dv_demux && sc->dv_audio_container) {
  2813. avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
  2814. av_free(pkt->data);
  2815. pkt->size = 0;
  2816. ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
  2817. if (ret < 0)
  2818. return ret;
  2819. }
  2820. #endif
  2821. }
  2822. pkt->stream_index = sc->ffindex;
  2823. pkt->dts = sample->timestamp;
  2824. if (sc->ctts_data && sc->ctts_index < sc->ctts_count) {
  2825. pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
  2826. /* update ctts context */
  2827. sc->ctts_sample++;
  2828. if (sc->ctts_index < sc->ctts_count &&
  2829. sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
  2830. sc->ctts_index++;
  2831. sc->ctts_sample = 0;
  2832. }
  2833. if (sc->wrong_dts)
  2834. pkt->dts = AV_NOPTS_VALUE;
  2835. } else {
  2836. int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
  2837. st->index_entries[sc->current_sample].timestamp : st->duration;
  2838. pkt->duration = next_dts - pkt->dts;
  2839. pkt->pts = pkt->dts;
  2840. }
  2841. if (st->discard == AVDISCARD_ALL)
  2842. goto retry;
  2843. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
  2844. pkt->pos = sample->pos;
  2845. av_dlog(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
  2846. pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
  2847. return 0;
  2848. }
  2849. static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
  2850. {
  2851. MOVStreamContext *sc = st->priv_data;
  2852. int sample, time_sample;
  2853. int i;
  2854. sample = av_index_search_timestamp(st, timestamp, flags);
  2855. av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  2856. if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
  2857. sample = 0;
  2858. if (sample < 0) /* not sure what to do */
  2859. return AVERROR_INVALIDDATA;
  2860. sc->current_sample = sample;
  2861. av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
  2862. /* adjust ctts index */
  2863. if (sc->ctts_data) {
  2864. time_sample = 0;
  2865. for (i = 0; i < sc->ctts_count; i++) {
  2866. int next = time_sample + sc->ctts_data[i].count;
  2867. if (next > sc->current_sample) {
  2868. sc->ctts_index = i;
  2869. sc->ctts_sample = sc->current_sample - time_sample;
  2870. break;
  2871. }
  2872. time_sample = next;
  2873. }
  2874. }
  2875. return sample;
  2876. }
  2877. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  2878. {
  2879. AVStream *st;
  2880. int64_t seek_timestamp, timestamp;
  2881. int sample;
  2882. int i;
  2883. if (stream_index >= s->nb_streams)
  2884. return AVERROR_INVALIDDATA;
  2885. st = s->streams[stream_index];
  2886. sample = mov_seek_stream(s, st, sample_time, flags);
  2887. if (sample < 0)
  2888. return sample;
  2889. /* adjust seek timestamp to found sample timestamp */
  2890. seek_timestamp = st->index_entries[sample].timestamp;
  2891. for (i = 0; i < s->nb_streams; i++) {
  2892. MOVStreamContext *sc = s->streams[i]->priv_data;
  2893. st = s->streams[i];
  2894. st->skip_samples = (sample_time <= 0) ? sc->start_pad : 0;
  2895. if (stream_index == i)
  2896. continue;
  2897. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  2898. mov_seek_stream(s, st, timestamp, flags);
  2899. }
  2900. return 0;
  2901. }
  2902. static const AVOption options[] = {
  2903. {"use_absolute_path",
  2904. "allow using absolute path when opening alias, this is a possible security issue",
  2905. offsetof(MOVContext, use_absolute_path), FF_OPT_TYPE_INT, {.dbl = 0},
  2906. 0, 1, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_DECODING_PARAM},
  2907. {NULL}
  2908. };
  2909. static const AVClass class = {
  2910. .class_name = "mov,mp4,m4a,3gp,3g2,mj2",
  2911. .item_name = av_default_item_name,
  2912. .option = options,
  2913. .version = LIBAVUTIL_VERSION_INT,
  2914. };
  2915. AVInputFormat ff_mov_demuxer = {
  2916. .name = "mov,mp4,m4a,3gp,3g2,mj2",
  2917. .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
  2918. .priv_data_size = sizeof(MOVContext),
  2919. .read_probe = mov_probe,
  2920. .read_header = mov_read_header,
  2921. .read_packet = mov_read_packet,
  2922. .read_close = mov_read_close,
  2923. .read_seek = mov_read_seek,
  2924. .priv_class = &class,
  2925. };