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.

1626 lines
60KB

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