mxfenc.c 71 KB

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