mxfenc.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  1. /*
  2. * MXF muxer
  3. * Copyright (c) 2008 GUCAS, Zhentan Feng <spyfeng at gmail dot com>
  4. * Copyright (c) 2008 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /*
  23. * References
  24. * SMPTE 336M KLV Data Encoding Protocol Using Key-Length-Value
  25. * SMPTE 377M MXF File Format Specifications
  26. * SMPTE 379M MXF Generic Container
  27. * SMPTE 381M Mapping MPEG Streams into the MXF Generic Container
  28. * SMPTE RP210: SMPTE Metadata Dictionary
  29. * SMPTE RP224: Registry of SMPTE Universal Labels
  30. */
  31. //#define DEBUG
  32. #include <math.h>
  33. #include <time.h>
  34. #include "libavutil/random_seed.h"
  35. #include "libavcodec/bytestream.h"
  36. #include "audiointerleave.h"
  37. #include "avformat.h"
  38. #include "mxf.h"
  39. static const int NTSC_samples_per_frame[] = { 1602, 1601, 1602, 1601, 1602, 0 };
  40. static const int PAL_samples_per_frame[] = { 1920, 0 };
  41. AVOutputFormat mxf_d10_muxer;
  42. #define EDIT_UNITS_PER_BODY 250
  43. #define KAG_SIZE 512
  44. typedef struct {
  45. int local_tag;
  46. UID uid;
  47. } MXFLocalTagPair;
  48. typedef struct {
  49. uint8_t flags;
  50. uint64_t offset;
  51. unsigned slice_offset; ///< offset of audio slice
  52. } MXFIndexEntry;
  53. typedef struct {
  54. AudioInterleaveContext aic;
  55. UID track_essence_element_key;
  56. int index; ///< index in mxf_essence_container_uls table
  57. const UID *codec_ul;
  58. int order; ///< interleaving order if dts are equal
  59. int interlaced; ///< wether picture is interlaced
  60. int temporal_reordering;
  61. AVRational aspect_ratio; ///< display aspect ratio
  62. int closed_gop; ///< gop is closed, used in mpeg-2 frame parsing
  63. } MXFStreamContext;
  64. typedef struct {
  65. UID container_ul;
  66. UID element_ul;
  67. UID codec_ul;
  68. void (*write_desc)();
  69. } MXFContainerEssenceEntry;
  70. static const struct {
  71. enum CodecID id;
  72. int index;
  73. } mxf_essence_mappings[] = {
  74. { CODEC_ID_MPEG2VIDEO, 0 },
  75. { CODEC_ID_PCM_S24LE, 1 },
  76. { CODEC_ID_PCM_S16LE, 1 },
  77. { CODEC_ID_NONE }
  78. };
  79. static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st);
  80. static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st);
  81. static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st);
  82. static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st);
  83. static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st);
  84. static const MXFContainerEssenceEntry mxf_essence_container_uls[] = {
  85. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
  86. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
  87. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
  88. mxf_write_mpegvideo_desc },
  89. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },
  90. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 },
  91. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
  92. mxf_write_aes3_desc },
  93. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
  94. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
  95. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
  96. mxf_write_wav_desc },
  97. // D-10 625/50 PAL 50mb/s
  98. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
  99. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
  100. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 },
  101. mxf_write_cdci_desc },
  102. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
  103. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
  104. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
  105. mxf_write_generic_sound_desc },
  106. // D-10 525/60 NTSC 50mb/s
  107. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
  108. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
  109. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x02 },
  110. mxf_write_cdci_desc },
  111. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 },
  112. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
  113. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
  114. mxf_write_generic_sound_desc },
  115. // D-10 625/50 PAL 40mb/s
  116. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
  117. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
  118. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x03 },
  119. mxf_write_cdci_desc },
  120. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 },
  121. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
  122. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
  123. mxf_write_generic_sound_desc },
  124. // D-10 525/60 NTSC 40mb/s
  125. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
  126. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
  127. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x04 },
  128. mxf_write_cdci_desc },
  129. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 },
  130. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
  131. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
  132. mxf_write_generic_sound_desc },
  133. // D-10 625/50 PAL 30mb/s
  134. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
  135. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
  136. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 },
  137. mxf_write_cdci_desc },
  138. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 },
  139. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
  140. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
  141. mxf_write_generic_sound_desc },
  142. // D-10 525/60 NTSC 30mb/s
  143. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
  144. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
  145. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x06 },
  146. mxf_write_cdci_desc },
  147. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 },
  148. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
  149. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
  150. mxf_write_generic_sound_desc },
  151. { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  152. { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  153. { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  154. NULL },
  155. };
  156. typedef struct MXFContext {
  157. int64_t footer_partition_offset;
  158. int essence_container_count;
  159. AVRational time_base;
  160. int header_written;
  161. MXFIndexEntry *index_entries;
  162. unsigned edit_units_count;
  163. uint64_t timestamp; ///< timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
  164. uint8_t slice_count; ///< index slice count minus 1 (1 if no audio, 0 otherwise)
  165. int last_indexed_edit_unit;
  166. uint64_t *body_partition_offset;
  167. unsigned body_partitions_count;
  168. int last_key_index; ///< index of last key frame
  169. uint64_t duration;
  170. AVStream *timecode_track;
  171. int timecode_base; ///< rounded time code base (25 or 30)
  172. int timecode_start; ///< frame number computed from mpeg-2 gop header timecode
  173. int timecode_drop_frame; ///< time code use drop frame method frop mpeg-2 essence gop header
  174. int edit_unit_byte_count; ///< fixed edit unit byte count
  175. uint64_t body_offset;
  176. uint32_t instance_number;
  177. uint8_t umid[16]; ///< unique material identifier
  178. } MXFContext;
  179. static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
  180. static const uint8_t umid_ul[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13 };
  181. /**
  182. * complete key for operation pattern, partitions, and primer pack
  183. */
  184. static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x09,0x00 };
  185. static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
  186. static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
  187. static const uint8_t index_table_segment_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 };
  188. static const uint8_t random_index_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 };
  189. static const uint8_t header_open_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x02,0x01,0x00 }; // OpenIncomplete
  190. static const uint8_t header_closed_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x02,0x04,0x00 }; // ClosedComplete
  191. static const uint8_t klv_fill_key[] = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 };
  192. static const uint8_t body_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }; // ClosedComplete
  193. /**
  194. * partial key for header metadata
  195. */
  196. static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
  197. static const uint8_t multiple_desc_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
  198. /**
  199. * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
  200. */
  201. static const MXFLocalTagPair mxf_local_tag_batch[] = {
  202. // preface set
  203. { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
  204. { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
  205. { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
  206. { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
  207. { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
  208. { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
  209. { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
  210. { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
  211. // Identification
  212. { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
  213. { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
  214. { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */
  215. { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */
  216. { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
  217. { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
  218. // Content Storage
  219. { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */
  220. { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */
  221. // Essence Container Data
  222. { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
  223. { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
  224. // Package
  225. { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
  226. { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
  227. { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */
  228. { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
  229. { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
  230. // Track
  231. { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */
  232. { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */
  233. { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
  234. { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
  235. { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
  236. // Sequence
  237. { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
  238. { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
  239. { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
  240. // Source Clip
  241. { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */
  242. { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
  243. { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */
  244. // Timecode Component
  245. { 0x1501, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x05,0x00,0x00}}, /* Start Time Code */
  246. { 0x1502, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x04,0x01,0x01,0x02,0x06,0x00,0x00}}, /* Rounded Time Code Base */
  247. { 0x1503, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x04,0x01,0x01,0x05,0x00,0x00,0x00}}, /* Drop Frame */
  248. // File Descriptor
  249. { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */
  250. { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
  251. { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */
  252. { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
  253. // Generic Picture Essence Descriptor
  254. { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */
  255. { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */
  256. { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
  257. { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */
  258. { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */
  259. { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */
  260. { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
  261. { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */
  262. // CDCI Picture Essence Descriptor
  263. { 0x3301, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x0A,0x00,0x00,0x00}}, /* Component Depth */
  264. { 0x3302, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x05,0x00,0x00,0x00}}, /* Horizontal Subsampling */
  265. // Generic Sound Essence Descriptor
  266. { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */
  267. { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
  268. { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
  269. { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */
  270. { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */
  271. // Index Table Segment
  272. { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */
  273. { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */
  274. { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */
  275. { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */
  276. { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */
  277. { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */
  278. { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */
  279. { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */
  280. // MPEG video Descriptor
  281. { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */
  282. { 0x8007, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0A,0x00,0x00}}, /* ProfileAndLevel */
  283. // Wave Audio Essence Descriptor
  284. { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */
  285. { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */
  286. };
  287. static void mxf_write_uuid(ByteIOContext *pb, enum MXFMetadataSetType type, int value)
  288. {
  289. put_buffer(pb, uuid_base, 12);
  290. put_be16(pb, type);
  291. put_be16(pb, value);
  292. }
  293. static void mxf_write_umid(AVFormatContext *s, int type)
  294. {
  295. MXFContext *mxf = s->priv_data;
  296. put_buffer(s->pb, umid_ul, 13);
  297. put_be24(s->pb, mxf->instance_number);
  298. put_buffer(s->pb, mxf->umid, 15);
  299. put_byte(s->pb, type);
  300. }
  301. static void mxf_write_refs_count(ByteIOContext *pb, int ref_count)
  302. {
  303. put_be32(pb, ref_count);
  304. put_be32(pb, 16);
  305. }
  306. static int klv_ber_length(uint64_t len)
  307. {
  308. if (len < 128)
  309. return 1;
  310. else
  311. return (av_log2(len) >> 3) + 2;
  312. }
  313. static int klv_encode_ber_length(ByteIOContext *pb, uint64_t len)
  314. {
  315. // Determine the best BER size
  316. int size;
  317. if (len < 128) {
  318. //short form
  319. put_byte(pb, len);
  320. return 1;
  321. }
  322. size = (av_log2(len) >> 3) + 1;
  323. // long form
  324. put_byte(pb, 0x80 + size);
  325. while(size) {
  326. size--;
  327. put_byte(pb, len >> 8 * size & 0xff);
  328. }
  329. return 0;
  330. }
  331. static void klv_encode_ber4_length(ByteIOContext *pb, int len)
  332. {
  333. put_byte(pb, 0x80 + 3);
  334. put_be24(pb, len);
  335. }
  336. /*
  337. * Get essence container ul index
  338. */
  339. static int mxf_get_essence_container_ul_index(enum CodecID id)
  340. {
  341. int i;
  342. for (i = 0; mxf_essence_mappings[i].id; i++)
  343. if (mxf_essence_mappings[i].id == id)
  344. return mxf_essence_mappings[i].index;
  345. return -1;
  346. }
  347. static void mxf_write_primer_pack(AVFormatContext *s)
  348. {
  349. ByteIOContext *pb = s->pb;
  350. int local_tag_number, i = 0;
  351. local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch);
  352. put_buffer(pb, primer_pack_key, 16);
  353. klv_encode_ber_length(pb, local_tag_number * 18 + 8);
  354. put_be32(pb, local_tag_number); // local_tag num
  355. put_be32(pb, 18); // item size, always 18 according to the specs
  356. for (i = 0; i < local_tag_number; i++) {
  357. put_be16(pb, mxf_local_tag_batch[i].local_tag);
  358. put_buffer(pb, mxf_local_tag_batch[i].uid, 16);
  359. }
  360. }
  361. static void mxf_write_local_tag(ByteIOContext *pb, int size, int tag)
  362. {
  363. put_be16(pb, tag);
  364. put_be16(pb, size);
  365. }
  366. static void mxf_write_metadata_key(ByteIOContext *pb, unsigned int value)
  367. {
  368. put_buffer(pb, header_metadata_key, 13);
  369. put_be24(pb, value);
  370. }
  371. static void mxf_free(AVFormatContext *s)
  372. {
  373. int i;
  374. for (i = 0; i < s->nb_streams; i++) {
  375. AVStream *st = s->streams[i];
  376. av_freep(&st->priv_data);
  377. }
  378. }
  379. static const MXFCodecUL *mxf_get_data_definition_ul(int type)
  380. {
  381. const MXFCodecUL *uls = ff_mxf_data_definition_uls;
  382. while (uls->uid[0]) {
  383. if (type == uls->id)
  384. break;
  385. uls++;
  386. }
  387. return uls;
  388. }
  389. static void mxf_write_essence_container_refs(AVFormatContext *s)
  390. {
  391. MXFContext *c = s->priv_data;
  392. ByteIOContext *pb = s->pb;
  393. int i;
  394. mxf_write_refs_count(pb, c->essence_container_count);
  395. av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count);
  396. for (i = 0; i < c->essence_container_count; i++) {
  397. MXFStreamContext *sc = s->streams[i]->priv_data;
  398. put_buffer(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
  399. }
  400. }
  401. static void mxf_write_preface(AVFormatContext *s)
  402. {
  403. MXFContext *mxf = s->priv_data;
  404. ByteIOContext *pb = s->pb;
  405. mxf_write_metadata_key(pb, 0x012f00);
  406. PRINT_KEY(s, "preface key", pb->buf_ptr - 16);
  407. klv_encode_ber_length(pb, 130 + 16 * mxf->essence_container_count);
  408. // write preface set uid
  409. mxf_write_local_tag(pb, 16, 0x3C0A);
  410. mxf_write_uuid(pb, Preface, 0);
  411. PRINT_KEY(s, "preface uid", pb->buf_ptr - 16);
  412. // last modified date
  413. mxf_write_local_tag(pb, 8, 0x3B02);
  414. put_be64(pb, mxf->timestamp);
  415. // write version
  416. mxf_write_local_tag(pb, 2, 0x3B05);
  417. put_be16(pb, 258); // v1.2
  418. // write identification_refs
  419. mxf_write_local_tag(pb, 16 + 8, 0x3B06);
  420. mxf_write_refs_count(pb, 1);
  421. mxf_write_uuid(pb, Identification, 0);
  422. // write content_storage_refs
  423. mxf_write_local_tag(pb, 16, 0x3B03);
  424. mxf_write_uuid(pb, ContentStorage, 0);
  425. // operational pattern
  426. mxf_write_local_tag(pb, 16, 0x3B09);
  427. put_buffer(pb, op1a_ul, 16);
  428. // write essence_container_refs
  429. mxf_write_local_tag(pb, 8 + 16 * mxf->essence_container_count, 0x3B0A);
  430. mxf_write_essence_container_refs(s);
  431. // write dm_scheme_refs
  432. mxf_write_local_tag(pb, 8, 0x3B0B);
  433. put_be64(pb, 0);
  434. }
  435. /*
  436. * Write a local tag containing an ascii string as utf-16
  437. */
  438. static void mxf_write_local_tag_utf16(ByteIOContext *pb, int tag, const char *value)
  439. {
  440. int i, size = strlen(value);
  441. mxf_write_local_tag(pb, size*2, tag);
  442. for (i = 0; i < size; i++)
  443. put_be16(pb, value[i]);
  444. }
  445. static void mxf_write_identification(AVFormatContext *s)
  446. {
  447. MXFContext *mxf = s->priv_data;
  448. ByteIOContext *pb = s->pb;
  449. const char *company = "FFmpeg";
  450. const char *product = "OP1a Muxer";
  451. const char *version;
  452. int length;
  453. mxf_write_metadata_key(pb, 0x013000);
  454. PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
  455. version = s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT ?
  456. "0.0.0" : AV_STRINGIFY(LIBAVFORMAT_VERSION);
  457. length = 84 + (strlen(company)+strlen(product)+strlen(version))*2; // utf-16
  458. klv_encode_ber_length(pb, length);
  459. // write uid
  460. mxf_write_local_tag(pb, 16, 0x3C0A);
  461. mxf_write_uuid(pb, Identification, 0);
  462. PRINT_KEY(s, "identification uid", pb->buf_ptr - 16);
  463. // write generation uid
  464. mxf_write_local_tag(pb, 16, 0x3C09);
  465. mxf_write_uuid(pb, Identification, 1);
  466. mxf_write_local_tag_utf16(pb, 0x3C01, company); // Company Name
  467. mxf_write_local_tag_utf16(pb, 0x3C02, product); // Product Name
  468. mxf_write_local_tag_utf16(pb, 0x3C04, version); // Version String
  469. // write product uid
  470. mxf_write_local_tag(pb, 16, 0x3C05);
  471. mxf_write_uuid(pb, Identification, 2);
  472. // modification date
  473. mxf_write_local_tag(pb, 8, 0x3C06);
  474. put_be64(pb, mxf->timestamp);
  475. }
  476. static void mxf_write_content_storage(AVFormatContext *s)
  477. {
  478. ByteIOContext *pb = s->pb;
  479. mxf_write_metadata_key(pb, 0x011800);
  480. PRINT_KEY(s, "content storage key", pb->buf_ptr - 16);
  481. klv_encode_ber_length(pb, 92);
  482. // write uid
  483. mxf_write_local_tag(pb, 16, 0x3C0A);
  484. mxf_write_uuid(pb, ContentStorage, 0);
  485. PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16);
  486. // write package reference
  487. mxf_write_local_tag(pb, 16 * 2 + 8, 0x1901);
  488. mxf_write_refs_count(pb, 2);
  489. mxf_write_uuid(pb, MaterialPackage, 0);
  490. mxf_write_uuid(pb, SourcePackage, 0);
  491. // write essence container data
  492. mxf_write_local_tag(pb, 8 + 16, 0x1902);
  493. mxf_write_refs_count(pb, 1);
  494. mxf_write_uuid(pb, EssenceContainerData, 0);
  495. }
  496. static void mxf_write_track(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
  497. {
  498. MXFContext *mxf = s->priv_data;
  499. ByteIOContext *pb = s->pb;
  500. MXFStreamContext *sc = st->priv_data;
  501. mxf_write_metadata_key(pb, 0x013b00);
  502. PRINT_KEY(s, "track key", pb->buf_ptr - 16);
  503. klv_encode_ber_length(pb, 80);
  504. // write track uid
  505. mxf_write_local_tag(pb, 16, 0x3C0A);
  506. mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, st->index);
  507. PRINT_KEY(s, "track uid", pb->buf_ptr - 16);
  508. // write track id
  509. mxf_write_local_tag(pb, 4, 0x4801);
  510. put_be32(pb, st->index+2);
  511. // write track number
  512. mxf_write_local_tag(pb, 4, 0x4804);
  513. if (type == MaterialPackage)
  514. put_be32(pb, 0); // track number of material package is 0
  515. else
  516. put_buffer(pb, sc->track_essence_element_key + 12, 4);
  517. mxf_write_local_tag(pb, 8, 0x4B01);
  518. put_be32(pb, mxf->time_base.den);
  519. put_be32(pb, mxf->time_base.num);
  520. // write origin
  521. mxf_write_local_tag(pb, 8, 0x4B02);
  522. put_be64(pb, 0);
  523. // write sequence refs
  524. mxf_write_local_tag(pb, 16, 0x4803);
  525. mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
  526. }
  527. static const uint8_t smpte_12m_timecode_track_data_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x01,0x03,0x02,0x01,0x01,0x00,0x00,0x00 };
  528. static void mxf_write_common_fields(AVFormatContext *s, AVStream *st)
  529. {
  530. MXFContext *mxf = s->priv_data;
  531. ByteIOContext *pb = s->pb;
  532. // find data define uls
  533. mxf_write_local_tag(pb, 16, 0x0201);
  534. if (st == mxf->timecode_track)
  535. put_buffer(pb, smpte_12m_timecode_track_data_ul, 16);
  536. else {
  537. const MXFCodecUL *data_def_ul = mxf_get_data_definition_ul(st->codec->codec_type);
  538. put_buffer(pb, data_def_ul->uid, 16);
  539. }
  540. // write duration
  541. mxf_write_local_tag(pb, 8, 0x0202);
  542. put_be64(pb, mxf->duration);
  543. }
  544. static void mxf_write_sequence(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
  545. {
  546. MXFContext *mxf = s->priv_data;
  547. ByteIOContext *pb = s->pb;
  548. enum MXFMetadataSetType component;
  549. mxf_write_metadata_key(pb, 0x010f00);
  550. PRINT_KEY(s, "sequence key", pb->buf_ptr - 16);
  551. klv_encode_ber_length(pb, 80);
  552. mxf_write_local_tag(pb, 16, 0x3C0A);
  553. mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
  554. PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16);
  555. mxf_write_common_fields(s, st);
  556. // write structural component
  557. mxf_write_local_tag(pb, 16 + 8, 0x1001);
  558. mxf_write_refs_count(pb, 1);
  559. if (st == mxf->timecode_track)
  560. component = TimecodeComponent;
  561. else
  562. component = SourceClip;
  563. if (type == SourcePackage)
  564. component += TypeBottom;
  565. mxf_write_uuid(pb, component, st->index);
  566. }
  567. static void mxf_write_timecode_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
  568. {
  569. MXFContext *mxf = s->priv_data;
  570. ByteIOContext *pb = s->pb;
  571. mxf_write_metadata_key(pb, 0x011400);
  572. klv_encode_ber_length(pb, 75);
  573. // UID
  574. mxf_write_local_tag(pb, 16, 0x3C0A);
  575. mxf_write_uuid(pb, type == MaterialPackage ? TimecodeComponent :
  576. TimecodeComponent + TypeBottom, st->index);
  577. mxf_write_common_fields(s, st);
  578. // Start Time Code
  579. mxf_write_local_tag(pb, 8, 0x1501);
  580. put_be64(pb, mxf->timecode_start);
  581. // Rounded Time Code Base
  582. mxf_write_local_tag(pb, 2, 0x1502);
  583. put_be16(pb, mxf->timecode_base);
  584. // Drop Frame
  585. mxf_write_local_tag(pb, 1, 0x1503);
  586. put_byte(pb, mxf->timecode_drop_frame);
  587. }
  588. static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
  589. {
  590. ByteIOContext *pb = s->pb;
  591. int i;
  592. mxf_write_metadata_key(pb, 0x011100);
  593. PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16);
  594. klv_encode_ber_length(pb, 108);
  595. // write uid
  596. mxf_write_local_tag(pb, 16, 0x3C0A);
  597. mxf_write_uuid(pb, type == MaterialPackage ? SourceClip: SourceClip + TypeBottom, st->index);
  598. PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16);
  599. mxf_write_common_fields(s, st);
  600. // write start_position
  601. mxf_write_local_tag(pb, 8, 0x1201);
  602. put_be64(pb, 0);
  603. // write source package uid, end of the reference
  604. mxf_write_local_tag(pb, 32, 0x1101);
  605. if (type == SourcePackage) {
  606. for (i = 0; i < 4; i++)
  607. put_be64(pb, 0);
  608. } else
  609. mxf_write_umid(s, 1);
  610. // write source track id
  611. mxf_write_local_tag(pb, 4, 0x1102);
  612. if (type == SourcePackage)
  613. put_be32(pb, 0);
  614. else
  615. put_be32(pb, st->index+2);
  616. }
  617. static void mxf_write_multi_descriptor(AVFormatContext *s)
  618. {
  619. MXFContext *mxf = s->priv_data;
  620. ByteIOContext *pb = s->pb;
  621. const uint8_t *ul;
  622. int i;
  623. mxf_write_metadata_key(pb, 0x014400);
  624. PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16);
  625. klv_encode_ber_length(pb, 64 + 16 * s->nb_streams);
  626. mxf_write_local_tag(pb, 16, 0x3C0A);
  627. mxf_write_uuid(pb, MultipleDescriptor, 0);
  628. PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16);
  629. // write sample rate
  630. mxf_write_local_tag(pb, 8, 0x3001);
  631. put_be32(pb, mxf->time_base.den);
  632. put_be32(pb, mxf->time_base.num);
  633. // write essence container ul
  634. mxf_write_local_tag(pb, 16, 0x3004);
  635. if (mxf->essence_container_count > 1)
  636. ul = multiple_desc_ul;
  637. else {
  638. MXFStreamContext *sc = s->streams[0]->priv_data;
  639. ul = mxf_essence_container_uls[sc->index].container_ul;
  640. }
  641. put_buffer(pb, ul, 16);
  642. // write sub descriptor refs
  643. mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x3F01);
  644. mxf_write_refs_count(pb, s->nb_streams);
  645. for (i = 0; i < s->nb_streams; i++)
  646. mxf_write_uuid(pb, SubDescriptor, i);
  647. }
  648. static void mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
  649. {
  650. MXFContext *mxf = s->priv_data;
  651. MXFStreamContext *sc = st->priv_data;
  652. ByteIOContext *pb = s->pb;
  653. put_buffer(pb, key, 16);
  654. klv_encode_ber4_length(pb, size+20+8+12+20);
  655. mxf_write_local_tag(pb, 16, 0x3C0A);
  656. mxf_write_uuid(pb, SubDescriptor, st->index);
  657. mxf_write_local_tag(pb, 4, 0x3006);
  658. put_be32(pb, st->index+2);
  659. mxf_write_local_tag(pb, 8, 0x3001);
  660. put_be32(pb, mxf->time_base.den);
  661. put_be32(pb, mxf->time_base.num);
  662. mxf_write_local_tag(pb, 16, 0x3004);
  663. put_buffer(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
  664. }
  665. static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
  666. static const UID mxf_wav_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
  667. static const UID mxf_aes3_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 };
  668. static const UID mxf_cdci_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x28,0x00 };
  669. static const UID mxf_generic_sound_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x42,0x00 };
  670. static void mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
  671. {
  672. MXFStreamContext *sc = st->priv_data;
  673. ByteIOContext *pb = s->pb;
  674. int stored_height = (st->codec->height+15)/16*16;
  675. int display_height;
  676. int f1, f2;
  677. mxf_write_generic_desc(s, st, key, size+8+8+8+8+8+8+5+16+sc->interlaced*4+12+20);
  678. mxf_write_local_tag(pb, 4, 0x3203);
  679. put_be32(pb, st->codec->width);
  680. mxf_write_local_tag(pb, 4, 0x3202);
  681. put_be32(pb, stored_height>>sc->interlaced);
  682. mxf_write_local_tag(pb, 4, 0x3209);
  683. put_be32(pb, st->codec->width);
  684. if (st->codec->height == 608) // PAL + VBI
  685. display_height = 576;
  686. else if (st->codec->height == 512) // NTSC + VBI
  687. display_height = 486;
  688. else
  689. display_height = st->codec->height;
  690. mxf_write_local_tag(pb, 4, 0x3208);
  691. put_be32(pb, display_height>>sc->interlaced);
  692. // component depth
  693. mxf_write_local_tag(pb, 4, 0x3301);
  694. put_be32(pb, 8);
  695. // horizontal subsampling
  696. mxf_write_local_tag(pb, 4, 0x3302);
  697. put_be32(pb, 2);
  698. // frame layout
  699. mxf_write_local_tag(pb, 1, 0x320C);
  700. put_byte(pb, sc->interlaced);
  701. // video line map
  702. switch (st->codec->height) {
  703. case 576: f1 = 23; f2 = 336; break;
  704. case 608: f1 = 7; f2 = 320; break;
  705. case 480: f1 = 20; f2 = 283; break;
  706. case 512: f1 = 7; f2 = 270; break;
  707. case 720: f1 = 26; f2 = 0; break; // progressive
  708. case 1080: f1 = 21; f2 = 584; break;
  709. default: f1 = 0; f2 = 0; break;
  710. }
  711. if (!sc->interlaced) {
  712. f2 = 0;
  713. f1 *= 2;
  714. }
  715. mxf_write_local_tag(pb, 12+sc->interlaced*4, 0x320D);
  716. put_be32(pb, sc->interlaced ? 2 : 1);
  717. put_be32(pb, 4);
  718. put_be32(pb, f1);
  719. if (sc->interlaced)
  720. put_be32(pb, f2);
  721. mxf_write_local_tag(pb, 8, 0x320E);
  722. put_be32(pb, sc->aspect_ratio.num);
  723. put_be32(pb, sc->aspect_ratio.den);
  724. mxf_write_local_tag(pb, 16, 0x3201);
  725. put_buffer(pb, *sc->codec_ul, 16);
  726. }
  727. static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st)
  728. {
  729. mxf_write_cdci_common(s, st, mxf_cdci_descriptor_key, 0);
  730. }
  731. static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
  732. {
  733. ByteIOContext *pb = s->pb;
  734. int profile_and_level = (st->codec->profile<<4) | st->codec->level;
  735. mxf_write_cdci_common(s, st, mxf_mpegvideo_descriptor_key, 8+5);
  736. // bit rate
  737. mxf_write_local_tag(pb, 4, 0x8000);
  738. put_be32(pb, st->codec->bit_rate);
  739. // profile and level
  740. mxf_write_local_tag(pb, 1, 0x8007);
  741. if (!st->codec->profile)
  742. profile_and_level |= 0x80; // escape bit
  743. put_byte(pb, profile_and_level);
  744. }
  745. static void mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
  746. {
  747. ByteIOContext *pb = s->pb;
  748. mxf_write_generic_desc(s, st, key, size+5+12+8+8);
  749. // audio locked
  750. mxf_write_local_tag(pb, 1, 0x3D02);
  751. put_byte(pb, 1);
  752. // write audio sampling rate
  753. mxf_write_local_tag(pb, 8, 0x3D03);
  754. put_be32(pb, st->codec->sample_rate);
  755. put_be32(pb, 1);
  756. mxf_write_local_tag(pb, 4, 0x3D07);
  757. put_be32(pb, st->codec->channels);
  758. mxf_write_local_tag(pb, 4, 0x3D01);
  759. put_be32(pb, av_get_bits_per_sample(st->codec->codec_id));
  760. }
  761. static void mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key, unsigned size)
  762. {
  763. ByteIOContext *pb = s->pb;
  764. mxf_write_generic_sound_common(s, st, key, size+6+8);
  765. mxf_write_local_tag(pb, 2, 0x3D0A);
  766. put_be16(pb, st->codec->block_align);
  767. // avg bytes per sec
  768. mxf_write_local_tag(pb, 4, 0x3D09);
  769. put_be32(pb, st->codec->block_align*st->codec->sample_rate);
  770. }
  771. static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st)
  772. {
  773. mxf_write_wav_common(s, st, mxf_wav_descriptor_key, 0);
  774. }
  775. static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st)
  776. {
  777. mxf_write_wav_common(s, st, mxf_aes3_descriptor_key, 0);
  778. }
  779. static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st)
  780. {
  781. mxf_write_generic_sound_common(s, st, mxf_generic_sound_descriptor_key, 0);
  782. }
  783. static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type)
  784. {
  785. MXFContext *mxf = s->priv_data;
  786. ByteIOContext *pb = s->pb;
  787. int i, track_count = s->nb_streams+1;
  788. if (type == MaterialPackage) {
  789. mxf_write_metadata_key(pb, 0x013600);
  790. PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);
  791. klv_encode_ber_length(pb, 92 + 16*track_count);
  792. } else {
  793. mxf_write_metadata_key(pb, 0x013700);
  794. PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);
  795. klv_encode_ber_length(pb, 112 + 16*track_count); // 20 bytes length for descriptor reference
  796. }
  797. // write uid
  798. mxf_write_local_tag(pb, 16, 0x3C0A);
  799. mxf_write_uuid(pb, type, 0);
  800. av_log(s,AV_LOG_DEBUG, "package type:%d\n", type);
  801. PRINT_KEY(s, "package uid", pb->buf_ptr - 16);
  802. // write package umid
  803. mxf_write_local_tag(pb, 32, 0x4401);
  804. mxf_write_umid(s, type == SourcePackage);
  805. PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);
  806. // package creation date
  807. mxf_write_local_tag(pb, 8, 0x4405);
  808. put_be64(pb, mxf->timestamp);
  809. // package modified date
  810. mxf_write_local_tag(pb, 8, 0x4404);
  811. put_be64(pb, mxf->timestamp);
  812. // write track refs
  813. mxf_write_local_tag(pb, track_count*16 + 8, 0x4403);
  814. mxf_write_refs_count(pb, track_count);
  815. mxf_write_uuid(pb, type == MaterialPackage ? Track :
  816. Track + TypeBottom, -1); // timecode track
  817. for (i = 0; i < s->nb_streams; i++)
  818. mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, i);
  819. // write multiple descriptor reference
  820. if (type == SourcePackage) {
  821. mxf_write_local_tag(pb, 16, 0x4701);
  822. if (s->nb_streams > 1) {
  823. mxf_write_uuid(pb, MultipleDescriptor, 0);
  824. mxf_write_multi_descriptor(s);
  825. } else
  826. mxf_write_uuid(pb, SubDescriptor, 0);
  827. }
  828. // write timecode track
  829. mxf_write_track(s, mxf->timecode_track, type);
  830. mxf_write_sequence(s, mxf->timecode_track, type);
  831. mxf_write_timecode_component(s, mxf->timecode_track, type);
  832. for (i = 0; i < s->nb_streams; i++) {
  833. AVStream *st = s->streams[i];
  834. mxf_write_track(s, st, type);
  835. mxf_write_sequence(s, st, type);
  836. mxf_write_structural_component(s, st, type);
  837. if (type == SourcePackage) {
  838. MXFStreamContext *sc = st->priv_data;
  839. mxf_essence_container_uls[sc->index].write_desc(s, st);
  840. }
  841. }
  842. }
  843. static int mxf_write_essence_container_data(AVFormatContext *s)
  844. {
  845. ByteIOContext *pb = s->pb;
  846. mxf_write_metadata_key(pb, 0x012300);
  847. klv_encode_ber_length(pb, 72);
  848. mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID
  849. mxf_write_uuid(pb, EssenceContainerData, 0);
  850. mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID
  851. mxf_write_umid(s, 1);
  852. mxf_write_local_tag(pb, 4, 0x3F07); // BodySID
  853. put_be32(pb, 1);
  854. mxf_write_local_tag(pb, 4, 0x3F06); // IndexSID
  855. put_be32(pb, 2);
  856. return 0;
  857. }
  858. static int mxf_write_header_metadata_sets(AVFormatContext *s)
  859. {
  860. mxf_write_preface(s);
  861. mxf_write_identification(s);
  862. mxf_write_content_storage(s);
  863. mxf_write_package(s, MaterialPackage);
  864. mxf_write_package(s, SourcePackage);
  865. mxf_write_essence_container_data(s);
  866. return 0;
  867. }
  868. static unsigned klv_fill_size(uint64_t size)
  869. {
  870. unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));
  871. if (pad < 20) // smallest fill item possible
  872. return pad + KAG_SIZE;
  873. else
  874. return pad & (KAG_SIZE-1);
  875. }
  876. static void mxf_write_index_table_segment(AVFormatContext *s)
  877. {
  878. MXFContext *mxf = s->priv_data;
  879. ByteIOContext *pb = s->pb;
  880. int i, j;
  881. int temporal_reordering = 0;
  882. int key_index = mxf->last_key_index;
  883. av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
  884. if (!mxf->edit_units_count && !mxf->edit_unit_byte_count)
  885. return;
  886. put_buffer(pb, index_table_segment_key, 16);
  887. if (mxf->edit_unit_byte_count) {
  888. klv_encode_ber_length(pb, 80);
  889. } else {
  890. klv_encode_ber_length(pb, 85 + 12+(s->nb_streams+1)*6 +
  891. 12+mxf->edit_units_count*(11+mxf->slice_count*4));
  892. }
  893. // instance id
  894. mxf_write_local_tag(pb, 16, 0x3C0A);
  895. mxf_write_uuid(pb, IndexTableSegment, 0);
  896. // index edit rate
  897. mxf_write_local_tag(pb, 8, 0x3F0B);
  898. put_be32(pb, mxf->time_base.den);
  899. put_be32(pb, mxf->time_base.num);
  900. // index start position
  901. mxf_write_local_tag(pb, 8, 0x3F0C);
  902. put_be64(pb, mxf->last_indexed_edit_unit);
  903. // index duration
  904. mxf_write_local_tag(pb, 8, 0x3F0D);
  905. if (mxf->edit_unit_byte_count)
  906. put_be64(pb, 0); // index table covers whole container
  907. else
  908. put_be64(pb, mxf->edit_units_count);
  909. // edit unit byte count
  910. mxf_write_local_tag(pb, 4, 0x3F05);
  911. put_be32(pb, mxf->edit_unit_byte_count);
  912. // index sid
  913. mxf_write_local_tag(pb, 4, 0x3F06);
  914. put_be32(pb, 2);
  915. // body sid
  916. mxf_write_local_tag(pb, 4, 0x3F07);
  917. put_be32(pb, 1);
  918. if (!mxf->edit_unit_byte_count) {
  919. // real slice count - 1
  920. mxf_write_local_tag(pb, 1, 0x3F08);
  921. put_byte(pb, mxf->slice_count);
  922. // delta entry array
  923. mxf_write_local_tag(pb, 8 + (s->nb_streams+1)*6, 0x3F09);
  924. put_be32(pb, s->nb_streams+1); // num of entries
  925. put_be32(pb, 6); // size of one entry
  926. // write system item delta entry
  927. put_byte(pb, 0);
  928. put_byte(pb, 0); // slice entry
  929. put_be32(pb, 0); // element delta
  930. for (i = 0; i < s->nb_streams; i++) {
  931. AVStream *st = s->streams[i];
  932. MXFStreamContext *sc = st->priv_data;
  933. put_byte(pb, sc->temporal_reordering);
  934. if (sc->temporal_reordering)
  935. temporal_reordering = 1;
  936. if (i == 0) { // video track
  937. put_byte(pb, 0); // slice number
  938. put_be32(pb, KAG_SIZE); // system item size including klv fill
  939. } else { // audio track
  940. unsigned audio_frame_size = sc->aic.samples[0]*sc->aic.sample_size;
  941. audio_frame_size += klv_fill_size(audio_frame_size);
  942. put_byte(pb, 1);
  943. put_be32(pb, (i-1)*audio_frame_size); // element delta
  944. }
  945. }
  946. mxf_write_local_tag(pb, 8 + mxf->edit_units_count*(11+mxf->slice_count*4), 0x3F0A);
  947. put_be32(pb, mxf->edit_units_count); // num of entries
  948. put_be32(pb, 11+mxf->slice_count*4); // size of one entry
  949. for (i = 0; i < mxf->edit_units_count; i++) {
  950. int temporal_offset = 0;
  951. if (temporal_reordering) {
  952. for (j = i+1; j < mxf->edit_units_count; j++) {
  953. temporal_offset++;
  954. if (mxf->index_entries[j].flags & 0x10) { // backward prediction
  955. // next is not b, so is reordered
  956. if (!(mxf->index_entries[i+1].flags & 0x10)) {
  957. if ((mxf->index_entries[i].flags & 0x11) == 0) // I frame
  958. temporal_offset = 0;
  959. else
  960. temporal_offset = -temporal_offset;
  961. }
  962. break;
  963. }
  964. }
  965. }
  966. put_byte(pb, temporal_offset);
  967. if (!(mxf->index_entries[i].flags & 0x33)) { // I frame
  968. if (mxf->index_entries[i].flags & 0x40 && // seq header
  969. (!temporal_reordering || !temporal_offset))
  970. mxf->index_entries[i].flags |= 0x80; // random access
  971. mxf->last_key_index = key_index;
  972. key_index = i;
  973. }
  974. if ((mxf->index_entries[i].flags & 0x30) == 0x30) { // back and forward prediction
  975. put_byte(pb, mxf->last_key_index - i);
  976. } else {
  977. put_byte(pb, key_index - i); // key frame offset
  978. if ((mxf->index_entries[i].flags & 0x20) == 0x20) // only forward
  979. mxf->last_key_index = key_index;
  980. }
  981. put_byte(pb, mxf->index_entries[i].flags);
  982. // stream offset
  983. put_be64(pb, mxf->index_entries[i].offset);
  984. if (s->nb_streams > 1)
  985. put_be32(pb, mxf->index_entries[i].slice_offset);
  986. }
  987. mxf->last_key_index = key_index - mxf->edit_units_count;
  988. mxf->last_indexed_edit_unit += mxf->edit_units_count;
  989. mxf->edit_units_count = 0;
  990. }
  991. }
  992. static void mxf_write_klv_fill(AVFormatContext *s)
  993. {
  994. unsigned pad = klv_fill_size(url_ftell(s->pb));
  995. if (pad) {
  996. put_buffer(s->pb, klv_fill_key, 16);
  997. pad -= 16 + 4;
  998. klv_encode_ber4_length(s->pb, pad);
  999. for (; pad; pad--)
  1000. put_byte(s->pb, 0);
  1001. assert(!(url_ftell(s->pb) & (KAG_SIZE-1)));
  1002. }
  1003. }
  1004. static void mxf_write_partition(AVFormatContext *s, int bodysid,
  1005. int indexsid,
  1006. const uint8_t *key, int write_metadata)
  1007. {
  1008. MXFContext *mxf = s->priv_data;
  1009. ByteIOContext *pb = s->pb;
  1010. int64_t header_byte_count_offset;
  1011. unsigned index_byte_count = 0;
  1012. uint64_t partition_offset = url_ftell(pb);
  1013. if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
  1014. index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
  1015. 12+mxf->edit_units_count*(11+mxf->slice_count*4);
  1016. else if (mxf->edit_unit_byte_count && indexsid)
  1017. index_byte_count = 80;
  1018. if (index_byte_count) {
  1019. // add encoded ber length
  1020. index_byte_count += 16 + klv_ber_length(index_byte_count);
  1021. index_byte_count += klv_fill_size(index_byte_count);
  1022. }
  1023. if (!memcmp(key, body_partition_key, 16)) {
  1024. mxf->body_partition_offset =
  1025. av_realloc(mxf->body_partition_offset,
  1026. (mxf->body_partitions_count+1)*
  1027. sizeof(*mxf->body_partition_offset));
  1028. mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;
  1029. }
  1030. // write klv
  1031. put_buffer(pb, key, 16);
  1032. klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count);
  1033. // write partition value
  1034. put_be16(pb, 1); // majorVersion
  1035. put_be16(pb, 2); // minorVersion
  1036. put_be32(pb, KAG_SIZE); // KAGSize
  1037. put_be64(pb, partition_offset); // ThisPartition
  1038. if (!memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)
  1039. put_be64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); // PreviousPartition
  1040. else if (!memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)
  1041. put_be64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); // PreviousPartition
  1042. else
  1043. put_be64(pb, 0);
  1044. put_be64(pb, mxf->footer_partition_offset); // footerPartition
  1045. // set offset
  1046. header_byte_count_offset = url_ftell(pb);
  1047. put_be64(pb, 0); // headerByteCount, update later
  1048. // indexTable
  1049. put_be64(pb, index_byte_count); // indexByteCount
  1050. put_be32(pb, index_byte_count ? indexsid : 0); // indexSID
  1051. // BodyOffset
  1052. if (bodysid && mxf->edit_units_count && mxf->body_partitions_count) {
  1053. put_be64(pb, mxf->body_offset);
  1054. } else
  1055. put_be64(pb, 0);
  1056. put_be32(pb, bodysid); // bodySID
  1057. // operational pattern
  1058. put_buffer(pb, op1a_ul, 16);
  1059. // essence container
  1060. mxf_write_essence_container_refs(s);
  1061. if (write_metadata) {
  1062. // mark the start of the headermetadata and calculate metadata size
  1063. int64_t pos, start;
  1064. unsigned header_byte_count;
  1065. mxf_write_klv_fill(s);
  1066. start = url_ftell(s->pb);
  1067. mxf_write_primer_pack(s);
  1068. mxf_write_header_metadata_sets(s);
  1069. pos = url_ftell(s->pb);
  1070. header_byte_count = pos - start + klv_fill_size(pos);
  1071. // update header_byte_count
  1072. url_fseek(pb, header_byte_count_offset, SEEK_SET);
  1073. put_be64(pb, header_byte_count);
  1074. url_fseek(pb, pos, SEEK_SET);
  1075. }
  1076. put_flush_packet(pb);
  1077. }
  1078. static const UID mxf_mpeg2_codec_uls[] = {
  1079. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
  1080. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
  1081. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
  1082. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
  1083. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
  1084. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
  1085. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
  1086. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
  1087. };
  1088. static const UID *mxf_get_mpeg2_codec_ul(AVCodecContext *avctx)
  1089. {
  1090. int long_gop = avctx->gop_size > 1 || avctx->has_b_frames;
  1091. if (avctx->profile == 4) { // Main
  1092. if (avctx->level == 8) // Main
  1093. return &mxf_mpeg2_codec_uls[0+long_gop];
  1094. else if (avctx->level == 4) // High
  1095. return &mxf_mpeg2_codec_uls[4+long_gop];
  1096. } else if (avctx->profile == 0) { // 422
  1097. if (avctx->level == 5) // Main
  1098. return &mxf_mpeg2_codec_uls[2+long_gop];
  1099. else if (avctx->level == 2) // High
  1100. return &mxf_mpeg2_codec_uls[6+long_gop];
  1101. }
  1102. return NULL;
  1103. }
  1104. static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt, int *flags)
  1105. {
  1106. MXFStreamContext *sc = st->priv_data;
  1107. MXFContext *mxf = s->priv_data;
  1108. uint32_t c = -1;
  1109. int i;
  1110. *flags = 0;
  1111. for(i = 0; i < pkt->size - 4; i++) {
  1112. c = (c<<8) + pkt->data[i];
  1113. if (c == 0x1b5) {
  1114. if ((pkt->data[i+1] & 0xf0) == 0x10) { // seq ext
  1115. st->codec->profile = pkt->data[i+1] & 0x07;
  1116. st->codec->level = pkt->data[i+2] >> 4;
  1117. } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext
  1118. sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame
  1119. break;
  1120. }
  1121. } else if (c == 0x1b8) { // gop
  1122. if (pkt->data[i+4]>>6 & 0x01) { // closed
  1123. sc->closed_gop = 1;
  1124. if (*flags & 0x40) // sequence header present
  1125. *flags |= 0x80; // random access
  1126. }
  1127. if (!mxf->header_written) {
  1128. unsigned hours = (pkt->data[i+1]>>2) & 0x1f;
  1129. unsigned minutes = ((pkt->data[i+1] & 0x03) << 4) | (pkt->data[i+2]>>4);
  1130. unsigned seconds = ((pkt->data[i+2] & 0x07) << 3) | (pkt->data[i+3]>>5);
  1131. unsigned frames = ((pkt->data[i+3] & 0x1f) << 1) | (pkt->data[i+4]>>7);
  1132. mxf->timecode_drop_frame = !!(pkt->data[i+1] & 0x80);
  1133. mxf->timecode_start = (hours*3600 + minutes*60 + seconds) *
  1134. mxf->timecode_base + frames;
  1135. if (mxf->timecode_drop_frame) {
  1136. unsigned tminutes = 60 * hours + minutes;
  1137. mxf->timecode_start -= 2 * (tminutes - tminutes / 10);
  1138. }
  1139. av_log(s, AV_LOG_DEBUG, "frame %d %d:%d:%d%c%d\n", mxf->timecode_start,
  1140. hours, minutes, seconds, mxf->timecode_drop_frame ? ';':':', frames);
  1141. }
  1142. } else if (c == 0x1b3) { // seq
  1143. *flags |= 0x40;
  1144. switch ((pkt->data[i+4]>>4) & 0xf) {
  1145. case 2: sc->aspect_ratio = (AVRational){ 4, 3}; break;
  1146. case 3: sc->aspect_ratio = (AVRational){ 16, 9}; break;
  1147. case 4: sc->aspect_ratio = (AVRational){221,100}; break;
  1148. default:
  1149. av_reduce(&sc->aspect_ratio.num, &sc->aspect_ratio.den,
  1150. st->codec->width, st->codec->height, 1024*1024);
  1151. }
  1152. } else if (c == 0x100) { // pic
  1153. int pict_type = (pkt->data[i+2]>>3) & 0x07;
  1154. if (pict_type == 2) { // P frame
  1155. *flags |= 0x22;
  1156. sc->closed_gop = 0; // reset closed gop, don't matter anymore
  1157. } else if (pict_type == 3) { // B frame
  1158. if (sc->closed_gop)
  1159. *flags |= 0x13; // only backward prediction
  1160. else
  1161. *flags |= 0x33;
  1162. sc->temporal_reordering = -1;
  1163. } else if (!pict_type) {
  1164. av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n");
  1165. return 0;
  1166. }
  1167. }
  1168. }
  1169. if (s->oformat != &mxf_d10_muxer)
  1170. sc->codec_ul = mxf_get_mpeg2_codec_ul(st->codec);
  1171. return !!sc->codec_ul;
  1172. }
  1173. static uint64_t mxf_parse_timestamp(time_t timestamp)
  1174. {
  1175. struct tm *time = gmtime(&timestamp);
  1176. return (uint64_t)(time->tm_year+1900) << 48 |
  1177. (uint64_t)(time->tm_mon+1) << 40 |
  1178. (uint64_t) time->tm_mday << 32 |
  1179. time->tm_hour << 24 |
  1180. time->tm_min << 16 |
  1181. time->tm_sec << 8;
  1182. }
  1183. static void mxf_gen_umid(AVFormatContext *s)
  1184. {
  1185. MXFContext *mxf = s->priv_data;
  1186. uint32_t seed = ff_random_get_seed();
  1187. uint64_t umid = seed + 0x5294713400000000LL;
  1188. AV_WB64(mxf->umid , umid);
  1189. AV_WB64(mxf->umid+8, umid>>8);
  1190. mxf->instance_number = seed;
  1191. }
  1192. static int mxf_write_header(AVFormatContext *s)
  1193. {
  1194. MXFContext *mxf = s->priv_data;
  1195. int i;
  1196. uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0};
  1197. const int *samples_per_frame = NULL;
  1198. if (!s->nb_streams)
  1199. return -1;
  1200. for (i = 0; i < s->nb_streams; i++) {
  1201. AVStream *st = s->streams[i];
  1202. MXFStreamContext *sc = av_mallocz(sizeof(*sc));
  1203. if (!sc)
  1204. return AVERROR(ENOMEM);
  1205. st->priv_data = sc;
  1206. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  1207. if (i != 0) {
  1208. av_log(s, AV_LOG_ERROR, "video stream must be first track\n");
  1209. return -1;
  1210. }
  1211. if (fabs(av_q2d(st->codec->time_base) - 1/25.0) < 0.0001) {
  1212. samples_per_frame = PAL_samples_per_frame;
  1213. mxf->time_base = (AVRational){ 1, 25 };
  1214. mxf->timecode_base = 25;
  1215. } else if (fabs(av_q2d(st->codec->time_base) - 1001/30000.0) < 0.0001) {
  1216. samples_per_frame = NTSC_samples_per_frame;
  1217. mxf->time_base = (AVRational){ 1001, 30000 };
  1218. mxf->timecode_base = 30;
  1219. } else {
  1220. av_log(s, AV_LOG_ERROR, "unsupported video frame rate\n");
  1221. return -1;
  1222. }
  1223. av_set_pts_info(st, 64, mxf->time_base.num, mxf->time_base.den);
  1224. if (s->oformat == &mxf_d10_muxer) {
  1225. if (st->codec->bit_rate == 50000000)
  1226. if (mxf->time_base.den == 25) sc->index = 3;
  1227. else sc->index = 5;
  1228. else if (st->codec->bit_rate == 40000000)
  1229. if (mxf->time_base.den == 25) sc->index = 7;
  1230. else sc->index = 9;
  1231. else if (st->codec->bit_rate == 30000000)
  1232. if (mxf->time_base.den == 25) sc->index = 11;
  1233. else sc->index = 13;
  1234. else {
  1235. av_log(s, AV_LOG_ERROR, "error MXF D-10 only support 30/40/50 mbit/s\n");
  1236. return -1;
  1237. }
  1238. mxf->edit_unit_byte_count = KAG_SIZE; // system element
  1239. mxf->edit_unit_byte_count += 16 + 4 + (uint64_t)st->codec->bit_rate *
  1240. mxf->time_base.num / (8*mxf->time_base.den);
  1241. mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
  1242. mxf->edit_unit_byte_count += 16 + 4 + 4 + samples_per_frame[0]*8*4;
  1243. mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
  1244. }
  1245. } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
  1246. if (st->codec->sample_rate != 48000) {
  1247. av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n");
  1248. return -1;
  1249. }
  1250. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  1251. if (s->oformat == &mxf_d10_muxer) {
  1252. if (st->index != 1) {
  1253. av_log(s, AV_LOG_ERROR, "MXF D-10 only support one audio track\n");
  1254. return -1;
  1255. }
  1256. if (st->codec->codec_id != CODEC_ID_PCM_S16LE &&
  1257. st->codec->codec_id != CODEC_ID_PCM_S24LE) {
  1258. av_log(s, AV_LOG_ERROR, "MXF D-10 only support 16 or 24 bits le audio\n");
  1259. }
  1260. sc->index = ((MXFStreamContext*)s->streams[0]->priv_data)->index + 1;
  1261. } else
  1262. mxf->slice_count = 1;
  1263. }
  1264. if (!sc->index) {
  1265. sc->index = mxf_get_essence_container_ul_index(st->codec->codec_id);
  1266. if (sc->index == -1) {
  1267. av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, "
  1268. "codec not currently supported in container\n", i);
  1269. return -1;
  1270. }
  1271. }
  1272. sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
  1273. memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15);
  1274. sc->track_essence_element_key[15] = present[sc->index];
  1275. PRINT_KEY(s, "track essence element key", sc->track_essence_element_key);
  1276. if (!present[sc->index])
  1277. mxf->essence_container_count++;
  1278. present[sc->index]++;
  1279. }
  1280. if (s->oformat == &mxf_d10_muxer) {
  1281. mxf->essence_container_count = 1;
  1282. }
  1283. if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
  1284. mxf_gen_umid(s);
  1285. for (i = 0; i < s->nb_streams; i++) {
  1286. MXFStreamContext *sc = s->streams[i]->priv_data;
  1287. // update element count
  1288. sc->track_essence_element_key[13] = present[sc->index];
  1289. sc->order = AV_RB32(sc->track_essence_element_key+12);
  1290. }
  1291. if (s->timestamp)
  1292. mxf->timestamp = mxf_parse_timestamp(s->timestamp);
  1293. mxf->duration = -1;
  1294. mxf->timecode_track = av_mallocz(sizeof(*mxf->timecode_track));
  1295. if (!mxf->timecode_track)
  1296. return AVERROR(ENOMEM);
  1297. mxf->timecode_track->priv_data = av_mallocz(sizeof(MXFStreamContext));
  1298. if (!mxf->timecode_track->priv_data)
  1299. return AVERROR(ENOMEM);
  1300. mxf->timecode_track->index = -1;
  1301. if (!samples_per_frame)
  1302. samples_per_frame = PAL_samples_per_frame;
  1303. if (ff_audio_interleave_init(s, samples_per_frame, mxf->time_base) < 0)
  1304. return -1;
  1305. return 0;
  1306. }
  1307. static const uint8_t system_metadata_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 };
  1308. static const uint8_t system_metadata_package_set_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x43,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x02,0x01 };
  1309. static uint32_t ff_framenum_to_12m_time_code(unsigned frame, int drop, int fps)
  1310. {
  1311. return (0 << 31) | // color frame flag
  1312. (0 << 30) | // drop frame flag
  1313. ( ((frame % fps) / 10) << 28) | // tens of frames
  1314. ( ((frame % fps) % 10) << 24) | // units of frames
  1315. (0 << 23) | // field phase (NTSC), b0 (PAL)
  1316. ((((frame / fps) % 60) / 10) << 20) | // tens of seconds
  1317. ((((frame / fps) % 60) % 10) << 16) | // units of seconds
  1318. (0 << 15) | // b0 (NTSC), b2 (PAL)
  1319. ((((frame / (fps * 60)) % 60) / 10) << 12) | // tens of minutes
  1320. ((((frame / (fps * 60)) % 60) % 10) << 8) | // units of minutes
  1321. (0 << 7) | // b1
  1322. (0 << 6) | // b2 (NSC), field phase (PAL)
  1323. ((((frame / (fps * 3600) % 24)) / 10) << 4) | // tens of hours
  1324. ( (frame / (fps * 3600) % 24)) % 10; // units of hours
  1325. }
  1326. static void mxf_write_system_item(AVFormatContext *s)
  1327. {
  1328. MXFContext *mxf = s->priv_data;
  1329. ByteIOContext *pb = s->pb;
  1330. unsigned frame;
  1331. uint32_t time_code;
  1332. frame = mxf->timecode_start + mxf->last_indexed_edit_unit + mxf->edit_units_count;
  1333. // write system metadata pack
  1334. put_buffer(pb, system_metadata_pack_key, 16);
  1335. klv_encode_ber4_length(pb, 57);
  1336. put_byte(pb, 0x5c); // UL, user date/time stamp, picture and sound item present
  1337. put_byte(pb, 0x04); // content package rate
  1338. put_byte(pb, 0x00); // content package type
  1339. put_be16(pb, 0x00); // channel handle
  1340. put_be16(pb, frame); // continuity count
  1341. if (mxf->essence_container_count > 1)
  1342. put_buffer(pb, multiple_desc_ul, 16);
  1343. else {
  1344. MXFStreamContext *sc = s->streams[0]->priv_data;
  1345. put_buffer(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
  1346. }
  1347. put_byte(pb, 0);
  1348. put_be64(pb, 0);
  1349. put_be64(pb, 0); // creation date/time stamp
  1350. put_byte(pb, 0x81); // SMPTE 12M time code
  1351. time_code = ff_framenum_to_12m_time_code(frame, mxf->timecode_drop_frame, mxf->timecode_base);
  1352. put_be32(pb, time_code);
  1353. put_be32(pb, 0); // binary group data
  1354. put_be64(pb, 0);
  1355. // write system metadata package set
  1356. put_buffer(pb, system_metadata_package_set_key, 16);
  1357. klv_encode_ber4_length(pb, 35);
  1358. put_byte(pb, 0x83); // UMID
  1359. put_be16(pb, 0x20);
  1360. mxf_write_umid(s, 1);
  1361. }
  1362. static void mxf_write_d10_video_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
  1363. {
  1364. MXFContext *mxf = s->priv_data;
  1365. ByteIOContext *pb = s->pb;
  1366. int packet_size = (uint64_t)st->codec->bit_rate*mxf->time_base.num /
  1367. (8*mxf->time_base.den); // frame size
  1368. int pad;
  1369. packet_size += 16 + 4;
  1370. packet_size += klv_fill_size(packet_size);
  1371. klv_encode_ber4_length(pb, pkt->size);
  1372. put_buffer(pb, pkt->data, pkt->size);
  1373. // ensure CBR muxing by padding to correct video frame size
  1374. pad = packet_size - pkt->size - 16 - 4;
  1375. if (pad > 20) {
  1376. put_buffer(s->pb, klv_fill_key, 16);
  1377. pad -= 16 + 4;
  1378. klv_encode_ber4_length(s->pb, pad);
  1379. for (; pad; pad--)
  1380. put_byte(s->pb, 0);
  1381. assert(!(url_ftell(s->pb) & (KAG_SIZE-1)));
  1382. } else {
  1383. av_log(s, AV_LOG_WARNING, "cannot fill d-10 video packet\n");
  1384. for (; pad > 0; pad--)
  1385. put_byte(s->pb, 0);
  1386. }
  1387. }
  1388. static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
  1389. {
  1390. MXFContext *mxf = s->priv_data;
  1391. ByteIOContext *pb = s->pb;
  1392. int frame_size = pkt->size / st->codec->block_align;
  1393. uint8_t *samples = pkt->data;
  1394. uint8_t *end = pkt->data + pkt->size;
  1395. int i;
  1396. klv_encode_ber4_length(pb, 4 + frame_size*4*8);
  1397. put_byte(pb, (frame_size == 1920 ? 0 : (mxf->edit_units_count-1) % 5 + 1));
  1398. put_le16(pb, frame_size);
  1399. put_byte(pb, (1<<st->codec->channels)-1);
  1400. while (samples < end) {
  1401. for (i = 0; i < st->codec->channels; i++) {
  1402. uint32_t sample;
  1403. if (st->codec->codec_id == CODEC_ID_PCM_S24LE) {
  1404. sample = AV_RL24(samples)<< 4;
  1405. samples += 3;
  1406. } else {
  1407. sample = AV_RL16(samples)<<12;
  1408. samples += 2;
  1409. }
  1410. put_le32(pb, sample | i);
  1411. }
  1412. for (; i < 8; i++)
  1413. put_le32(pb, i);
  1414. }
  1415. }
  1416. static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
  1417. {
  1418. MXFContext *mxf = s->priv_data;
  1419. ByteIOContext *pb = s->pb;
  1420. AVStream *st = s->streams[pkt->stream_index];
  1421. MXFStreamContext *sc = st->priv_data;
  1422. int flags = 0;
  1423. if (!mxf->edit_unit_byte_count && !(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) {
  1424. mxf->index_entries = av_realloc(mxf->index_entries,
  1425. (mxf->edit_units_count + EDIT_UNITS_PER_BODY)*sizeof(*mxf->index_entries));
  1426. if (!mxf->index_entries) {
  1427. av_log(s, AV_LOG_ERROR, "could not allocate index entries\n");
  1428. return -1;
  1429. }
  1430. }
  1431. if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
  1432. if (!mxf_parse_mpeg2_frame(s, st, pkt, &flags)) {
  1433. av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
  1434. return -1;
  1435. }
  1436. }
  1437. if (!mxf->header_written) {
  1438. if (mxf->edit_unit_byte_count) {
  1439. mxf_write_partition(s, 1, 2, header_open_partition_key, 1);
  1440. mxf_write_klv_fill(s);
  1441. mxf_write_index_table_segment(s);
  1442. } else {
  1443. mxf_write_partition(s, 0, 0, header_open_partition_key, 1);
  1444. }
  1445. mxf->header_written = 1;
  1446. }
  1447. if (st->index == 0) {
  1448. if (!mxf->edit_unit_byte_count &&
  1449. (!mxf->edit_units_count || mxf->edit_units_count > EDIT_UNITS_PER_BODY) &&
  1450. !(flags & 0x33)) { // I frame, Gop start
  1451. mxf_write_klv_fill(s);
  1452. mxf_write_partition(s, 1, 2, body_partition_key, 0);
  1453. mxf_write_klv_fill(s);
  1454. mxf_write_index_table_segment(s);
  1455. }
  1456. mxf_write_klv_fill(s);
  1457. mxf_write_system_item(s);
  1458. if (!mxf->edit_unit_byte_count) {
  1459. mxf->index_entries[mxf->edit_units_count].offset = mxf->body_offset;
  1460. mxf->index_entries[mxf->edit_units_count].flags = flags;
  1461. mxf->body_offset += KAG_SIZE; // size of system element
  1462. }
  1463. mxf->edit_units_count++;
  1464. } else if (!mxf->edit_unit_byte_count && st->index == 1) {
  1465. mxf->index_entries[mxf->edit_units_count-1].slice_offset =
  1466. mxf->body_offset - mxf->index_entries[mxf->edit_units_count-1].offset;
  1467. }
  1468. mxf_write_klv_fill(s);
  1469. put_buffer(pb, sc->track_essence_element_key, 16); // write key
  1470. if (s->oformat == &mxf_d10_muxer) {
  1471. if (st->codec->codec_type == CODEC_TYPE_VIDEO)
  1472. mxf_write_d10_video_packet(s, st, pkt);
  1473. else
  1474. mxf_write_d10_audio_packet(s, st, pkt);
  1475. } else {
  1476. klv_encode_ber4_length(pb, pkt->size); // write length
  1477. put_buffer(pb, pkt->data, pkt->size);
  1478. mxf->body_offset += 16+4+pkt->size + klv_fill_size(16+4+pkt->size);
  1479. }
  1480. put_flush_packet(pb);
  1481. return 0;
  1482. }
  1483. static void mxf_write_random_index_pack(AVFormatContext *s)
  1484. {
  1485. MXFContext *mxf = s->priv_data;
  1486. ByteIOContext *pb = s->pb;
  1487. uint64_t pos = url_ftell(pb);
  1488. int i;
  1489. put_buffer(pb, random_index_pack_key, 16);
  1490. klv_encode_ber_length(pb, 28 + 12*mxf->body_partitions_count);
  1491. if (mxf->edit_unit_byte_count)
  1492. put_be32(pb, 1); // BodySID of header partition
  1493. else
  1494. put_be32(pb, 0);
  1495. put_be64(pb, 0); // offset of header partition
  1496. for (i = 0; i < mxf->body_partitions_count; i++) {
  1497. put_be32(pb, 1); // BodySID
  1498. put_be64(pb, mxf->body_partition_offset[i]);
  1499. }
  1500. put_be32(pb, 0); // BodySID of footer partition
  1501. put_be64(pb, mxf->footer_partition_offset);
  1502. put_be32(pb, url_ftell(pb) - pos + 4);
  1503. }
  1504. static int mxf_write_footer(AVFormatContext *s)
  1505. {
  1506. MXFContext *mxf = s->priv_data;
  1507. ByteIOContext *pb = s->pb;
  1508. mxf->duration = mxf->last_indexed_edit_unit + mxf->edit_units_count;
  1509. mxf_write_klv_fill(s);
  1510. mxf->footer_partition_offset = url_ftell(pb);
  1511. if (mxf->edit_unit_byte_count) { // no need to repeat index
  1512. mxf_write_partition(s, 0, 0, footer_partition_key, 0);
  1513. } else {
  1514. mxf_write_partition(s, 0, 2, footer_partition_key, 0);
  1515. mxf_write_klv_fill(s);
  1516. mxf_write_index_table_segment(s);
  1517. }
  1518. mxf_write_klv_fill(s);
  1519. mxf_write_random_index_pack(s);
  1520. if (!url_is_streamed(s->pb)) {
  1521. url_fseek(pb, 0, SEEK_SET);
  1522. if (mxf->edit_unit_byte_count) {
  1523. mxf_write_partition(s, 1, 2, header_closed_partition_key, 1);
  1524. mxf_write_klv_fill(s);
  1525. mxf_write_index_table_segment(s);
  1526. } else {
  1527. mxf_write_partition(s, 0, 0, header_closed_partition_key, 1);
  1528. }
  1529. }
  1530. put_flush_packet(pb);
  1531. ff_audio_interleave_close(s);
  1532. av_freep(&mxf->index_entries);
  1533. av_freep(&mxf->body_partition_offset);
  1534. av_freep(&mxf->timecode_track->priv_data);
  1535. av_freep(&mxf->timecode_track);
  1536. mxf_free(s);
  1537. return 0;
  1538. }
  1539. static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
  1540. {
  1541. AVPacketList *pktl;
  1542. int stream_count = 0;
  1543. int streams[MAX_STREAMS];
  1544. memset(streams, 0, sizeof(streams));
  1545. pktl = s->packet_buffer;
  1546. while (pktl) {
  1547. //av_log(s, AV_LOG_DEBUG, "show st:%d dts:%lld\n", pktl->pkt.stream_index, pktl->pkt.dts);
  1548. if (!streams[pktl->pkt.stream_index])
  1549. stream_count++;
  1550. streams[pktl->pkt.stream_index]++;
  1551. pktl = pktl->next;
  1552. }
  1553. if (stream_count && (s->nb_streams == stream_count || flush)) {
  1554. pktl = s->packet_buffer;
  1555. if (s->nb_streams != stream_count) {
  1556. AVPacketList *last = NULL;
  1557. // find last packet in edit unit
  1558. while (pktl) {
  1559. if (!stream_count || pktl->pkt.stream_index == 0)
  1560. break;
  1561. last = pktl;
  1562. pktl = pktl->next;
  1563. stream_count--;
  1564. }
  1565. // purge packet queue
  1566. while (pktl) {
  1567. AVPacketList *next = pktl->next;
  1568. if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl)
  1569. s->streams[pktl->pkt.stream_index]->last_in_packet_buffer= NULL;
  1570. av_free_packet(&pktl->pkt);
  1571. av_freep(&pktl);
  1572. pktl = next;
  1573. }
  1574. if (last)
  1575. last->next = NULL;
  1576. else {
  1577. s->packet_buffer = NULL;
  1578. s->packet_buffer_end= NULL;
  1579. goto out;
  1580. }
  1581. pktl = s->packet_buffer;
  1582. }
  1583. *out = pktl->pkt;
  1584. //av_log(s, AV_LOG_DEBUG, "out st:%d dts:%lld\n", (*out).stream_index, (*out).dts);
  1585. s->packet_buffer = pktl->next;
  1586. if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl)
  1587. s->streams[pktl->pkt.stream_index]->last_in_packet_buffer= NULL;
  1588. if(!s->packet_buffer)
  1589. s->packet_buffer_end= NULL;
  1590. av_freep(&pktl);
  1591. return 1;
  1592. } else {
  1593. out:
  1594. av_init_packet(out);
  1595. return 0;
  1596. }
  1597. }
  1598. static int mxf_compare_timestamps(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
  1599. {
  1600. MXFStreamContext *sc = s->streams[pkt ->stream_index]->priv_data;
  1601. MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data;
  1602. return next->dts > pkt->dts ||
  1603. (next->dts == pkt->dts && sc->order < sc2->order);
  1604. }
  1605. static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
  1606. {
  1607. return ff_audio_rechunk_interleave(s, out, pkt, flush,
  1608. mxf_interleave_get_packet, mxf_compare_timestamps);
  1609. }
  1610. AVOutputFormat mxf_muxer = {
  1611. "mxf",
  1612. NULL_IF_CONFIG_SMALL("Material eXchange Format"),
  1613. "application/mxf",
  1614. "mxf",
  1615. sizeof(MXFContext),
  1616. CODEC_ID_PCM_S16LE,
  1617. CODEC_ID_MPEG2VIDEO,
  1618. mxf_write_header,
  1619. mxf_write_packet,
  1620. mxf_write_footer,
  1621. AVFMT_NOTIMESTAMPS,
  1622. NULL,
  1623. mxf_interleave,
  1624. };
  1625. AVOutputFormat mxf_d10_muxer = {
  1626. "mxf_d10",
  1627. NULL_IF_CONFIG_SMALL("Material eXchange Format, D-10 Mapping"),
  1628. "application/mxf",
  1629. NULL,
  1630. sizeof(MXFContext),
  1631. CODEC_ID_PCM_S16LE,
  1632. CODEC_ID_MPEG2VIDEO,
  1633. mxf_write_header,
  1634. mxf_write_packet,
  1635. mxf_write_footer,
  1636. AVFMT_NOTIMESTAMPS,
  1637. NULL,
  1638. mxf_interleave,
  1639. };