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.

1091 lines
39KB

  1. /*
  2. * MXF muxer
  3. * Copyright (c) 2008 GUCAS, Zhentan Feng <spyfeng at gmail dot com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /*
  22. * References
  23. * SMPTE 336M KLV Data Encoding Protocol Using Key-Length-Value
  24. * SMPTE 377M MXF File Format Specifications
  25. * SMPTE 379M MXF Generic Container
  26. * SMPTE 381M Mapping MPEG Streams into the MXF Generic Container
  27. * SMPTE RP210: SMPTE Metadata Dictionary
  28. * SMPTE RP224: Registry of SMPTE Universal Labels
  29. */
  30. //#define DEBUG
  31. #include "libavutil/fifo.h"
  32. #include "mxf.h"
  33. static const int NTSC_samples_per_frame[] = { 1602, 1601, 1602, 1601, 1602, 0 };
  34. static const int PAL_samples_per_frame[] = { 1920, 0 };
  35. typedef struct {
  36. AVFifoBuffer fifo;
  37. unsigned fifo_size; ///< current fifo size allocated
  38. uint64_t dts; ///< current dts
  39. int sample_size; ///< size of one sample all channels included
  40. const int *samples_per_frame; ///< must be 0 terminated
  41. const int *samples; ///< current samples per frame, pointer to samples_per_frame
  42. } AudioInterleaveContext;
  43. typedef struct {
  44. int local_tag;
  45. UID uid;
  46. } MXFLocalTagPair;
  47. typedef struct {
  48. AudioInterleaveContext aic;
  49. UID track_essence_element_key;
  50. int index; //<<< index in mxf_essence_container_uls table
  51. const UID *codec_ul;
  52. int64_t duration;
  53. int order; ///< interleaving order if dts are equal
  54. int interlaced; ///< wether picture is interlaced
  55. } MXFStreamContext;
  56. typedef struct {
  57. UID container_ul;
  58. UID element_ul;
  59. UID codec_ul;
  60. enum CodecID id;
  61. void (*write_desc)();
  62. } MXFContainerEssenceEntry;
  63. static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st);
  64. static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st);
  65. static const MXFContainerEssenceEntry mxf_essence_container_uls[] = {
  66. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
  67. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
  68. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
  69. CODEC_ID_MPEG2VIDEO, mxf_write_mpegvideo_desc },
  70. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
  71. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
  72. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
  73. CODEC_ID_PCM_S16LE, mxf_write_wav_desc },
  74. { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  75. { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  76. { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  77. CODEC_ID_NONE, NULL },
  78. };
  79. typedef struct MXFContext {
  80. int64_t footer_partition_offset;
  81. int essence_container_count;
  82. uint8_t essence_containers_indices[FF_ARRAY_ELEMS(mxf_essence_container_uls)];
  83. AVRational time_base;
  84. int header_written;
  85. } MXFContext;
  86. static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
  87. static const uint8_t umid_base[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13,0x00,0x00,0x00 };
  88. /**
  89. * complete key for operation pattern, partitions, and primer pack
  90. */
  91. static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x01,0x00 };
  92. static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
  93. static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
  94. 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
  95. 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
  96. /**
  97. * partial key for header metadata
  98. */
  99. static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
  100. static const uint8_t multiple_desc_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
  101. /**
  102. * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
  103. */
  104. static const MXFLocalTagPair mxf_local_tag_batch[] = {
  105. // preface set
  106. { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
  107. { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
  108. { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
  109. { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
  110. { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
  111. { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
  112. { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
  113. { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
  114. // Identification
  115. { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
  116. { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
  117. { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */
  118. { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */
  119. { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
  120. { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
  121. // Content Storage
  122. { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */
  123. { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */
  124. // Essence Container Data
  125. { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
  126. { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
  127. // Package
  128. { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
  129. { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
  130. { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */
  131. { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
  132. { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
  133. // Track
  134. { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */
  135. { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */
  136. { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
  137. { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
  138. { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
  139. // Sequence
  140. { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
  141. { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
  142. { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
  143. // Source Clip
  144. { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */
  145. { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
  146. { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */
  147. // File Descriptor
  148. { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */
  149. { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
  150. { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */
  151. { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
  152. // Generic Picture Essence Descriptor
  153. { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
  154. { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */
  155. { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
  156. { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */
  157. // Generic Sound Essence Descriptor
  158. { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
  159. { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
  160. { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */
  161. { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */
  162. };
  163. static void mxf_write_uuid(ByteIOContext *pb, enum MXFMetadataSetType type, int value)
  164. {
  165. put_buffer(pb, uuid_base, 12);
  166. put_be16(pb, type);
  167. put_be16(pb, value);
  168. }
  169. static void mxf_write_umid(ByteIOContext *pb, enum MXFMetadataSetType type, int value)
  170. {
  171. put_buffer(pb, umid_base, 16);
  172. mxf_write_uuid(pb, type, value);
  173. }
  174. static void mxf_write_refs_count(ByteIOContext *pb, int ref_count)
  175. {
  176. put_be32(pb, ref_count);
  177. put_be32(pb, 16);
  178. }
  179. static int klv_encode_ber_length(ByteIOContext *pb, uint64_t len)
  180. {
  181. // Determine the best BER size
  182. int size;
  183. if (len < 128) {
  184. //short form
  185. put_byte(pb, len);
  186. return 1;
  187. }
  188. size = (av_log2(len) >> 3) + 1;
  189. // long form
  190. put_byte(pb, 0x80 + size);
  191. while(size) {
  192. size --;
  193. put_byte(pb, len >> 8 * size & 0xff);
  194. }
  195. return 0;
  196. }
  197. /*
  198. * Get essence container ul index
  199. */
  200. static int mxf_get_essence_container_ul_index(enum CodecID id)
  201. {
  202. int i;
  203. for (i = 0; i < FF_ARRAY_ELEMS(mxf_essence_container_uls); i++)
  204. if (mxf_essence_container_uls[i].id == id)
  205. return i;
  206. return -1;
  207. }
  208. static void mxf_write_primer_pack(AVFormatContext *s)
  209. {
  210. ByteIOContext *pb = s->pb;
  211. int local_tag_number, i = 0;
  212. local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch);
  213. put_buffer(pb, primer_pack_key, 16);
  214. klv_encode_ber_length(pb, local_tag_number * 18 + 8);
  215. put_be32(pb, local_tag_number); // local_tag num
  216. put_be32(pb, 18); // item size, always 18 according to the specs
  217. for (i = 0; i < local_tag_number; i++) {
  218. put_be16(pb, mxf_local_tag_batch[i].local_tag);
  219. put_buffer(pb, mxf_local_tag_batch[i].uid, 16);
  220. }
  221. }
  222. static void mxf_write_local_tag(ByteIOContext *pb, int size, int tag)
  223. {
  224. put_be16(pb, tag);
  225. put_be16(pb, size);
  226. }
  227. static void mxf_write_metadata_key(ByteIOContext *pb, unsigned int value)
  228. {
  229. put_buffer(pb, header_metadata_key, 13);
  230. put_be24(pb, value);
  231. }
  232. static void mxf_free(AVFormatContext *s)
  233. {
  234. int i;
  235. for (i = 0; i < s->nb_streams; i++) {
  236. AVStream *st = s->streams[i];
  237. av_freep(&st->priv_data);
  238. }
  239. }
  240. static const MXFDataDefinitionUL *mxf_get_data_definition_ul(enum CodecType type)
  241. {
  242. const MXFDataDefinitionUL *uls = ff_mxf_data_definition_uls;
  243. while (uls->type != CODEC_TYPE_DATA) {
  244. if (type == uls->type)
  245. break;
  246. uls++;
  247. }
  248. return uls;
  249. }
  250. static void mxf_write_essence_container_refs(AVFormatContext *s)
  251. {
  252. MXFContext *c = s->priv_data;
  253. ByteIOContext *pb = s->pb;
  254. int i;
  255. mxf_write_refs_count(pb, c->essence_container_count);
  256. av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count);
  257. for (i = 0; i < c->essence_container_count; i++) {
  258. put_buffer(pb, mxf_essence_container_uls[c->essence_containers_indices[i]].container_ul, 16);
  259. PRINT_KEY(s, "essence container ul:\n", mxf_essence_container_uls[c->essence_containers_indices[i]].container_ul);
  260. }
  261. }
  262. static void mxf_write_preface(AVFormatContext *s)
  263. {
  264. MXFContext *mxf = s->priv_data;
  265. ByteIOContext *pb = s->pb;
  266. mxf_write_metadata_key(pb, 0x012f00);
  267. PRINT_KEY(s, "preface key", pb->buf_ptr - 16);
  268. klv_encode_ber_length(pb, 130 + 16 * mxf->essence_container_count);
  269. // write preface set uid
  270. mxf_write_local_tag(pb, 16, 0x3C0A);
  271. mxf_write_uuid(pb, Preface, 0);
  272. PRINT_KEY(s, "preface uid", pb->buf_ptr - 16);
  273. // write create date as unknown
  274. mxf_write_local_tag(pb, 8, 0x3B02);
  275. put_be64(pb, 0);
  276. // write version
  277. mxf_write_local_tag(pb, 2, 0x3B05);
  278. put_be16(pb, 1);
  279. // write identification_refs
  280. mxf_write_local_tag(pb, 16 + 8, 0x3B06);
  281. mxf_write_refs_count(pb, 1);
  282. mxf_write_uuid(pb, Identification, 0);
  283. // write content_storage_refs
  284. mxf_write_local_tag(pb, 16, 0x3B03);
  285. mxf_write_uuid(pb, ContentStorage, 0);
  286. mxf_write_local_tag(pb, 16, 0x3B09);
  287. put_buffer(pb, op1a_ul, 16);
  288. // write essence_container_refs
  289. mxf_write_local_tag(pb, 8 + 16 * mxf->essence_container_count, 0x3B0A);
  290. mxf_write_essence_container_refs(s);
  291. // write dm_scheme_refs
  292. mxf_write_local_tag(pb, 8, 0x3B0B);
  293. put_be64(pb, 0);
  294. }
  295. /*
  296. * Write a local tag containing an ascii string as utf-16
  297. */
  298. static void mxf_write_local_tag_utf16(ByteIOContext *pb, int tag, const char *value)
  299. {
  300. int i, size = strlen(value);
  301. mxf_write_local_tag(pb, size*2, tag);
  302. for (i = 0; i < size; i++)
  303. put_be16(pb, value[i]);
  304. }
  305. static void mxf_write_identification(AVFormatContext *s)
  306. {
  307. ByteIOContext *pb = s->pb;
  308. const char *company = "FFmpeg";
  309. const char *product = "OP1a Muxer";
  310. const char *version;
  311. int length;
  312. mxf_write_metadata_key(pb, 0x013000);
  313. PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
  314. version = s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT ?
  315. "0.0.0" : AV_STRINGIFY(LIBAVFORMAT_VERSION);
  316. length = 84 + (strlen(company)+strlen(product)+strlen(version))*2; // utf-16
  317. klv_encode_ber_length(pb, length);
  318. // write uid
  319. mxf_write_local_tag(pb, 16, 0x3C0A);
  320. mxf_write_uuid(pb, Identification, 0);
  321. PRINT_KEY(s, "identification uid", pb->buf_ptr - 16);
  322. // write generation uid
  323. mxf_write_local_tag(pb, 16, 0x3C09);
  324. mxf_write_uuid(pb, Identification, 1);
  325. mxf_write_local_tag_utf16(pb, 0x3C01, company); // Company Name
  326. mxf_write_local_tag_utf16(pb, 0x3C02, product); // Product Name
  327. mxf_write_local_tag_utf16(pb, 0x3C04, version); // Version String
  328. // write product uid
  329. mxf_write_local_tag(pb, 16, 0x3C05);
  330. mxf_write_uuid(pb, Identification, 2);
  331. // write modified date
  332. mxf_write_local_tag(pb, 8, 0x3C06);
  333. put_be64(pb, 0);
  334. }
  335. static void mxf_write_content_storage(AVFormatContext *s)
  336. {
  337. ByteIOContext *pb = s->pb;
  338. mxf_write_metadata_key(pb, 0x011800);
  339. PRINT_KEY(s, "content storage key", pb->buf_ptr - 16);
  340. klv_encode_ber_length(pb, 92);
  341. // write uid
  342. mxf_write_local_tag(pb, 16, 0x3C0A);
  343. mxf_write_uuid(pb, ContentStorage, 0);
  344. PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16);
  345. // write package reference
  346. mxf_write_local_tag(pb, 16 * 2 + 8, 0x1901);
  347. mxf_write_refs_count(pb, 2);
  348. mxf_write_uuid(pb, MaterialPackage, 0);
  349. mxf_write_uuid(pb, SourcePackage, 0);
  350. // write essence container data
  351. mxf_write_local_tag(pb, 8 + 16, 0x1902);
  352. mxf_write_refs_count(pb, 1);
  353. mxf_write_uuid(pb, EssenceContainerData, 0);
  354. }
  355. static void mxf_write_track(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
  356. {
  357. ByteIOContext *pb = s->pb;
  358. MXFStreamContext *sc = st->priv_data;
  359. mxf_write_metadata_key(pb, 0x013b00);
  360. PRINT_KEY(s, "track key", pb->buf_ptr - 16);
  361. klv_encode_ber_length(pb, 80);
  362. // write track uid
  363. mxf_write_local_tag(pb, 16, 0x3C0A);
  364. mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, st->index);
  365. PRINT_KEY(s, "track uid", pb->buf_ptr - 16);
  366. // write track id
  367. mxf_write_local_tag(pb, 4, 0x4801);
  368. put_be32(pb, st->index);
  369. // write track number
  370. mxf_write_local_tag(pb, 4, 0x4804);
  371. if (type == MaterialPackage)
  372. put_be32(pb, 0); // track number of material package is 0
  373. else
  374. put_buffer(pb, sc->track_essence_element_key + 12, 4);
  375. mxf_write_local_tag(pb, 8, 0x4B01);
  376. put_be32(pb, st->time_base.den);
  377. put_be32(pb, st->time_base.num);
  378. // write origin
  379. mxf_write_local_tag(pb, 8, 0x4B02);
  380. put_be64(pb, 0);
  381. // write sequence refs
  382. mxf_write_local_tag(pb, 16, 0x4803);
  383. mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
  384. }
  385. static void mxf_write_common_fields(ByteIOContext *pb, AVStream *st)
  386. {
  387. const MXFDataDefinitionUL *data_def_ul = mxf_get_data_definition_ul(st->codec->codec_type);
  388. MXFStreamContext *sc = st->priv_data;
  389. // find data define uls
  390. mxf_write_local_tag(pb, 16, 0x0201);
  391. put_buffer(pb, data_def_ul->uid, 16);
  392. // write duration
  393. mxf_write_local_tag(pb, 8, 0x0202);
  394. put_be64(pb, sc->duration);
  395. }
  396. static void mxf_write_sequence(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
  397. {
  398. ByteIOContext *pb = s->pb;
  399. mxf_write_metadata_key(pb, 0x010f00);
  400. PRINT_KEY(s, "sequence key", pb->buf_ptr - 16);
  401. klv_encode_ber_length(pb, 80);
  402. mxf_write_local_tag(pb, 16, 0x3C0A);
  403. mxf_write_uuid(pb, type == MaterialPackage ? Sequence: Sequence + TypeBottom, st->index);
  404. PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16);
  405. mxf_write_common_fields(pb, st);
  406. // write structural component
  407. mxf_write_local_tag(pb, 16 + 8, 0x1001);
  408. mxf_write_refs_count(pb, 1);
  409. mxf_write_uuid(pb, type == MaterialPackage ? SourceClip: SourceClip + TypeBottom, st->index);
  410. }
  411. static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, enum MXFMetadataSetType type)
  412. {
  413. ByteIOContext *pb = s->pb;
  414. int i;
  415. mxf_write_metadata_key(pb, 0x011100);
  416. PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16);
  417. klv_encode_ber_length(pb, 108);
  418. // write uid
  419. mxf_write_local_tag(pb, 16, 0x3C0A);
  420. mxf_write_uuid(pb, type == MaterialPackage ? SourceClip: SourceClip + TypeBottom, st->index);
  421. PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16);
  422. mxf_write_common_fields(pb, st);
  423. // write start_position
  424. mxf_write_local_tag(pb, 8, 0x1201);
  425. put_be64(pb, 0);
  426. // write source package uid, end of the reference
  427. mxf_write_local_tag(pb, 32, 0x1101);
  428. if (type == SourcePackage) {
  429. for (i = 0; i < 4; i++)
  430. put_be64(pb, 0);
  431. } else
  432. mxf_write_umid(pb, SourcePackage, 0);
  433. // write source track id
  434. mxf_write_local_tag(pb, 4, 0x1102);
  435. if (type == SourcePackage)
  436. put_be32(pb, 0);
  437. else
  438. put_be32(pb, st->index);
  439. }
  440. static void mxf_write_multi_descriptor(AVFormatContext *s)
  441. {
  442. ByteIOContext *pb = s->pb;
  443. int i;
  444. mxf_write_metadata_key(pb, 0x014400);
  445. PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16);
  446. klv_encode_ber_length(pb, 64 + 16 * s->nb_streams);
  447. mxf_write_local_tag(pb, 16, 0x3C0A);
  448. mxf_write_uuid(pb, MultipleDescriptor, 0);
  449. PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16);
  450. // write sample rate
  451. mxf_write_local_tag(pb, 8, 0x3001);
  452. put_be32(pb, s->streams[0]->time_base.den);
  453. put_be32(pb, s->streams[0]->time_base.num);
  454. // write essence container ul
  455. mxf_write_local_tag(pb, 16, 0x3004);
  456. put_buffer(pb, multiple_desc_ul, 16);
  457. // write sub descriptor refs
  458. mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x3F01);
  459. mxf_write_refs_count(pb, s->nb_streams);
  460. for (i = 0; i < s->nb_streams; i++)
  461. mxf_write_uuid(pb, SubDescriptor, i);
  462. }
  463. static void mxf_write_generic_desc(ByteIOContext *pb, AVStream *st, const UID key, unsigned size)
  464. {
  465. MXFStreamContext *sc = st->priv_data;
  466. put_buffer(pb, key, 16);
  467. klv_encode_ber_length(pb, size);
  468. mxf_write_local_tag(pb, 16, 0x3C0A);
  469. mxf_write_uuid(pb, SubDescriptor, st->index);
  470. mxf_write_local_tag(pb, 4, 0x3006);
  471. put_be32(pb, st->index);
  472. mxf_write_local_tag(pb, 8, 0x3001);
  473. put_be32(pb, st->time_base.den);
  474. put_be32(pb, st->time_base.num);
  475. mxf_write_local_tag(pb, 16, 0x3004);
  476. put_buffer(pb, mxf_essence_container_uls[sc->index].container_ul, 16);
  477. }
  478. static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
  479. static const UID mxf_wav_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
  480. static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
  481. {
  482. MXFStreamContext *sc = st->priv_data;
  483. ByteIOContext *pb = s->pb;
  484. int stored_height = (st->codec->height+15)/16*16;
  485. AVRational dar;
  486. mxf_write_generic_desc(pb, st, mxf_mpegvideo_descriptor_key, 108);
  487. mxf_write_local_tag(pb, 4, 0x3203);
  488. put_be32(pb, st->codec->width);
  489. mxf_write_local_tag(pb, 4, 0x3202);
  490. put_be32(pb, stored_height>>sc->interlaced);
  491. av_reduce(&dar.num, &dar.den,
  492. st->codec->width*st->codec->sample_aspect_ratio.num,
  493. st->codec->height*st->codec->sample_aspect_ratio.den,
  494. 1024*1024);
  495. mxf_write_local_tag(pb, 8, 0x320E);
  496. put_be32(pb, dar.num);
  497. put_be32(pb, dar.den);
  498. mxf_write_local_tag(pb, 16, 0x3201);
  499. put_buffer(pb, *sc->codec_ul, 16);
  500. }
  501. static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st)
  502. {
  503. ByteIOContext *pb = s->pb;
  504. mxf_write_generic_desc(pb, st, mxf_wav_descriptor_key, 88);
  505. // write audio sampling rate
  506. mxf_write_local_tag(pb, 8, 0x3D03);
  507. put_be32(pb, st->codec->sample_rate);
  508. put_be32(pb, 1);
  509. mxf_write_local_tag(pb, 4, 0x3D07);
  510. put_be32(pb, st->codec->channels);
  511. mxf_write_local_tag(pb, 4, 0x3D01);
  512. put_be32(pb, st->codec->bits_per_coded_sample);
  513. }
  514. static void mxf_write_package(AVFormatContext *s, enum MXFMetadataSetType type)
  515. {
  516. ByteIOContext *pb = s->pb;
  517. int i;
  518. if (type == MaterialPackage) {
  519. mxf_write_metadata_key(pb, 0x013600);
  520. PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);
  521. klv_encode_ber_length(pb, 92 + 16 * s->nb_streams);
  522. } else {
  523. mxf_write_metadata_key(pb, 0x013700);
  524. PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);
  525. klv_encode_ber_length(pb, 112 + 16 * s->nb_streams); // 20 bytes length for descriptor reference
  526. }
  527. // write uid
  528. mxf_write_local_tag(pb, 16, 0x3C0A);
  529. mxf_write_uuid(pb, type, 0);
  530. av_log(s,AV_LOG_DEBUG, "package type:%d\n", type);
  531. PRINT_KEY(s, "package uid", pb->buf_ptr - 16);
  532. // write package umid
  533. mxf_write_local_tag(pb, 32, 0x4401);
  534. mxf_write_umid(pb, type, 0);
  535. PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);
  536. // write create date
  537. mxf_write_local_tag(pb, 8, 0x4405);
  538. put_be64(pb, 0);
  539. // write modified date
  540. mxf_write_local_tag(pb, 8, 0x4404);
  541. put_be64(pb, 0);
  542. // write track refs
  543. mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x4403);
  544. mxf_write_refs_count(pb, s->nb_streams);
  545. for (i = 0; i < s->nb_streams; i++)
  546. mxf_write_uuid(pb, type == MaterialPackage ? Track : Track + TypeBottom, i);
  547. // write multiple descriptor reference
  548. if (type == SourcePackage) {
  549. mxf_write_local_tag(pb, 16, 0x4701);
  550. if (s->nb_streams > 1) {
  551. mxf_write_uuid(pb, MultipleDescriptor, 0);
  552. mxf_write_multi_descriptor(s);
  553. } else
  554. mxf_write_uuid(pb, SubDescriptor, 0);
  555. }
  556. for (i = 0; i < s->nb_streams; i++) {
  557. AVStream *st = s->streams[i];
  558. mxf_write_track(s, st, type);
  559. mxf_write_sequence(s, st, type);
  560. mxf_write_structural_component(s, st, type);
  561. if (type == SourcePackage) {
  562. MXFStreamContext *sc = st->priv_data;
  563. mxf_essence_container_uls[sc->index].write_desc(s, st);
  564. }
  565. }
  566. }
  567. static int mxf_write_essence_container_data(AVFormatContext *s)
  568. {
  569. ByteIOContext *pb = s->pb;
  570. mxf_write_metadata_key(pb, 0x012300);
  571. klv_encode_ber_length(pb, 64);
  572. mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID
  573. mxf_write_uuid(pb, EssenceContainerData, 0);
  574. mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID
  575. mxf_write_umid(pb, SourcePackage, 0);
  576. mxf_write_local_tag(pb, 4, 0x3F07); // BodySID
  577. put_be32(pb, 1);
  578. return 0;
  579. }
  580. static int mxf_write_header_metadata_sets(AVFormatContext *s)
  581. {
  582. mxf_write_preface(s);
  583. mxf_write_identification(s);
  584. mxf_write_content_storage(s);
  585. mxf_write_package(s, MaterialPackage);
  586. mxf_write_package(s, SourcePackage);
  587. mxf_write_essence_container_data(s);
  588. return 0;
  589. }
  590. static void mxf_write_partition(AVFormatContext *s, int bodysid, const uint8_t *key, int write_metadata)
  591. {
  592. MXFContext *mxf = s->priv_data;
  593. ByteIOContext *pb = s->pb;
  594. int64_t header_byte_count_offset;
  595. // write klv
  596. put_buffer(pb, key, 16);
  597. klv_encode_ber_length(pb, 88 + 16 * mxf->essence_container_count);
  598. // write partition value
  599. put_be16(pb, 1); // majorVersion
  600. put_be16(pb, 2); // minorVersion
  601. put_be32(pb, 1); // kagSize
  602. put_be64(pb, url_ftell(pb) - 25); // thisPartition
  603. put_be64(pb, 0); // previousPartition
  604. put_be64(pb, mxf->footer_partition_offset); // footerPartition
  605. // set offset
  606. header_byte_count_offset = url_ftell(pb);
  607. put_be64(pb, 0); // headerByteCount, update later
  608. // no indexTable
  609. put_be64(pb, 0); // indexByteCount
  610. put_be32(pb, 0); // indexSID
  611. put_be64(pb, 0); // bodyOffset
  612. put_be32(pb, bodysid); // bodySID
  613. put_buffer(pb, op1a_ul, 16); // operational pattern
  614. // essence container
  615. mxf_write_essence_container_refs(s);
  616. if (write_metadata) {
  617. // mark the start of the headermetadata and calculate metadata size
  618. int64_t pos, start = url_ftell(s->pb);
  619. mxf_write_primer_pack(s);
  620. mxf_write_header_metadata_sets(s);
  621. pos = url_ftell(s->pb);
  622. // update header_byte_count
  623. url_fseek(pb, header_byte_count_offset, SEEK_SET);
  624. put_be64(pb, pos - start);
  625. url_fseek(pb, pos, SEEK_SET);
  626. }
  627. put_flush_packet(pb);
  628. }
  629. static const UID mxf_mpeg2_codec_uls[] = {
  630. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
  631. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
  632. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
  633. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
  634. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
  635. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
  636. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
  637. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
  638. };
  639. static const UID *mxf_get_mpeg2_codec_ul(AVCodecContext *avctx)
  640. {
  641. if (avctx->profile == 4) { // Main
  642. if (avctx->level == 8) // Main
  643. return avctx->gop_size ?
  644. &mxf_mpeg2_codec_uls[1] :
  645. &mxf_mpeg2_codec_uls[0];
  646. else if (avctx->level == 4) // High
  647. return avctx->gop_size ?
  648. &mxf_mpeg2_codec_uls[5] :
  649. &mxf_mpeg2_codec_uls[4];
  650. } else if (avctx->profile == 0) { // 422
  651. if (avctx->level == 5) // Main
  652. return avctx->gop_size ?
  653. &mxf_mpeg2_codec_uls[3] :
  654. &mxf_mpeg2_codec_uls[2];
  655. else if (avctx->level == 2) // High
  656. return avctx->gop_size ?
  657. &mxf_mpeg2_codec_uls[7] :
  658. &mxf_mpeg2_codec_uls[6];
  659. }
  660. return NULL;
  661. }
  662. static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
  663. {
  664. MXFStreamContext *sc = st->priv_data;
  665. uint32_t c = -1;
  666. int i;
  667. for(i = 0; i < pkt->size - 4; i++) {
  668. c = (c<<8) + pkt->data[i];
  669. if (c == 0x1B5) {
  670. if (i + 2 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x10) { // seq ext
  671. st->codec->profile = pkt->data[i+1] & 0x07;
  672. st->codec->level = pkt->data[i+2] >> 4;
  673. } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext
  674. sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame
  675. break;
  676. }
  677. }
  678. }
  679. sc->codec_ul = mxf_get_mpeg2_codec_ul(st->codec);
  680. return !!sc->codec_ul;
  681. }
  682. static int ff_audio_interleave_init(AVFormatContext *s, const int *samples_per_frame)
  683. {
  684. int i;
  685. if (!samples_per_frame)
  686. samples_per_frame = PAL_samples_per_frame;
  687. for (i = 0; i < s->nb_streams; i++) {
  688. AVStream *st = s->streams[i];
  689. AudioInterleaveContext *aic = st->priv_data;
  690. if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
  691. aic->sample_size = (st->codec->channels *
  692. av_get_bits_per_sample(st->codec->codec_id)) / 8;
  693. if (!aic->sample_size) {
  694. av_log(s, AV_LOG_ERROR, "could not compute sample size\n");
  695. return -1;
  696. }
  697. aic->samples_per_frame = samples_per_frame;
  698. aic->samples = aic->samples_per_frame;
  699. av_fifo_init(&aic->fifo, 100 * *aic->samples);
  700. }
  701. }
  702. return 0;
  703. }
  704. static int mxf_write_header(AVFormatContext *s)
  705. {
  706. MXFContext *mxf = s->priv_data;
  707. int i;
  708. uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0};
  709. const int *samples_per_frame = NULL;
  710. for (i = 0; i < s->nb_streams; i++) {
  711. AVStream *st = s->streams[i];
  712. MXFStreamContext *sc = av_mallocz(sizeof(*sc));
  713. if (!sc)
  714. return AVERROR(ENOMEM);
  715. st->priv_data = sc;
  716. if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
  717. if (!av_cmp_q(st->codec->time_base, (AVRational){ 1, 25 })) {
  718. samples_per_frame = PAL_samples_per_frame;
  719. mxf->time_base = (AVRational){ 1, 25 };
  720. } else if (!av_cmp_q(st->codec->time_base, (AVRational){ 1001, 30000 })) {
  721. samples_per_frame = NTSC_samples_per_frame;
  722. mxf->time_base = (AVRational){ 1001, 30000 };
  723. } else {
  724. av_log(s, AV_LOG_ERROR, "unsupported video frame rate\n");
  725. return -1;
  726. }
  727. } else if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
  728. if (st->codec->sample_rate != 48000) {
  729. av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n");
  730. return -1;
  731. }
  732. }
  733. sc->duration = -1;
  734. sc->index = mxf_get_essence_container_ul_index(st->codec->codec_id);
  735. if (sc->index == -1) {
  736. av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, "
  737. "codec not currently supported in container\n", i);
  738. return -1;
  739. }
  740. sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
  741. if (!present[sc->index]) {
  742. mxf->essence_containers_indices[mxf->essence_container_count++] = sc->index;
  743. present[sc->index] = 1;
  744. } else
  745. present[sc->index]++;
  746. memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15);
  747. sc->track_essence_element_key[15] = present[sc->index];
  748. PRINT_KEY(s, "track essence element key", sc->track_essence_element_key);
  749. }
  750. for (i = 0; i < s->nb_streams; i++) {
  751. MXFStreamContext *sc = s->streams[i]->priv_data;
  752. av_set_pts_info(s->streams[i], 64, mxf->time_base.num, mxf->time_base.den);
  753. // update element count
  754. sc->track_essence_element_key[13] = present[sc->index];
  755. sc->order = AV_RB32(sc->track_essence_element_key+12);
  756. }
  757. if (ff_audio_interleave_init(s, samples_per_frame) < 0)
  758. return -1;
  759. return 0;
  760. }
  761. static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
  762. {
  763. MXFContext *mxf = s->priv_data;
  764. ByteIOContext *pb = s->pb;
  765. AVStream *st = s->streams[pkt->stream_index];
  766. MXFStreamContext *sc = st->priv_data;
  767. if (!mxf->header_written) {
  768. if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO) {
  769. if (!mxf_parse_mpeg2_frame(s, st, pkt)) {
  770. av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
  771. return -1;
  772. }
  773. }
  774. mxf_write_partition(s, 1, header_open_partition_key, 1);
  775. mxf->header_written = 1;
  776. }
  777. put_buffer(pb, sc->track_essence_element_key, 16); // write key
  778. klv_encode_ber_length(pb, pkt->size); // write length
  779. put_buffer(pb, pkt->data, pkt->size); // write value
  780. sc->duration = FFMAX(pkt->pts + pkt->duration, sc->duration);
  781. put_flush_packet(pb);
  782. return 0;
  783. }
  784. static int mxf_write_footer(AVFormatContext *s)
  785. {
  786. MXFContext *mxf = s->priv_data;
  787. ByteIOContext *pb = s->pb;
  788. mxf->footer_partition_offset = url_ftell(pb);
  789. mxf_write_partition(s, 0, footer_partition_key, 0);
  790. if (!url_is_streamed(s->pb)) {
  791. url_fseek(pb, 0, SEEK_SET);
  792. mxf_write_partition(s, 1, header_closed_partition_key, 1);
  793. }
  794. mxf_free(s);
  795. return 0;
  796. }
  797. static int mxf_interleave_new_audio_packet(AVFormatContext *s, AVPacket *pkt,
  798. int stream_index, int flush)
  799. {
  800. AVStream *st = s->streams[stream_index];
  801. AudioInterleaveContext *aic = st->priv_data;
  802. int size = FFMIN(av_fifo_size(&aic->fifo), *aic->samples * aic->sample_size);
  803. if (!size || (!flush && size == av_fifo_size(&aic->fifo)))
  804. return 0;
  805. av_new_packet(pkt, size);
  806. av_fifo_read(&aic->fifo, pkt->data, size);
  807. pkt->dts = pkt->pts = aic->dts;
  808. pkt->duration = av_rescale_q(*aic->samples,
  809. (AVRational){ 1, st->codec->sample_rate },
  810. st->time_base);
  811. pkt->stream_index = stream_index;
  812. aic->dts += pkt->duration;
  813. aic->samples++;
  814. if (!*aic->samples)
  815. aic->samples = aic->samples_per_frame;
  816. return size;
  817. }
  818. static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, int flush)
  819. {
  820. AVPacketList *pktl;
  821. int stream_count = 0;
  822. int streams[MAX_STREAMS];
  823. memset(streams, 0, sizeof(streams));
  824. pktl = s->packet_buffer;
  825. while (pktl) {
  826. //av_log(s, AV_LOG_DEBUG, "show st:%d dts:%lld\n", pktl->pkt.stream_index, pktl->pkt.dts);
  827. if (!streams[pktl->pkt.stream_index])
  828. stream_count++;
  829. streams[pktl->pkt.stream_index]++;
  830. pktl = pktl->next;
  831. }
  832. if (stream_count && (s->nb_streams == stream_count || flush)) {
  833. pktl = s->packet_buffer;
  834. *out = pktl->pkt;
  835. //av_log(s, AV_LOG_DEBUG, "out st:%d dts:%lld\n", (*out).stream_index, (*out).dts);
  836. s->packet_buffer = pktl->next;
  837. av_freep(&pktl);
  838. if (flush && stream_count < s->nb_streams) {
  839. // purge packet queue
  840. pktl = s->packet_buffer;
  841. while (pktl) {
  842. AVPacketList *next = pktl->next;
  843. av_free_packet(&pktl->pkt);
  844. av_freep(&pktl);
  845. pktl = next;
  846. }
  847. s->packet_buffer = NULL;
  848. }
  849. return 1;
  850. } else {
  851. av_init_packet(out);
  852. return 0;
  853. }
  854. }
  855. static int mxf_compare_timestamps(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
  856. {
  857. AVStream *st = s->streams[pkt ->stream_index];
  858. AVStream *st2 = s->streams[next->stream_index];
  859. MXFStreamContext *sc = st ->priv_data;
  860. MXFStreamContext *sc2 = st2->priv_data;
  861. int64_t left = st2->time_base.num * (int64_t)st ->time_base.den;
  862. int64_t right = st ->time_base.num * (int64_t)st2->time_base.den;
  863. return next->dts * left > pkt->dts * right || // FIXME this can overflow
  864. (next->dts * left == pkt->dts * right && sc->order < sc2->order);
  865. }
  866. static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
  867. {
  868. int i;
  869. if (pkt) {
  870. AVStream *st = s->streams[pkt->stream_index];
  871. AudioInterleaveContext *aic = st->priv_data;
  872. if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
  873. av_fifo_generic_write(&aic->fifo, pkt->data, pkt->size, NULL);
  874. } else {
  875. // rewrite pts and dts to be decoded time line position
  876. pkt->pts = pkt->dts = aic->dts;
  877. aic->dts += pkt->duration;
  878. ff_interleave_add_packet(s, pkt, mxf_compare_timestamps);
  879. }
  880. }
  881. for (i = 0; i < s->nb_streams; i++) {
  882. AVStream *st = s->streams[i];
  883. if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
  884. AVPacket new_pkt;
  885. while (mxf_interleave_new_audio_packet(s, &new_pkt, i, flush))
  886. ff_interleave_add_packet(s, &new_pkt, mxf_compare_timestamps);
  887. }
  888. }
  889. return mxf_interleave_get_packet(s, out, flush);
  890. }
  891. AVOutputFormat mxf_muxer = {
  892. "mxf",
  893. NULL_IF_CONFIG_SMALL("Material eXchange Format"),
  894. NULL,
  895. "mxf",
  896. sizeof(MXFContext),
  897. CODEC_ID_PCM_S16LE,
  898. CODEC_ID_MPEG2VIDEO,
  899. mxf_write_header,
  900. mxf_write_packet,
  901. mxf_write_footer,
  902. 0,
  903. NULL,
  904. mxf_interleave,
  905. };