mxfenc.c 72 KB

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