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.

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