mov.c 86 KB

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