mxfenc.c 71 KB

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