mov.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776
  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/intreadwrite.h"
  26. #include "libavutil/intfloat_readwrite.h"
  27. #include "libavutil/mathematics.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/dict.h"
  30. #include "avformat.h"
  31. #include "avio_internal.h"
  32. #include "riff.h"
  33. #include "isom.h"
  34. #include "libavcodec/get_bits.h"
  35. #include "id3v1.h"
  36. #if CONFIG_ZLIB
  37. #include <zlib.h>
  38. #endif
  39. /*
  40. * First version by Francois Revol revol@free.fr
  41. * Seek function by Gael Chardon gael.dev@4now.net
  42. *
  43. * Features and limitations:
  44. * - reads most of the QT files I have (at least the structure),
  45. * Sample QuickTime files with mp3 audio can be found at: http://www.3ivx.com/showcase.html
  46. * - the code is quite ugly... maybe I won't do it recursive next time :-)
  47. *
  48. * Funny I didn't know about http://sourceforge.net/projects/qt-ffmpeg/
  49. * when coding this :) (it's a writer anyway)
  50. *
  51. * Reference documents:
  52. * http://www.geocities.com/xhelmboyx/quicktime/formats/qtm-layout.txt
  53. * Apple:
  54. * http://developer.apple.com/documentation/QuickTime/QTFF/
  55. * http://developer.apple.com/documentation/QuickTime/QTFF/qtff.pdf
  56. * QuickTime is a trademark of Apple (AFAIK :))
  57. */
  58. #include "qtpalette.h"
  59. #undef NDEBUG
  60. #include <assert.h>
  61. /* XXX: it's the first time I make a recursive parser I think... sorry if it's ugly :P */
  62. /* those functions parse an atom */
  63. /* return code:
  64. 0: continue to parse next atom
  65. <0: error occurred, exit
  66. */
  67. /* links atom IDs to parse functions */
  68. typedef struct MOVParseTableEntry {
  69. uint32_t type;
  70. int (*parse)(MOVContext *ctx, AVIOContext *pb, MOVAtom atom);
  71. } MOVParseTableEntry;
  72. static const MOVParseTableEntry mov_default_parse_table[];
  73. static int mov_metadata_track_or_disc_number(MOVContext *c, AVIOContext *pb,
  74. unsigned len, const char *key)
  75. {
  76. char buf[16];
  77. short current, total;
  78. avio_rb16(pb); // unknown
  79. current = avio_rb16(pb);
  80. total = avio_rb16(pb);
  81. if (!total)
  82. snprintf(buf, sizeof(buf), "%d", current);
  83. else
  84. snprintf(buf, sizeof(buf), "%d/%d", current, total);
  85. av_dict_set(&c->fc->metadata, key, buf, 0);
  86. return 0;
  87. }
  88. static int mov_metadata_int8_bypass_padding(MOVContext *c, AVIOContext *pb,
  89. unsigned len, const char *key)
  90. {
  91. char buf[16];
  92. /* bypass padding bytes */
  93. avio_r8(pb);
  94. avio_r8(pb);
  95. avio_r8(pb);
  96. snprintf(buf, sizeof(buf), "%hu", avio_r8(pb));
  97. av_dict_set(&c->fc->metadata, key, buf, 0);
  98. return 0;
  99. }
  100. static int mov_metadata_int8_no_padding(MOVContext *c, AVIOContext *pb,
  101. unsigned len, const char *key)
  102. {
  103. char buf[16];
  104. snprintf(buf, sizeof(buf), "%hu", avio_r8(pb));
  105. av_dict_set(&c->fc->metadata, key, buf, 0);
  106. return 0;
  107. }
  108. static int mov_metadata_gnre(MOVContext *c, AVIOContext *pb,
  109. unsigned len, const char *key)
  110. {
  111. short genre;
  112. char buf[20];
  113. avio_r8(pb); // unknown
  114. genre = avio_r8(pb);
  115. if (genre < 1 || genre > ID3v1_GENRE_MAX)
  116. return 0;
  117. snprintf(buf, sizeof(buf), "%s", ff_id3v1_genre_str[genre-1]);
  118. av_dict_set(&c->fc->metadata, key, buf, 0);
  119. return 0;
  120. }
  121. static const uint32_t mac_to_unicode[128] = {
  122. 0x00C4,0x00C5,0x00C7,0x00C9,0x00D1,0x00D6,0x00DC,0x00E1,
  123. 0x00E0,0x00E2,0x00E4,0x00E3,0x00E5,0x00E7,0x00E9,0x00E8,
  124. 0x00EA,0x00EB,0x00ED,0x00EC,0x00EE,0x00EF,0x00F1,0x00F3,
  125. 0x00F2,0x00F4,0x00F6,0x00F5,0x00FA,0x00F9,0x00FB,0x00FC,
  126. 0x2020,0x00B0,0x00A2,0x00A3,0x00A7,0x2022,0x00B6,0x00DF,
  127. 0x00AE,0x00A9,0x2122,0x00B4,0x00A8,0x2260,0x00C6,0x00D8,
  128. 0x221E,0x00B1,0x2264,0x2265,0x00A5,0x00B5,0x2202,0x2211,
  129. 0x220F,0x03C0,0x222B,0x00AA,0x00BA,0x03A9,0x00E6,0x00F8,
  130. 0x00BF,0x00A1,0x00AC,0x221A,0x0192,0x2248,0x2206,0x00AB,
  131. 0x00BB,0x2026,0x00A0,0x00C0,0x00C3,0x00D5,0x0152,0x0153,
  132. 0x2013,0x2014,0x201C,0x201D,0x2018,0x2019,0x00F7,0x25CA,
  133. 0x00FF,0x0178,0x2044,0x20AC,0x2039,0x203A,0xFB01,0xFB02,
  134. 0x2021,0x00B7,0x201A,0x201E,0x2030,0x00C2,0x00CA,0x00C1,
  135. 0x00CB,0x00C8,0x00CD,0x00CE,0x00CF,0x00CC,0x00D3,0x00D4,
  136. 0xF8FF,0x00D2,0x00DA,0x00DB,0x00D9,0x0131,0x02C6,0x02DC,
  137. 0x00AF,0x02D8,0x02D9,0x02DA,0x00B8,0x02DD,0x02DB,0x02C7,
  138. };
  139. static int mov_read_mac_string(MOVContext *c, AVIOContext *pb, int len,
  140. char *dst, int dstlen)
  141. {
  142. char *p = dst;
  143. char *end = dst+dstlen-1;
  144. int i;
  145. for (i = 0; i < len; i++) {
  146. uint8_t t, c = avio_r8(pb);
  147. if (c < 0x80 && p < end)
  148. *p++ = c;
  149. else
  150. PUT_UTF8(mac_to_unicode[c-0x80], t, if (p < end) *p++ = t;);
  151. }
  152. *p = 0;
  153. return p - dst;
  154. }
  155. static int mov_read_udta_string(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  156. {
  157. #ifdef MOV_EXPORT_ALL_METADATA
  158. char tmp_key[5];
  159. #endif
  160. char str[1024], key2[16], language[4] = {0};
  161. const char *key = NULL;
  162. uint16_t str_size, langcode = 0;
  163. uint32_t data_type = 0;
  164. int (*parse)(MOVContext*, AVIOContext*, unsigned, const char*) = NULL;
  165. switch (atom.type) {
  166. case MKTAG(0xa9,'n','a','m'): key = "title"; break;
  167. case MKTAG(0xa9,'a','u','t'):
  168. case MKTAG(0xa9,'A','R','T'): key = "artist"; break;
  169. case MKTAG( 'a','A','R','T'): key = "album_artist"; break;
  170. case MKTAG(0xa9,'w','r','t'): key = "composer"; break;
  171. case MKTAG( 'c','p','r','t'):
  172. case MKTAG(0xa9,'c','p','y'): key = "copyright"; break;
  173. case MKTAG(0xa9,'g','r','p'): key = "grouping"; break;
  174. case MKTAG(0xa9,'l','y','r'): key = "lyrics"; break;
  175. case MKTAG(0xa9,'c','m','t'):
  176. case MKTAG(0xa9,'i','n','f'): key = "comment"; break;
  177. case MKTAG(0xa9,'a','l','b'): key = "album"; break;
  178. case MKTAG(0xa9,'d','a','y'): key = "date"; break;
  179. case MKTAG(0xa9,'g','e','n'): key = "genre"; break;
  180. case MKTAG( 'g','n','r','e'): key = "genre";
  181. parse = mov_metadata_gnre; break;
  182. case MKTAG(0xa9,'t','o','o'):
  183. case MKTAG(0xa9,'s','w','r'): key = "encoder"; break;
  184. case MKTAG(0xa9,'e','n','c'): key = "encoder"; break;
  185. case MKTAG( 'd','e','s','c'): key = "description";break;
  186. case MKTAG( 'l','d','e','s'): key = "synopsis"; break;
  187. case MKTAG( 't','v','s','h'): key = "show"; break;
  188. case MKTAG( 't','v','e','n'): key = "episode_id";break;
  189. case MKTAG( 't','v','n','n'): key = "network"; break;
  190. case MKTAG( 't','r','k','n'): key = "track";
  191. parse = mov_metadata_track_or_disc_number; break;
  192. case MKTAG( 'd','i','s','k'): key = "disc";
  193. parse = mov_metadata_track_or_disc_number; break;
  194. case MKTAG( 't','v','e','s'): key = "episode_sort";
  195. parse = mov_metadata_int8_bypass_padding; break;
  196. case MKTAG( 't','v','s','n'): key = "season_number";
  197. parse = mov_metadata_int8_bypass_padding; break;
  198. case MKTAG( 's','t','i','k'): key = "media_type";
  199. parse = mov_metadata_int8_no_padding; break;
  200. case MKTAG( 'h','d','v','d'): key = "hd_video";
  201. parse = mov_metadata_int8_no_padding; break;
  202. case MKTAG( 'p','g','a','p'): key = "gapless_playback";
  203. parse = mov_metadata_int8_no_padding; break;
  204. }
  205. if (c->itunes_metadata && atom.size > 8) {
  206. int data_size = avio_rb32(pb);
  207. int tag = avio_rl32(pb);
  208. if (tag == MKTAG('d','a','t','a')) {
  209. data_type = avio_rb32(pb); // type
  210. avio_rb32(pb); // unknown
  211. str_size = data_size - 16;
  212. atom.size -= 16;
  213. } else return 0;
  214. } else if (atom.size > 4 && key && !c->itunes_metadata) {
  215. str_size = avio_rb16(pb); // string length
  216. langcode = avio_rb16(pb);
  217. ff_mov_lang_to_iso639(langcode, language);
  218. atom.size -= 4;
  219. } else
  220. str_size = atom.size;
  221. #ifdef MOV_EXPORT_ALL_METADATA
  222. if (!key) {
  223. snprintf(tmp_key, 5, "%.4s", (char*)&atom.type);
  224. key = tmp_key;
  225. }
  226. #endif
  227. if (!key)
  228. return 0;
  229. if (atom.size < 0)
  230. return -1;
  231. str_size = FFMIN3(sizeof(str)-1, str_size, atom.size);
  232. if (parse)
  233. parse(c, pb, str_size, key);
  234. else {
  235. if (data_type == 3 || (data_type == 0 && langcode < 0x800)) { // MAC Encoded
  236. mov_read_mac_string(c, pb, str_size, str, sizeof(str));
  237. } else {
  238. avio_read(pb, str, str_size);
  239. str[str_size] = 0;
  240. }
  241. av_dict_set(&c->fc->metadata, key, str, 0);
  242. if (*language && strcmp(language, "und")) {
  243. snprintf(key2, sizeof(key2), "%s-%s", key, language);
  244. av_dict_set(&c->fc->metadata, key2, str, 0);
  245. }
  246. }
  247. av_dlog(c->fc, "lang \"%3s\" ", language);
  248. av_dlog(c->fc, "tag \"%s\" value \"%s\" atom \"%.4s\" %d %"PRId64"\n",
  249. key, str, (char*)&atom.type, str_size, atom.size);
  250. return 0;
  251. }
  252. static int mov_read_chpl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  253. {
  254. int64_t start;
  255. int i, nb_chapters, str_len, version;
  256. char str[256+1];
  257. if ((atom.size -= 5) < 0)
  258. return 0;
  259. version = avio_r8(pb);
  260. avio_rb24(pb);
  261. if (version)
  262. avio_rb32(pb); // ???
  263. nb_chapters = avio_r8(pb);
  264. for (i = 0; i < nb_chapters; i++) {
  265. if (atom.size < 9)
  266. return 0;
  267. start = avio_rb64(pb);
  268. str_len = avio_r8(pb);
  269. if ((atom.size -= 9+str_len) < 0)
  270. return 0;
  271. avio_read(pb, str, str_len);
  272. str[str_len] = 0;
  273. avpriv_new_chapter(c->fc, i, (AVRational){1,10000000}, start, AV_NOPTS_VALUE, str);
  274. }
  275. return 0;
  276. }
  277. static int mov_read_default(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  278. {
  279. int64_t total_size = 0;
  280. MOVAtom a;
  281. int i;
  282. if (atom.size < 0)
  283. atom.size = INT64_MAX;
  284. while (total_size + 8 < atom.size && !url_feof(pb)) {
  285. int (*parse)(MOVContext*, AVIOContext*, MOVAtom) = NULL;
  286. a.size = atom.size;
  287. a.type=0;
  288. if (atom.size >= 8) {
  289. a.size = avio_rb32(pb);
  290. a.type = avio_rl32(pb);
  291. }
  292. av_dlog(c->fc, "type: %08x '%.4s' parent:'%.4s' sz: %"PRId64" %"PRId64" %"PRId64"\n",
  293. a.type, (char*)&a.type, (char*)&atom.type, a.size, total_size, atom.size);
  294. total_size += 8;
  295. if (a.size == 1) { /* 64 bit extended size */
  296. a.size = avio_rb64(pb) - 8;
  297. total_size += 8;
  298. }
  299. if (a.size == 0) {
  300. a.size = atom.size - total_size;
  301. if (a.size <= 8)
  302. break;
  303. }
  304. a.size -= 8;
  305. if (a.size < 0)
  306. break;
  307. a.size = FFMIN(a.size, atom.size - total_size);
  308. for (i = 0; mov_default_parse_table[i].type; i++)
  309. if (mov_default_parse_table[i].type == a.type) {
  310. parse = mov_default_parse_table[i].parse;
  311. break;
  312. }
  313. // container is user data
  314. if (!parse && (atom.type == MKTAG('u','d','t','a') ||
  315. atom.type == MKTAG('i','l','s','t')))
  316. parse = mov_read_udta_string;
  317. if (!parse) { /* skip leaf atoms data */
  318. avio_skip(pb, a.size);
  319. } else {
  320. int64_t start_pos = avio_tell(pb);
  321. int64_t left;
  322. int err = parse(c, pb, a);
  323. if (err < 0)
  324. return err;
  325. if (c->found_moov && c->found_mdat &&
  326. (!pb->seekable || start_pos + a.size == avio_size(pb)))
  327. return 0;
  328. left = a.size - avio_tell(pb) + start_pos;
  329. if (left > 0) /* skip garbage at atom end */
  330. avio_skip(pb, left);
  331. }
  332. total_size += a.size;
  333. }
  334. if (total_size < atom.size && atom.size < 0x7ffff)
  335. avio_skip(pb, atom.size - total_size);
  336. return 0;
  337. }
  338. static int mov_read_dref(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  339. {
  340. AVStream *st;
  341. MOVStreamContext *sc;
  342. int entries, i, j;
  343. if (c->fc->nb_streams < 1)
  344. return 0;
  345. st = c->fc->streams[c->fc->nb_streams-1];
  346. sc = st->priv_data;
  347. avio_rb32(pb); // version + flags
  348. entries = avio_rb32(pb);
  349. if (entries >= UINT_MAX / sizeof(*sc->drefs))
  350. return -1;
  351. sc->drefs = av_mallocz(entries * sizeof(*sc->drefs));
  352. if (!sc->drefs)
  353. return AVERROR(ENOMEM);
  354. sc->drefs_count = entries;
  355. for (i = 0; i < sc->drefs_count; i++) {
  356. MOVDref *dref = &sc->drefs[i];
  357. uint32_t size = avio_rb32(pb);
  358. int64_t next = avio_tell(pb) + size - 4;
  359. if (size < 12)
  360. return -1;
  361. dref->type = avio_rl32(pb);
  362. avio_rb32(pb); // version + flags
  363. av_dlog(c->fc, "type %.4s size %d\n", (char*)&dref->type, size);
  364. if (dref->type == MKTAG('a','l','i','s') && size > 150) {
  365. /* macintosh alias record */
  366. uint16_t volume_len, len;
  367. int16_t type;
  368. avio_skip(pb, 10);
  369. volume_len = avio_r8(pb);
  370. volume_len = FFMIN(volume_len, 27);
  371. avio_read(pb, dref->volume, 27);
  372. dref->volume[volume_len] = 0;
  373. av_log(c->fc, AV_LOG_DEBUG, "volume %s, len %d\n", dref->volume, volume_len);
  374. avio_skip(pb, 12);
  375. len = avio_r8(pb);
  376. len = FFMIN(len, 63);
  377. avio_read(pb, dref->filename, 63);
  378. dref->filename[len] = 0;
  379. av_log(c->fc, AV_LOG_DEBUG, "filename %s, len %d\n", dref->filename, len);
  380. avio_skip(pb, 16);
  381. /* read next level up_from_alias/down_to_target */
  382. dref->nlvl_from = avio_rb16(pb);
  383. dref->nlvl_to = avio_rb16(pb);
  384. av_log(c->fc, AV_LOG_DEBUG, "nlvl from %d, nlvl to %d\n",
  385. dref->nlvl_from, dref->nlvl_to);
  386. avio_skip(pb, 16);
  387. for (type = 0; type != -1 && avio_tell(pb) < next; ) {
  388. type = avio_rb16(pb);
  389. len = avio_rb16(pb);
  390. av_log(c->fc, AV_LOG_DEBUG, "type %d, len %d\n", type, len);
  391. if (len&1)
  392. len += 1;
  393. if (type == 2) { // absolute path
  394. av_free(dref->path);
  395. dref->path = av_mallocz(len+1);
  396. if (!dref->path)
  397. return AVERROR(ENOMEM);
  398. avio_read(pb, dref->path, len);
  399. if (len > volume_len && !strncmp(dref->path, dref->volume, volume_len)) {
  400. len -= volume_len;
  401. memmove(dref->path, dref->path+volume_len, len);
  402. dref->path[len] = 0;
  403. }
  404. for (j = 0; j < len; j++)
  405. if (dref->path[j] == ':')
  406. dref->path[j] = '/';
  407. av_log(c->fc, AV_LOG_DEBUG, "path %s\n", dref->path);
  408. } else if (type == 0) { // directory name
  409. av_free(dref->dir);
  410. dref->dir = av_malloc(len+1);
  411. if (!dref->dir)
  412. return AVERROR(ENOMEM);
  413. avio_read(pb, dref->dir, len);
  414. dref->dir[len] = 0;
  415. for (j = 0; j < len; j++)
  416. if (dref->dir[j] == ':')
  417. dref->dir[j] = '/';
  418. av_log(c->fc, AV_LOG_DEBUG, "dir %s\n", dref->dir);
  419. } else
  420. avio_skip(pb, len);
  421. }
  422. }
  423. avio_seek(pb, next, SEEK_SET);
  424. }
  425. return 0;
  426. }
  427. static int mov_read_hdlr(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  428. {
  429. AVStream *st;
  430. uint32_t type;
  431. uint32_t av_unused ctype;
  432. int title_size;
  433. char *title_str;
  434. if (c->fc->nb_streams < 1) // meta before first trak
  435. return 0;
  436. st = c->fc->streams[c->fc->nb_streams-1];
  437. avio_r8(pb); /* version */
  438. avio_rb24(pb); /* flags */
  439. /* component type */
  440. ctype = avio_rl32(pb);
  441. type = avio_rl32(pb); /* component subtype */
  442. av_dlog(c->fc, "ctype= %.4s (0x%08x)\n", (char*)&ctype, ctype);
  443. av_dlog(c->fc, "stype= %.4s\n", (char*)&type);
  444. if (type == MKTAG('v','i','d','e'))
  445. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  446. else if (type == MKTAG('s','o','u','n'))
  447. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  448. else if (type == MKTAG('m','1','a',' '))
  449. st->codec->codec_id = CODEC_ID_MP2;
  450. else if ((type == MKTAG('s','u','b','p')) || (type == MKTAG('c','l','c','p')))
  451. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  452. avio_rb32(pb); /* component manufacture */
  453. avio_rb32(pb); /* component flags */
  454. avio_rb32(pb); /* component flags mask */
  455. title_size = atom.size - 24;
  456. if (title_size > 0) {
  457. title_str = av_malloc(title_size + 1); /* Add null terminator */
  458. if (!title_str)
  459. return AVERROR(ENOMEM);
  460. avio_read(pb, title_str, title_size);
  461. title_str[title_size] = 0;
  462. av_dict_set(&st->metadata, "handler_name", title_str, 0);
  463. av_freep(&title_str);
  464. }
  465. return 0;
  466. }
  467. int ff_mov_read_esds(AVFormatContext *fc, AVIOContext *pb, MOVAtom atom)
  468. {
  469. AVStream *st;
  470. int tag;
  471. if (fc->nb_streams < 1)
  472. return 0;
  473. st = fc->streams[fc->nb_streams-1];
  474. avio_rb32(pb); /* version + flags */
  475. ff_mp4_read_descr(fc, pb, &tag);
  476. if (tag == MP4ESDescrTag) {
  477. ff_mp4_parse_es_descr(pb, NULL);
  478. } else
  479. avio_rb16(pb); /* ID */
  480. ff_mp4_read_descr(fc, pb, &tag);
  481. if (tag == MP4DecConfigDescrTag)
  482. ff_mp4_read_dec_config_descr(fc, st, pb);
  483. return 0;
  484. }
  485. static int mov_read_esds(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  486. {
  487. return ff_mov_read_esds(c->fc, pb, atom);
  488. }
  489. static int mov_read_dac3(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  490. {
  491. AVStream *st;
  492. int ac3info, acmod, lfeon, bsmod;
  493. if (c->fc->nb_streams < 1)
  494. return 0;
  495. st = c->fc->streams[c->fc->nb_streams-1];
  496. ac3info = avio_rb24(pb);
  497. bsmod = (ac3info >> 14) & 0x7;
  498. acmod = (ac3info >> 11) & 0x7;
  499. lfeon = (ac3info >> 10) & 0x1;
  500. st->codec->channels = ((int[]){2,1,2,3,3,4,4,5})[acmod] + lfeon;
  501. st->codec->audio_service_type = bsmod;
  502. if (st->codec->channels > 1 && bsmod == 0x7)
  503. st->codec->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
  504. return 0;
  505. }
  506. static int mov_read_wfex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  507. {
  508. AVStream *st;
  509. if (c->fc->nb_streams < 1)
  510. return 0;
  511. st = c->fc->streams[c->fc->nb_streams-1];
  512. ff_get_wav_header(pb, st->codec, atom.size);
  513. return 0;
  514. }
  515. static int mov_read_pasp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  516. {
  517. const int num = avio_rb32(pb);
  518. const int den = avio_rb32(pb);
  519. AVStream *st;
  520. if (c->fc->nb_streams < 1)
  521. return 0;
  522. st = c->fc->streams[c->fc->nb_streams-1];
  523. if ((st->sample_aspect_ratio.den != 1 || st->sample_aspect_ratio.num) && // default
  524. (den != st->sample_aspect_ratio.den || num != st->sample_aspect_ratio.num)) {
  525. av_log(c->fc, AV_LOG_WARNING,
  526. "sample aspect ratio already set to %d:%d, ignoring 'pasp' atom (%d:%d)\n",
  527. st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
  528. num, den);
  529. } else if (den != 0) {
  530. st->sample_aspect_ratio.num = num;
  531. st->sample_aspect_ratio.den = den;
  532. }
  533. return 0;
  534. }
  535. /* this atom contains actual media data */
  536. static int mov_read_mdat(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  537. {
  538. if (atom.size == 0) /* wrong one (MP4) */
  539. return 0;
  540. c->found_mdat=1;
  541. return 0; /* now go for moov */
  542. }
  543. /* read major brand, minor version and compatible brands and store them as metadata */
  544. static int mov_read_ftyp(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  545. {
  546. uint32_t minor_ver;
  547. int comp_brand_size;
  548. char minor_ver_str[11]; /* 32 bit integer -> 10 digits + null */
  549. char* comp_brands_str;
  550. uint8_t type[5] = {0};
  551. avio_read(pb, type, 4);
  552. if (strcmp(type, "qt "))
  553. c->isom = 1;
  554. av_log(c->fc, AV_LOG_DEBUG, "ISO: File Type Major Brand: %.4s\n",(char *)&type);
  555. av_dict_set(&c->fc->metadata, "major_brand", type, 0);
  556. minor_ver = avio_rb32(pb); /* minor version */
  557. snprintf(minor_ver_str, sizeof(minor_ver_str), "%d", minor_ver);
  558. av_dict_set(&c->fc->metadata, "minor_version", minor_ver_str, 0);
  559. comp_brand_size = atom.size - 8;
  560. if (comp_brand_size < 0)
  561. return -1;
  562. comp_brands_str = av_malloc(comp_brand_size + 1); /* Add null terminator */
  563. if (!comp_brands_str)
  564. return AVERROR(ENOMEM);
  565. avio_read(pb, comp_brands_str, comp_brand_size);
  566. comp_brands_str[comp_brand_size] = 0;
  567. av_dict_set(&c->fc->metadata, "compatible_brands", comp_brands_str, 0);
  568. av_freep(&comp_brands_str);
  569. return 0;
  570. }
  571. /* this atom should contain all header atoms */
  572. static int mov_read_moov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  573. {
  574. if (mov_read_default(c, pb, atom) < 0)
  575. return -1;
  576. /* we parsed the 'moov' atom, we can terminate the parsing as soon as we find the 'mdat' */
  577. /* so we don't parse the whole file if over a network */
  578. c->found_moov=1;
  579. return 0; /* now go for mdat */
  580. }
  581. static int mov_read_moof(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  582. {
  583. c->fragment.moof_offset = avio_tell(pb) - 8;
  584. av_dlog(c->fc, "moof offset %"PRIx64"\n", c->fragment.moof_offset);
  585. return mov_read_default(c, pb, atom);
  586. }
  587. static void mov_metadata_creation_time(AVDictionary **metadata, time_t time)
  588. {
  589. char buffer[32];
  590. if (time) {
  591. struct tm *ptm;
  592. time -= 2082844800; /* seconds between 1904-01-01 and Epoch */
  593. ptm = gmtime(&time);
  594. if (!ptm) return;
  595. strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ptm);
  596. av_dict_set(metadata, "creation_time", buffer, 0);
  597. }
  598. }
  599. static int mov_read_mdhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  600. {
  601. AVStream *st;
  602. MOVStreamContext *sc;
  603. int version;
  604. char language[4] = {0};
  605. unsigned lang;
  606. time_t creation_time;
  607. if (c->fc->nb_streams < 1)
  608. return 0;
  609. st = c->fc->streams[c->fc->nb_streams-1];
  610. sc = st->priv_data;
  611. version = avio_r8(pb);
  612. if (version > 1)
  613. return -1; /* unsupported */
  614. avio_rb24(pb); /* flags */
  615. if (version == 1) {
  616. creation_time = avio_rb64(pb);
  617. avio_rb64(pb);
  618. } else {
  619. creation_time = avio_rb32(pb);
  620. avio_rb32(pb); /* modification time */
  621. }
  622. mov_metadata_creation_time(&st->metadata, creation_time);
  623. sc->time_scale = avio_rb32(pb);
  624. st->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  625. lang = avio_rb16(pb); /* language */
  626. if (ff_mov_lang_to_iso639(lang, language))
  627. av_dict_set(&st->metadata, "language", language, 0);
  628. avio_rb16(pb); /* quality */
  629. return 0;
  630. }
  631. static int mov_read_mvhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  632. {
  633. time_t creation_time;
  634. int version = avio_r8(pb); /* version */
  635. avio_rb24(pb); /* flags */
  636. if (version == 1) {
  637. creation_time = avio_rb64(pb);
  638. avio_rb64(pb);
  639. } else {
  640. creation_time = avio_rb32(pb);
  641. avio_rb32(pb); /* modification time */
  642. }
  643. mov_metadata_creation_time(&c->fc->metadata, creation_time);
  644. c->time_scale = avio_rb32(pb); /* time scale */
  645. av_dlog(c->fc, "time scale = %i\n", c->time_scale);
  646. c->duration = (version == 1) ? avio_rb64(pb) : avio_rb32(pb); /* duration */
  647. avio_rb32(pb); /* preferred scale */
  648. avio_rb16(pb); /* preferred volume */
  649. avio_skip(pb, 10); /* reserved */
  650. avio_skip(pb, 36); /* display matrix */
  651. avio_rb32(pb); /* preview time */
  652. avio_rb32(pb); /* preview duration */
  653. avio_rb32(pb); /* poster time */
  654. avio_rb32(pb); /* selection time */
  655. avio_rb32(pb); /* selection duration */
  656. avio_rb32(pb); /* current time */
  657. avio_rb32(pb); /* next track ID */
  658. return 0;
  659. }
  660. static int mov_read_smi(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  661. {
  662. AVStream *st;
  663. if (c->fc->nb_streams < 1)
  664. return 0;
  665. st = c->fc->streams[c->fc->nb_streams-1];
  666. if ((uint64_t)atom.size > (1<<30))
  667. return -1;
  668. // currently SVQ3 decoder expect full STSD header - so let's fake it
  669. // this should be fixed and just SMI header should be passed
  670. av_free(st->codec->extradata);
  671. st->codec->extradata = av_mallocz(atom.size + 0x5a + FF_INPUT_BUFFER_PADDING_SIZE);
  672. if (!st->codec->extradata)
  673. return AVERROR(ENOMEM);
  674. st->codec->extradata_size = 0x5a + atom.size;
  675. memcpy(st->codec->extradata, "SVQ3", 4); // fake
  676. avio_read(pb, st->codec->extradata + 0x5a, atom.size);
  677. av_dlog(c->fc, "Reading SMI %"PRId64" %s\n", atom.size, st->codec->extradata + 0x5a);
  678. return 0;
  679. }
  680. static int mov_read_enda(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  681. {
  682. AVStream *st;
  683. int little_endian;
  684. if (c->fc->nb_streams < 1)
  685. return 0;
  686. st = c->fc->streams[c->fc->nb_streams-1];
  687. little_endian = avio_rb16(pb) & 0xFF;
  688. av_dlog(c->fc, "enda %d\n", little_endian);
  689. if (little_endian == 1) {
  690. switch (st->codec->codec_id) {
  691. case CODEC_ID_PCM_S24BE:
  692. st->codec->codec_id = CODEC_ID_PCM_S24LE;
  693. break;
  694. case CODEC_ID_PCM_S32BE:
  695. st->codec->codec_id = CODEC_ID_PCM_S32LE;
  696. break;
  697. case CODEC_ID_PCM_F32BE:
  698. st->codec->codec_id = CODEC_ID_PCM_F32LE;
  699. break;
  700. case CODEC_ID_PCM_F64BE:
  701. st->codec->codec_id = CODEC_ID_PCM_F64LE;
  702. break;
  703. default:
  704. break;
  705. }
  706. }
  707. return 0;
  708. }
  709. /* FIXME modify qdm2/svq3/h264 decoders to take full atom as extradata */
  710. static int mov_read_extradata(MOVContext *c, AVIOContext *pb, MOVAtom atom,
  711. enum CodecID codec_id)
  712. {
  713. AVStream *st;
  714. uint64_t size;
  715. uint8_t *buf;
  716. if (c->fc->nb_streams < 1) // will happen with jp2 files
  717. return 0;
  718. st= c->fc->streams[c->fc->nb_streams-1];
  719. if (st->codec->codec_id != codec_id)
  720. return 0; /* unexpected codec_id - don't mess with extradata */
  721. size= (uint64_t)st->codec->extradata_size + atom.size + 8 + FF_INPUT_BUFFER_PADDING_SIZE;
  722. if (size > INT_MAX || (uint64_t)atom.size > INT_MAX)
  723. return -1;
  724. buf= av_realloc(st->codec->extradata, size);
  725. if (!buf)
  726. return -1;
  727. st->codec->extradata= buf;
  728. buf+= st->codec->extradata_size;
  729. st->codec->extradata_size= size - FF_INPUT_BUFFER_PADDING_SIZE;
  730. AV_WB32( buf , atom.size + 8);
  731. AV_WL32( buf + 4, atom.type);
  732. avio_read(pb, buf + 8, atom.size);
  733. return 0;
  734. }
  735. /* wrapper functions for reading ALAC/AVS/MJPEG/MJPEG2000 extradata atoms only for those codecs */
  736. static int mov_read_alac(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  737. {
  738. return mov_read_extradata(c, pb, atom, CODEC_ID_ALAC);
  739. }
  740. static int mov_read_avss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  741. {
  742. return mov_read_extradata(c, pb, atom, CODEC_ID_AVS);
  743. }
  744. static int mov_read_fiel(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  745. {
  746. return mov_read_extradata(c, pb, atom, CODEC_ID_MJPEG);
  747. }
  748. static int mov_read_jp2h(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  749. {
  750. return mov_read_extradata(c, pb, atom, CODEC_ID_JPEG2000);
  751. }
  752. static int mov_read_wave(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  753. {
  754. AVStream *st;
  755. if (c->fc->nb_streams < 1)
  756. return 0;
  757. st = c->fc->streams[c->fc->nb_streams-1];
  758. if ((uint64_t)atom.size > (1<<30))
  759. return -1;
  760. if (st->codec->codec_id == CODEC_ID_QDM2 || st->codec->codec_id == CODEC_ID_QDMC) {
  761. // pass all frma atom to codec, needed at least for QDMC and QDM2
  762. av_free(st->codec->extradata);
  763. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  764. if (!st->codec->extradata)
  765. return AVERROR(ENOMEM);
  766. st->codec->extradata_size = atom.size;
  767. avio_read(pb, st->codec->extradata, atom.size);
  768. } else if (atom.size > 8) { /* to read frma, esds atoms */
  769. if (mov_read_default(c, pb, atom) < 0)
  770. return -1;
  771. } else
  772. avio_skip(pb, atom.size);
  773. return 0;
  774. }
  775. /**
  776. * This function reads atom content and puts data in extradata without tag
  777. * nor size unlike mov_read_extradata.
  778. */
  779. static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  780. {
  781. AVStream *st;
  782. if (c->fc->nb_streams < 1)
  783. return 0;
  784. st = c->fc->streams[c->fc->nb_streams-1];
  785. if ((uint64_t)atom.size > (1<<30))
  786. return -1;
  787. av_free(st->codec->extradata);
  788. st->codec->extradata = av_mallocz(atom.size + FF_INPUT_BUFFER_PADDING_SIZE);
  789. if (!st->codec->extradata)
  790. return AVERROR(ENOMEM);
  791. st->codec->extradata_size = atom.size;
  792. avio_read(pb, st->codec->extradata, atom.size);
  793. return 0;
  794. }
  795. /**
  796. * An strf atom is a BITMAPINFOHEADER struct. This struct is 40 bytes itself,
  797. * but can have extradata appended at the end after the 40 bytes belonging
  798. * to the struct.
  799. */
  800. static int mov_read_strf(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  801. {
  802. AVStream *st;
  803. if (c->fc->nb_streams < 1)
  804. return 0;
  805. if (atom.size <= 40)
  806. return 0;
  807. st = c->fc->streams[c->fc->nb_streams-1];
  808. if ((uint64_t)atom.size > (1<<30))
  809. return -1;
  810. av_free(st->codec->extradata);
  811. st->codec->extradata = av_mallocz(atom.size - 40 + FF_INPUT_BUFFER_PADDING_SIZE);
  812. if (!st->codec->extradata)
  813. return AVERROR(ENOMEM);
  814. st->codec->extradata_size = atom.size - 40;
  815. avio_skip(pb, 40);
  816. avio_read(pb, st->codec->extradata, atom.size - 40);
  817. return 0;
  818. }
  819. static int mov_read_stco(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  820. {
  821. AVStream *st;
  822. MOVStreamContext *sc;
  823. unsigned int i, entries;
  824. if (c->fc->nb_streams < 1)
  825. return 0;
  826. st = c->fc->streams[c->fc->nb_streams-1];
  827. sc = st->priv_data;
  828. avio_r8(pb); /* version */
  829. avio_rb24(pb); /* flags */
  830. entries = avio_rb32(pb);
  831. if (entries >= UINT_MAX/sizeof(int64_t))
  832. return -1;
  833. sc->chunk_offsets = av_malloc(entries * sizeof(int64_t));
  834. if (!sc->chunk_offsets)
  835. return AVERROR(ENOMEM);
  836. sc->chunk_count = entries;
  837. if (atom.type == MKTAG('s','t','c','o'))
  838. for (i=0; i<entries; i++)
  839. sc->chunk_offsets[i] = avio_rb32(pb);
  840. else if (atom.type == MKTAG('c','o','6','4'))
  841. for (i=0; i<entries; i++)
  842. sc->chunk_offsets[i] = avio_rb64(pb);
  843. else
  844. return -1;
  845. return 0;
  846. }
  847. /**
  848. * Compute codec id for 'lpcm' tag.
  849. * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
  850. */
  851. enum CodecID ff_mov_get_lpcm_codec_id(int bps, int flags)
  852. {
  853. if (flags & 1) { // floating point
  854. if (flags & 2) { // big endian
  855. if (bps == 32) return CODEC_ID_PCM_F32BE;
  856. else if (bps == 64) return CODEC_ID_PCM_F64BE;
  857. } else {
  858. if (bps == 32) return CODEC_ID_PCM_F32LE;
  859. else if (bps == 64) return CODEC_ID_PCM_F64LE;
  860. }
  861. } else {
  862. if (flags & 2) {
  863. if (bps == 8)
  864. // signed integer
  865. if (flags & 4) return CODEC_ID_PCM_S8;
  866. else return CODEC_ID_PCM_U8;
  867. else if (bps == 16) return CODEC_ID_PCM_S16BE;
  868. else if (bps == 24) return CODEC_ID_PCM_S24BE;
  869. else if (bps == 32) return CODEC_ID_PCM_S32BE;
  870. } else {
  871. if (bps == 8)
  872. if (flags & 4) return CODEC_ID_PCM_S8;
  873. else return CODEC_ID_PCM_U8;
  874. else if (bps == 16) return CODEC_ID_PCM_S16LE;
  875. else if (bps == 24) return CODEC_ID_PCM_S24LE;
  876. else if (bps == 32) return CODEC_ID_PCM_S32LE;
  877. }
  878. }
  879. return CODEC_ID_NONE;
  880. }
  881. int ff_mov_read_stsd_entries(MOVContext *c, AVIOContext *pb, int entries)
  882. {
  883. AVStream *st;
  884. MOVStreamContext *sc;
  885. int j, pseudo_stream_id;
  886. if (c->fc->nb_streams < 1)
  887. return 0;
  888. st = c->fc->streams[c->fc->nb_streams-1];
  889. sc = st->priv_data;
  890. for (pseudo_stream_id=0; pseudo_stream_id<entries; pseudo_stream_id++) {
  891. //Parsing Sample description table
  892. enum CodecID id;
  893. int dref_id = 1;
  894. MOVAtom a = { AV_RL32("stsd") };
  895. int64_t start_pos = avio_tell(pb);
  896. int size = avio_rb32(pb); /* size */
  897. uint32_t format = avio_rl32(pb); /* data format */
  898. if (size >= 16) {
  899. avio_rb32(pb); /* reserved */
  900. avio_rb16(pb); /* reserved */
  901. dref_id = avio_rb16(pb);
  902. }
  903. if (st->codec->codec_tag &&
  904. st->codec->codec_tag != format &&
  905. (c->fc->video_codec_id ? ff_codec_get_id(codec_movvideo_tags, format) != c->fc->video_codec_id
  906. : st->codec->codec_tag != MKTAG('j','p','e','g'))
  907. ){
  908. /* Multiple fourcc, we skip JPEG. This is not correct, we should
  909. * export it as a separate AVStream but this needs a few changes
  910. * in the MOV demuxer, patch welcome. */
  911. multiple_stsd:
  912. av_log(c->fc, AV_LOG_WARNING, "multiple fourcc not supported\n");
  913. avio_skip(pb, size - (avio_tell(pb) - start_pos));
  914. continue;
  915. }
  916. /* we cannot demux concatenated h264 streams because of different extradata */
  917. if (st->codec->codec_tag && st->codec->codec_tag == AV_RL32("avc1"))
  918. goto multiple_stsd;
  919. sc->pseudo_stream_id = st->codec->codec_tag ? -1 : pseudo_stream_id;
  920. sc->dref_id= dref_id;
  921. st->codec->codec_tag = format;
  922. id = ff_codec_get_id(codec_movaudio_tags, format);
  923. if (id<=0 && ((format&0xFFFF) == 'm'+('s'<<8) || (format&0xFFFF) == 'T'+('S'<<8)))
  924. id = ff_codec_get_id(ff_codec_wav_tags, av_bswap32(format)&0xFFFF);
  925. if (st->codec->codec_type != AVMEDIA_TYPE_VIDEO && id > 0) {
  926. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  927. } else if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO && /* do not overwrite codec type */
  928. format && format != MKTAG('m','p','4','s')) { /* skip old asf mpeg4 tag */
  929. id = ff_codec_get_id(codec_movvideo_tags, format);
  930. if (id <= 0)
  931. id = ff_codec_get_id(ff_codec_bmp_tags, format);
  932. if (id > 0)
  933. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  934. else if (st->codec->codec_type == AVMEDIA_TYPE_DATA){
  935. id = ff_codec_get_id(ff_codec_movsubtitle_tags, format);
  936. if (id > 0)
  937. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  938. }
  939. }
  940. av_dlog(c->fc, "size=%d 4CC= %c%c%c%c codec_type=%d\n", size,
  941. (format >> 0) & 0xff, (format >> 8) & 0xff, (format >> 16) & 0xff,
  942. (format >> 24) & 0xff, st->codec->codec_type);
  943. if (st->codec->codec_type==AVMEDIA_TYPE_VIDEO) {
  944. unsigned int color_depth, len;
  945. int color_greyscale;
  946. st->codec->codec_id = id;
  947. avio_rb16(pb); /* version */
  948. avio_rb16(pb); /* revision level */
  949. avio_rb32(pb); /* vendor */
  950. avio_rb32(pb); /* temporal quality */
  951. avio_rb32(pb); /* spatial quality */
  952. st->codec->width = avio_rb16(pb); /* width */
  953. st->codec->height = avio_rb16(pb); /* height */
  954. avio_rb32(pb); /* horiz resolution */
  955. avio_rb32(pb); /* vert resolution */
  956. avio_rb32(pb); /* data size, always 0 */
  957. avio_rb16(pb); /* frames per samples */
  958. len = avio_r8(pb); /* codec name, pascal string */
  959. if (len > 31)
  960. len = 31;
  961. mov_read_mac_string(c, pb, len, st->codec->codec_name, 32);
  962. if (len < 31)
  963. avio_skip(pb, 31 - len);
  964. /* codec_tag YV12 triggers an UV swap in rawdec.c */
  965. if (!memcmp(st->codec->codec_name, "Planar Y'CbCr 8-bit 4:2:0", 25))
  966. st->codec->codec_tag=MKTAG('I', '4', '2', '0');
  967. st->codec->bits_per_coded_sample = avio_rb16(pb); /* depth */
  968. st->codec->color_table_id = avio_rb16(pb); /* colortable id */
  969. av_dlog(c->fc, "depth %d, ctab id %d\n",
  970. st->codec->bits_per_coded_sample, st->codec->color_table_id);
  971. /* figure out the palette situation */
  972. color_depth = st->codec->bits_per_coded_sample & 0x1F;
  973. color_greyscale = st->codec->bits_per_coded_sample & 0x20;
  974. /* if the depth is 2, 4, or 8 bpp, file is palettized */
  975. if ((color_depth == 2) || (color_depth == 4) ||
  976. (color_depth == 8)) {
  977. /* for palette traversal */
  978. unsigned int color_start, color_count, color_end;
  979. unsigned char r, g, b;
  980. if (color_greyscale) {
  981. int color_index, color_dec;
  982. /* compute the greyscale palette */
  983. st->codec->bits_per_coded_sample = color_depth;
  984. color_count = 1 << color_depth;
  985. color_index = 255;
  986. color_dec = 256 / (color_count - 1);
  987. for (j = 0; j < color_count; j++) {
  988. if (id == CODEC_ID_CINEPAK){
  989. r = g = b = color_count - 1 - color_index;
  990. }else
  991. r = g = b = color_index;
  992. sc->palette[j] =
  993. (r << 16) | (g << 8) | (b);
  994. color_index -= color_dec;
  995. if (color_index < 0)
  996. color_index = 0;
  997. }
  998. } else if (st->codec->color_table_id) {
  999. const uint8_t *color_table;
  1000. /* if flag bit 3 is set, use the default palette */
  1001. color_count = 1 << color_depth;
  1002. if (color_depth == 2)
  1003. color_table = ff_qt_default_palette_4;
  1004. else if (color_depth == 4)
  1005. color_table = ff_qt_default_palette_16;
  1006. else
  1007. color_table = ff_qt_default_palette_256;
  1008. for (j = 0; j < color_count; j++) {
  1009. r = color_table[j * 3 + 0];
  1010. g = color_table[j * 3 + 1];
  1011. b = color_table[j * 3 + 2];
  1012. sc->palette[j] =
  1013. (r << 16) | (g << 8) | (b);
  1014. }
  1015. } else {
  1016. /* load the palette from the file */
  1017. color_start = avio_rb32(pb);
  1018. color_count = avio_rb16(pb);
  1019. color_end = avio_rb16(pb);
  1020. if ((color_start <= 255) &&
  1021. (color_end <= 255)) {
  1022. for (j = color_start; j <= color_end; j++) {
  1023. /* each R, G, or B component is 16 bits;
  1024. * only use the top 8 bits; skip alpha bytes
  1025. * up front */
  1026. avio_r8(pb);
  1027. avio_r8(pb);
  1028. r = avio_r8(pb);
  1029. avio_r8(pb);
  1030. g = avio_r8(pb);
  1031. avio_r8(pb);
  1032. b = avio_r8(pb);
  1033. avio_r8(pb);
  1034. sc->palette[j] =
  1035. (r << 16) | (g << 8) | (b);
  1036. }
  1037. }
  1038. }
  1039. sc->has_palette = 1;
  1040. }
  1041. } else if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
  1042. int bits_per_sample, flags;
  1043. uint16_t version = avio_rb16(pb);
  1044. st->codec->codec_id = id;
  1045. avio_rb16(pb); /* revision level */
  1046. avio_rb32(pb); /* vendor */
  1047. st->codec->channels = avio_rb16(pb); /* channel count */
  1048. av_dlog(c->fc, "audio channels %d\n", st->codec->channels);
  1049. st->codec->bits_per_coded_sample = avio_rb16(pb); /* sample size */
  1050. sc->audio_cid = avio_rb16(pb);
  1051. avio_rb16(pb); /* packet size = 0 */
  1052. st->codec->sample_rate = ((avio_rb32(pb) >> 16));
  1053. //Read QT version 1 fields. In version 0 these do not exist.
  1054. av_dlog(c->fc, "version =%d, isom =%d\n",version,c->isom);
  1055. if (!c->isom) {
  1056. if (version==1) {
  1057. sc->samples_per_frame = avio_rb32(pb);
  1058. avio_rb32(pb); /* bytes per packet */
  1059. sc->bytes_per_frame = avio_rb32(pb);
  1060. avio_rb32(pb); /* bytes per sample */
  1061. } else if (version==2) {
  1062. avio_rb32(pb); /* sizeof struct only */
  1063. st->codec->sample_rate = av_int2dbl(avio_rb64(pb)); /* float 64 */
  1064. st->codec->channels = avio_rb32(pb);
  1065. avio_rb32(pb); /* always 0x7F000000 */
  1066. st->codec->bits_per_coded_sample = avio_rb32(pb); /* bits per channel if sound is uncompressed */
  1067. flags = avio_rb32(pb); /* lpcm format specific flag */
  1068. sc->bytes_per_frame = avio_rb32(pb); /* bytes per audio packet if constant */
  1069. sc->samples_per_frame = avio_rb32(pb); /* lpcm frames per audio packet if constant */
  1070. if (format == MKTAG('l','p','c','m'))
  1071. st->codec->codec_id = ff_mov_get_lpcm_codec_id(st->codec->bits_per_coded_sample, flags);
  1072. }
  1073. }
  1074. switch (st->codec->codec_id) {
  1075. case CODEC_ID_PCM_S8:
  1076. case CODEC_ID_PCM_U8:
  1077. if (st->codec->bits_per_coded_sample == 16)
  1078. st->codec->codec_id = CODEC_ID_PCM_S16BE;
  1079. break;
  1080. case CODEC_ID_PCM_S16LE:
  1081. case CODEC_ID_PCM_S16BE:
  1082. if (st->codec->bits_per_coded_sample == 8)
  1083. st->codec->codec_id = CODEC_ID_PCM_S8;
  1084. else if (st->codec->bits_per_coded_sample == 24)
  1085. st->codec->codec_id =
  1086. st->codec->codec_id == CODEC_ID_PCM_S16BE ?
  1087. CODEC_ID_PCM_S24BE : CODEC_ID_PCM_S24LE;
  1088. break;
  1089. /* set values for old format before stsd version 1 appeared */
  1090. case CODEC_ID_MACE3:
  1091. sc->samples_per_frame = 6;
  1092. sc->bytes_per_frame = 2*st->codec->channels;
  1093. break;
  1094. case CODEC_ID_MACE6:
  1095. sc->samples_per_frame = 6;
  1096. sc->bytes_per_frame = 1*st->codec->channels;
  1097. break;
  1098. case CODEC_ID_ADPCM_IMA_QT:
  1099. sc->samples_per_frame = 64;
  1100. sc->bytes_per_frame = 34*st->codec->channels;
  1101. break;
  1102. case CODEC_ID_GSM:
  1103. sc->samples_per_frame = 160;
  1104. sc->bytes_per_frame = 33;
  1105. break;
  1106. default:
  1107. break;
  1108. }
  1109. bits_per_sample = av_get_bits_per_sample(st->codec->codec_id);
  1110. if (bits_per_sample) {
  1111. st->codec->bits_per_coded_sample = bits_per_sample;
  1112. sc->sample_size = (bits_per_sample >> 3) * st->codec->channels;
  1113. }
  1114. } else if (st->codec->codec_type==AVMEDIA_TYPE_SUBTITLE){
  1115. // ttxt stsd contains display flags, justification, background
  1116. // color, fonts, and default styles, so fake an atom to read it
  1117. MOVAtom fake_atom = { .size = size - (avio_tell(pb) - start_pos) };
  1118. if (format != AV_RL32("mp4s")) // mp4s contains a regular esds atom
  1119. mov_read_glbl(c, pb, fake_atom);
  1120. st->codec->codec_id= id;
  1121. st->codec->width = sc->width;
  1122. st->codec->height = sc->height;
  1123. } else {
  1124. if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
  1125. int val;
  1126. avio_rb32(pb); /* reserved */
  1127. val = avio_rb32(pb); /* flags */
  1128. if (val & 1)
  1129. st->codec->flags2 |= CODEC_FLAG2_DROP_FRAME_TIMECODE;
  1130. avio_rb32(pb);
  1131. avio_rb32(pb);
  1132. st->codec->time_base.den = get_byte(pb);
  1133. st->codec->time_base.num = 1;
  1134. }
  1135. /* other codec type, just skip (rtp, mp4s, ...) */
  1136. avio_skip(pb, size - (avio_tell(pb) - start_pos));
  1137. }
  1138. /* this will read extra atoms at the end (wave, alac, damr, avcC, SMI ...) */
  1139. a.size = size - (avio_tell(pb) - start_pos);
  1140. if (a.size > 8) {
  1141. if (mov_read_default(c, pb, a) < 0)
  1142. return -1;
  1143. } else if (a.size > 0)
  1144. avio_skip(pb, a.size);
  1145. }
  1146. if (st->codec->codec_type==AVMEDIA_TYPE_AUDIO && st->codec->sample_rate==0 && sc->time_scale>1)
  1147. st->codec->sample_rate= sc->time_scale;
  1148. /* special codec parameters handling */
  1149. switch (st->codec->codec_id) {
  1150. #if CONFIG_DV_DEMUXER
  1151. case CODEC_ID_DVAUDIO:
  1152. c->dv_fctx = avformat_alloc_context();
  1153. c->dv_demux = avpriv_dv_init_demux(c->dv_fctx);
  1154. if (!c->dv_demux) {
  1155. av_log(c->fc, AV_LOG_ERROR, "dv demux context init error\n");
  1156. return -1;
  1157. }
  1158. sc->dv_audio_container = 1;
  1159. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  1160. break;
  1161. #endif
  1162. /* no ifdef since parameters are always those */
  1163. case CODEC_ID_QCELP:
  1164. // force sample rate for qcelp when not stored in mov
  1165. if (st->codec->codec_tag != MKTAG('Q','c','l','p'))
  1166. st->codec->sample_rate = 8000;
  1167. st->codec->frame_size= 160;
  1168. st->codec->channels= 1; /* really needed */
  1169. break;
  1170. case CODEC_ID_AMR_NB:
  1171. case CODEC_ID_AMR_WB:
  1172. st->codec->frame_size= sc->samples_per_frame;
  1173. st->codec->channels= 1; /* really needed */
  1174. /* force sample rate for amr, stsd in 3gp does not store sample rate */
  1175. if (st->codec->codec_id == CODEC_ID_AMR_NB)
  1176. st->codec->sample_rate = 8000;
  1177. else if (st->codec->codec_id == CODEC_ID_AMR_WB)
  1178. st->codec->sample_rate = 16000;
  1179. break;
  1180. case CODEC_ID_MP2:
  1181. case CODEC_ID_MP3:
  1182. st->codec->codec_type = AVMEDIA_TYPE_AUDIO; /* force type after stsd for m1a hdlr */
  1183. st->need_parsing = AVSTREAM_PARSE_FULL;
  1184. break;
  1185. case CODEC_ID_GSM:
  1186. case CODEC_ID_ADPCM_MS:
  1187. case CODEC_ID_ADPCM_IMA_WAV:
  1188. st->codec->frame_size = sc->samples_per_frame;
  1189. st->codec->block_align = sc->bytes_per_frame;
  1190. break;
  1191. case CODEC_ID_ALAC:
  1192. if (st->codec->extradata_size == 36) {
  1193. st->codec->frame_size = AV_RB32(st->codec->extradata+12);
  1194. st->codec->channels = AV_RB8 (st->codec->extradata+21);
  1195. st->codec->sample_rate = AV_RB32(st->codec->extradata+32);
  1196. }
  1197. break;
  1198. case CODEC_ID_AC3:
  1199. st->need_parsing = AVSTREAM_PARSE_FULL;
  1200. break;
  1201. default:
  1202. break;
  1203. }
  1204. return 0;
  1205. }
  1206. static int mov_read_stsd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1207. {
  1208. int entries;
  1209. avio_r8(pb); /* version */
  1210. avio_rb24(pb); /* flags */
  1211. entries = avio_rb32(pb);
  1212. return ff_mov_read_stsd_entries(c, pb, entries);
  1213. }
  1214. static int mov_read_stsc(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1215. {
  1216. AVStream *st;
  1217. MOVStreamContext *sc;
  1218. unsigned int i, entries;
  1219. if (c->fc->nb_streams < 1)
  1220. return 0;
  1221. st = c->fc->streams[c->fc->nb_streams-1];
  1222. sc = st->priv_data;
  1223. avio_r8(pb); /* version */
  1224. avio_rb24(pb); /* flags */
  1225. entries = avio_rb32(pb);
  1226. av_dlog(c->fc, "track[%i].stsc.entries = %i\n", c->fc->nb_streams-1, entries);
  1227. if (entries >= UINT_MAX / sizeof(*sc->stsc_data))
  1228. return -1;
  1229. sc->stsc_data = av_malloc(entries * sizeof(*sc->stsc_data));
  1230. if (!sc->stsc_data)
  1231. return AVERROR(ENOMEM);
  1232. sc->stsc_count = entries;
  1233. for (i=0; i<entries; i++) {
  1234. sc->stsc_data[i].first = avio_rb32(pb);
  1235. sc->stsc_data[i].count = avio_rb32(pb);
  1236. sc->stsc_data[i].id = avio_rb32(pb);
  1237. }
  1238. return 0;
  1239. }
  1240. static int mov_read_stps(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1241. {
  1242. AVStream *st;
  1243. MOVStreamContext *sc;
  1244. unsigned i, entries;
  1245. if (c->fc->nb_streams < 1)
  1246. return 0;
  1247. st = c->fc->streams[c->fc->nb_streams-1];
  1248. sc = st->priv_data;
  1249. avio_rb32(pb); // version + flags
  1250. entries = avio_rb32(pb);
  1251. if (entries >= UINT_MAX / sizeof(*sc->stps_data))
  1252. return -1;
  1253. sc->stps_data = av_malloc(entries * sizeof(*sc->stps_data));
  1254. if (!sc->stps_data)
  1255. return AVERROR(ENOMEM);
  1256. sc->stps_count = entries;
  1257. for (i = 0; i < entries; i++) {
  1258. sc->stps_data[i] = avio_rb32(pb);
  1259. //av_dlog(c->fc, "stps %d\n", sc->stps_data[i]);
  1260. }
  1261. return 0;
  1262. }
  1263. static int mov_read_stss(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1264. {
  1265. AVStream *st;
  1266. MOVStreamContext *sc;
  1267. unsigned int i, entries;
  1268. if (c->fc->nb_streams < 1)
  1269. return 0;
  1270. st = c->fc->streams[c->fc->nb_streams-1];
  1271. sc = st->priv_data;
  1272. avio_r8(pb); /* version */
  1273. avio_rb24(pb); /* flags */
  1274. entries = avio_rb32(pb);
  1275. av_dlog(c->fc, "keyframe_count = %d\n", entries);
  1276. if (entries >= UINT_MAX / sizeof(int))
  1277. return -1;
  1278. sc->keyframes = av_malloc(entries * sizeof(int));
  1279. if (!sc->keyframes)
  1280. return AVERROR(ENOMEM);
  1281. sc->keyframe_count = entries;
  1282. for (i=0; i<entries; i++) {
  1283. sc->keyframes[i] = avio_rb32(pb);
  1284. //av_dlog(c->fc, "keyframes[]=%d\n", sc->keyframes[i]);
  1285. }
  1286. return 0;
  1287. }
  1288. static int mov_read_stsz(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1289. {
  1290. AVStream *st;
  1291. MOVStreamContext *sc;
  1292. unsigned int i, entries, sample_size, field_size, num_bytes;
  1293. GetBitContext gb;
  1294. unsigned char* buf;
  1295. if (c->fc->nb_streams < 1)
  1296. return 0;
  1297. st = c->fc->streams[c->fc->nb_streams-1];
  1298. sc = st->priv_data;
  1299. avio_r8(pb); /* version */
  1300. avio_rb24(pb); /* flags */
  1301. if (atom.type == MKTAG('s','t','s','z')) {
  1302. sample_size = avio_rb32(pb);
  1303. if (!sc->sample_size) /* do not overwrite value computed in stsd */
  1304. sc->sample_size = sample_size;
  1305. field_size = 32;
  1306. } else {
  1307. sample_size = 0;
  1308. avio_rb24(pb); /* reserved */
  1309. field_size = avio_r8(pb);
  1310. }
  1311. entries = avio_rb32(pb);
  1312. av_dlog(c->fc, "sample_size = %d sample_count = %d\n", sc->sample_size, entries);
  1313. sc->sample_count = entries;
  1314. if (sample_size)
  1315. return 0;
  1316. if (field_size != 4 && field_size != 8 && field_size != 16 && field_size != 32) {
  1317. av_log(c->fc, AV_LOG_ERROR, "Invalid sample field size %d\n", field_size);
  1318. return -1;
  1319. }
  1320. if (entries >= UINT_MAX / sizeof(int) || entries >= (UINT_MAX - 4) / field_size)
  1321. return -1;
  1322. sc->sample_sizes = av_malloc(entries * sizeof(int));
  1323. if (!sc->sample_sizes)
  1324. return AVERROR(ENOMEM);
  1325. num_bytes = (entries*field_size+4)>>3;
  1326. buf = av_malloc(num_bytes+FF_INPUT_BUFFER_PADDING_SIZE);
  1327. if (!buf) {
  1328. av_freep(&sc->sample_sizes);
  1329. return AVERROR(ENOMEM);
  1330. }
  1331. if (avio_read(pb, buf, num_bytes) < num_bytes) {
  1332. av_freep(&sc->sample_sizes);
  1333. av_free(buf);
  1334. return -1;
  1335. }
  1336. init_get_bits(&gb, buf, 8*num_bytes);
  1337. for (i=0; i<entries; i++)
  1338. sc->sample_sizes[i] = get_bits_long(&gb, field_size);
  1339. av_free(buf);
  1340. return 0;
  1341. }
  1342. static int mov_read_stts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1343. {
  1344. AVStream *st;
  1345. MOVStreamContext *sc;
  1346. unsigned int i, entries;
  1347. int64_t duration=0;
  1348. int64_t total_sample_count=0;
  1349. if (c->fc->nb_streams < 1)
  1350. return 0;
  1351. st = c->fc->streams[c->fc->nb_streams-1];
  1352. sc = st->priv_data;
  1353. avio_r8(pb); /* version */
  1354. avio_rb24(pb); /* flags */
  1355. entries = avio_rb32(pb);
  1356. av_dlog(c->fc, "track[%i].stts.entries = %i\n",
  1357. c->fc->nb_streams-1, entries);
  1358. if (entries >= UINT_MAX / sizeof(*sc->stts_data))
  1359. return -1;
  1360. sc->stts_data = av_malloc(entries * sizeof(*sc->stts_data));
  1361. if (!sc->stts_data)
  1362. return AVERROR(ENOMEM);
  1363. sc->stts_count = entries;
  1364. for (i=0; i<entries; i++) {
  1365. int sample_duration;
  1366. int sample_count;
  1367. sample_count=avio_rb32(pb);
  1368. sample_duration = avio_rb32(pb);
  1369. /* sample_duration < 0 is invalid based on the spec */
  1370. if (sample_duration < 0) {
  1371. av_log(c->fc, AV_LOG_ERROR, "Invalid SampleDelta in STTS %d", sample_duration);
  1372. sample_duration = 1;
  1373. }
  1374. sc->stts_data[i].count= sample_count;
  1375. sc->stts_data[i].duration= sample_duration;
  1376. av_dlog(c->fc, "sample_count=%d, sample_duration=%d\n",
  1377. sample_count, sample_duration);
  1378. duration+=(int64_t)sample_duration*sample_count;
  1379. total_sample_count+=sample_count;
  1380. }
  1381. st->nb_frames= total_sample_count;
  1382. if (duration)
  1383. st->duration= duration;
  1384. return 0;
  1385. }
  1386. static int mov_read_ctts(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1387. {
  1388. AVStream *st;
  1389. MOVStreamContext *sc;
  1390. unsigned int i, entries;
  1391. if (c->fc->nb_streams < 1)
  1392. return 0;
  1393. st = c->fc->streams[c->fc->nb_streams-1];
  1394. sc = st->priv_data;
  1395. avio_r8(pb); /* version */
  1396. avio_rb24(pb); /* flags */
  1397. entries = avio_rb32(pb);
  1398. av_dlog(c->fc, "track[%i].ctts.entries = %i\n", c->fc->nb_streams-1, entries);
  1399. if (entries >= UINT_MAX / sizeof(*sc->ctts_data))
  1400. return -1;
  1401. sc->ctts_data = av_malloc(entries * sizeof(*sc->ctts_data));
  1402. if (!sc->ctts_data)
  1403. return AVERROR(ENOMEM);
  1404. sc->ctts_count = entries;
  1405. for (i=0; i<entries; i++) {
  1406. int count =avio_rb32(pb);
  1407. int duration =avio_rb32(pb);
  1408. sc->ctts_data[i].count = count;
  1409. sc->ctts_data[i].duration= duration;
  1410. if (duration < 0 && i+2<entries)
  1411. sc->dts_shift = FFMAX(sc->dts_shift, -duration);
  1412. }
  1413. av_dlog(c->fc, "dts shift %d\n", sc->dts_shift);
  1414. return 0;
  1415. }
  1416. static void mov_build_index(MOVContext *mov, AVStream *st)
  1417. {
  1418. MOVStreamContext *sc = st->priv_data;
  1419. int64_t current_offset;
  1420. int64_t current_dts = 0;
  1421. unsigned int stts_index = 0;
  1422. unsigned int stsc_index = 0;
  1423. unsigned int stss_index = 0;
  1424. unsigned int stps_index = 0;
  1425. unsigned int i, j;
  1426. uint64_t stream_size = 0;
  1427. /* adjust first dts according to edit list */
  1428. if ((sc->empty_duration || sc->start_time) && mov->time_scale > 0) {
  1429. if (sc->empty_duration)
  1430. sc->empty_duration = av_rescale(sc->empty_duration, sc->time_scale, mov->time_scale);
  1431. sc->time_offset = sc->start_time - sc->empty_duration;
  1432. current_dts = -sc->time_offset;
  1433. if (sc->ctts_data && sc->stts_data &&
  1434. sc->ctts_data[0].duration / FFMAX(sc->stts_data[0].duration, 1) > 16) {
  1435. /* more than 16 frames delay, dts are likely wrong
  1436. this happens with files created by iMovie */
  1437. sc->wrong_dts = 1;
  1438. st->codec->has_b_frames = 1;
  1439. }
  1440. }
  1441. /* only use old uncompressed audio chunk demuxing when stts specifies it */
  1442. if (!(st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  1443. sc->stts_count == 1 && sc->stts_data[0].duration == 1)) {
  1444. unsigned int current_sample = 0;
  1445. unsigned int stts_sample = 0;
  1446. unsigned int sample_size;
  1447. unsigned int distance = 0;
  1448. int key_off = sc->keyframes && sc->keyframes[0] == 1;
  1449. current_dts -= sc->dts_shift;
  1450. if (sc->sample_count >= UINT_MAX / sizeof(*st->index_entries))
  1451. return;
  1452. st->index_entries = av_malloc(sc->sample_count*sizeof(*st->index_entries));
  1453. if (!st->index_entries)
  1454. return;
  1455. st->index_entries_allocated_size = sc->sample_count*sizeof(*st->index_entries);
  1456. for (i = 0; i < sc->chunk_count; i++) {
  1457. current_offset = sc->chunk_offsets[i];
  1458. while (stsc_index + 1 < sc->stsc_count &&
  1459. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1460. stsc_index++;
  1461. for (j = 0; j < sc->stsc_data[stsc_index].count; j++) {
  1462. int keyframe = 0;
  1463. if (current_sample >= sc->sample_count) {
  1464. av_log(mov->fc, AV_LOG_ERROR, "wrong sample count\n");
  1465. return;
  1466. }
  1467. if (!sc->keyframe_count || current_sample+key_off == sc->keyframes[stss_index]) {
  1468. keyframe = 1;
  1469. if (stss_index + 1 < sc->keyframe_count)
  1470. stss_index++;
  1471. } else if (sc->stps_count && current_sample+key_off == sc->stps_data[stps_index]) {
  1472. keyframe = 1;
  1473. if (stps_index + 1 < sc->stps_count)
  1474. stps_index++;
  1475. }
  1476. if (keyframe)
  1477. distance = 0;
  1478. sample_size = sc->sample_size > 0 ? sc->sample_size : sc->sample_sizes[current_sample];
  1479. if (sc->pseudo_stream_id == -1 ||
  1480. sc->stsc_data[stsc_index].id - 1 == sc->pseudo_stream_id) {
  1481. AVIndexEntry *e = &st->index_entries[st->nb_index_entries++];
  1482. e->pos = current_offset;
  1483. e->timestamp = current_dts;
  1484. e->size = sample_size;
  1485. e->min_distance = distance;
  1486. e->flags = keyframe ? AVINDEX_KEYFRAME : 0;
  1487. av_dlog(mov->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1488. "size %d, distance %d, keyframe %d\n", st->index, current_sample,
  1489. current_offset, current_dts, sample_size, distance, keyframe);
  1490. }
  1491. current_offset += sample_size;
  1492. stream_size += sample_size;
  1493. current_dts += sc->stts_data[stts_index].duration;
  1494. distance++;
  1495. stts_sample++;
  1496. current_sample++;
  1497. if (stts_index + 1 < sc->stts_count && stts_sample == sc->stts_data[stts_index].count) {
  1498. stts_sample = 0;
  1499. stts_index++;
  1500. }
  1501. }
  1502. }
  1503. if (st->duration > 0)
  1504. st->codec->bit_rate = stream_size*8*sc->time_scale/st->duration;
  1505. } else {
  1506. unsigned chunk_samples, total = 0;
  1507. // compute total chunk count
  1508. for (i = 0; i < sc->stsc_count; i++) {
  1509. unsigned count, chunk_count;
  1510. chunk_samples = sc->stsc_data[i].count;
  1511. if (sc->samples_per_frame && chunk_samples % sc->samples_per_frame) {
  1512. av_log(mov->fc, AV_LOG_ERROR, "error unaligned chunk\n");
  1513. return;
  1514. }
  1515. if (sc->samples_per_frame >= 160) { // gsm
  1516. count = chunk_samples / sc->samples_per_frame;
  1517. } else if (sc->samples_per_frame > 1) {
  1518. unsigned samples = (1024/sc->samples_per_frame)*sc->samples_per_frame;
  1519. count = (chunk_samples+samples-1) / samples;
  1520. } else {
  1521. count = (chunk_samples+1023) / 1024;
  1522. }
  1523. if (i < sc->stsc_count - 1)
  1524. chunk_count = sc->stsc_data[i+1].first - sc->stsc_data[i].first;
  1525. else
  1526. chunk_count = sc->chunk_count - (sc->stsc_data[i].first - 1);
  1527. total += chunk_count * count;
  1528. }
  1529. av_dlog(mov->fc, "chunk count %d\n", total);
  1530. if (total >= UINT_MAX / sizeof(*st->index_entries))
  1531. return;
  1532. st->index_entries = av_malloc(total*sizeof(*st->index_entries));
  1533. if (!st->index_entries)
  1534. return;
  1535. st->index_entries_allocated_size = total*sizeof(*st->index_entries);
  1536. // populate index
  1537. for (i = 0; i < sc->chunk_count; i++) {
  1538. current_offset = sc->chunk_offsets[i];
  1539. if (stsc_index + 1 < sc->stsc_count &&
  1540. i + 1 == sc->stsc_data[stsc_index + 1].first)
  1541. stsc_index++;
  1542. chunk_samples = sc->stsc_data[stsc_index].count;
  1543. while (chunk_samples > 0) {
  1544. AVIndexEntry *e;
  1545. unsigned size, samples;
  1546. if (sc->samples_per_frame >= 160) { // gsm
  1547. samples = sc->samples_per_frame;
  1548. size = sc->bytes_per_frame;
  1549. } else {
  1550. if (sc->samples_per_frame > 1) {
  1551. samples = FFMIN((1024 / sc->samples_per_frame)*
  1552. sc->samples_per_frame, chunk_samples);
  1553. size = (samples / sc->samples_per_frame) * sc->bytes_per_frame;
  1554. } else {
  1555. samples = FFMIN(1024, chunk_samples);
  1556. size = samples * sc->sample_size;
  1557. }
  1558. }
  1559. if (st->nb_index_entries >= total) {
  1560. av_log(mov->fc, AV_LOG_ERROR, "wrong chunk count %d\n", total);
  1561. return;
  1562. }
  1563. e = &st->index_entries[st->nb_index_entries++];
  1564. e->pos = current_offset;
  1565. e->timestamp = current_dts;
  1566. e->size = size;
  1567. e->min_distance = 0;
  1568. e->flags = AVINDEX_KEYFRAME;
  1569. av_dlog(mov->fc, "AVIndex stream %d, chunk %d, offset %"PRIx64", dts %"PRId64", "
  1570. "size %d, duration %d\n", st->index, i, current_offset, current_dts,
  1571. size, samples);
  1572. current_offset += size;
  1573. current_dts += samples;
  1574. chunk_samples -= samples;
  1575. }
  1576. }
  1577. }
  1578. }
  1579. static int mov_open_dref(AVIOContext **pb, const char *src, MOVDref *ref)
  1580. {
  1581. /* try relative path, we do not try the absolute because it can leak information about our
  1582. system to an attacker */
  1583. if (ref->nlvl_to > 0 && ref->nlvl_from > 0) {
  1584. char filename[1024];
  1585. const char *src_path;
  1586. int i, l;
  1587. /* find a source dir */
  1588. src_path = strrchr(src, '/');
  1589. if (src_path)
  1590. src_path++;
  1591. else
  1592. src_path = src;
  1593. /* find a next level down to target */
  1594. for (i = 0, l = strlen(ref->path) - 1; l >= 0; l--)
  1595. if (ref->path[l] == '/') {
  1596. if (i == ref->nlvl_to - 1)
  1597. break;
  1598. else
  1599. i++;
  1600. }
  1601. /* compose filename if next level down to target was found */
  1602. if (i == ref->nlvl_to - 1 && src_path - src < sizeof(filename)) {
  1603. memcpy(filename, src, src_path - src);
  1604. filename[src_path - src] = 0;
  1605. for (i = 1; i < ref->nlvl_from; i++)
  1606. av_strlcat(filename, "../", 1024);
  1607. av_strlcat(filename, ref->path + l + 1, 1024);
  1608. if (!avio_open(pb, filename, AVIO_FLAG_READ))
  1609. return 0;
  1610. }
  1611. }
  1612. return AVERROR(ENOENT);
  1613. }
  1614. static int mov_read_trak(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1615. {
  1616. AVStream *st;
  1617. MOVStreamContext *sc;
  1618. int ret;
  1619. st = avformat_new_stream(c->fc, NULL);
  1620. if (!st) return AVERROR(ENOMEM);
  1621. st->id = c->fc->nb_streams;
  1622. sc = av_mallocz(sizeof(MOVStreamContext));
  1623. if (!sc) return AVERROR(ENOMEM);
  1624. st->priv_data = sc;
  1625. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  1626. sc->ffindex = st->index;
  1627. if ((ret = mov_read_default(c, pb, atom)) < 0)
  1628. return ret;
  1629. /* sanity checks */
  1630. if (sc->chunk_count && (!sc->stts_count || !sc->stsc_count ||
  1631. (!sc->sample_size && !sc->sample_count))) {
  1632. av_log(c->fc, AV_LOG_ERROR, "stream %d, missing mandatory atoms, broken header\n",
  1633. st->index);
  1634. return 0;
  1635. }
  1636. if (sc->time_scale <= 0) {
  1637. av_log(c->fc, AV_LOG_WARNING, "stream %d, timescale not set\n", st->index);
  1638. sc->time_scale = c->time_scale;
  1639. if (sc->time_scale <= 0)
  1640. sc->time_scale = 1;
  1641. }
  1642. av_set_pts_info(st, 64, 1, sc->time_scale);
  1643. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  1644. !st->codec->frame_size && sc->stts_count == 1) {
  1645. st->codec->frame_size = av_rescale(sc->stts_data[0].duration,
  1646. st->codec->sample_rate, sc->time_scale);
  1647. av_dlog(c->fc, "frame size %d\n", st->codec->frame_size);
  1648. }
  1649. mov_build_index(c, st);
  1650. if (sc->dref_id-1 < sc->drefs_count && sc->drefs[sc->dref_id-1].path) {
  1651. MOVDref *dref = &sc->drefs[sc->dref_id - 1];
  1652. if (mov_open_dref(&sc->pb, c->fc->filename, dref) < 0)
  1653. av_log(c->fc, AV_LOG_ERROR,
  1654. "stream %d, error opening alias: path='%s', dir='%s', "
  1655. "filename='%s', volume='%s', nlvl_from=%d, nlvl_to=%d\n",
  1656. st->index, dref->path, dref->dir, dref->filename,
  1657. dref->volume, dref->nlvl_from, dref->nlvl_to);
  1658. } else
  1659. sc->pb = c->fc->pb;
  1660. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  1661. if (!st->sample_aspect_ratio.num &&
  1662. (st->codec->width != sc->width || st->codec->height != sc->height)) {
  1663. st->sample_aspect_ratio = av_d2q(((double)st->codec->height * sc->width) /
  1664. ((double)st->codec->width * sc->height), INT_MAX);
  1665. }
  1666. av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
  1667. sc->time_scale*st->nb_frames, st->duration, INT_MAX);
  1668. if (sc->stts_count == 1 || (sc->stts_count == 2 && sc->stts_data[1].count == 1))
  1669. av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den,
  1670. sc->time_scale, sc->stts_data[0].duration, INT_MAX);
  1671. }
  1672. switch (st->codec->codec_id) {
  1673. #if CONFIG_H261_DECODER
  1674. case CODEC_ID_H261:
  1675. #endif
  1676. #if CONFIG_H263_DECODER
  1677. case CODEC_ID_H263:
  1678. #endif
  1679. #if CONFIG_H264_DECODER
  1680. case CODEC_ID_H264:
  1681. #endif
  1682. #if CONFIG_MPEG4_DECODER
  1683. case CODEC_ID_MPEG4:
  1684. #endif
  1685. st->codec->width = 0; /* let decoder init width/height */
  1686. st->codec->height= 0;
  1687. break;
  1688. }
  1689. /* Do not need those anymore. */
  1690. av_freep(&sc->chunk_offsets);
  1691. av_freep(&sc->stsc_data);
  1692. av_freep(&sc->sample_sizes);
  1693. av_freep(&sc->keyframes);
  1694. av_freep(&sc->stts_data);
  1695. av_freep(&sc->stps_data);
  1696. return 0;
  1697. }
  1698. static int mov_read_ilst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1699. {
  1700. int ret;
  1701. c->itunes_metadata = 1;
  1702. ret = mov_read_default(c, pb, atom);
  1703. c->itunes_metadata = 0;
  1704. return ret;
  1705. }
  1706. static int mov_read_meta(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1707. {
  1708. while (atom.size > 8) {
  1709. uint32_t tag = avio_rl32(pb);
  1710. atom.size -= 4;
  1711. if (tag == MKTAG('h','d','l','r')) {
  1712. avio_seek(pb, -8, SEEK_CUR);
  1713. atom.size += 8;
  1714. return mov_read_default(c, pb, atom);
  1715. }
  1716. }
  1717. return 0;
  1718. }
  1719. static int mov_read_tkhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1720. {
  1721. int i;
  1722. int width;
  1723. int height;
  1724. int64_t disp_transform[2];
  1725. int display_matrix[3][2];
  1726. AVStream *st;
  1727. MOVStreamContext *sc;
  1728. int version;
  1729. if (c->fc->nb_streams < 1)
  1730. return 0;
  1731. st = c->fc->streams[c->fc->nb_streams-1];
  1732. sc = st->priv_data;
  1733. version = avio_r8(pb);
  1734. avio_rb24(pb); /* flags */
  1735. /*
  1736. MOV_TRACK_ENABLED 0x0001
  1737. MOV_TRACK_IN_MOVIE 0x0002
  1738. MOV_TRACK_IN_PREVIEW 0x0004
  1739. MOV_TRACK_IN_POSTER 0x0008
  1740. */
  1741. if (version == 1) {
  1742. avio_rb64(pb);
  1743. avio_rb64(pb);
  1744. } else {
  1745. avio_rb32(pb); /* creation time */
  1746. avio_rb32(pb); /* modification time */
  1747. }
  1748. st->id = (int)avio_rb32(pb); /* track id (NOT 0 !)*/
  1749. avio_rb32(pb); /* reserved */
  1750. /* highlevel (considering edits) duration in movie timebase */
  1751. (version == 1) ? avio_rb64(pb) : avio_rb32(pb);
  1752. avio_rb32(pb); /* reserved */
  1753. avio_rb32(pb); /* reserved */
  1754. avio_rb16(pb); /* layer */
  1755. avio_rb16(pb); /* alternate group */
  1756. avio_rb16(pb); /* volume */
  1757. avio_rb16(pb); /* reserved */
  1758. //read in the display matrix (outlined in ISO 14496-12, Section 6.2.2)
  1759. // they're kept in fixed point format through all calculations
  1760. // ignore u,v,z b/c we don't need the scale factor to calc aspect ratio
  1761. for (i = 0; i < 3; i++) {
  1762. display_matrix[i][0] = avio_rb32(pb); // 16.16 fixed point
  1763. display_matrix[i][1] = avio_rb32(pb); // 16.16 fixed point
  1764. avio_rb32(pb); // 2.30 fixed point (not used)
  1765. }
  1766. width = avio_rb32(pb); // 16.16 fixed point track width
  1767. height = avio_rb32(pb); // 16.16 fixed point track height
  1768. sc->width = width >> 16;
  1769. sc->height = height >> 16;
  1770. if (display_matrix[0][0] == -65536 && display_matrix[1][1] == -65536) {
  1771. av_dict_set(&st->metadata, "rotate", "180", 0);
  1772. }
  1773. // transform the display width/height according to the matrix
  1774. // skip this if the display matrix is the default identity matrix
  1775. // or if it is rotating the picture, ex iPhone 3GS
  1776. // to keep the same scale, use [width height 1<<16]
  1777. if (width && height &&
  1778. ((display_matrix[0][0] != 65536 ||
  1779. display_matrix[1][1] != 65536) &&
  1780. !display_matrix[0][1] &&
  1781. !display_matrix[1][0] &&
  1782. !display_matrix[2][0] && !display_matrix[2][1])) {
  1783. for (i = 0; i < 2; i++)
  1784. disp_transform[i] =
  1785. (int64_t) width * display_matrix[0][i] +
  1786. (int64_t) height * display_matrix[1][i] +
  1787. ((int64_t) display_matrix[2][i] << 16);
  1788. //sample aspect ratio is new width/height divided by old width/height
  1789. st->sample_aspect_ratio = av_d2q(
  1790. ((double) disp_transform[0] * height) /
  1791. ((double) disp_transform[1] * width), INT_MAX);
  1792. }
  1793. return 0;
  1794. }
  1795. static int mov_read_tfhd(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1796. {
  1797. MOVFragment *frag = &c->fragment;
  1798. MOVTrackExt *trex = NULL;
  1799. int flags, track_id, i;
  1800. avio_r8(pb); /* version */
  1801. flags = avio_rb24(pb);
  1802. track_id = avio_rb32(pb);
  1803. if (!track_id)
  1804. return -1;
  1805. frag->track_id = track_id;
  1806. for (i = 0; i < c->trex_count; i++)
  1807. if (c->trex_data[i].track_id == frag->track_id) {
  1808. trex = &c->trex_data[i];
  1809. break;
  1810. }
  1811. if (!trex) {
  1812. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding trex\n");
  1813. return -1;
  1814. }
  1815. if (flags & 0x01) frag->base_data_offset = avio_rb64(pb);
  1816. else frag->base_data_offset = frag->moof_offset;
  1817. if (flags & 0x02) frag->stsd_id = avio_rb32(pb);
  1818. else frag->stsd_id = trex->stsd_id;
  1819. frag->duration = flags & 0x08 ? avio_rb32(pb) : trex->duration;
  1820. frag->size = flags & 0x10 ? avio_rb32(pb) : trex->size;
  1821. frag->flags = flags & 0x20 ? avio_rb32(pb) : trex->flags;
  1822. av_dlog(c->fc, "frag flags 0x%x\n", frag->flags);
  1823. return 0;
  1824. }
  1825. static int mov_read_chap(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1826. {
  1827. c->chapter_track = avio_rb32(pb);
  1828. return 0;
  1829. }
  1830. static int mov_read_trex(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1831. {
  1832. MOVTrackExt *trex;
  1833. if ((uint64_t)c->trex_count+1 >= UINT_MAX / sizeof(*c->trex_data))
  1834. return -1;
  1835. trex = av_realloc(c->trex_data, (c->trex_count+1)*sizeof(*c->trex_data));
  1836. if (!trex)
  1837. return AVERROR(ENOMEM);
  1838. c->trex_data = trex;
  1839. trex = &c->trex_data[c->trex_count++];
  1840. avio_r8(pb); /* version */
  1841. avio_rb24(pb); /* flags */
  1842. trex->track_id = avio_rb32(pb);
  1843. trex->stsd_id = avio_rb32(pb);
  1844. trex->duration = avio_rb32(pb);
  1845. trex->size = avio_rb32(pb);
  1846. trex->flags = avio_rb32(pb);
  1847. return 0;
  1848. }
  1849. static int mov_read_trun(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1850. {
  1851. MOVFragment *frag = &c->fragment;
  1852. AVStream *st = NULL;
  1853. MOVStreamContext *sc;
  1854. MOVStts *ctts_data;
  1855. uint64_t offset;
  1856. int64_t dts;
  1857. int data_offset = 0;
  1858. unsigned entries, first_sample_flags = frag->flags;
  1859. int flags, distance, i;
  1860. for (i = 0; i < c->fc->nb_streams; i++) {
  1861. if (c->fc->streams[i]->id == frag->track_id) {
  1862. st = c->fc->streams[i];
  1863. break;
  1864. }
  1865. }
  1866. if (!st) {
  1867. av_log(c->fc, AV_LOG_ERROR, "could not find corresponding track id %d\n", frag->track_id);
  1868. return -1;
  1869. }
  1870. sc = st->priv_data;
  1871. if (sc->pseudo_stream_id+1 != frag->stsd_id)
  1872. return 0;
  1873. avio_r8(pb); /* version */
  1874. flags = avio_rb24(pb);
  1875. entries = avio_rb32(pb);
  1876. av_dlog(c->fc, "flags 0x%x entries %d\n", flags, entries);
  1877. /* Always assume the presence of composition time offsets.
  1878. * Without this assumption, for instance, we cannot deal with a track in fragmented movies that meet the following.
  1879. * 1) in the initial movie, there are no samples.
  1880. * 2) in the first movie fragment, there is only one sample without composition time offset.
  1881. * 3) in the subsequent movie fragments, there are samples with composition time offset. */
  1882. if (!sc->ctts_count && sc->sample_count)
  1883. {
  1884. /* Complement ctts table if moov atom doesn't have ctts atom. */
  1885. ctts_data = av_malloc(sizeof(*sc->ctts_data));
  1886. if (!ctts_data)
  1887. return AVERROR(ENOMEM);
  1888. sc->ctts_data = ctts_data;
  1889. sc->ctts_data[sc->ctts_count].count = sc->sample_count;
  1890. sc->ctts_data[sc->ctts_count].duration = 0;
  1891. sc->ctts_count++;
  1892. }
  1893. if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
  1894. return -1;
  1895. ctts_data = av_realloc(sc->ctts_data,
  1896. (entries+sc->ctts_count)*sizeof(*sc->ctts_data));
  1897. if (!ctts_data)
  1898. return AVERROR(ENOMEM);
  1899. sc->ctts_data = ctts_data;
  1900. if (flags & 0x001) data_offset = avio_rb32(pb);
  1901. if (flags & 0x004) first_sample_flags = avio_rb32(pb);
  1902. dts = st->duration - sc->time_offset;
  1903. offset = frag->base_data_offset + data_offset;
  1904. distance = 0;
  1905. av_dlog(c->fc, "first sample flags 0x%x\n", first_sample_flags);
  1906. for (i = 0; i < entries; i++) {
  1907. unsigned sample_size = frag->size;
  1908. int sample_flags = i ? frag->flags : first_sample_flags;
  1909. unsigned sample_duration = frag->duration;
  1910. int keyframe;
  1911. if (flags & 0x100) sample_duration = avio_rb32(pb);
  1912. if (flags & 0x200) sample_size = avio_rb32(pb);
  1913. if (flags & 0x400) sample_flags = avio_rb32(pb);
  1914. sc->ctts_data[sc->ctts_count].count = 1;
  1915. sc->ctts_data[sc->ctts_count].duration = (flags & 0x800) ? avio_rb32(pb) : 0;
  1916. sc->ctts_count++;
  1917. if ((keyframe = st->codec->codec_type == AVMEDIA_TYPE_AUDIO ||
  1918. (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))
  1919. distance = 0;
  1920. av_add_index_entry(st, offset, dts, sample_size, distance,
  1921. keyframe ? AVINDEX_KEYFRAME : 0);
  1922. av_dlog(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
  1923. "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,
  1924. offset, dts, sample_size, distance, keyframe);
  1925. distance++;
  1926. dts += sample_duration;
  1927. offset += sample_size;
  1928. }
  1929. frag->moof_offset = offset;
  1930. st->duration = dts + sc->time_offset;
  1931. return 0;
  1932. }
  1933. /* this atom should be null (from specs), but some buggy files put the 'moov' atom inside it... */
  1934. /* like the files created with Adobe Premiere 5.0, for samples see */
  1935. /* http://graphics.tudelft.nl/~wouter/publications/soundtests/ */
  1936. static int mov_read_wide(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1937. {
  1938. int err;
  1939. if (atom.size < 8)
  1940. return 0; /* continue */
  1941. if (avio_rb32(pb) != 0) { /* 0 sized mdat atom... use the 'wide' atom size */
  1942. avio_skip(pb, atom.size - 4);
  1943. return 0;
  1944. }
  1945. atom.type = avio_rl32(pb);
  1946. atom.size -= 8;
  1947. if (atom.type != MKTAG('m','d','a','t')) {
  1948. avio_skip(pb, atom.size);
  1949. return 0;
  1950. }
  1951. err = mov_read_mdat(c, pb, atom);
  1952. return err;
  1953. }
  1954. static int mov_read_cmov(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  1955. {
  1956. #if CONFIG_ZLIB
  1957. AVIOContext ctx;
  1958. uint8_t *cmov_data;
  1959. uint8_t *moov_data; /* uncompressed data */
  1960. long cmov_len, moov_len;
  1961. int ret = -1;
  1962. avio_rb32(pb); /* dcom atom */
  1963. if (avio_rl32(pb) != MKTAG('d','c','o','m'))
  1964. return -1;
  1965. if (avio_rl32(pb) != MKTAG('z','l','i','b')) {
  1966. av_log(c->fc, AV_LOG_ERROR, "unknown compression for cmov atom !");
  1967. return -1;
  1968. }
  1969. avio_rb32(pb); /* cmvd atom */
  1970. if (avio_rl32(pb) != MKTAG('c','m','v','d'))
  1971. return -1;
  1972. moov_len = avio_rb32(pb); /* uncompressed size */
  1973. cmov_len = atom.size - 6 * 4;
  1974. cmov_data = av_malloc(cmov_len);
  1975. if (!cmov_data)
  1976. return AVERROR(ENOMEM);
  1977. moov_data = av_malloc(moov_len);
  1978. if (!moov_data) {
  1979. av_free(cmov_data);
  1980. return AVERROR(ENOMEM);
  1981. }
  1982. avio_read(pb, cmov_data, cmov_len);
  1983. if (uncompress (moov_data, (uLongf *) &moov_len, (const Bytef *)cmov_data, cmov_len) != Z_OK)
  1984. goto free_and_return;
  1985. if (ffio_init_context(&ctx, moov_data, moov_len, 0, NULL, NULL, NULL, NULL) != 0)
  1986. goto free_and_return;
  1987. atom.type = MKTAG('m','o','o','v');
  1988. atom.size = moov_len;
  1989. ret = mov_read_default(c, &ctx, atom);
  1990. free_and_return:
  1991. av_free(moov_data);
  1992. av_free(cmov_data);
  1993. return ret;
  1994. #else
  1995. av_log(c->fc, AV_LOG_ERROR, "this file requires zlib support compiled in\n");
  1996. return -1;
  1997. #endif
  1998. }
  1999. /* edit list atom */
  2000. static int mov_read_elst(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2001. {
  2002. MOVStreamContext *sc;
  2003. int i, edit_count, version, edit_start_index = 0;
  2004. if (c->fc->nb_streams < 1)
  2005. return 0;
  2006. sc = c->fc->streams[c->fc->nb_streams-1]->priv_data;
  2007. version = avio_r8(pb); /* version */
  2008. avio_rb24(pb); /* flags */
  2009. edit_count = avio_rb32(pb); /* entries */
  2010. if ((uint64_t)edit_count*12+8 > atom.size)
  2011. return -1;
  2012. for (i=0; i<edit_count; i++){
  2013. int64_t time;
  2014. int64_t duration;
  2015. if (version == 1) {
  2016. duration = avio_rb64(pb);
  2017. time = avio_rb64(pb);
  2018. } else {
  2019. duration = avio_rb32(pb); /* segment duration */
  2020. time = (int32_t)avio_rb32(pb); /* media time */
  2021. }
  2022. avio_rb32(pb); /* Media rate */
  2023. if (i == 0 && time == -1) {
  2024. sc->empty_duration = duration;
  2025. edit_start_index = 1;
  2026. } else if (i == edit_start_index && time >= 0)
  2027. sc->start_time = time;
  2028. }
  2029. if (edit_count > 1)
  2030. av_log(c->fc, AV_LOG_WARNING, "multiple edit list entries, "
  2031. "a/v desync might occur, patch welcome\n");
  2032. av_dlog(c->fc, "track[%i].edit_count = %i\n", c->fc->nb_streams-1, edit_count);
  2033. return 0;
  2034. }
  2035. static int mov_read_chan(MOVContext *c, AVIOContext *pb, MOVAtom atom)
  2036. {
  2037. if (atom.size < 16)
  2038. return AVERROR_INVALIDDATA;
  2039. avio_skip(pb, 4);
  2040. ff_mov_read_chan(c->fc, atom.size - 4, c->fc->streams[0]->codec);
  2041. return 0;
  2042. }
  2043. static const MOVParseTableEntry mov_default_parse_table[] = {
  2044. { MKTAG('a','v','s','s'), mov_read_avss },
  2045. { MKTAG('c','h','p','l'), mov_read_chpl },
  2046. { MKTAG('c','o','6','4'), mov_read_stco },
  2047. { MKTAG('c','t','t','s'), mov_read_ctts }, /* composition time to sample */
  2048. { MKTAG('d','i','n','f'), mov_read_default },
  2049. { MKTAG('d','r','e','f'), mov_read_dref },
  2050. { MKTAG('e','d','t','s'), mov_read_default },
  2051. { MKTAG('e','l','s','t'), mov_read_elst },
  2052. { MKTAG('e','n','d','a'), mov_read_enda },
  2053. { MKTAG('f','i','e','l'), mov_read_fiel },
  2054. { MKTAG('f','t','y','p'), mov_read_ftyp },
  2055. { MKTAG('g','l','b','l'), mov_read_glbl },
  2056. { MKTAG('h','d','l','r'), mov_read_hdlr },
  2057. { MKTAG('i','l','s','t'), mov_read_ilst },
  2058. { MKTAG('j','p','2','h'), mov_read_jp2h },
  2059. { MKTAG('m','d','a','t'), mov_read_mdat },
  2060. { MKTAG('m','d','h','d'), mov_read_mdhd },
  2061. { MKTAG('m','d','i','a'), mov_read_default },
  2062. { MKTAG('m','e','t','a'), mov_read_meta },
  2063. { MKTAG('m','i','n','f'), mov_read_default },
  2064. { MKTAG('m','o','o','f'), mov_read_moof },
  2065. { MKTAG('m','o','o','v'), mov_read_moov },
  2066. { MKTAG('m','v','e','x'), mov_read_default },
  2067. { MKTAG('m','v','h','d'), mov_read_mvhd },
  2068. { MKTAG('S','M','I',' '), mov_read_smi }, /* Sorenson extension ??? */
  2069. { MKTAG('a','l','a','c'), mov_read_alac }, /* alac specific atom */
  2070. { MKTAG('a','v','c','C'), mov_read_glbl },
  2071. { MKTAG('p','a','s','p'), mov_read_pasp },
  2072. { MKTAG('s','t','b','l'), mov_read_default },
  2073. { MKTAG('s','t','c','o'), mov_read_stco },
  2074. { MKTAG('s','t','p','s'), mov_read_stps },
  2075. { MKTAG('s','t','r','f'), mov_read_strf },
  2076. { MKTAG('s','t','s','c'), mov_read_stsc },
  2077. { MKTAG('s','t','s','d'), mov_read_stsd }, /* sample description */
  2078. { MKTAG('s','t','s','s'), mov_read_stss }, /* sync sample */
  2079. { MKTAG('s','t','s','z'), mov_read_stsz }, /* sample size */
  2080. { MKTAG('s','t','t','s'), mov_read_stts },
  2081. { MKTAG('s','t','z','2'), mov_read_stsz }, /* compact sample size */
  2082. { MKTAG('t','k','h','d'), mov_read_tkhd }, /* track header */
  2083. { MKTAG('t','f','h','d'), mov_read_tfhd }, /* track fragment header */
  2084. { MKTAG('t','r','a','k'), mov_read_trak },
  2085. { MKTAG('t','r','a','f'), mov_read_default },
  2086. { MKTAG('t','r','e','f'), mov_read_default },
  2087. { MKTAG('c','h','a','p'), mov_read_chap },
  2088. { MKTAG('t','r','e','x'), mov_read_trex },
  2089. { MKTAG('t','r','u','n'), mov_read_trun },
  2090. { MKTAG('u','d','t','a'), mov_read_default },
  2091. { MKTAG('w','a','v','e'), mov_read_wave },
  2092. { MKTAG('e','s','d','s'), mov_read_esds },
  2093. { MKTAG('d','a','c','3'), mov_read_dac3 }, /* AC-3 info */
  2094. { MKTAG('w','i','d','e'), mov_read_wide }, /* place holder */
  2095. { MKTAG('w','f','e','x'), mov_read_wfex },
  2096. { MKTAG('c','m','o','v'), mov_read_cmov },
  2097. { MKTAG('c','h','a','n'), mov_read_chan },
  2098. { 0, NULL }
  2099. };
  2100. static int mov_probe(AVProbeData *p)
  2101. {
  2102. unsigned int offset;
  2103. uint32_t tag;
  2104. int score = 0;
  2105. /* check file header */
  2106. offset = 0;
  2107. for (;;) {
  2108. /* ignore invalid offset */
  2109. if ((offset + 8) > (unsigned int)p->buf_size)
  2110. return score;
  2111. tag = AV_RL32(p->buf + offset + 4);
  2112. switch(tag) {
  2113. /* check for obvious tags */
  2114. case MKTAG('j','P',' ',' '): /* jpeg 2000 signature */
  2115. case MKTAG('m','o','o','v'):
  2116. case MKTAG('m','d','a','t'):
  2117. case MKTAG('p','n','o','t'): /* detect movs with preview pics like ew.mov and april.mov */
  2118. case MKTAG('u','d','t','a'): /* Packet Video PVAuthor adds this and a lot of more junk */
  2119. case MKTAG('f','t','y','p'):
  2120. return AVPROBE_SCORE_MAX;
  2121. /* those are more common words, so rate then a bit less */
  2122. case MKTAG('e','d','i','w'): /* xdcam files have reverted first tags */
  2123. case MKTAG('w','i','d','e'):
  2124. case MKTAG('f','r','e','e'):
  2125. case MKTAG('j','u','n','k'):
  2126. case MKTAG('p','i','c','t'):
  2127. return AVPROBE_SCORE_MAX - 5;
  2128. case MKTAG(0x82,0x82,0x7f,0x7d):
  2129. case MKTAG('s','k','i','p'):
  2130. case MKTAG('u','u','i','d'):
  2131. case MKTAG('p','r','f','l'):
  2132. offset = AV_RB32(p->buf+offset) + offset;
  2133. /* if we only find those cause probedata is too small at least rate them */
  2134. score = AVPROBE_SCORE_MAX - 50;
  2135. break;
  2136. default:
  2137. /* unrecognized tag */
  2138. return score;
  2139. }
  2140. }
  2141. }
  2142. // must be done after parsing all trak because there's no order requirement
  2143. static void mov_read_chapters(AVFormatContext *s)
  2144. {
  2145. MOVContext *mov = s->priv_data;
  2146. AVStream *st = NULL;
  2147. MOVStreamContext *sc;
  2148. int64_t cur_pos;
  2149. int i;
  2150. for (i = 0; i < s->nb_streams; i++)
  2151. if (s->streams[i]->id == mov->chapter_track) {
  2152. st = s->streams[i];
  2153. break;
  2154. }
  2155. if (!st) {
  2156. av_log(s, AV_LOG_ERROR, "Referenced QT chapter track not found\n");
  2157. return;
  2158. }
  2159. st->discard = AVDISCARD_ALL;
  2160. sc = st->priv_data;
  2161. cur_pos = avio_tell(sc->pb);
  2162. for (i = 0; i < st->nb_index_entries; i++) {
  2163. AVIndexEntry *sample = &st->index_entries[i];
  2164. int64_t end = i+1 < st->nb_index_entries ? st->index_entries[i+1].timestamp : st->duration;
  2165. uint8_t *title;
  2166. uint16_t ch;
  2167. int len, title_len;
  2168. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  2169. av_log(s, AV_LOG_ERROR, "Chapter %d not found in file\n", i);
  2170. goto finish;
  2171. }
  2172. // the first two bytes are the length of the title
  2173. len = avio_rb16(sc->pb);
  2174. if (len > sample->size-2)
  2175. continue;
  2176. title_len = 2*len + 1;
  2177. if (!(title = av_mallocz(title_len)))
  2178. goto finish;
  2179. // The samples could theoretically be in any encoding if there's an encd
  2180. // atom following, but in practice are only utf-8 or utf-16, distinguished
  2181. // instead by the presence of a BOM
  2182. if (!len) {
  2183. title[0] = 0;
  2184. } else {
  2185. ch = avio_rb16(sc->pb);
  2186. if (ch == 0xfeff)
  2187. avio_get_str16be(sc->pb, len, title, title_len);
  2188. else if (ch == 0xfffe)
  2189. avio_get_str16le(sc->pb, len, title, title_len);
  2190. else {
  2191. AV_WB16(title, ch);
  2192. if (len == 1 || len == 2)
  2193. title[len] = 0;
  2194. else
  2195. get_strz(sc->pb, title + 2, len - 1);
  2196. }
  2197. }
  2198. avpriv_new_chapter(s, i, st->time_base, sample->timestamp, end, title);
  2199. av_freep(&title);
  2200. }
  2201. finish:
  2202. avio_seek(sc->pb, cur_pos, SEEK_SET);
  2203. }
  2204. static int mov_read_header(AVFormatContext *s, AVFormatParameters *ap)
  2205. {
  2206. MOVContext *mov = s->priv_data;
  2207. AVIOContext *pb = s->pb;
  2208. int err;
  2209. MOVAtom atom = { AV_RL32("root") };
  2210. mov->fc = s;
  2211. /* .mov and .mp4 aren't streamable anyway (only progressive download if moov is before mdat) */
  2212. if (pb->seekable)
  2213. atom.size = avio_size(pb);
  2214. else
  2215. atom.size = INT64_MAX;
  2216. /* check MOV header */
  2217. if ((err = mov_read_default(mov, pb, atom)) < 0) {
  2218. av_log(s, AV_LOG_ERROR, "error reading header: %d\n", err);
  2219. return err;
  2220. }
  2221. if (!mov->found_moov) {
  2222. av_log(s, AV_LOG_ERROR, "moov atom not found\n");
  2223. return -1;
  2224. }
  2225. av_dlog(mov->fc, "on_parse_exit_offset=%"PRId64"\n", avio_tell(pb));
  2226. if (pb->seekable && mov->chapter_track > 0)
  2227. mov_read_chapters(s);
  2228. return 0;
  2229. }
  2230. static AVIndexEntry *mov_find_next_sample(AVFormatContext *s, AVStream **st)
  2231. {
  2232. AVIndexEntry *sample = NULL;
  2233. int64_t best_dts = INT64_MAX;
  2234. int i;
  2235. for (i = 0; i < s->nb_streams; i++) {
  2236. AVStream *avst = s->streams[i];
  2237. MOVStreamContext *msc = avst->priv_data;
  2238. if (msc->pb && msc->current_sample < avst->nb_index_entries) {
  2239. AVIndexEntry *current_sample = &avst->index_entries[msc->current_sample];
  2240. int64_t dts = av_rescale(current_sample->timestamp, AV_TIME_BASE, msc->time_scale);
  2241. av_dlog(s, "stream %d, sample %d, dts %"PRId64"\n", i, msc->current_sample, dts);
  2242. if (!sample || (!s->pb->seekable && current_sample->pos < sample->pos) ||
  2243. (s->pb->seekable &&
  2244. ((msc->pb != s->pb && dts < best_dts) || (msc->pb == s->pb &&
  2245. ((FFABS(best_dts - dts) <= AV_TIME_BASE && current_sample->pos < sample->pos) ||
  2246. (FFABS(best_dts - dts) > AV_TIME_BASE && dts < best_dts)))))) {
  2247. sample = current_sample;
  2248. best_dts = dts;
  2249. *st = avst;
  2250. }
  2251. }
  2252. }
  2253. return sample;
  2254. }
  2255. static int mov_read_packet(AVFormatContext *s, AVPacket *pkt)
  2256. {
  2257. MOVContext *mov = s->priv_data;
  2258. MOVStreamContext *sc;
  2259. AVIndexEntry *sample;
  2260. AVStream *st = NULL;
  2261. int ret;
  2262. retry:
  2263. sample = mov_find_next_sample(s, &st);
  2264. if (!sample) {
  2265. mov->found_mdat = 0;
  2266. if (s->pb->seekable||
  2267. mov_read_default(mov, s->pb, (MOVAtom){ AV_RL32("root"), INT64_MAX }) < 0 ||
  2268. url_feof(s->pb))
  2269. return AVERROR_EOF;
  2270. av_dlog(s, "read fragments, offset 0x%"PRIx64"\n", avio_tell(s->pb));
  2271. goto retry;
  2272. }
  2273. sc = st->priv_data;
  2274. /* must be done just before reading, to avoid infinite loop on sample */
  2275. sc->current_sample++;
  2276. if (st->discard != AVDISCARD_ALL) {
  2277. if (avio_seek(sc->pb, sample->pos, SEEK_SET) != sample->pos) {
  2278. av_log(mov->fc, AV_LOG_ERROR, "stream %d, offset 0x%"PRIx64": partial file\n",
  2279. sc->ffindex, sample->pos);
  2280. return -1;
  2281. }
  2282. ret = av_get_packet(sc->pb, pkt, sample->size);
  2283. if (ret < 0)
  2284. return ret;
  2285. if (sc->has_palette) {
  2286. uint8_t *pal;
  2287. pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
  2288. if (!pal) {
  2289. av_log(mov->fc, AV_LOG_ERROR, "Cannot append palette to packet\n");
  2290. } else {
  2291. memcpy(pal, sc->palette, AVPALETTE_SIZE);
  2292. sc->has_palette = 0;
  2293. }
  2294. }
  2295. #if CONFIG_DV_DEMUXER
  2296. if (mov->dv_demux && sc->dv_audio_container) {
  2297. avpriv_dv_produce_packet(mov->dv_demux, pkt, pkt->data, pkt->size, pkt->pos);
  2298. av_free(pkt->data);
  2299. pkt->size = 0;
  2300. ret = avpriv_dv_get_packet(mov->dv_demux, pkt);
  2301. if (ret < 0)
  2302. return ret;
  2303. }
  2304. #endif
  2305. }
  2306. pkt->stream_index = sc->ffindex;
  2307. pkt->dts = sample->timestamp;
  2308. if (sc->ctts_data) {
  2309. pkt->pts = pkt->dts + sc->dts_shift + sc->ctts_data[sc->ctts_index].duration;
  2310. /* update ctts context */
  2311. sc->ctts_sample++;
  2312. if (sc->ctts_index < sc->ctts_count &&
  2313. sc->ctts_data[sc->ctts_index].count == sc->ctts_sample) {
  2314. sc->ctts_index++;
  2315. sc->ctts_sample = 0;
  2316. }
  2317. if (sc->wrong_dts)
  2318. pkt->dts = AV_NOPTS_VALUE;
  2319. } else {
  2320. int64_t next_dts = (sc->current_sample < st->nb_index_entries) ?
  2321. st->index_entries[sc->current_sample].timestamp : st->duration;
  2322. pkt->duration = next_dts - pkt->dts;
  2323. pkt->pts = pkt->dts;
  2324. }
  2325. if (st->discard == AVDISCARD_ALL)
  2326. goto retry;
  2327. pkt->flags |= sample->flags & AVINDEX_KEYFRAME ? AV_PKT_FLAG_KEY : 0;
  2328. pkt->pos = sample->pos;
  2329. av_dlog(s, "stream %d, pts %"PRId64", dts %"PRId64", pos 0x%"PRIx64", duration %d\n",
  2330. pkt->stream_index, pkt->pts, pkt->dts, pkt->pos, pkt->duration);
  2331. return 0;
  2332. }
  2333. static int mov_seek_stream(AVFormatContext *s, AVStream *st, int64_t timestamp, int flags)
  2334. {
  2335. MOVStreamContext *sc = st->priv_data;
  2336. int sample, time_sample;
  2337. int i;
  2338. sample = av_index_search_timestamp(st, timestamp, flags);
  2339. av_dlog(s, "stream %d, timestamp %"PRId64", sample %d\n", st->index, timestamp, sample);
  2340. if (sample < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
  2341. sample = 0;
  2342. if (sample < 0) /* not sure what to do */
  2343. return -1;
  2344. sc->current_sample = sample;
  2345. av_dlog(s, "stream %d, found sample %d\n", st->index, sc->current_sample);
  2346. /* adjust ctts index */
  2347. if (sc->ctts_data) {
  2348. time_sample = 0;
  2349. for (i = 0; i < sc->ctts_count; i++) {
  2350. int next = time_sample + sc->ctts_data[i].count;
  2351. if (next > sc->current_sample) {
  2352. sc->ctts_index = i;
  2353. sc->ctts_sample = sc->current_sample - time_sample;
  2354. break;
  2355. }
  2356. time_sample = next;
  2357. }
  2358. }
  2359. return sample;
  2360. }
  2361. static int mov_read_seek(AVFormatContext *s, int stream_index, int64_t sample_time, int flags)
  2362. {
  2363. AVStream *st;
  2364. int64_t seek_timestamp, timestamp;
  2365. int sample;
  2366. int i;
  2367. if (stream_index >= s->nb_streams)
  2368. return -1;
  2369. if (sample_time < 0)
  2370. sample_time = 0;
  2371. st = s->streams[stream_index];
  2372. sample = mov_seek_stream(s, st, sample_time, flags);
  2373. if (sample < 0)
  2374. return -1;
  2375. /* adjust seek timestamp to found sample timestamp */
  2376. seek_timestamp = st->index_entries[sample].timestamp;
  2377. for (i = 0; i < s->nb_streams; i++) {
  2378. st = s->streams[i];
  2379. if (stream_index == i)
  2380. continue;
  2381. timestamp = av_rescale_q(seek_timestamp, s->streams[stream_index]->time_base, st->time_base);
  2382. mov_seek_stream(s, st, timestamp, flags);
  2383. }
  2384. return 0;
  2385. }
  2386. static int mov_read_close(AVFormatContext *s)
  2387. {
  2388. MOVContext *mov = s->priv_data;
  2389. int i, j;
  2390. for (i = 0; i < s->nb_streams; i++) {
  2391. AVStream *st = s->streams[i];
  2392. MOVStreamContext *sc = st->priv_data;
  2393. av_freep(&sc->ctts_data);
  2394. for (j = 0; j < sc->drefs_count; j++) {
  2395. av_freep(&sc->drefs[j].path);
  2396. av_freep(&sc->drefs[j].dir);
  2397. }
  2398. av_freep(&sc->drefs);
  2399. if (sc->pb && sc->pb != s->pb)
  2400. avio_close(sc->pb);
  2401. }
  2402. if (mov->dv_demux) {
  2403. for (i = 0; i < mov->dv_fctx->nb_streams; i++) {
  2404. av_freep(&mov->dv_fctx->streams[i]->codec);
  2405. av_freep(&mov->dv_fctx->streams[i]);
  2406. }
  2407. av_freep(&mov->dv_fctx);
  2408. av_freep(&mov->dv_demux);
  2409. }
  2410. av_freep(&mov->trex_data);
  2411. return 0;
  2412. }
  2413. AVInputFormat ff_mov_demuxer = {
  2414. .name = "mov,mp4,m4a,3gp,3g2,mj2",
  2415. .long_name = NULL_IF_CONFIG_SMALL("QuickTime/MPEG-4/Motion JPEG 2000 format"),
  2416. .priv_data_size = sizeof(MOVContext),
  2417. .read_probe = mov_probe,
  2418. .read_header = mov_read_header,
  2419. .read_packet = mov_read_packet,
  2420. .read_close = mov_read_close,
  2421. .read_seek = mov_read_seek,
  2422. };