You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1896 lines
71KB

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