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.

3213 lines
126KB

  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. * signal_standard, color_siting, store_user_comments, sample rate and klv_fill_key version
  24. * fixes sponsored by NOA GmbH
  25. */
  26. /*
  27. * References
  28. * SMPTE 336M KLV Data Encoding Protocol Using Key-Length-Value
  29. * SMPTE 377M MXF File Format Specifications
  30. * SMPTE 379M MXF Generic Container
  31. * SMPTE 381M Mapping MPEG Streams into the MXF Generic Container
  32. * SMPTE 422M Mapping JPEG 2000 Codestreams into the MXF Generic Container
  33. * SMPTE RP210: SMPTE Metadata Dictionary
  34. * SMPTE RP224: Registry of SMPTE Universal Labels
  35. */
  36. #include <inttypes.h>
  37. #include <math.h>
  38. #include <time.h>
  39. #include "libavutil/opt.h"
  40. #include "libavutil/random_seed.h"
  41. #include "libavutil/timecode.h"
  42. #include "libavutil/avassert.h"
  43. #include "libavutil/pixdesc.h"
  44. #include "libavutil/time_internal.h"
  45. #include "libavcodec/bytestream.h"
  46. #include "libavcodec/dnxhddata.h"
  47. #include "libavcodec/dv_profile.h"
  48. #include "libavcodec/h264_ps.h"
  49. #include "libavcodec/golomb.h"
  50. #include "libavcodec/internal.h"
  51. #include "audiointerleave.h"
  52. #include "avformat.h"
  53. #include "avio_internal.h"
  54. #include "internal.h"
  55. #include "avc.h"
  56. #include "mxf.h"
  57. #include "config.h"
  58. extern AVOutputFormat ff_mxf_d10_muxer;
  59. extern AVOutputFormat ff_mxf_opatom_muxer;
  60. #define EDIT_UNITS_PER_BODY 250
  61. #define KAG_SIZE 512
  62. typedef struct MXFLocalTagPair {
  63. int local_tag;
  64. UID uid;
  65. } MXFLocalTagPair;
  66. typedef struct MXFIndexEntry {
  67. uint64_t offset;
  68. unsigned slice_offset; ///< offset of audio slice
  69. uint16_t temporal_ref;
  70. uint8_t flags;
  71. } MXFIndexEntry;
  72. typedef struct MXFStreamContext {
  73. AudioInterleaveContext aic;
  74. UID track_essence_element_key;
  75. int index; ///< index in mxf_essence_container_uls table
  76. const UID *codec_ul;
  77. const UID *container_ul;
  78. int order; ///< interleaving order if dts are equal
  79. int interlaced; ///< whether picture is interlaced
  80. int field_dominance; ///< tff=1, bff=2
  81. int component_depth;
  82. int color_siting;
  83. int signal_standard;
  84. int h_chroma_sub_sample;
  85. int v_chroma_sub_sample;
  86. int temporal_reordering;
  87. AVRational aspect_ratio; ///< display aspect ratio
  88. int closed_gop; ///< gop is closed, used in mpeg-2 frame parsing
  89. int video_bit_rate;
  90. int slice_offset;
  91. int frame_size; ///< frame size in bytes
  92. int seq_closed_gop; ///< all gops in sequence are closed, used in mpeg-2 descriptor
  93. int max_gop; ///< maximum gop size, used by mpeg-2 descriptor
  94. int b_picture_count; ///< maximum number of consecutive b pictures, used in mpeg-2 descriptor
  95. int low_delay; ///< low delay, used in mpeg-2 descriptor
  96. int avc_intra;
  97. } MXFStreamContext;
  98. typedef struct MXFContainerEssenceEntry {
  99. UID container_ul;
  100. UID element_ul;
  101. UID codec_ul;
  102. void (*write_desc)(AVFormatContext *, AVStream *);
  103. } MXFContainerEssenceEntry;
  104. typedef struct MXFPackage {
  105. char *name;
  106. enum MXFMetadataSetType type;
  107. int instance;
  108. struct MXFPackage *ref;
  109. } MXFPackage;
  110. enum ULIndex {
  111. INDEX_MPEG2 = 0,
  112. INDEX_AES3,
  113. INDEX_WAV,
  114. INDEX_D10_VIDEO,
  115. INDEX_D10_AUDIO,
  116. INDEX_DV,
  117. INDEX_DNXHD,
  118. INDEX_JPEG2000,
  119. INDEX_H264,
  120. INDEX_S436M,
  121. INDEX_PRORES,
  122. };
  123. static const struct {
  124. enum AVCodecID id;
  125. enum ULIndex index;
  126. } mxf_essence_mappings[] = {
  127. { AV_CODEC_ID_MPEG2VIDEO, INDEX_MPEG2 },
  128. { AV_CODEC_ID_PCM_S24LE, INDEX_AES3 },
  129. { AV_CODEC_ID_PCM_S16LE, INDEX_AES3 },
  130. { AV_CODEC_ID_DVVIDEO, INDEX_DV },
  131. { AV_CODEC_ID_DNXHD, INDEX_DNXHD },
  132. { AV_CODEC_ID_JPEG2000, INDEX_JPEG2000 },
  133. { AV_CODEC_ID_H264, INDEX_H264 },
  134. { AV_CODEC_ID_PRORES, INDEX_PRORES },
  135. { AV_CODEC_ID_NONE }
  136. };
  137. static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st);
  138. static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st);
  139. static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st);
  140. static void mxf_write_h264_desc(AVFormatContext *s, AVStream *st);
  141. static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st);
  142. static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st);
  143. static void mxf_write_s436m_anc_desc(AVFormatContext *s, AVStream *st);
  144. static const MXFContainerEssenceEntry mxf_essence_container_uls[] = {
  145. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x03,0x01,0x02,0x04,0x60,0x01 },
  146. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
  147. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
  148. mxf_write_mpegvideo_desc },
  149. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x03,0x00 },
  150. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x03,0x00 },
  151. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
  152. mxf_write_aes3_desc },
  153. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x06,0x01,0x00 },
  154. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x16,0x01,0x01,0x00 },
  155. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
  156. mxf_write_wav_desc },
  157. // D-10 Video
  158. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
  159. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x05,0x01,0x01,0x00 },
  160. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 },
  161. mxf_write_cdci_desc },
  162. // D-10 Audio
  163. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 },
  164. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x06,0x01,0x10,0x00 },
  165. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x02,0x02,0x01,0x00,0x00,0x00,0x00 },
  166. mxf_write_generic_sound_desc },
  167. // DV
  168. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x7F,0x01 },
  169. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x18,0x01,0x01,0x00 },
  170. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x00,0x00,0x00 },
  171. mxf_write_cdci_desc },
  172. // DNxHD
  173. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x11,0x01,0x00 },
  174. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
  175. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0A,0x04,0x01,0x02,0x02,0x71,0x01,0x00,0x00 },
  176. mxf_write_cdci_desc },
  177. // JPEG2000
  178. { { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x0d,0x01,0x03,0x01,0x02,0x0c,0x01,0x00 },
  179. { 0x06,0x0e,0x2b,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x08,0x00 },
  180. { 0x06,0x0e,0x2b,0x34,0x04,0x01,0x01,0x07,0x04,0x01,0x02,0x02,0x03,0x01,0x01,0x00 },
  181. mxf_write_cdci_desc },
  182. // H.264
  183. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x0D,0x01,0x03,0x01,0x02,0x10,0x60,0x01 },
  184. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x15,0x01,0x05,0x00 },
  185. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x00,0x00,0x00 },
  186. mxf_write_h264_desc },
  187. // S436M ANC
  188. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x0D,0x01,0x03,0x01,0x02,0x0e,0x00,0x00 },
  189. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0D,0x01,0x03,0x01,0x17,0x01,0x02,0x00 },
  190. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x01,0x5C,0x00 },
  191. mxf_write_s436m_anc_desc },
  192. // ProRes
  193. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x0d,0x01,0x03,0x01,0x02,0x1c,0x01,0x00 },
  194. { 0x06,0x0E,0x2B,0x34,0x01,0x02,0x01,0x01,0x0d,0x01,0x03,0x01,0x15,0x01,0x17,0x00 },
  195. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x03,0x00 },
  196. mxf_write_cdci_desc },
  197. { { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  198. { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  199. { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  200. NULL },
  201. };
  202. static const UID mxf_d10_codec_uls[] = {
  203. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x01 }, // D-10 625/50 PAL 50mb/s
  204. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x02 }, // D-10 525/50 NTSC 50mb/s
  205. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x03 }, // D-10 625/50 PAL 40mb/s
  206. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x04 }, // D-10 525/50 NTSC 40mb/s
  207. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x05 }, // D-10 625/50 PAL 30mb/s
  208. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x01,0x02,0x01,0x06 }, // D-10 525/50 NTSC 30mb/s
  209. };
  210. static const UID mxf_d10_container_uls[] = {
  211. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x01,0x01 }, // D-10 625/50 PAL 50mb/s
  212. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x02,0x01 }, // D-10 525/50 NTSC 50mb/s
  213. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x03,0x01 }, // D-10 625/50 PAL 40mb/s
  214. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x04,0x01 }, // D-10 525/50 NTSC 40mb/s
  215. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x05,0x01 }, // D-10 625/50 PAL 30mb/s
  216. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x01,0x06,0x01 }, // D-10 525/50 NTSC 30mb/s
  217. };
  218. typedef struct MXFContext {
  219. AVClass *av_class;
  220. int64_t footer_partition_offset;
  221. int essence_container_count;
  222. AVRational time_base;
  223. int header_written;
  224. MXFIndexEntry *index_entries;
  225. unsigned edit_units_count;
  226. uint64_t timestamp; ///< timestamp, as year(16),month(8),day(8),hour(8),minutes(8),msec/4(8)
  227. uint8_t slice_count; ///< index slice count minus 1 (1 if no audio, 0 otherwise)
  228. int last_indexed_edit_unit;
  229. uint64_t *body_partition_offset;
  230. unsigned body_partitions_count;
  231. int last_key_index; ///< index of last key frame
  232. uint64_t duration;
  233. AVTimecode tc; ///< timecode context
  234. AVStream *timecode_track;
  235. int timecode_base; ///< rounded time code base (25 or 30)
  236. int edit_unit_byte_count; ///< fixed edit unit byte count
  237. int content_package_rate; ///< content package rate in system element, see SMPTE 326M
  238. uint64_t body_offset;
  239. uint32_t instance_number;
  240. uint8_t umid[16]; ///< unique material identifier
  241. int channel_count;
  242. int signal_standard;
  243. uint32_t tagged_value_count;
  244. AVRational audio_edit_rate;
  245. int store_user_comments;
  246. int track_instance_count; // used to generate MXFTrack uuids
  247. int cbr_index; ///< use a constant bitrate index
  248. } MXFContext;
  249. static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
  250. static const uint8_t umid_ul[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x01,0x0D,0x00,0x13 };
  251. /**
  252. * complete key for operation pattern, partitions, and primer pack
  253. */
  254. static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x09,0x00 };
  255. static const uint8_t opatom_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x02,0x0D,0x01,0x02,0x01,0x10,0x03,0x00,0x00 };
  256. static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
  257. static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
  258. static const uint8_t index_table_segment_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x02,0x01,0x01,0x10,0x01,0x00 };
  259. static const uint8_t random_index_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x11,0x01,0x00 };
  260. 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
  261. 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
  262. static const uint8_t klv_fill_key[] = { 0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x10,0x01,0x00,0x00,0x00 };
  263. static const uint8_t body_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x03,0x04,0x00 }; // ClosedComplete
  264. /**
  265. * partial key for header metadata
  266. */
  267. static const uint8_t header_metadata_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01 };
  268. static const uint8_t multiple_desc_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x0D,0x01,0x03,0x01,0x02,0x7F,0x01,0x00 };
  269. /**
  270. * SMPTE RP210 http://www.smpte-ra.org/mdd/index.html
  271. * https://smpte-ra.org/sites/default/files/Labels.xml
  272. */
  273. static const MXFLocalTagPair mxf_local_tag_batch[] = {
  274. // preface set
  275. { 0x3C0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x02,0x00,0x00,0x00,0x00}}, /* Instance UID */
  276. { 0x3B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x04,0x00,0x00}}, /* Last Modified Date */
  277. { 0x3B05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x05,0x00,0x00,0x00}}, /* Version */
  278. { 0x3B07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x01,0x02,0x01,0x04,0x00,0x00,0x00}}, /* Object Model Version */
  279. { 0x3B06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x04,0x00,0x00}}, /* Identifications reference */
  280. { 0x3B03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x01,0x00,0x00}}, /* Content Storage reference */
  281. { 0x3B09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x03,0x00,0x00,0x00,0x00}}, /* Operational Pattern UL */
  282. { 0x3B0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x01,0x00,0x00}}, /* Essence Containers UL batch */
  283. { 0x3B0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x01,0x02,0x02,0x10,0x02,0x02,0x00,0x00}}, /* DM Schemes UL batch */
  284. // Identification
  285. { 0x3C09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x01,0x00,0x00,0x00}}, /* This Generation UID */
  286. { 0x3C01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x02,0x01,0x00,0x00}}, /* Company Name */
  287. { 0x3C02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x03,0x01,0x00,0x00}}, /* Product Name */
  288. { 0x3C03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x04,0x00,0x00,0x00}}, /* Product Version */
  289. { 0x3C04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x05,0x01,0x00,0x00}}, /* Version String */
  290. { 0x3C05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x07,0x00,0x00,0x00}}, /* Product ID */
  291. { 0x3C06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x03,0x00,0x00}}, /* Modification Date */
  292. { 0x3C07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x0A,0x00,0x00,0x00}}, /* Toolkit Version */
  293. { 0x3C08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x20,0x07,0x01,0x06,0x01,0x00,0x00}}, /* Platform */
  294. // Content Storage
  295. { 0x1901, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x01,0x00,0x00}}, /* Package strong reference batch */
  296. { 0x1902, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x05,0x02,0x00,0x00}}, /* Package strong reference batch */
  297. // Essence Container Data
  298. { 0x2701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x06,0x01,0x00,0x00,0x00}}, /* Linked Package UID */
  299. { 0x3F07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x04,0x00,0x00,0x00,0x00}}, /* BodySID */
  300. // Package
  301. { 0x4401, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x15,0x10,0x00,0x00,0x00,0x00}}, /* Package UID */
  302. { 0x4405, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x01,0x03,0x00,0x00}}, /* Package Creation Date */
  303. { 0x4404, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x10,0x02,0x05,0x00,0x00}}, /* Package Modified Date */
  304. { 0x4402, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x03,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Package Name */
  305. { 0x4403, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x05,0x00,0x00}}, /* Tracks Strong reference array */
  306. { 0x4701, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x03,0x00,0x00}}, /* Descriptor */
  307. // Track
  308. { 0x4801, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x07,0x01,0x01,0x00,0x00,0x00,0x00}}, /* Track ID */
  309. { 0x4804, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x01,0x04,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Track Number */
  310. { 0x4B01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x05,0x30,0x04,0x05,0x00,0x00,0x00,0x00}}, /* Edit Rate */
  311. { 0x4B02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x03,0x00,0x00}}, /* Origin */
  312. { 0x4803, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x02,0x04,0x00,0x00}}, /* Sequence reference */
  313. // Sequence
  314. { 0x0201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x07,0x01,0x00,0x00,0x00,0x00,0x00}}, /* Data Definition UL */
  315. { 0x0202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x02,0x01,0x01,0x03,0x00,0x00}}, /* Duration */
  316. { 0x1001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x06,0x09,0x00,0x00}}, /* Structural Components reference array */
  317. // Source Clip
  318. { 0x1201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x04,0x00,0x00}}, /* Start position */
  319. { 0x1101, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x01,0x00,0x00,0x00}}, /* SourcePackageID */
  320. { 0x1102, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x03,0x02,0x00,0x00,0x00}}, /* SourceTrackID */
  321. // Timecode Component
  322. { 0x1501, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x07,0x02,0x01,0x03,0x01,0x05,0x00,0x00}}, /* Start Time Code */
  323. { 0x1502, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x04,0x01,0x01,0x02,0x06,0x00,0x00}}, /* Rounded Time Code Base */
  324. { 0x1503, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x04,0x01,0x01,0x05,0x00,0x00,0x00}}, /* Drop Frame */
  325. // File Descriptor
  326. { 0x3F01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x04,0x06,0x0B,0x00,0x00}}, /* Sub Descriptors reference array */
  327. { 0x3006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x06,0x01,0x01,0x03,0x05,0x00,0x00,0x00}}, /* Linked Track ID */
  328. { 0x3001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x01,0x00,0x00,0x00,0x00}}, /* SampleRate */
  329. { 0x3002, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x06,0x01,0x02,0x00,0x00,0x00,0x00}}, /* ContainerDuration */
  330. { 0x3004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x06,0x01,0x01,0x04,0x01,0x02,0x00,0x00}}, /* Essence Container */
  331. // Generic Picture Essence Descriptor
  332. { 0x320C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Frame Layout */
  333. { 0x320D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x02,0x05,0x00,0x00,0x00}}, /* Video Line Map */
  334. { 0x3203, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x02,0x00,0x00,0x00}}, /* Stored Width */
  335. { 0x3202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x02,0x01,0x00,0x00,0x00}}, /* Stored Height */
  336. { 0x3216, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x03,0x02,0x08,0x00,0x00,0x00}}, /* Stored F2 Offset */
  337. { 0x3205, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x08,0x00,0x00,0x00}}, /* Sampled Width */
  338. { 0x3204, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x07,0x00,0x00,0x00}}, /* Sampled Height */
  339. { 0x3206, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x09,0x00,0x00,0x00}}, /* Sampled X Offset */
  340. { 0x3207, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0A,0x00,0x00,0x00}}, /* Sampled Y Offset */
  341. { 0x3209, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0C,0x00,0x00,0x00}}, /* Display Width */
  342. { 0x3208, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0B,0x00,0x00,0x00}}, /* Display Height */
  343. { 0x320A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0D,0x00,0x00,0x00}}, /* Display X offset */
  344. { 0x320B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x0E,0x00,0x00,0x00}}, /* Presentation Y offset */
  345. { 0x3217, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x03,0x02,0x07,0x00,0x00,0x00}}, /* Display F2 offset */
  346. { 0x320E, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x00,0x00,0x00}}, /* Aspect Ratio */
  347. { 0x3210, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x02,0x01,0x01,0x01,0x02,0x00}}, /* Transfer characteristic */
  348. { 0x3213, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x18,0x01,0x02,0x00,0x00,0x00,0x00}}, /* Image Start Offset */
  349. { 0x3214, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x18,0x01,0x03,0x00,0x00,0x00,0x00}}, /* Image End Offset */
  350. { 0x3201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x06,0x01,0x00,0x00,0x00,0x00}}, /* Picture Essence Coding */
  351. { 0x3212, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x03,0x01,0x06,0x00,0x00,0x00}}, /* Field Dominance (Opt) */
  352. { 0x3215, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x05,0x01,0x13,0x00,0x00,0x00,0x00}}, /* Signal Standard */
  353. // CDCI Picture Essence Descriptor
  354. { 0x3301, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x0A,0x00,0x00,0x00}}, /* Component Depth */
  355. { 0x3302, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x05,0x00,0x00,0x00}}, /* Horizontal Subsampling */
  356. { 0x3308, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x01,0x10,0x00,0x00,0x00}}, /* Vertical Subsampling */
  357. { 0x3303, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x01,0x06,0x00,0x00,0x00}}, /* Color Siting */
  358. { 0x3307, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x18,0x01,0x04,0x00,0x00,0x00,0x00}}, /* Padding Bits */
  359. { 0x3304, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x03,0x03,0x00,0x00,0x00}}, /* Black Ref level */
  360. { 0x3305, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x01,0x05,0x03,0x04,0x00,0x00,0x00}}, /* White Ref level */
  361. { 0x3306, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x01,0x05,0x03,0x05,0x00,0x00,0x00}}, /* Color Range */
  362. // Generic Sound Essence Descriptor
  363. { 0x3D02, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x01,0x04,0x00,0x00,0x00}}, /* Locked/Unlocked */
  364. { 0x3D03, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x01,0x01,0x01,0x00,0x00}}, /* Audio sampling rate */
  365. { 0x3D04, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x01,0x04,0x02,0x01,0x01,0x03,0x00,0x00,0x00}}, /* Audio Ref Level */
  366. { 0x3D07, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x01,0x01,0x04,0x00,0x00,0x00}}, /* ChannelCount */
  367. { 0x3D01, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x02,0x03,0x03,0x04,0x00,0x00,0x00}}, /* Quantization bits */
  368. { 0x3D06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x04,0x02,0x04,0x02,0x00,0x00,0x00,0x00}}, /* Sound Essence Compression */
  369. // Index Table Segment
  370. { 0x3F0B, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x05,0x30,0x04,0x06,0x00,0x00,0x00,0x00}}, /* Index Edit Rate */
  371. { 0x3F0C, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x01,0x03,0x01,0x0A,0x00,0x00}}, /* Index Start Position */
  372. { 0x3F0D, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x07,0x02,0x02,0x01,0x01,0x02,0x00,0x00}}, /* Index Duration */
  373. { 0x3F05, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x06,0x02,0x01,0x00,0x00,0x00,0x00}}, /* Edit Unit Byte Count */
  374. { 0x3F06, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x01,0x03,0x04,0x05,0x00,0x00,0x00,0x00}}, /* IndexSID */
  375. { 0x3F08, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x04,0x04,0x04,0x04,0x01,0x01,0x00,0x00,0x00}}, /* Slice Count */
  376. { 0x3F09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x01,0x06,0x00,0x00,0x00}}, /* Delta Entry Array */
  377. { 0x3F0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x04,0x04,0x02,0x05,0x00,0x00,0x00}}, /* Index Entry Array */
  378. // MPEG video Descriptor
  379. { 0x8000, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0B,0x00,0x00}}, /* BitRate */
  380. { 0x8003, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x05,0x00,0x00}}, /* LowDelay */
  381. { 0x8004, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x06,0x00,0x00}}, /* ClosedGOP */
  382. { 0x8006, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x08,0x00,0x00}}, /* MaxGOP */
  383. { 0x8007, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x0A,0x00,0x00}}, /* ProfileAndLevel */
  384. { 0x8008, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x01,0x06,0x02,0x01,0x09,0x00,0x00}}, /* BPictureCount */
  385. // Wave Audio Essence Descriptor
  386. { 0x3D09, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x03,0x05,0x00,0x00,0x00}}, /* Average Bytes Per Second */
  387. { 0x3D0A, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x05,0x04,0x02,0x03,0x02,0x01,0x00,0x00,0x00}}, /* Block Align */
  388. };
  389. static const MXFLocalTagPair mxf_avc_subdescriptor_local_tags[] = {
  390. { 0x8100, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x09,0x06,0x01,0x01,0x04,0x06,0x10,0x00,0x00}}, /* SubDescriptors */
  391. { 0x8200, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x0E,0x04,0x01,0x06,0x06,0x01,0x0E,0x00,0x00}}, /* AVC Decoding Delay */
  392. { 0x8201, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x0E,0x04,0x01,0x06,0x06,0x01,0x0A,0x00,0x00}}, /* AVC Profile */
  393. { 0x8202, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x0E,0x04,0x01,0x06,0x06,0x01,0x0D,0x00,0x00}}, /* AVC Level */
  394. };
  395. static const MXFLocalTagPair mxf_user_comments_local_tag[] = {
  396. { 0x4406, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x0C,0x00,0x00,0x00}}, /* User Comments */
  397. { 0x5001, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x09,0x01,0x00,0x00}}, /* Name */
  398. { 0x5003, {0x06,0x0E,0x2B,0x34,0x01,0x01,0x01,0x02,0x03,0x02,0x01,0x02,0x0A,0x01,0x00,0x00}}, /* Value */
  399. };
  400. static void mxf_write_uuid(AVIOContext *pb, enum MXFMetadataSetType type, int value)
  401. {
  402. avio_write(pb, uuid_base, 12);
  403. avio_wb16(pb, type);
  404. avio_wb16(pb, value);
  405. }
  406. static void mxf_write_umid(AVFormatContext *s, int type)
  407. {
  408. MXFContext *mxf = s->priv_data;
  409. avio_write(s->pb, umid_ul, 13);
  410. avio_wb24(s->pb, mxf->instance_number);
  411. avio_write(s->pb, mxf->umid, 15);
  412. avio_w8(s->pb, type);
  413. }
  414. static void mxf_write_refs_count(AVIOContext *pb, int ref_count)
  415. {
  416. avio_wb32(pb, ref_count);
  417. avio_wb32(pb, 16);
  418. }
  419. static int klv_ber_length(uint64_t len)
  420. {
  421. if (len < 128)
  422. return 1;
  423. else
  424. return (av_log2(len) >> 3) + 2;
  425. }
  426. static int klv_encode_ber_length(AVIOContext *pb, uint64_t len)
  427. {
  428. // Determine the best BER size
  429. int size = klv_ber_length(len);
  430. if (size == 1) {
  431. //short form
  432. avio_w8(pb, len);
  433. return 1;
  434. }
  435. size --;
  436. // long form
  437. avio_w8(pb, 0x80 + size);
  438. while(size) {
  439. size--;
  440. avio_w8(pb, len >> 8 * size & 0xff);
  441. }
  442. return 0;
  443. }
  444. static void klv_encode_ber4_length(AVIOContext *pb, int len)
  445. {
  446. avio_w8(pb, 0x80 + 3);
  447. avio_wb24(pb, len);
  448. }
  449. static void klv_encode_ber9_length(AVIOContext *pb, uint64_t len)
  450. {
  451. avio_w8(pb, 0x80 + 8);
  452. avio_wb64(pb, len);
  453. }
  454. /*
  455. * Get essence container ul index
  456. */
  457. static int mxf_get_essence_container_ul_index(enum AVCodecID id)
  458. {
  459. int i;
  460. for (i = 0; mxf_essence_mappings[i].id; i++)
  461. if (mxf_essence_mappings[i].id == id)
  462. return mxf_essence_mappings[i].index;
  463. return -1;
  464. }
  465. static void mxf_write_local_tags(AVIOContext *pb, const MXFLocalTagPair *local_tags, int count)
  466. {
  467. int i;
  468. for (i = 0; i < count; i++) {
  469. avio_wb16(pb, local_tags[i].local_tag);
  470. avio_write(pb, local_tags[i].uid, 16);
  471. }
  472. }
  473. static void mxf_write_primer_pack(AVFormatContext *s)
  474. {
  475. MXFContext *mxf = s->priv_data;
  476. AVIOContext *pb = s->pb;
  477. int local_tag_number, i = 0;
  478. int avc_tags_count = 0;
  479. local_tag_number = FF_ARRAY_ELEMS(mxf_local_tag_batch);
  480. local_tag_number += mxf->store_user_comments * FF_ARRAY_ELEMS(mxf_user_comments_local_tag);
  481. for (i = 0; i < s->nb_streams; i++) {
  482. MXFStreamContext *sc = s->streams[i]->priv_data;
  483. if (s->streams[i]->codecpar->codec_id == AV_CODEC_ID_H264 && !sc->avc_intra) {
  484. avc_tags_count = FF_ARRAY_ELEMS(mxf_avc_subdescriptor_local_tags);
  485. local_tag_number += avc_tags_count;
  486. }
  487. }
  488. avio_write(pb, primer_pack_key, 16);
  489. klv_encode_ber_length(pb, local_tag_number * 18 + 8);
  490. avio_wb32(pb, local_tag_number); // local_tag num
  491. avio_wb32(pb, 18); // item size, always 18 according to the specs
  492. for (i = 0; i < FF_ARRAY_ELEMS(mxf_local_tag_batch); i++) {
  493. avio_wb16(pb, mxf_local_tag_batch[i].local_tag);
  494. avio_write(pb, mxf_local_tag_batch[i].uid, 16);
  495. }
  496. if (mxf->store_user_comments)
  497. for (i = 0; i < FF_ARRAY_ELEMS(mxf_user_comments_local_tag); i++) {
  498. avio_wb16(pb, mxf_user_comments_local_tag[i].local_tag);
  499. avio_write(pb, mxf_user_comments_local_tag[i].uid, 16);
  500. }
  501. if (avc_tags_count > 0)
  502. mxf_write_local_tags(pb, mxf_avc_subdescriptor_local_tags, avc_tags_count);
  503. }
  504. static void mxf_write_local_tag(AVIOContext *pb, int size, int tag)
  505. {
  506. avio_wb16(pb, tag);
  507. avio_wb16(pb, size);
  508. }
  509. static void mxf_write_metadata_key(AVIOContext *pb, unsigned int value)
  510. {
  511. avio_write(pb, header_metadata_key, 13);
  512. avio_wb24(pb, value);
  513. }
  514. static const MXFCodecUL *mxf_get_data_definition_ul(int type)
  515. {
  516. const MXFCodecUL *uls = ff_mxf_data_definition_uls;
  517. while (uls->uid[0]) {
  518. if (type == uls->id)
  519. break;
  520. uls++;
  521. }
  522. return uls;
  523. }
  524. //one EC -> one descriptor. N ECs -> MultipleDescriptor + N descriptors
  525. #define DESCRIPTOR_COUNT(essence_container_count) \
  526. (essence_container_count > 1 ? essence_container_count + 1 : essence_container_count)
  527. static void mxf_write_essence_container_refs(AVFormatContext *s)
  528. {
  529. MXFContext *c = s->priv_data;
  530. AVIOContext *pb = s->pb;
  531. int i;
  532. mxf_write_refs_count(pb, DESCRIPTOR_COUNT(c->essence_container_count));
  533. av_log(s,AV_LOG_DEBUG, "essence container count:%d\n", c->essence_container_count);
  534. for (i = 0; i < s->nb_streams; i++) {
  535. MXFStreamContext *sc = s->streams[i]->priv_data;
  536. // check first track of essence container type and only write it once
  537. if (sc->track_essence_element_key[15] != 0)
  538. continue;
  539. avio_write(pb, *sc->container_ul, 16);
  540. if (c->essence_container_count == 1)
  541. break;
  542. }
  543. if (c->essence_container_count > 1)
  544. avio_write(pb, multiple_desc_ul, 16);
  545. }
  546. static void mxf_write_preface(AVFormatContext *s)
  547. {
  548. MXFContext *mxf = s->priv_data;
  549. AVIOContext *pb = s->pb;
  550. mxf_write_metadata_key(pb, 0x012f00);
  551. PRINT_KEY(s, "preface key", pb->buf_ptr - 16);
  552. klv_encode_ber_length(pb, 138 + 16LL * DESCRIPTOR_COUNT(mxf->essence_container_count));
  553. // write preface set uid
  554. mxf_write_local_tag(pb, 16, 0x3C0A);
  555. mxf_write_uuid(pb, Preface, 0);
  556. PRINT_KEY(s, "preface uid", pb->buf_ptr - 16);
  557. // last modified date
  558. mxf_write_local_tag(pb, 8, 0x3B02);
  559. avio_wb64(pb, mxf->timestamp);
  560. // write version
  561. mxf_write_local_tag(pb, 2, 0x3B05);
  562. avio_wb16(pb, 259); // v1.3
  563. // Object Model Version
  564. mxf_write_local_tag(pb, 4, 0x3B07);
  565. avio_wb32(pb, 1);
  566. // write identification_refs
  567. mxf_write_local_tag(pb, 16 + 8, 0x3B06);
  568. mxf_write_refs_count(pb, 1);
  569. mxf_write_uuid(pb, Identification, 0);
  570. // write content_storage_refs
  571. mxf_write_local_tag(pb, 16, 0x3B03);
  572. mxf_write_uuid(pb, ContentStorage, 0);
  573. // operational pattern
  574. mxf_write_local_tag(pb, 16, 0x3B09);
  575. if (s->oformat == &ff_mxf_opatom_muxer)
  576. avio_write(pb, opatom_ul, 16);
  577. else
  578. avio_write(pb, op1a_ul, 16);
  579. // write essence_container_refs
  580. mxf_write_local_tag(pb, 8 + 16LL * DESCRIPTOR_COUNT(mxf->essence_container_count), 0x3B0A);
  581. mxf_write_essence_container_refs(s);
  582. // write dm_scheme_refs
  583. mxf_write_local_tag(pb, 8, 0x3B0B);
  584. avio_wb64(pb, 0);
  585. }
  586. /*
  587. * Returns the length of the UTF-16 string, in 16-bit characters, that would result
  588. * from decoding the utf-8 string.
  589. */
  590. static uint64_t mxf_utf16len(const char *utf8_str)
  591. {
  592. const uint8_t *q = utf8_str;
  593. uint64_t size = 0;
  594. while (*q) {
  595. uint32_t ch;
  596. GET_UTF8(ch, *q++, goto invalid;)
  597. if (ch < 0x10000)
  598. size++;
  599. else
  600. size += 2;
  601. continue;
  602. invalid:
  603. av_log(NULL, AV_LOG_ERROR, "Invalid UTF8 sequence in mxf_utf16len\n\n");
  604. }
  605. size += 1;
  606. return size;
  607. }
  608. /*
  609. * Returns the calculated length a local tag containing an utf-8 string as utf-16
  610. */
  611. static int mxf_utf16_local_tag_length(const char *utf8_str)
  612. {
  613. uint64_t size;
  614. if (!utf8_str)
  615. return 0;
  616. size = mxf_utf16len(utf8_str);
  617. if (size >= UINT16_MAX/2) {
  618. av_log(NULL, AV_LOG_ERROR, "utf16 local tag size %"PRIx64" invalid (too large), ignoring\n", size);
  619. return 0;
  620. }
  621. return 4 + size * 2;
  622. }
  623. /*
  624. * Write a local tag containing an utf-8 string as utf-16
  625. */
  626. static void mxf_write_local_tag_utf16(AVIOContext *pb, int tag, const char *value)
  627. {
  628. uint64_t size = mxf_utf16len(value);
  629. if (size >= UINT16_MAX/2) {
  630. av_log(NULL, AV_LOG_ERROR, "utf16 local tag size %"PRIx64" invalid (too large), ignoring\n", size);
  631. return;
  632. }
  633. mxf_write_local_tag(pb, size*2, tag);
  634. avio_put_str16be(pb, value);
  635. }
  636. static void store_version(AVFormatContext *s){
  637. AVIOContext *pb = s->pb;
  638. if (s->flags & AVFMT_FLAG_BITEXACT) {
  639. avio_wb16(pb, 0); // major
  640. avio_wb16(pb, 0); // minor
  641. avio_wb16(pb, 0); // tertiary
  642. } else {
  643. avio_wb16(pb, LIBAVFORMAT_VERSION_MAJOR); // major
  644. avio_wb16(pb, LIBAVFORMAT_VERSION_MINOR); // minor
  645. avio_wb16(pb, LIBAVFORMAT_VERSION_MICRO); // tertiary
  646. }
  647. avio_wb16(pb, 0); // patch
  648. avio_wb16(pb, 0); // release
  649. }
  650. static void mxf_write_identification(AVFormatContext *s)
  651. {
  652. MXFContext *mxf = s->priv_data;
  653. AVIOContext *pb = s->pb;
  654. const char *company = "FFmpeg";
  655. const char *product = s->oformat != &ff_mxf_opatom_muxer ? "OP1a Muxer" : "OPAtom Muxer";
  656. const char *version;
  657. int length;
  658. mxf_write_metadata_key(pb, 0x013000);
  659. PRINT_KEY(s, "identification key", pb->buf_ptr - 16);
  660. version = s->flags & AVFMT_FLAG_BITEXACT ?
  661. "0.0.0" : AV_STRINGIFY(LIBAVFORMAT_VERSION);
  662. length = 100 +mxf_utf16_local_tag_length(company) +
  663. mxf_utf16_local_tag_length(product) +
  664. mxf_utf16_local_tag_length(version);
  665. klv_encode_ber_length(pb, length);
  666. // write uid
  667. mxf_write_local_tag(pb, 16, 0x3C0A);
  668. mxf_write_uuid(pb, Identification, 0);
  669. PRINT_KEY(s, "identification uid", pb->buf_ptr - 16);
  670. // write generation uid
  671. mxf_write_local_tag(pb, 16, 0x3C09);
  672. mxf_write_uuid(pb, Identification, 1);
  673. mxf_write_local_tag_utf16(pb, 0x3C01, company); // Company Name
  674. mxf_write_local_tag_utf16(pb, 0x3C02, product); // Product Name
  675. mxf_write_local_tag(pb, 10, 0x3C03); // Product Version
  676. store_version(s);
  677. mxf_write_local_tag_utf16(pb, 0x3C04, version); // Version String
  678. // write product uid
  679. mxf_write_local_tag(pb, 16, 0x3C05);
  680. mxf_write_uuid(pb, Identification, 2);
  681. // modification date
  682. mxf_write_local_tag(pb, 8, 0x3C06);
  683. avio_wb64(pb, mxf->timestamp);
  684. mxf_write_local_tag(pb, 10, 0x3C07); // Toolkit Version
  685. store_version(s);
  686. }
  687. static void mxf_write_content_storage(AVFormatContext *s, MXFPackage *packages, int package_count)
  688. {
  689. AVIOContext *pb = s->pb;
  690. int i;
  691. mxf_write_metadata_key(pb, 0x011800);
  692. PRINT_KEY(s, "content storage key", pb->buf_ptr - 16);
  693. klv_encode_ber_length(pb, 60 + (16 * package_count));
  694. // write uid
  695. mxf_write_local_tag(pb, 16, 0x3C0A);
  696. mxf_write_uuid(pb, ContentStorage, 0);
  697. PRINT_KEY(s, "content storage uid", pb->buf_ptr - 16);
  698. // write package reference
  699. mxf_write_local_tag(pb, 16 * package_count + 8, 0x1901);
  700. mxf_write_refs_count(pb, package_count);
  701. for (i = 0; i < package_count; i++) {
  702. mxf_write_uuid(pb, packages[i].type, packages[i].instance);
  703. }
  704. // write essence container data
  705. mxf_write_local_tag(pb, 8 + 16, 0x1902);
  706. mxf_write_refs_count(pb, 1);
  707. mxf_write_uuid(pb, EssenceContainerData, 0);
  708. }
  709. static void mxf_write_track(AVFormatContext *s, AVStream *st, MXFPackage *package)
  710. {
  711. MXFContext *mxf = s->priv_data;
  712. AVIOContext *pb = s->pb;
  713. MXFStreamContext *sc = st->priv_data;
  714. mxf_write_metadata_key(pb, 0x013b00);
  715. PRINT_KEY(s, "track key", pb->buf_ptr - 16);
  716. klv_encode_ber_length(pb, 80);
  717. // write track uid
  718. mxf_write_local_tag(pb, 16, 0x3C0A);
  719. mxf_write_uuid(pb, Track, mxf->track_instance_count);
  720. PRINT_KEY(s, "track uid", pb->buf_ptr - 16);
  721. // write track id
  722. mxf_write_local_tag(pb, 4, 0x4801);
  723. avio_wb32(pb, st->index+2);
  724. // write track number
  725. mxf_write_local_tag(pb, 4, 0x4804);
  726. if (package->type == MaterialPackage)
  727. avio_wb32(pb, 0); // track number of material package is 0
  728. else
  729. avio_write(pb, sc->track_essence_element_key + 12, 4);
  730. // write edit rate
  731. mxf_write_local_tag(pb, 8, 0x4B01);
  732. if (st == mxf->timecode_track && s->oformat == &ff_mxf_opatom_muxer) {
  733. avio_wb32(pb, mxf->tc.rate.num);
  734. avio_wb32(pb, mxf->tc.rate.den);
  735. } else {
  736. avio_wb32(pb, mxf->time_base.den);
  737. avio_wb32(pb, mxf->time_base.num);
  738. }
  739. // write origin
  740. mxf_write_local_tag(pb, 8, 0x4B02);
  741. avio_wb64(pb, 0);
  742. // write sequence refs
  743. mxf_write_local_tag(pb, 16, 0x4803);
  744. mxf_write_uuid(pb, Sequence, mxf->track_instance_count);
  745. }
  746. 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 };
  747. static void mxf_write_common_fields(AVFormatContext *s, AVStream *st)
  748. {
  749. MXFContext *mxf = s->priv_data;
  750. AVIOContext *pb = s->pb;
  751. // find data define uls
  752. mxf_write_local_tag(pb, 16, 0x0201);
  753. if (st == mxf->timecode_track)
  754. avio_write(pb, smpte_12m_timecode_track_data_ul, 16);
  755. else {
  756. const MXFCodecUL *data_def_ul = mxf_get_data_definition_ul(st->codecpar->codec_type);
  757. avio_write(pb, data_def_ul->uid, 16);
  758. }
  759. // write duration
  760. mxf_write_local_tag(pb, 8, 0x0202);
  761. if (st != mxf->timecode_track && s->oformat == &ff_mxf_opatom_muxer && st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  762. avio_wb64(pb, mxf->body_offset / mxf->edit_unit_byte_count);
  763. } else {
  764. avio_wb64(pb, mxf->duration);
  765. }
  766. }
  767. static void mxf_write_sequence(AVFormatContext *s, AVStream *st, MXFPackage *package)
  768. {
  769. MXFContext *mxf = s->priv_data;
  770. AVIOContext *pb = s->pb;
  771. enum MXFMetadataSetType component;
  772. mxf_write_metadata_key(pb, 0x010f00);
  773. PRINT_KEY(s, "sequence key", pb->buf_ptr - 16);
  774. klv_encode_ber_length(pb, 80);
  775. mxf_write_local_tag(pb, 16, 0x3C0A);
  776. mxf_write_uuid(pb, Sequence, mxf->track_instance_count);
  777. PRINT_KEY(s, "sequence uid", pb->buf_ptr - 16);
  778. mxf_write_common_fields(s, st);
  779. // write structural component
  780. mxf_write_local_tag(pb, 16 + 8, 0x1001);
  781. mxf_write_refs_count(pb, 1);
  782. if (st == mxf->timecode_track)
  783. component = TimecodeComponent;
  784. else
  785. component = SourceClip;
  786. mxf_write_uuid(pb, component, mxf->track_instance_count);
  787. }
  788. static void mxf_write_timecode_component(AVFormatContext *s, AVStream *st, MXFPackage *package)
  789. {
  790. MXFContext *mxf = s->priv_data;
  791. AVIOContext *pb = s->pb;
  792. mxf_write_metadata_key(pb, 0x011400);
  793. klv_encode_ber_length(pb, 75);
  794. // UID
  795. mxf_write_local_tag(pb, 16, 0x3C0A);
  796. mxf_write_uuid(pb, TimecodeComponent, mxf->track_instance_count);
  797. mxf_write_common_fields(s, st);
  798. // Start Time Code
  799. mxf_write_local_tag(pb, 8, 0x1501);
  800. avio_wb64(pb, mxf->tc.start);
  801. // Rounded Time Code Base
  802. mxf_write_local_tag(pb, 2, 0x1502);
  803. avio_wb16(pb, mxf->timecode_base);
  804. // Drop Frame
  805. mxf_write_local_tag(pb, 1, 0x1503);
  806. avio_w8(pb, !!(mxf->tc.flags & AV_TIMECODE_FLAG_DROPFRAME));
  807. }
  808. static void mxf_write_structural_component(AVFormatContext *s, AVStream *st, MXFPackage *package)
  809. {
  810. MXFContext *mxf = s->priv_data;
  811. AVIOContext *pb = s->pb;
  812. int i;
  813. mxf_write_metadata_key(pb, 0x011100);
  814. PRINT_KEY(s, "sturctural component key", pb->buf_ptr - 16);
  815. klv_encode_ber_length(pb, 108);
  816. // write uid
  817. mxf_write_local_tag(pb, 16, 0x3C0A);
  818. mxf_write_uuid(pb, SourceClip, mxf->track_instance_count);
  819. PRINT_KEY(s, "structural component uid", pb->buf_ptr - 16);
  820. mxf_write_common_fields(s, st);
  821. // write start_position
  822. mxf_write_local_tag(pb, 8, 0x1201);
  823. avio_wb64(pb, 0);
  824. // write source package uid, end of the reference
  825. mxf_write_local_tag(pb, 32, 0x1101);
  826. if (!package->ref) {
  827. for (i = 0; i < 4; i++)
  828. avio_wb64(pb, 0);
  829. } else
  830. mxf_write_umid(s, package->ref->instance);
  831. // write source track id
  832. mxf_write_local_tag(pb, 4, 0x1102);
  833. if (package->type == SourcePackage && !package->ref)
  834. avio_wb32(pb, 0);
  835. else
  836. avio_wb32(pb, st->index+2);
  837. }
  838. static void mxf_write_tape_descriptor(AVFormatContext *s)
  839. {
  840. AVIOContext *pb = s->pb;
  841. mxf_write_metadata_key(pb, 0x012e00);
  842. PRINT_KEY(s, "tape descriptor key", pb->buf_ptr - 16);
  843. klv_encode_ber_length(pb, 20);
  844. mxf_write_local_tag(pb, 16, 0x3C0A);
  845. mxf_write_uuid(pb, TapeDescriptor, 0);
  846. PRINT_KEY(s, "tape_desc uid", pb->buf_ptr - 16);
  847. }
  848. static void mxf_write_multi_descriptor(AVFormatContext *s)
  849. {
  850. MXFContext *mxf = s->priv_data;
  851. AVIOContext *pb = s->pb;
  852. const uint8_t *ul;
  853. int i;
  854. mxf_write_metadata_key(pb, 0x014400);
  855. PRINT_KEY(s, "multiple descriptor key", pb->buf_ptr - 16);
  856. klv_encode_ber_length(pb, 64 + 16LL * s->nb_streams);
  857. mxf_write_local_tag(pb, 16, 0x3C0A);
  858. mxf_write_uuid(pb, MultipleDescriptor, 0);
  859. PRINT_KEY(s, "multi_desc uid", pb->buf_ptr - 16);
  860. // write sample rate
  861. mxf_write_local_tag(pb, 8, 0x3001);
  862. avio_wb32(pb, mxf->time_base.den);
  863. avio_wb32(pb, mxf->time_base.num);
  864. // write essence container ul
  865. mxf_write_local_tag(pb, 16, 0x3004);
  866. if (mxf->essence_container_count > 1)
  867. ul = multiple_desc_ul;
  868. else {
  869. MXFStreamContext *sc = s->streams[0]->priv_data;
  870. ul = *sc->container_ul;
  871. }
  872. avio_write(pb, ul, 16);
  873. // write sub descriptor refs
  874. mxf_write_local_tag(pb, s->nb_streams * 16 + 8, 0x3F01);
  875. mxf_write_refs_count(pb, s->nb_streams);
  876. for (i = 0; i < s->nb_streams; i++)
  877. mxf_write_uuid(pb, SubDescriptor, i);
  878. }
  879. static int64_t mxf_write_generic_desc(AVFormatContext *s, AVStream *st, const UID key)
  880. {
  881. MXFContext *mxf = s->priv_data;
  882. MXFStreamContext *sc = st->priv_data;
  883. AVIOContext *pb = s->pb;
  884. int64_t pos;
  885. avio_write(pb, key, 16);
  886. klv_encode_ber4_length(pb, 0);
  887. pos = avio_tell(pb);
  888. mxf_write_local_tag(pb, 16, 0x3C0A);
  889. mxf_write_uuid(pb, SubDescriptor, st->index);
  890. mxf_write_local_tag(pb, 4, 0x3006);
  891. avio_wb32(pb, st->index+2);
  892. mxf_write_local_tag(pb, 8, 0x3001);
  893. if (s->oformat == &ff_mxf_d10_muxer) {
  894. avio_wb32(pb, mxf->time_base.den);
  895. avio_wb32(pb, mxf->time_base.num);
  896. } else {
  897. if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16LE ||
  898. st->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE) {
  899. avio_wb32(pb, st->codecpar->sample_rate);
  900. avio_wb32(pb, 1);
  901. } else {
  902. avio_wb32(pb, mxf->time_base.den);
  903. avio_wb32(pb, mxf->time_base.num);
  904. }
  905. }
  906. mxf_write_local_tag(pb, 16, 0x3004);
  907. avio_write(pb, *sc->container_ul, 16);
  908. return pos;
  909. }
  910. static const UID mxf_s436m_anc_descriptor_key = { 0x06,0x0e,0x2b,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x5c,0x00 };
  911. static const UID mxf_mpegvideo_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x51,0x00 };
  912. static const UID mxf_wav_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x48,0x00 };
  913. static const UID mxf_aes3_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x47,0x00 };
  914. static const UID mxf_cdci_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x28,0x00 };
  915. static const UID mxf_generic_sound_descriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0D,0x01,0x01,0x01,0x01,0x01,0x42,0x00 };
  916. static const UID mxf_avc_subdescriptor_key = { 0x06,0x0E,0x2B,0x34,0x02,0x53,0x01,0x01,0x0d,0x01,0x01,0x01,0x01,0x01,0x6E,0x00 };
  917. static int get_trc(UID ul, enum AVColorTransferCharacteristic trc)
  918. {
  919. switch (trc){
  920. case AVCOL_TRC_GAMMA28 :
  921. case AVCOL_TRC_GAMMA22 :
  922. memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x01,0x00,0x00}), 16);
  923. return 0;
  924. case AVCOL_TRC_BT709 :
  925. case AVCOL_TRC_SMPTE170M :
  926. memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x02,0x00,0x00}), 16);
  927. return 0;
  928. case AVCOL_TRC_SMPTE240M :
  929. memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x01,0x01,0x01,0x03,0x00,0x00}), 16);
  930. return 0;
  931. case AVCOL_TRC_BT1361_ECG:
  932. memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x06,0x04,0x01,0x01,0x01,0x01,0x05,0x00,0x00}), 16);
  933. return 0;
  934. case AVCOL_TRC_LINEAR :
  935. memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x06,0x04,0x01,0x01,0x01,0x01,0x06,0x00,0x00}), 16);
  936. return 0;
  937. case AVCOL_TRC_SMPTE428 :
  938. memcpy(ul, ((UID){0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x08,0x04,0x01,0x01,0x01,0x01,0x07,0x00,0x00}), 16);
  939. return 0;
  940. default:
  941. return -1;
  942. }
  943. }
  944. static int64_t mxf_write_cdci_common(AVFormatContext *s, AVStream *st, const UID key)
  945. {
  946. MXFStreamContext *sc = st->priv_data;
  947. AVIOContext *pb = s->pb;
  948. int stored_width = 0;
  949. int stored_height = (st->codecpar->height+15)/16*16;
  950. int display_height;
  951. int f1, f2;
  952. UID transfer_ul = {0};
  953. int64_t pos = mxf_write_generic_desc(s, st, key);
  954. get_trc(transfer_ul, st->codecpar->color_trc);
  955. if (st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO) {
  956. if (st->codecpar->height == 1080)
  957. stored_width = 1920;
  958. else if (st->codecpar->height == 720)
  959. stored_width = 1280;
  960. }
  961. if (!stored_width)
  962. stored_width = (st->codecpar->width+15)/16*16;
  963. mxf_write_local_tag(pb, 4, 0x3203);
  964. avio_wb32(pb, stored_width);
  965. mxf_write_local_tag(pb, 4, 0x3202);
  966. avio_wb32(pb, stored_height>>sc->interlaced);
  967. if (s->oformat == &ff_mxf_d10_muxer) {
  968. //Stored F2 Offset
  969. mxf_write_local_tag(pb, 4, 0x3216);
  970. avio_wb32(pb, 0);
  971. //Image Start Offset
  972. mxf_write_local_tag(pb, 4, 0x3213);
  973. avio_wb32(pb, 0);
  974. //Image End Offset
  975. mxf_write_local_tag(pb, 4, 0x3214);
  976. avio_wb32(pb, 0);
  977. }
  978. //Sampled width
  979. mxf_write_local_tag(pb, 4, 0x3205);
  980. avio_wb32(pb, stored_width);
  981. //Samples height
  982. mxf_write_local_tag(pb, 4, 0x3204);
  983. avio_wb32(pb, st->codecpar->height>>sc->interlaced);
  984. //Sampled X Offset
  985. mxf_write_local_tag(pb, 4, 0x3206);
  986. avio_wb32(pb, 0);
  987. //Sampled Y Offset
  988. mxf_write_local_tag(pb, 4, 0x3207);
  989. avio_wb32(pb, 0);
  990. mxf_write_local_tag(pb, 4, 0x3209);
  991. avio_wb32(pb, stored_width);
  992. if (st->codecpar->height == 608) // PAL + VBI
  993. display_height = 576;
  994. else if (st->codecpar->height == 512) // NTSC + VBI
  995. display_height = 486;
  996. else
  997. display_height = st->codecpar->height;
  998. mxf_write_local_tag(pb, 4, 0x3208);
  999. avio_wb32(pb, display_height>>sc->interlaced);
  1000. // display X offset
  1001. mxf_write_local_tag(pb, 4, 0x320A);
  1002. avio_wb32(pb, 0);
  1003. // display Y offset
  1004. mxf_write_local_tag(pb, 4, 0x320B);
  1005. avio_wb32(pb, (st->codecpar->height - display_height)>>sc->interlaced);
  1006. if (sc->interlaced) {
  1007. //Display F2 Offset
  1008. mxf_write_local_tag(pb, 4, 0x3217);
  1009. avio_wb32(pb, -((st->codecpar->height - display_height)&1));
  1010. }
  1011. // component depth
  1012. mxf_write_local_tag(pb, 4, 0x3301);
  1013. avio_wb32(pb, sc->component_depth);
  1014. // horizontal subsampling
  1015. mxf_write_local_tag(pb, 4, 0x3302);
  1016. avio_wb32(pb, sc->h_chroma_sub_sample);
  1017. // vertical subsampling
  1018. mxf_write_local_tag(pb, 4, 0x3308);
  1019. avio_wb32(pb, sc->v_chroma_sub_sample);
  1020. // color siting
  1021. mxf_write_local_tag(pb, 1, 0x3303);
  1022. avio_w8(pb, sc->color_siting);
  1023. // Padding Bits
  1024. mxf_write_local_tag(pb, 2, 0x3307);
  1025. avio_wb16(pb, 0);
  1026. if (st->codecpar->color_range != AVCOL_RANGE_UNSPECIFIED) {
  1027. int black = 0,
  1028. white = (1<<sc->component_depth) - 1,
  1029. color = (1<<sc->component_depth) - 1;
  1030. if (st->codecpar->color_range == AVCOL_RANGE_MPEG) {
  1031. black = 1 << (sc->component_depth - 4);
  1032. white = 235 << (sc->component_depth - 8);
  1033. color = (14 << (sc->component_depth - 4)) + 1;
  1034. }
  1035. mxf_write_local_tag(pb, 4, 0x3304);
  1036. avio_wb32(pb, black);
  1037. mxf_write_local_tag(pb, 4, 0x3305);
  1038. avio_wb32(pb, white);
  1039. mxf_write_local_tag(pb, 4, 0x3306);
  1040. avio_wb32(pb, color);
  1041. }
  1042. if (sc->signal_standard) {
  1043. mxf_write_local_tag(pb, 1, 0x3215);
  1044. avio_w8(pb, sc->signal_standard);
  1045. }
  1046. // frame layout
  1047. mxf_write_local_tag(pb, 1, 0x320C);
  1048. avio_w8(pb, sc->interlaced);
  1049. // video line map
  1050. switch (st->codecpar->height) {
  1051. case 576: f1 = 23; f2 = st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO ? 335 : 336; break;
  1052. case 608: f1 = 7; f2 = 320; break;
  1053. case 480: f1 = 20; f2 = st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO ? 285 : 283; break;
  1054. case 512: f1 = 7; f2 = 270; break;
  1055. case 720: f1 = 26; f2 = 0; break; // progressive
  1056. case 1080: f1 = 21; f2 = 584; break;
  1057. default: f1 = 0; f2 = 0; break;
  1058. }
  1059. if (!sc->interlaced && f2) {
  1060. f2 = 0;
  1061. f1 *= 2;
  1062. }
  1063. mxf_write_local_tag(pb, 16, 0x320D);
  1064. avio_wb32(pb, 2);
  1065. avio_wb32(pb, 4);
  1066. avio_wb32(pb, f1);
  1067. avio_wb32(pb, f2);
  1068. mxf_write_local_tag(pb, 8, 0x320E);
  1069. avio_wb32(pb, sc->aspect_ratio.num);
  1070. avio_wb32(pb, sc->aspect_ratio.den);
  1071. //Transfer characteristic
  1072. if (transfer_ul[0]) {
  1073. mxf_write_local_tag(pb, 16, 0x3210);
  1074. avio_write(pb, transfer_ul, 16);
  1075. };
  1076. mxf_write_local_tag(pb, 16, 0x3201);
  1077. avio_write(pb, *sc->codec_ul, 16);
  1078. if (sc->interlaced && sc->field_dominance) {
  1079. mxf_write_local_tag(pb, 1, 0x3212);
  1080. avio_w8(pb, sc->field_dominance);
  1081. }
  1082. if (st->codecpar->codec_id == AV_CODEC_ID_H264 && !sc->avc_intra) {
  1083. // write avc sub descriptor ref
  1084. mxf_write_local_tag(pb, 8 + 16, 0x8100);
  1085. mxf_write_refs_count(pb, 1);
  1086. mxf_write_uuid(pb, AVCSubDescriptor, 0);
  1087. }
  1088. return pos;
  1089. }
  1090. static void mxf_update_klv_size(AVIOContext *pb, int64_t pos)
  1091. {
  1092. int64_t cur_pos = avio_tell(pb);
  1093. int size = cur_pos - pos;
  1094. avio_seek(pb, pos - 4, SEEK_SET);
  1095. klv_encode_ber4_length(pb, size);
  1096. avio_seek(pb, cur_pos, SEEK_SET);
  1097. }
  1098. static void mxf_write_avc_subdesc(AVFormatContext *s, AVStream *st)
  1099. {
  1100. AVIOContext *pb = s->pb;
  1101. int64_t pos;
  1102. avio_write(pb, mxf_avc_subdescriptor_key, 16);
  1103. klv_encode_ber4_length(pb, 0);
  1104. pos = avio_tell(pb);
  1105. mxf_write_local_tag(pb, 16, 0x3C0A);
  1106. mxf_write_uuid(pb, AVCSubDescriptor, 0);
  1107. mxf_write_local_tag(pb, 1, 0x8200);
  1108. avio_w8(pb, 0xFF); // AVC Decoding Delay, unknown
  1109. mxf_write_local_tag(pb, 1, 0x8201);
  1110. avio_w8(pb, st->codecpar->profile); // AVC Profile
  1111. mxf_write_local_tag(pb, 1, 0x8202);
  1112. avio_w8(pb, st->codecpar->level); // AVC Level
  1113. mxf_update_klv_size(s->pb, pos);
  1114. }
  1115. static void mxf_write_cdci_desc(AVFormatContext *s, AVStream *st)
  1116. {
  1117. int64_t pos = mxf_write_cdci_common(s, st, mxf_cdci_descriptor_key);
  1118. mxf_update_klv_size(s->pb, pos);
  1119. if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
  1120. mxf_write_avc_subdesc(s, st);
  1121. }
  1122. }
  1123. static void mxf_write_h264_desc(AVFormatContext *s, AVStream *st)
  1124. {
  1125. MXFStreamContext *sc = st->priv_data;
  1126. if (sc->avc_intra) {
  1127. mxf_write_mpegvideo_desc(s, st);
  1128. } else {
  1129. int64_t pos = mxf_write_cdci_common(s, st, mxf_cdci_descriptor_key);
  1130. mxf_update_klv_size(s->pb, pos);
  1131. mxf_write_avc_subdesc(s, st);
  1132. }
  1133. }
  1134. static void mxf_write_s436m_anc_desc(AVFormatContext *s, AVStream *st)
  1135. {
  1136. int64_t pos = mxf_write_generic_desc(s, st, mxf_s436m_anc_descriptor_key);
  1137. mxf_update_klv_size(s->pb, pos);
  1138. }
  1139. static void mxf_write_mpegvideo_desc(AVFormatContext *s, AVStream *st)
  1140. {
  1141. AVIOContext *pb = s->pb;
  1142. MXFStreamContext *sc = st->priv_data;
  1143. int profile_and_level = (st->codecpar->profile<<4) | st->codecpar->level;
  1144. int64_t pos = mxf_write_cdci_common(s, st, mxf_mpegvideo_descriptor_key);
  1145. if (st->codecpar->codec_id != AV_CODEC_ID_H264) {
  1146. // bit rate
  1147. mxf_write_local_tag(pb, 4, 0x8000);
  1148. avio_wb32(pb, sc->video_bit_rate);
  1149. // profile and level
  1150. mxf_write_local_tag(pb, 1, 0x8007);
  1151. if (!st->codecpar->profile)
  1152. profile_and_level |= 0x80; // escape bit
  1153. avio_w8(pb, profile_and_level);
  1154. // low delay
  1155. mxf_write_local_tag(pb, 1, 0x8003);
  1156. avio_w8(pb, sc->low_delay);
  1157. // closed gop
  1158. mxf_write_local_tag(pb, 1, 0x8004);
  1159. avio_w8(pb, sc->seq_closed_gop);
  1160. // max gop
  1161. mxf_write_local_tag(pb, 2, 0x8006);
  1162. avio_wb16(pb, sc->max_gop);
  1163. // b picture count
  1164. mxf_write_local_tag(pb, 2, 0x8008);
  1165. avio_wb16(pb, sc->b_picture_count);
  1166. }
  1167. mxf_update_klv_size(pb, pos);
  1168. }
  1169. static int64_t mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st, const UID key)
  1170. {
  1171. AVIOContext *pb = s->pb;
  1172. MXFContext *mxf = s->priv_data;
  1173. int show_warnings = !mxf->footer_partition_offset;
  1174. int64_t pos = mxf_write_generic_desc(s, st, key);
  1175. if (s->oformat == &ff_mxf_opatom_muxer) {
  1176. mxf_write_local_tag(pb, 8, 0x3002);
  1177. avio_wb64(pb, mxf->body_offset / mxf->edit_unit_byte_count);
  1178. }
  1179. // audio locked
  1180. mxf_write_local_tag(pb, 1, 0x3D02);
  1181. avio_w8(pb, 1);
  1182. // write audio sampling rate
  1183. mxf_write_local_tag(pb, 8, 0x3D03);
  1184. avio_wb32(pb, st->codecpar->sample_rate);
  1185. avio_wb32(pb, 1);
  1186. if (s->oformat == &ff_mxf_d10_muxer) {
  1187. mxf_write_local_tag(pb, 1, 0x3D04);
  1188. avio_w8(pb, 0);
  1189. }
  1190. mxf_write_local_tag(pb, 4, 0x3D07);
  1191. if (mxf->channel_count == -1) {
  1192. if (show_warnings && (s->oformat == &ff_mxf_d10_muxer) && (st->codecpar->channels != 4) && (st->codecpar->channels != 8))
  1193. av_log(s, AV_LOG_WARNING, "the number of audio channels shall be 4 or 8 : the output will not comply to MXF D-10 specs, use -d10_channelcount to fix this\n");
  1194. avio_wb32(pb, st->codecpar->channels);
  1195. } else if (s->oformat == &ff_mxf_d10_muxer) {
  1196. if (show_warnings && (mxf->channel_count < st->codecpar->channels))
  1197. av_log(s, AV_LOG_WARNING, "d10_channelcount < actual number of audio channels : some channels will be discarded\n");
  1198. if (show_warnings && (mxf->channel_count != 4) && (mxf->channel_count != 8))
  1199. av_log(s, AV_LOG_WARNING, "d10_channelcount shall be set to 4 or 8 : the output will not comply to MXF D-10 specs\n");
  1200. avio_wb32(pb, mxf->channel_count);
  1201. } else {
  1202. avio_wb32(pb, st->codecpar->channels);
  1203. }
  1204. mxf_write_local_tag(pb, 4, 0x3D01);
  1205. avio_wb32(pb, av_get_bits_per_sample(st->codecpar->codec_id));
  1206. return pos;
  1207. }
  1208. static int64_t mxf_write_wav_common(AVFormatContext *s, AVStream *st, const UID key)
  1209. {
  1210. AVIOContext *pb = s->pb;
  1211. int64_t pos = mxf_write_generic_sound_common(s, st, key);
  1212. mxf_write_local_tag(pb, 2, 0x3D0A);
  1213. avio_wb16(pb, st->codecpar->block_align);
  1214. // avg bytes per sec
  1215. mxf_write_local_tag(pb, 4, 0x3D09);
  1216. avio_wb32(pb, st->codecpar->block_align*st->codecpar->sample_rate);
  1217. return pos;
  1218. }
  1219. static void mxf_write_wav_desc(AVFormatContext *s, AVStream *st)
  1220. {
  1221. int64_t pos = mxf_write_wav_common(s, st, mxf_wav_descriptor_key);
  1222. mxf_update_klv_size(s->pb, pos);
  1223. }
  1224. static void mxf_write_aes3_desc(AVFormatContext *s, AVStream *st)
  1225. {
  1226. int64_t pos = mxf_write_wav_common(s, st, mxf_aes3_descriptor_key);
  1227. mxf_update_klv_size(s->pb, pos);
  1228. }
  1229. static void mxf_write_generic_sound_desc(AVFormatContext *s, AVStream *st)
  1230. {
  1231. int64_t pos = mxf_write_generic_sound_common(s, st, mxf_generic_sound_descriptor_key);
  1232. mxf_update_klv_size(s->pb, pos);
  1233. }
  1234. static const uint8_t mxf_indirect_value_utf16le[] = { 0x4c,0x00,0x02,0x10,0x01,0x00,0x00,0x00,0x00,0x06,0x0e,0x2b,0x34,0x01,0x04,0x01,0x01 };
  1235. static int mxf_write_tagged_value(AVFormatContext *s, const char* name, const char* value)
  1236. {
  1237. MXFContext *mxf = s->priv_data;
  1238. AVIOContext *pb = s->pb;
  1239. int name_size = mxf_utf16_local_tag_length(name);
  1240. int indirect_value_size = 13 + mxf_utf16_local_tag_length(value);
  1241. if (!name_size || indirect_value_size == 13)
  1242. return 1;
  1243. mxf_write_metadata_key(pb, 0x013f00);
  1244. klv_encode_ber_length(pb, 24 + name_size + indirect_value_size);
  1245. // write instance UID
  1246. mxf_write_local_tag(pb, 16, 0x3C0A);
  1247. mxf_write_uuid(pb, TaggedValue, mxf->tagged_value_count);
  1248. // write name
  1249. mxf_write_local_tag_utf16(pb, 0x5001, name); // Name
  1250. // write indirect value
  1251. mxf_write_local_tag(pb, indirect_value_size, 0x5003);
  1252. avio_write(pb, mxf_indirect_value_utf16le, 17);
  1253. avio_put_str16le(pb, value);
  1254. mxf->tagged_value_count++;
  1255. return 0;
  1256. }
  1257. static int mxf_write_user_comments(AVFormatContext *s, const AVDictionary *m)
  1258. {
  1259. MXFContext *mxf = s->priv_data;
  1260. AVDictionaryEntry *t = NULL;
  1261. int count = 0;
  1262. while ((t = av_dict_get(m, "comment_", t, AV_DICT_IGNORE_SUFFIX))) {
  1263. if (mxf->tagged_value_count >= UINT16_MAX) {
  1264. av_log(s, AV_LOG_ERROR, "too many tagged values, ignoring remaining\n");
  1265. return count;
  1266. }
  1267. if (mxf_write_tagged_value(s, t->key + 8, t->value) == 0)
  1268. count++;
  1269. }
  1270. return count;
  1271. }
  1272. static void mxf_write_package(AVFormatContext *s, MXFPackage *package)
  1273. {
  1274. MXFContext *mxf = s->priv_data;
  1275. AVIOContext *pb = s->pb;
  1276. int i, track_count = s->nb_streams+1;
  1277. int name_size = mxf_utf16_local_tag_length(package->name);
  1278. int user_comment_count = 0;
  1279. if (package->type == MaterialPackage) {
  1280. if (mxf->store_user_comments)
  1281. user_comment_count = mxf_write_user_comments(s, s->metadata);
  1282. mxf_write_metadata_key(pb, 0x013600);
  1283. PRINT_KEY(s, "Material Package key", pb->buf_ptr - 16);
  1284. klv_encode_ber_length(pb, 92 + name_size + (16*track_count) + (16*user_comment_count) + 12LL*mxf->store_user_comments);
  1285. } else {
  1286. mxf_write_metadata_key(pb, 0x013700);
  1287. PRINT_KEY(s, "Source Package key", pb->buf_ptr - 16);
  1288. klv_encode_ber_length(pb, 112 + name_size + (16*track_count) + 12LL*mxf->store_user_comments); // 20 bytes length for descriptor reference
  1289. }
  1290. // write uid
  1291. mxf_write_local_tag(pb, 16, 0x3C0A);
  1292. mxf_write_uuid(pb, package->type, package->instance);
  1293. av_log(s, AV_LOG_DEBUG, "package type:%d\n", package->type);
  1294. PRINT_KEY(s, "package uid", pb->buf_ptr - 16);
  1295. // write package umid
  1296. mxf_write_local_tag(pb, 32, 0x4401);
  1297. mxf_write_umid(s, package->instance);
  1298. PRINT_KEY(s, "package umid second part", pb->buf_ptr - 16);
  1299. // package name
  1300. if (name_size)
  1301. mxf_write_local_tag_utf16(pb, 0x4402, package->name);
  1302. // package creation date
  1303. mxf_write_local_tag(pb, 8, 0x4405);
  1304. avio_wb64(pb, mxf->timestamp);
  1305. // package modified date
  1306. mxf_write_local_tag(pb, 8, 0x4404);
  1307. avio_wb64(pb, mxf->timestamp);
  1308. // write track refs
  1309. mxf_write_local_tag(pb, track_count*16 + 8, 0x4403);
  1310. mxf_write_refs_count(pb, track_count);
  1311. // these are the uuids of the tracks the will be written in mxf_write_track
  1312. for (i = 0; i < track_count; i++)
  1313. mxf_write_uuid(pb, Track, mxf->track_instance_count + i);
  1314. // write user comment refs
  1315. if (mxf->store_user_comments) {
  1316. mxf_write_local_tag(pb, user_comment_count*16 + 8, 0x4406);
  1317. mxf_write_refs_count(pb, user_comment_count);
  1318. for (i = 0; i < user_comment_count; i++)
  1319. mxf_write_uuid(pb, TaggedValue, mxf->tagged_value_count - user_comment_count + i);
  1320. }
  1321. // write multiple descriptor reference
  1322. if (package->type == SourcePackage && package->instance == 1) {
  1323. mxf_write_local_tag(pb, 16, 0x4701);
  1324. if (s->nb_streams > 1) {
  1325. mxf_write_uuid(pb, MultipleDescriptor, 0);
  1326. mxf_write_multi_descriptor(s);
  1327. } else
  1328. mxf_write_uuid(pb, SubDescriptor, 0);
  1329. } else if (package->type == SourcePackage && package->instance == 2) {
  1330. mxf_write_local_tag(pb, 16, 0x4701);
  1331. mxf_write_uuid(pb, TapeDescriptor, 0);
  1332. mxf_write_tape_descriptor(s);
  1333. }
  1334. /*
  1335. * for every 1 track in a package there is 1 sequence and 1 component.
  1336. * all 3 of these elements share the same instance number for generating
  1337. * there instance uuids. mxf->track_instance_count stores this value.
  1338. * mxf->track_instance_count is incremented after a group of all 3 of
  1339. * these elements are written.
  1340. */
  1341. // write timecode track
  1342. mxf_write_track(s, mxf->timecode_track, package);
  1343. mxf_write_sequence(s, mxf->timecode_track, package);
  1344. mxf_write_timecode_component(s, mxf->timecode_track, package);
  1345. mxf->track_instance_count++;
  1346. for (i = 0; i < s->nb_streams; i++) {
  1347. AVStream *st = s->streams[i];
  1348. mxf_write_track(s, st, package);
  1349. mxf_write_sequence(s, st, package);
  1350. mxf_write_structural_component(s, st, package);
  1351. mxf->track_instance_count++;
  1352. if (package->type == SourcePackage && package->instance == 1) {
  1353. MXFStreamContext *sc = st->priv_data;
  1354. mxf_essence_container_uls[sc->index].write_desc(s, st);
  1355. }
  1356. }
  1357. }
  1358. static int mxf_write_essence_container_data(AVFormatContext *s)
  1359. {
  1360. AVIOContext *pb = s->pb;
  1361. mxf_write_metadata_key(pb, 0x012300);
  1362. klv_encode_ber_length(pb, 72);
  1363. mxf_write_local_tag(pb, 16, 0x3C0A); // Instance UID
  1364. mxf_write_uuid(pb, EssenceContainerData, 0);
  1365. mxf_write_local_tag(pb, 32, 0x2701); // Linked Package UID
  1366. mxf_write_umid(s, 1);
  1367. mxf_write_local_tag(pb, 4, 0x3F07); // BodySID
  1368. avio_wb32(pb, 1);
  1369. mxf_write_local_tag(pb, 4, 0x3F06); // IndexSID
  1370. avio_wb32(pb, 2);
  1371. return 0;
  1372. }
  1373. static int mxf_write_header_metadata_sets(AVFormatContext *s)
  1374. {
  1375. MXFContext *mxf = s->priv_data;
  1376. AVDictionaryEntry *entry = NULL;
  1377. AVStream *st = NULL;
  1378. int i;
  1379. MXFPackage packages[3] = {{0}};
  1380. int package_count = 2;
  1381. packages[0].type = MaterialPackage;
  1382. packages[1].type = SourcePackage;
  1383. packages[1].instance = 1;
  1384. packages[0].ref = &packages[1];
  1385. if (entry = av_dict_get(s->metadata, "material_package_name", NULL, 0))
  1386. packages[0].name = entry->value;
  1387. if (entry = av_dict_get(s->metadata, "file_package_name", NULL, 0)) {
  1388. packages[1].name = entry->value;
  1389. } else {
  1390. /* check if any of the streams contain a file_package_name */
  1391. for (i = 0; i < s->nb_streams; i++) {
  1392. st = s->streams[i];
  1393. if (entry = av_dict_get(st->metadata, "file_package_name", NULL, 0)) {
  1394. packages[1].name = entry->value;
  1395. break;
  1396. }
  1397. }
  1398. }
  1399. entry = av_dict_get(s->metadata, "reel_name", NULL, 0);
  1400. if (entry) {
  1401. packages[2].name = entry->value;
  1402. packages[2].type = SourcePackage;
  1403. packages[2].instance = 2;
  1404. packages[1].ref = &packages[2];
  1405. package_count = 3;
  1406. }
  1407. mxf_write_preface(s);
  1408. mxf_write_identification(s);
  1409. mxf_write_content_storage(s, packages, package_count);
  1410. mxf->track_instance_count = 0;
  1411. for (i = 0; i < package_count; i++)
  1412. mxf_write_package(s, &packages[i]);
  1413. mxf_write_essence_container_data(s);
  1414. return 0;
  1415. }
  1416. static unsigned klv_fill_size(uint64_t size)
  1417. {
  1418. unsigned pad = KAG_SIZE - (size & (KAG_SIZE-1));
  1419. if (pad < 20) // smallest fill item possible
  1420. return pad + KAG_SIZE;
  1421. else
  1422. return pad & (KAG_SIZE-1);
  1423. }
  1424. static void mxf_write_index_table_segment(AVFormatContext *s)
  1425. {
  1426. MXFContext *mxf = s->priv_data;
  1427. AVIOContext *pb = s->pb;
  1428. int i, j, temporal_reordering = 0;
  1429. int key_index = mxf->last_key_index;
  1430. int prev_non_b_picture = 0;
  1431. int audio_frame_size = 0;
  1432. int64_t pos;
  1433. av_log(s, AV_LOG_DEBUG, "edit units count %d\n", mxf->edit_units_count);
  1434. if (!mxf->edit_units_count && !mxf->edit_unit_byte_count)
  1435. return;
  1436. avio_write(pb, index_table_segment_key, 16);
  1437. klv_encode_ber4_length(pb, 0);
  1438. pos = avio_tell(pb);
  1439. // instance id
  1440. mxf_write_local_tag(pb, 16, 0x3C0A);
  1441. mxf_write_uuid(pb, IndexTableSegment, 0);
  1442. // index edit rate
  1443. mxf_write_local_tag(pb, 8, 0x3F0B);
  1444. avio_wb32(pb, mxf->time_base.den);
  1445. avio_wb32(pb, mxf->time_base.num);
  1446. // index start position
  1447. mxf_write_local_tag(pb, 8, 0x3F0C);
  1448. avio_wb64(pb, mxf->last_indexed_edit_unit);
  1449. // index duration
  1450. mxf_write_local_tag(pb, 8, 0x3F0D);
  1451. if (mxf->edit_unit_byte_count)
  1452. avio_wb64(pb, 0); // index table covers whole container
  1453. else
  1454. avio_wb64(pb, mxf->edit_units_count);
  1455. // edit unit byte count
  1456. mxf_write_local_tag(pb, 4, 0x3F05);
  1457. avio_wb32(pb, mxf->edit_unit_byte_count);
  1458. // index sid
  1459. mxf_write_local_tag(pb, 4, 0x3F06);
  1460. avio_wb32(pb, 2);
  1461. // body sid
  1462. mxf_write_local_tag(pb, 4, 0x3F07);
  1463. avio_wb32(pb, 1);
  1464. // real slice count - 1
  1465. mxf_write_local_tag(pb, 1, 0x3F08);
  1466. avio_w8(pb, !mxf->edit_unit_byte_count); // only one slice for CBR
  1467. // delta entry array
  1468. mxf_write_local_tag(pb, 8 + (s->nb_streams+1)*6, 0x3F09);
  1469. avio_wb32(pb, s->nb_streams+1); // num of entries
  1470. avio_wb32(pb, 6); // size of one entry
  1471. // write system item delta entry
  1472. avio_w8(pb, 0);
  1473. avio_w8(pb, 0); // slice entry
  1474. avio_wb32(pb, 0); // element delta
  1475. // write each stream delta entry
  1476. for (i = 0; i < s->nb_streams; i++) {
  1477. AVStream *st = s->streams[i];
  1478. MXFStreamContext *sc = st->priv_data;
  1479. avio_w8(pb, sc->temporal_reordering);
  1480. if (sc->temporal_reordering)
  1481. temporal_reordering = 1;
  1482. if (mxf->edit_unit_byte_count) {
  1483. avio_w8(pb, 0); // slice number
  1484. avio_wb32(pb, sc->slice_offset);
  1485. } else if (i == 0) { // video track
  1486. avio_w8(pb, 0); // slice number
  1487. avio_wb32(pb, KAG_SIZE); // system item size including klv fill
  1488. } else { // audio or data track
  1489. if (!audio_frame_size) {
  1490. audio_frame_size = sc->frame_size;
  1491. audio_frame_size += klv_fill_size(audio_frame_size);
  1492. }
  1493. avio_w8(pb, 1);
  1494. avio_wb32(pb, (i-1)*audio_frame_size); // element delta
  1495. }
  1496. }
  1497. if (!mxf->edit_unit_byte_count) {
  1498. MXFStreamContext *sc = s->streams[0]->priv_data;
  1499. mxf_write_local_tag(pb, 8 + mxf->edit_units_count*15, 0x3F0A);
  1500. avio_wb32(pb, mxf->edit_units_count); // num of entries
  1501. avio_wb32(pb, 15); // size of one entry
  1502. for (i = 0; i < mxf->edit_units_count; i++) {
  1503. int temporal_offset = 0;
  1504. if (!(mxf->index_entries[i].flags & 0x33)) { // I-frame
  1505. sc->max_gop = FFMAX(sc->max_gop, i - mxf->last_key_index);
  1506. mxf->last_key_index = key_index;
  1507. key_index = i;
  1508. }
  1509. if (temporal_reordering) {
  1510. int pic_num_in_gop = i - key_index;
  1511. if (pic_num_in_gop != mxf->index_entries[i].temporal_ref) {
  1512. for (j = key_index; j < mxf->edit_units_count; j++) {
  1513. if (pic_num_in_gop == mxf->index_entries[j].temporal_ref)
  1514. break;
  1515. }
  1516. if (j == mxf->edit_units_count)
  1517. av_log(s, AV_LOG_WARNING, "missing frames\n");
  1518. temporal_offset = j - key_index - pic_num_in_gop;
  1519. }
  1520. }
  1521. avio_w8(pb, temporal_offset);
  1522. if ((mxf->index_entries[i].flags & 0x30) == 0x30) { // back and forward prediction
  1523. sc->b_picture_count = FFMAX(sc->b_picture_count, i - prev_non_b_picture);
  1524. avio_w8(pb, mxf->last_key_index - i);
  1525. } else {
  1526. avio_w8(pb, key_index - i); // key frame offset
  1527. if ((mxf->index_entries[i].flags & 0x20) == 0x20) // only forward
  1528. mxf->last_key_index = key_index;
  1529. prev_non_b_picture = i;
  1530. }
  1531. if (!(mxf->index_entries[i].flags & 0x33) && // I-frame
  1532. mxf->index_entries[i].flags & 0x40 && !temporal_offset)
  1533. mxf->index_entries[i].flags |= 0x80; // random access
  1534. avio_w8(pb, mxf->index_entries[i].flags);
  1535. // stream offset
  1536. avio_wb64(pb, mxf->index_entries[i].offset);
  1537. if (s->nb_streams > 1)
  1538. avio_wb32(pb, mxf->index_entries[i].slice_offset);
  1539. else
  1540. avio_wb32(pb, 0);
  1541. }
  1542. mxf->last_key_index = key_index - mxf->edit_units_count;
  1543. mxf->last_indexed_edit_unit += mxf->edit_units_count;
  1544. mxf->edit_units_count = 0;
  1545. }
  1546. mxf_update_klv_size(pb, pos);
  1547. }
  1548. static void mxf_write_klv_fill(AVFormatContext *s)
  1549. {
  1550. unsigned pad = klv_fill_size(avio_tell(s->pb));
  1551. if (pad) {
  1552. avio_write(s->pb, klv_fill_key, 16);
  1553. pad -= 16 + 4;
  1554. klv_encode_ber4_length(s->pb, pad);
  1555. ffio_fill(s->pb, 0, pad);
  1556. av_assert1(!(avio_tell(s->pb) & (KAG_SIZE-1)));
  1557. }
  1558. }
  1559. static int mxf_write_partition(AVFormatContext *s, int bodysid,
  1560. int indexsid,
  1561. const uint8_t *key, int write_metadata)
  1562. {
  1563. MXFContext *mxf = s->priv_data;
  1564. AVIOContext *pb = s->pb;
  1565. int64_t header_byte_count_offset;
  1566. unsigned index_byte_count = 0;
  1567. uint64_t partition_offset = avio_tell(pb);
  1568. int err;
  1569. if (!mxf->edit_unit_byte_count && mxf->edit_units_count)
  1570. index_byte_count = 85 + 12+(s->nb_streams+1)*6 +
  1571. 12+mxf->edit_units_count*15;
  1572. else if (mxf->edit_unit_byte_count && indexsid)
  1573. index_byte_count = 80;
  1574. if (index_byte_count) {
  1575. index_byte_count += 16 + 4; // add encoded ber4 length
  1576. index_byte_count += klv_fill_size(index_byte_count);
  1577. }
  1578. if (key && !memcmp(key, body_partition_key, 16)) {
  1579. if ((err = av_reallocp_array(&mxf->body_partition_offset, mxf->body_partitions_count + 1,
  1580. sizeof(*mxf->body_partition_offset))) < 0) {
  1581. mxf->body_partitions_count = 0;
  1582. return err;
  1583. }
  1584. mxf->body_partition_offset[mxf->body_partitions_count++] = partition_offset;
  1585. }
  1586. // write klv
  1587. if (key)
  1588. avio_write(pb, key, 16);
  1589. else
  1590. avio_write(pb, body_partition_key, 16);
  1591. klv_encode_ber4_length(pb, 88 + 16LL * DESCRIPTOR_COUNT(mxf->essence_container_count));
  1592. // write partition value
  1593. avio_wb16(pb, 1); // majorVersion
  1594. avio_wb16(pb, 3); // minorVersion
  1595. avio_wb32(pb, KAG_SIZE); // KAGSize
  1596. avio_wb64(pb, partition_offset); // ThisPartition
  1597. if (key && !memcmp(key, body_partition_key, 16) && mxf->body_partitions_count > 1)
  1598. avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-2]); // PreviousPartition
  1599. else if (key && !memcmp(key, footer_partition_key, 16) && mxf->body_partitions_count)
  1600. avio_wb64(pb, mxf->body_partition_offset[mxf->body_partitions_count-1]); // PreviousPartition
  1601. else
  1602. avio_wb64(pb, 0);
  1603. avio_wb64(pb, mxf->footer_partition_offset); // footerPartition
  1604. // set offset
  1605. header_byte_count_offset = avio_tell(pb);
  1606. avio_wb64(pb, 0); // headerByteCount, update later
  1607. // indexTable
  1608. avio_wb64(pb, index_byte_count); // indexByteCount
  1609. avio_wb32(pb, index_byte_count ? indexsid : 0); // indexSID
  1610. // BodyOffset
  1611. if (bodysid && mxf->edit_units_count && mxf->body_partitions_count && s->oformat != &ff_mxf_opatom_muxer)
  1612. avio_wb64(pb, mxf->body_offset);
  1613. else
  1614. avio_wb64(pb, 0);
  1615. avio_wb32(pb, bodysid); // bodySID
  1616. // operational pattern
  1617. if (s->oformat == &ff_mxf_opatom_muxer)
  1618. avio_write(pb, opatom_ul, 16);
  1619. else
  1620. avio_write(pb, op1a_ul, 16);
  1621. // essence container
  1622. mxf_write_essence_container_refs(s);
  1623. if (write_metadata) {
  1624. // mark the start of the headermetadata and calculate metadata size
  1625. int64_t pos, start;
  1626. unsigned header_byte_count;
  1627. mxf_write_klv_fill(s);
  1628. start = avio_tell(s->pb);
  1629. mxf_write_primer_pack(s);
  1630. mxf_write_klv_fill(s);
  1631. mxf_write_header_metadata_sets(s);
  1632. pos = avio_tell(s->pb);
  1633. header_byte_count = pos - start + klv_fill_size(pos);
  1634. // update header_byte_count
  1635. avio_seek(pb, header_byte_count_offset, SEEK_SET);
  1636. avio_wb64(pb, header_byte_count);
  1637. avio_seek(pb, pos, SEEK_SET);
  1638. }
  1639. if(key)
  1640. avio_write_marker(pb, AV_NOPTS_VALUE, AVIO_DATA_MARKER_FLUSH_POINT);
  1641. return 0;
  1642. }
  1643. static const struct {
  1644. int profile;
  1645. UID codec_ul;
  1646. } mxf_prores_codec_uls[] = {
  1647. { FF_PROFILE_PRORES_PROXY, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x01,0x00 } },
  1648. { FF_PROFILE_PRORES_LT, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x02,0x00 } },
  1649. { FF_PROFILE_PRORES_STANDARD, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x03,0x00 } },
  1650. { FF_PROFILE_PRORES_HQ, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x04,0x00 } },
  1651. { FF_PROFILE_PRORES_4444, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x05,0x00 } },
  1652. { FF_PROFILE_PRORES_XQ, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x03,0x06,0x06,0x00 } },
  1653. };
  1654. static int mxf_parse_prores_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
  1655. {
  1656. MXFContext *mxf = s->priv_data;
  1657. MXFStreamContext *sc = st->priv_data;
  1658. int i, profile;
  1659. if (mxf->header_written)
  1660. return 1;
  1661. sc->codec_ul = NULL;
  1662. profile = st->codecpar->profile;
  1663. for (i = 0; i < FF_ARRAY_ELEMS(mxf_prores_codec_uls); i++) {
  1664. if (profile == mxf_prores_codec_uls[i].profile) {
  1665. sc->codec_ul = &mxf_prores_codec_uls[i].codec_ul;
  1666. break;
  1667. }
  1668. }
  1669. if (!sc->codec_ul)
  1670. return 0;
  1671. sc->frame_size = pkt->size;
  1672. return 1;
  1673. }
  1674. static const struct {
  1675. int cid;
  1676. UID codec_ul;
  1677. } mxf_dnxhd_codec_uls[] = {
  1678. { 1235, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x01,0x00,0x00 } }, // 1080p 10bit HIGH
  1679. { 1237, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x03,0x00,0x00 } }, // 1080p 8bit MED
  1680. { 1238, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x04,0x00,0x00 } }, // 1080p 8bit HIGH
  1681. { 1241, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x07,0x00,0x00 } }, // 1080i 10bit HIGH
  1682. { 1242, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x08,0x00,0x00 } }, // 1080i 8bit MED
  1683. { 1243, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x09,0x00,0x00 } }, // 1080i 8bit HIGH
  1684. { 1244, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x0a,0x00,0x00 } }, // 1080i 8bit TR
  1685. { 1250, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x10,0x00,0x00 } }, // 720p 10bit
  1686. { 1251, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x11,0x00,0x00 } }, // 720p 8bit HIGH
  1687. { 1252, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x12,0x00,0x00 } }, // 720p 8bit MED
  1688. { 1253, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x13,0x00,0x00 } }, // 720p 8bit LOW
  1689. { 1256, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x16,0x00,0x00 } }, // 1080p 10bit 444
  1690. { 1258, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x18,0x00,0x00 } }, // 720p 8bit TR
  1691. { 1259, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x19,0x00,0x00 } }, // 1080p 8bit TR
  1692. { 1260, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x1a,0x00,0x00 } }, // 1080i 8bit TR MBAFF
  1693. { 1270, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x24,0x00,0x00 } }, // DNXHR 444
  1694. { 1271, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x25,0x00,0x00 } }, // DNXHR HQX
  1695. { 1272, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x26,0x00,0x00 } }, // DNXHR HQ
  1696. { 1273, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x27,0x00,0x00 } }, // DNXHR SQ
  1697. { 1274, { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x71,0x28,0x00,0x00 } }, // DNXHR LB
  1698. };
  1699. static int mxf_parse_dnxhd_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
  1700. {
  1701. MXFContext *mxf = s->priv_data;
  1702. MXFStreamContext *sc = st->priv_data;
  1703. int i, cid, frame_size = 0;
  1704. if (mxf->header_written)
  1705. return 1;
  1706. if (pkt->size < 43)
  1707. return 0;
  1708. sc->codec_ul = NULL;
  1709. cid = AV_RB32(pkt->data + 0x28);
  1710. for (i = 0; i < FF_ARRAY_ELEMS(mxf_dnxhd_codec_uls); i++) {
  1711. if (cid == mxf_dnxhd_codec_uls[i].cid) {
  1712. sc->codec_ul = &mxf_dnxhd_codec_uls[i].codec_ul;
  1713. break;
  1714. }
  1715. }
  1716. if (!sc->codec_ul)
  1717. return 0;
  1718. sc->component_depth = 0;
  1719. switch (pkt->data[0x21] >> 5) {
  1720. case 1: sc->component_depth = 8; break;
  1721. case 2: sc->component_depth = 10; break;
  1722. case 3: sc->component_depth = 12; break;
  1723. }
  1724. if (!sc->component_depth)
  1725. return 0;
  1726. if ((frame_size = avpriv_dnxhd_get_frame_size(cid)) == DNXHD_VARIABLE) {
  1727. frame_size = avpriv_dnxhd_get_hr_frame_size(cid, st->codecpar->width, st->codecpar->height);
  1728. }
  1729. if (frame_size < 0)
  1730. return 0;
  1731. if ((sc->interlaced = avpriv_dnxhd_get_interlaced(cid)) < 0)
  1732. return 0;
  1733. if (cid >= 1270) { // RI raster
  1734. av_reduce(&sc->aspect_ratio.num, &sc->aspect_ratio.den,
  1735. st->codecpar->width, st->codecpar->height,
  1736. INT_MAX);
  1737. } else {
  1738. sc->aspect_ratio = (AVRational){ 16, 9 };
  1739. }
  1740. sc->frame_size = pkt->size;
  1741. return 1;
  1742. }
  1743. static const struct {
  1744. const UID container_ul;
  1745. const UID codec_ul;
  1746. } mxf_dv_uls[] = {
  1747. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x01,0x01 }, // IEC DV25 525/60
  1748. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x01,0x00 } },
  1749. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x02,0x01 }, // IEC DV25 626/50
  1750. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x01,0x02,0x00 } },
  1751. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x40,0x01 }, // DV25 525/60
  1752. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x01,0x00 }, },
  1753. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x41,0x01 }, // DV25 625/50
  1754. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x02,0x00 }, },
  1755. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x50,0x01 }, // DV50 525/60
  1756. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x03,0x00 }, },
  1757. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x51,0x01 }, // DV50 625/50
  1758. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x04,0x00 }, },
  1759. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x60,0x01 }, // DV100 1080/60
  1760. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x05,0x00 }, },
  1761. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x61,0x01 }, // DV100 1080/50
  1762. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x06,0x00 }, },
  1763. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x62,0x01 }, // DV100 720/60
  1764. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x07,0x00 }, },
  1765. { { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x03,0x01,0x02,0x02,0x63,0x01 }, // DV100 720/50
  1766. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x04,0x01,0x02,0x02,0x02,0x02,0x08,0x00 }, },
  1767. };
  1768. static int mxf_parse_dv_frame(AVFormatContext *s, AVStream *st, AVPacket *pkt)
  1769. {
  1770. MXFContext *mxf = s->priv_data;
  1771. MXFStreamContext *sc = st->priv_data;
  1772. uint8_t *vs_pack, *vsc_pack;
  1773. int apt, ul_index, stype, pal;
  1774. if (mxf->header_written)
  1775. return 1;
  1776. // Check for minimal frame size
  1777. if (pkt->size < 120000)
  1778. return -1;
  1779. apt = pkt->data[4] & 0x7;
  1780. vs_pack = pkt->data + 80*5 + 48;
  1781. vsc_pack = pkt->data + 80*5 + 53;
  1782. stype = vs_pack[3] & 0x1f;
  1783. pal = (vs_pack[3] >> 5) & 0x1;
  1784. if ((vsc_pack[2] & 0x07) == 0x02) {
  1785. sc->aspect_ratio = (AVRational){ 16, 9 };
  1786. } else {
  1787. sc->aspect_ratio = (AVRational){ 4, 3 };
  1788. }
  1789. sc->interlaced = (vsc_pack[3] >> 4) & 0x01;
  1790. // TODO: fix dv encoder to set proper FF/FS value in VSC pack
  1791. // and set field dominance accordingly
  1792. // av_log(s, AV_LOG_DEBUG, "DV vsc pack ff/ss = %x\n", vsc_pack[2] >> 6);
  1793. switch (stype) {
  1794. case 0x18: // DV100 720p
  1795. ul_index = 8+pal;
  1796. if (sc->interlaced) {
  1797. av_log(s, AV_LOG_ERROR, "source marked as interlaced but codec profile is progressive\n");
  1798. sc->interlaced = 0;
  1799. }
  1800. break;
  1801. case 0x14: // DV100 1080i
  1802. ul_index = 6+pal;
  1803. break;
  1804. case 0x04: // DV50
  1805. ul_index = 4+pal;
  1806. break;
  1807. default: // DV25
  1808. if (!apt) { // IEC
  1809. ul_index = 0+pal;
  1810. } else {
  1811. ul_index = 2+pal;
  1812. }
  1813. }
  1814. sc->container_ul = &mxf_dv_uls[ul_index].container_ul;
  1815. sc->codec_ul = &mxf_dv_uls[ul_index].codec_ul;
  1816. sc->frame_size = pkt->size;
  1817. return 1;
  1818. }
  1819. static const struct {
  1820. UID uid;
  1821. int frame_size;
  1822. int profile;
  1823. uint8_t interlaced;
  1824. int intra_only; // 1 or 0 when there are separate UIDs for Long GOP and Intra, -1 when Intra/LGOP detection can be ignored
  1825. } mxf_h264_codec_uls[] = {
  1826. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x11,0x01 }, 0, 66, 0, -1 }, // AVC Baseline
  1827. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x20,0x01 }, 0, 77, 0, -1 }, // AVC Main
  1828. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x30,0x01 }, 0, 88, 0, -1 }, // AVC Extended
  1829. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x40,0x01 }, 0, 100, 0, -1 }, // AVC High
  1830. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x50,0x01 }, 0, 110, 0, 0 }, // AVC High 10
  1831. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x60,0x01 }, 0, 122, 0, 0 }, // AVC High 422
  1832. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x31,0x70,0x01 }, 0, 244, 0, 0 }, // AVC High 444
  1833. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x20,0x01 }, 0, 110, 0, 1 }, // AVC High 10 Intra
  1834. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x01 }, 232960, 110, 1, 1 }, // AVC High 10 Intra RP2027 Class 50 1080/59.94i
  1835. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x02 }, 281088, 110, 1, 1 }, // AVC High 10 Intra RP2027 Class 50 1080/50i
  1836. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x03 }, 232960, 110, 0, 1 }, // AVC High 10 Intra RP2027 Class 50 1080/29.97p
  1837. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x04 }, 281088, 110, 0, 1 }, // AVC High 10 Intra RP2027 Class 50 1080/25p
  1838. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x08 }, 116736, 110, 0, 1 }, // AVC High 10 Intra RP2027 Class 50 720/59.94p
  1839. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x21,0x09 }, 140800, 110, 0, 1 }, // AVC High 10 Intra RP2027 Class 50 720/50p
  1840. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x30,0x01 }, 0, 122, 0, 1 }, // AVC High 422 Intra
  1841. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x01 }, 472576, 122, 1, 1 }, // AVC High 422 Intra RP2027 Class 100 1080/59.94i
  1842. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x02 }, 568832, 122, 1, 1 }, // AVC High 422 Intra RP2027 Class 100 1080/50i
  1843. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x03 }, 472576, 122, 0, 1 }, // AVC High 422 Intra RP2027 Class 100 1080/29.97p
  1844. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x04 }, 568832, 122, 0, 1 }, // AVC High 422 Intra RP2027 Class 100 1080/25p
  1845. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x08 }, 236544, 122, 0, 1 }, // AVC High 422 Intra RP2027 Class 100 720/59.94p
  1846. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0a,0x04,0x01,0x02,0x02,0x01,0x32,0x31,0x09 }, 284672, 122, 0, 1 }, // AVC High 422 Intra RP2027 Class 100 720/50p
  1847. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x01,0x32,0x40,0x01 }, 0, 244, 0, 1 }, // AVC High 444 Intra
  1848. {{ 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x0d,0x04,0x01,0x02,0x02,0x01,0x32,0x50,0x01 }, 0, 44, 0, -1 }, // AVC CAVLC 444
  1849. };
  1850. static int mxf_parse_h264_frame(AVFormatContext *s, AVStream *st,
  1851. AVPacket *pkt, MXFIndexEntry *e)
  1852. {
  1853. MXFContext *mxf = s->priv_data;
  1854. MXFStreamContext *sc = st->priv_data;
  1855. H264SequenceParameterSet *sps = NULL;
  1856. GetBitContext gb;
  1857. const uint8_t *buf = pkt->data;
  1858. const uint8_t *buf_end = pkt->data + pkt->size;
  1859. const uint8_t *nal_end;
  1860. uint32_t state = -1;
  1861. int extra_size = 512; // support AVC Intra files without SPS/PPS header
  1862. int i, frame_size, slice_type, intra_only = 0;
  1863. for (;;) {
  1864. buf = avpriv_find_start_code(buf, buf_end, &state);
  1865. if (buf >= buf_end)
  1866. break;
  1867. switch (state & 0x1f) {
  1868. case H264_NAL_SPS:
  1869. e->flags |= 0x40;
  1870. if (mxf->header_written)
  1871. break;
  1872. nal_end = ff_avc_find_startcode(buf, buf_end);
  1873. sps = ff_avc_decode_sps(buf, nal_end - buf);
  1874. if (!sps) {
  1875. av_log(s, AV_LOG_ERROR, "error parsing sps\n");
  1876. return 0;
  1877. }
  1878. sc->aspect_ratio.num = st->codecpar->width * sps->sar.num;
  1879. sc->aspect_ratio.den = st->codecpar->height * sps->sar.den;
  1880. av_reduce(&sc->aspect_ratio.num, &sc->aspect_ratio.den,
  1881. sc->aspect_ratio.num, sc->aspect_ratio.den, 1024*1024);
  1882. intra_only = (sps->constraint_set_flags >> 3) & 1;
  1883. sc->interlaced = !sps->frame_mbs_only_flag;
  1884. sc->component_depth = sps->bit_depth_luma;
  1885. buf = nal_end;
  1886. break;
  1887. case H264_NAL_PPS:
  1888. if (e->flags & 0x40) { // sequence header present
  1889. e->flags |= 0x80; // random access
  1890. extra_size = 0;
  1891. }
  1892. break;
  1893. case H264_NAL_IDR_SLICE:
  1894. e->flags |= 0x04; // IDR Picture
  1895. buf = buf_end;
  1896. break;
  1897. case H264_NAL_SLICE:
  1898. init_get_bits8(&gb, buf, buf_end - buf);
  1899. get_ue_golomb_long(&gb); // skip first_mb_in_slice
  1900. slice_type = get_ue_golomb_31(&gb);
  1901. switch (slice_type % 5) {
  1902. case 0:
  1903. e->flags |= 0x20; // P Picture
  1904. e->flags |= 0x06; // P Picture
  1905. break;
  1906. case 1:
  1907. e->flags |= 0x30; // B Picture
  1908. e->flags |= 0x03; // non-referenced B Picture
  1909. break;
  1910. }
  1911. buf = buf_end;
  1912. break;
  1913. default:
  1914. break;
  1915. }
  1916. }
  1917. if (mxf->header_written)
  1918. return 1;
  1919. if (!sps)
  1920. sc->interlaced = st->codecpar->field_order != AV_FIELD_PROGRESSIVE ? 1 : 0;
  1921. sc->codec_ul = NULL;
  1922. frame_size = pkt->size + extra_size;
  1923. for (i = 0; i < FF_ARRAY_ELEMS(mxf_h264_codec_uls); i++) {
  1924. if (frame_size == mxf_h264_codec_uls[i].frame_size && sc->interlaced == mxf_h264_codec_uls[i].interlaced) {
  1925. sc->codec_ul = &mxf_h264_codec_uls[i].uid;
  1926. sc->component_depth = 10; // AVC Intra is always 10 Bit
  1927. sc->aspect_ratio = (AVRational){ 16, 9 }; // 16:9 is mandatory for broadcast HD
  1928. st->codecpar->profile = mxf_h264_codec_uls[i].profile;
  1929. sc->avc_intra = 1;
  1930. mxf->cbr_index = 1;
  1931. sc->frame_size = pkt->size;
  1932. if (sc->interlaced)
  1933. sc->field_dominance = 1; // top field first is mandatory for AVC Intra
  1934. break;
  1935. } else if (sps && mxf_h264_codec_uls[i].frame_size == 0 &&
  1936. mxf_h264_codec_uls[i].profile == sps->profile_idc &&
  1937. (mxf_h264_codec_uls[i].intra_only < 0 ||
  1938. mxf_h264_codec_uls[i].intra_only == intra_only)) {
  1939. sc->codec_ul = &mxf_h264_codec_uls[i].uid;
  1940. st->codecpar->profile = sps->profile_idc;
  1941. st->codecpar->level = sps->level_idc;
  1942. // continue to check for avc intra
  1943. }
  1944. }
  1945. av_free(sps);
  1946. if (!sc->codec_ul) {
  1947. av_log(s, AV_LOG_ERROR, "h264 profile not supported\n");
  1948. return 0;
  1949. }
  1950. return 1;
  1951. }
  1952. static const UID mxf_mpeg2_codec_uls[] = {
  1953. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x10,0x00 }, // MP-ML I-Frame
  1954. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x01,0x11,0x00 }, // MP-ML Long GOP
  1955. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x02,0x00 }, // 422P-ML I-Frame
  1956. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x02,0x03,0x00 }, // 422P-ML Long GOP
  1957. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x02,0x00 }, // MP-HL I-Frame
  1958. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x03,0x03,0x00 }, // MP-HL Long GOP
  1959. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x02,0x00 }, // 422P-HL I-Frame
  1960. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x04,0x03,0x00 }, // 422P-HL Long GOP
  1961. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x02,0x00 }, // MP@H-14 I-Frame
  1962. { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x03,0x04,0x01,0x02,0x02,0x01,0x05,0x03,0x00 }, // MP@H-14 Long GOP
  1963. };
  1964. static const UID *mxf_get_mpeg2_codec_ul(AVCodecParameters *par)
  1965. {
  1966. int long_gop = 1;
  1967. if (par->profile == 4) { // Main
  1968. if (par->level == 8) // Main
  1969. return &mxf_mpeg2_codec_uls[0+long_gop];
  1970. else if (par->level == 4) // High
  1971. return &mxf_mpeg2_codec_uls[4+long_gop];
  1972. else if (par->level == 6) // High 14
  1973. return &mxf_mpeg2_codec_uls[8+long_gop];
  1974. } else if (par->profile == 0) { // 422
  1975. if (par->level == 5) // Main
  1976. return &mxf_mpeg2_codec_uls[2+long_gop];
  1977. else if (par->level == 2) // High
  1978. return &mxf_mpeg2_codec_uls[6+long_gop];
  1979. }
  1980. return NULL;
  1981. }
  1982. static int mxf_parse_mpeg2_frame(AVFormatContext *s, AVStream *st,
  1983. AVPacket *pkt, MXFIndexEntry *e)
  1984. {
  1985. MXFStreamContext *sc = st->priv_data;
  1986. uint32_t c = -1;
  1987. int i;
  1988. for(i = 0; i < pkt->size - 4; i++) {
  1989. c = (c<<8) + pkt->data[i];
  1990. if (c == 0x1b5) {
  1991. if ((pkt->data[i+1] & 0xf0) == 0x10) { // seq ext
  1992. st->codecpar->profile = pkt->data[i+1] & 0x07;
  1993. st->codecpar->level = pkt->data[i+2] >> 4;
  1994. sc->low_delay = pkt->data[i+6] >> 7;
  1995. } else if (i + 5 < pkt->size && (pkt->data[i+1] & 0xf0) == 0x80) { // pict coding ext
  1996. sc->interlaced = !(pkt->data[i+5] & 0x80); // progressive frame
  1997. if (sc->interlaced)
  1998. sc->field_dominance = 1 + !(pkt->data[i+4] & 0x80); // top field first
  1999. break;
  2000. }
  2001. } else if (c == 0x1b8) { // gop
  2002. if (pkt->data[i+4]>>6 & 0x01) { // closed
  2003. if (sc->seq_closed_gop == -1)
  2004. sc->seq_closed_gop = 1;
  2005. sc->closed_gop = 1;
  2006. if (e->flags & 0x40) // sequence header present
  2007. e->flags |= 0x80; // random access
  2008. } else {
  2009. sc->seq_closed_gop = 0;
  2010. sc->closed_gop = 0;
  2011. }
  2012. } else if (c == 0x1b3) { // seq
  2013. e->flags |= 0x40;
  2014. switch ((pkt->data[i+4]>>4) & 0xf) {
  2015. case 2: sc->aspect_ratio = (AVRational){ 4, 3}; break;
  2016. case 3: sc->aspect_ratio = (AVRational){ 16, 9}; break;
  2017. case 4: sc->aspect_ratio = (AVRational){221,100}; break;
  2018. default:
  2019. av_reduce(&sc->aspect_ratio.num, &sc->aspect_ratio.den,
  2020. st->codecpar->width, st->codecpar->height, 1024*1024);
  2021. }
  2022. } else if (c == 0x100) { // pic
  2023. int pict_type = (pkt->data[i+2]>>3) & 0x07;
  2024. e->temporal_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
  2025. if (pict_type == 2) { // P-frame
  2026. e->flags |= 0x22;
  2027. sc->closed_gop = 0; // reset closed GOP, don't matter anymore
  2028. } else if (pict_type == 3) { // B-frame
  2029. if (sc->closed_gop)
  2030. e->flags |= 0x13; // only backward prediction
  2031. else
  2032. e->flags |= 0x33;
  2033. sc->temporal_reordering = -1;
  2034. } else if (!pict_type) {
  2035. av_log(s, AV_LOG_ERROR, "error parsing mpeg2 frame\n");
  2036. return 0;
  2037. }
  2038. }
  2039. }
  2040. if (s->oformat != &ff_mxf_d10_muxer)
  2041. sc->codec_ul = mxf_get_mpeg2_codec_ul(st->codecpar);
  2042. return !!sc->codec_ul;
  2043. }
  2044. static uint64_t mxf_parse_timestamp(int64_t timestamp64)
  2045. {
  2046. time_t timestamp = timestamp64 / 1000000;
  2047. struct tm tmbuf;
  2048. struct tm *time = gmtime_r(&timestamp, &tmbuf);
  2049. if (!time)
  2050. return 0;
  2051. return (uint64_t)(time->tm_year+1900) << 48 |
  2052. (uint64_t)(time->tm_mon+1) << 40 |
  2053. (uint64_t) time->tm_mday << 32 |
  2054. time->tm_hour << 24 |
  2055. time->tm_min << 16 |
  2056. time->tm_sec << 8 |
  2057. (timestamp64 % 1000000) / 4000;
  2058. }
  2059. static void mxf_gen_umid(AVFormatContext *s)
  2060. {
  2061. MXFContext *mxf = s->priv_data;
  2062. uint32_t seed = av_get_random_seed();
  2063. uint64_t umid = seed + 0x5294713400000000LL;
  2064. AV_WB64(mxf->umid , umid);
  2065. AV_WB64(mxf->umid+8, umid>>8);
  2066. mxf->instance_number = seed & 0xFFFFFF;
  2067. }
  2068. static int mxf_init_timecode(AVFormatContext *s, AVStream *st, AVRational rate)
  2069. {
  2070. MXFContext *mxf = s->priv_data;
  2071. AVDictionaryEntry *tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
  2072. if (!tcr)
  2073. tcr = av_dict_get(st->metadata, "timecode", NULL, 0);
  2074. if (tcr)
  2075. return av_timecode_init_from_string(&mxf->tc, rate, tcr->value, s);
  2076. else
  2077. return av_timecode_init(&mxf->tc, rate, 0, 0, s);
  2078. }
  2079. static int mxf_write_header(AVFormatContext *s)
  2080. {
  2081. MXFContext *mxf = s->priv_data;
  2082. int i, ret;
  2083. uint8_t present[FF_ARRAY_ELEMS(mxf_essence_container_uls)] = {0};
  2084. const MXFSamplesPerFrame *spf = NULL;
  2085. int64_t timestamp = 0;
  2086. if (!s->nb_streams)
  2087. return -1;
  2088. if (s->oformat == &ff_mxf_opatom_muxer && s->nb_streams !=1) {
  2089. av_log(s, AV_LOG_ERROR, "there must be exactly one stream for mxf opatom\n");
  2090. return -1;
  2091. }
  2092. if (!av_dict_get(s->metadata, "comment_", NULL, AV_DICT_IGNORE_SUFFIX))
  2093. mxf->store_user_comments = 0;
  2094. for (i = 0; i < s->nb_streams; i++) {
  2095. AVStream *st = s->streams[i];
  2096. MXFStreamContext *sc = av_mallocz(sizeof(*sc));
  2097. if (!sc)
  2098. return AVERROR(ENOMEM);
  2099. st->priv_data = sc;
  2100. sc->index = -1;
  2101. if (((i == 0) ^ (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)) && s->oformat != &ff_mxf_opatom_muxer) {
  2102. av_log(s, AV_LOG_ERROR, "there must be exactly one video stream and it must be the first one\n");
  2103. return -1;
  2104. }
  2105. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  2106. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(st->codecpar->format);
  2107. // TODO: should be avg_frame_rate
  2108. AVRational rate, tbc = st->time_base;
  2109. // Default component depth to 8
  2110. sc->component_depth = 8;
  2111. sc->h_chroma_sub_sample = 2;
  2112. sc->v_chroma_sub_sample = 2;
  2113. sc->color_siting = 0xFF;
  2114. if (st->codecpar->sample_aspect_ratio.num && st->codecpar->sample_aspect_ratio.den) {
  2115. sc->aspect_ratio = av_mul_q(st->codecpar->sample_aspect_ratio,
  2116. av_make_q(st->codecpar->width, st->codecpar->height));
  2117. }
  2118. if (pix_desc) {
  2119. sc->component_depth = pix_desc->comp[0].depth;
  2120. sc->h_chroma_sub_sample = 1 << pix_desc->log2_chroma_w;
  2121. sc->v_chroma_sub_sample = 1 << pix_desc->log2_chroma_h;
  2122. }
  2123. switch (ff_choose_chroma_location(s, st)) {
  2124. case AVCHROMA_LOC_TOPLEFT: sc->color_siting = 0; break;
  2125. case AVCHROMA_LOC_LEFT: sc->color_siting = 6; break;
  2126. case AVCHROMA_LOC_TOP: sc->color_siting = 1; break;
  2127. case AVCHROMA_LOC_CENTER: sc->color_siting = 3; break;
  2128. }
  2129. mxf->timecode_base = (tbc.den + tbc.num/2) / tbc.num;
  2130. spf = ff_mxf_get_samples_per_frame(s, tbc);
  2131. if (!spf) {
  2132. av_log(s, AV_LOG_ERROR, "Unsupported video frame rate %d/%d\n",
  2133. tbc.den, tbc.num);
  2134. return AVERROR(EINVAL);
  2135. }
  2136. mxf->content_package_rate = ff_mxf_get_content_package_rate(tbc);
  2137. mxf->time_base = spf->time_base;
  2138. rate = av_inv_q(mxf->time_base);
  2139. avpriv_set_pts_info(st, 64, mxf->time_base.num, mxf->time_base.den);
  2140. if((ret = mxf_init_timecode(s, st, rate)) < 0)
  2141. return ret;
  2142. if (st->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  2143. sc->seq_closed_gop = -1; // unknown yet
  2144. }
  2145. sc->video_bit_rate = st->codecpar->bit_rate;
  2146. if (s->oformat == &ff_mxf_d10_muxer ||
  2147. st->codecpar->codec_id == AV_CODEC_ID_DNXHD ||
  2148. st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO)
  2149. mxf->cbr_index = 1;
  2150. if (s->oformat == &ff_mxf_d10_muxer) {
  2151. int ntsc = mxf->time_base.den != 25;
  2152. int ul_index;
  2153. if (st->codecpar->codec_id != AV_CODEC_ID_MPEG2VIDEO) {
  2154. av_log(s, AV_LOG_ERROR, "error MXF D-10 only support MPEG-2 Video\n");
  2155. return AVERROR(EINVAL);
  2156. }
  2157. if ((sc->video_bit_rate == 50000000) && (mxf->time_base.den == 25)) {
  2158. ul_index = 0;
  2159. } else if ((sc->video_bit_rate == 49999840 || sc->video_bit_rate == 50000000) && ntsc) {
  2160. ul_index = 1;
  2161. } else if (sc->video_bit_rate == 40000000) {
  2162. ul_index = 2+ntsc;
  2163. } else if (sc->video_bit_rate == 30000000) {
  2164. ul_index = 4+ntsc;
  2165. } else {
  2166. av_log(s, AV_LOG_ERROR, "error MXF D-10 only support 30/40/50 mbit/s\n");
  2167. return -1;
  2168. }
  2169. sc->codec_ul = &mxf_d10_codec_uls[ul_index];
  2170. sc->container_ul = &mxf_d10_container_uls[ul_index];
  2171. sc->index = INDEX_D10_VIDEO;
  2172. sc->signal_standard = 1;
  2173. sc->color_siting = 0;
  2174. sc->frame_size = (int64_t)sc->video_bit_rate *
  2175. mxf->time_base.num / (8*mxf->time_base.den);
  2176. }
  2177. if (mxf->signal_standard >= 0)
  2178. sc->signal_standard = mxf->signal_standard;
  2179. } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  2180. if (st->codecpar->sample_rate != 48000) {
  2181. av_log(s, AV_LOG_ERROR, "only 48khz is implemented\n");
  2182. return -1;
  2183. }
  2184. avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
  2185. if (s->oformat == &ff_mxf_d10_muxer) {
  2186. if (st->index != 1) {
  2187. av_log(s, AV_LOG_ERROR, "MXF D-10 only support one audio track\n");
  2188. return -1;
  2189. }
  2190. if (st->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE &&
  2191. st->codecpar->codec_id != AV_CODEC_ID_PCM_S24LE) {
  2192. av_log(s, AV_LOG_ERROR, "MXF D-10 only support 16 or 24 bits le audio\n");
  2193. }
  2194. sc->index = INDEX_D10_AUDIO;
  2195. sc->container_ul = ((MXFStreamContext*)s->streams[0]->priv_data)->container_ul;
  2196. sc->frame_size = 4 + 8 * spf[0].samples_per_frame[0] * 4;
  2197. } else if (s->oformat == &ff_mxf_opatom_muxer) {
  2198. AVRational tbc = av_inv_q(mxf->audio_edit_rate);
  2199. if (st->codecpar->codec_id != AV_CODEC_ID_PCM_S16LE &&
  2200. st->codecpar->codec_id != AV_CODEC_ID_PCM_S24LE) {
  2201. av_log(s, AV_LOG_ERROR, "Only pcm_s16le and pcm_s24le audio codecs are implemented\n");
  2202. return AVERROR_PATCHWELCOME;
  2203. }
  2204. if (st->codecpar->channels != 1) {
  2205. av_log(s, AV_LOG_ERROR, "MXF OPAtom only supports single channel audio\n");
  2206. return AVERROR(EINVAL);
  2207. }
  2208. spf = ff_mxf_get_samples_per_frame(s, tbc);
  2209. if (!spf) {
  2210. av_log(s, AV_LOG_ERROR, "Unsupported timecode frame rate %d/%d\n", tbc.den, tbc.num);
  2211. return AVERROR(EINVAL);
  2212. }
  2213. mxf->time_base = st->time_base;
  2214. if((ret = mxf_init_timecode(s, st, av_inv_q(spf->time_base))) < 0)
  2215. return ret;
  2216. mxf->timecode_base = (tbc.den + tbc.num/2) / tbc.num;
  2217. mxf->edit_unit_byte_count = (av_get_bits_per_sample(st->codecpar->codec_id) * st->codecpar->channels) >> 3;
  2218. sc->index = INDEX_WAV;
  2219. } else {
  2220. mxf->slice_count = 1;
  2221. sc->frame_size = (st->codecpar->channels * spf[0].samples_per_frame[0] *
  2222. av_get_bits_per_sample(st->codecpar->codec_id)) / 8;
  2223. }
  2224. } else if (st->codecpar->codec_type == AVMEDIA_TYPE_DATA) {
  2225. AVDictionaryEntry *e = av_dict_get(st->metadata, "data_type", NULL, 0);
  2226. if (e && !strcmp(e->value, "vbi_vanc_smpte_436M")) {
  2227. sc->index = INDEX_S436M;
  2228. } else {
  2229. av_log(s, AV_LOG_ERROR, "track %d: unsupported data type\n", i);
  2230. return -1;
  2231. }
  2232. if (st->index != s->nb_streams - 1) {
  2233. av_log(s, AV_LOG_ERROR, "data track must be placed last\n");
  2234. return -1;
  2235. }
  2236. }
  2237. if (sc->index == -1) {
  2238. sc->index = mxf_get_essence_container_ul_index(st->codecpar->codec_id);
  2239. if (sc->index == -1) {
  2240. av_log(s, AV_LOG_ERROR, "track %d: could not find essence container ul, "
  2241. "codec not currently supported in container\n", i);
  2242. return -1;
  2243. }
  2244. }
  2245. if (!sc->codec_ul)
  2246. sc->codec_ul = &mxf_essence_container_uls[sc->index].codec_ul;
  2247. if (!sc->container_ul)
  2248. sc->container_ul = &mxf_essence_container_uls[sc->index].container_ul;
  2249. memcpy(sc->track_essence_element_key, mxf_essence_container_uls[sc->index].element_ul, 15);
  2250. sc->track_essence_element_key[15] = present[sc->index];
  2251. PRINT_KEY(s, "track essence element key", sc->track_essence_element_key);
  2252. if (!present[sc->index])
  2253. mxf->essence_container_count++;
  2254. present[sc->index]++;
  2255. }
  2256. if (s->oformat == &ff_mxf_d10_muxer || s->oformat == &ff_mxf_opatom_muxer) {
  2257. mxf->essence_container_count = 1;
  2258. }
  2259. if (!(s->flags & AVFMT_FLAG_BITEXACT))
  2260. mxf_gen_umid(s);
  2261. for (i = 0; i < s->nb_streams; i++) {
  2262. MXFStreamContext *sc = s->streams[i]->priv_data;
  2263. // update element count
  2264. sc->track_essence_element_key[13] = present[sc->index];
  2265. if (!memcmp(sc->track_essence_element_key, mxf_essence_container_uls[INDEX_DV].element_ul, 13)) // DV
  2266. sc->order = (0x15 << 24) | AV_RB32(sc->track_essence_element_key+13);
  2267. else
  2268. sc->order = AV_RB32(sc->track_essence_element_key+12);
  2269. }
  2270. if (ff_parse_creation_time_metadata(s, &timestamp, 0) > 0)
  2271. mxf->timestamp = mxf_parse_timestamp(timestamp);
  2272. mxf->duration = -1;
  2273. mxf->timecode_track = av_mallocz(sizeof(*mxf->timecode_track));
  2274. if (!mxf->timecode_track)
  2275. return AVERROR(ENOMEM);
  2276. mxf->timecode_track->priv_data = av_mallocz(sizeof(MXFStreamContext));
  2277. if (!mxf->timecode_track->priv_data)
  2278. return AVERROR(ENOMEM);
  2279. mxf->timecode_track->index = -1;
  2280. if (ff_audio_interleave_init(s, 0, av_inv_q(mxf->tc.rate)) < 0)
  2281. return -1;
  2282. return 0;
  2283. }
  2284. static const uint8_t system_metadata_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x03,0x01,0x04,0x01,0x01,0x00 };
  2285. 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 };
  2286. static void mxf_write_system_item(AVFormatContext *s)
  2287. {
  2288. MXFContext *mxf = s->priv_data;
  2289. AVIOContext *pb = s->pb;
  2290. unsigned frame;
  2291. uint32_t time_code;
  2292. int i, system_item_bitmap = 0x58; // UL, user date/time stamp, picture present
  2293. frame = mxf->last_indexed_edit_unit + mxf->edit_units_count;
  2294. // write system metadata pack
  2295. avio_write(pb, system_metadata_pack_key, 16);
  2296. klv_encode_ber4_length(pb, 57);
  2297. for (i = 0; i < s->nb_streams; i++) {
  2298. if (s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
  2299. system_item_bitmap |= 0x4;
  2300. else if (s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_DATA)
  2301. system_item_bitmap |= 0x2;
  2302. }
  2303. avio_w8(pb, system_item_bitmap);
  2304. avio_w8(pb, mxf->content_package_rate); // content package rate
  2305. avio_w8(pb, 0x00); // content package type
  2306. avio_wb16(pb, 0x00); // channel handle
  2307. avio_wb16(pb, frame & 0xFFFF); // continuity count, supposed to overflow
  2308. if (mxf->essence_container_count > 1)
  2309. avio_write(pb, multiple_desc_ul, 16);
  2310. else {
  2311. MXFStreamContext *sc = s->streams[0]->priv_data;
  2312. avio_write(pb, *sc->container_ul, 16);
  2313. }
  2314. avio_w8(pb, 0);
  2315. avio_wb64(pb, 0);
  2316. avio_wb64(pb, 0); // creation date/time stamp
  2317. avio_w8(pb, 0x81); // SMPTE 12M time code
  2318. time_code = av_timecode_get_smpte_from_framenum(&mxf->tc, frame);
  2319. avio_wb32(pb, time_code);
  2320. avio_wb32(pb, 0); // binary group data
  2321. avio_wb64(pb, 0);
  2322. // write system metadata package set
  2323. avio_write(pb, system_metadata_package_set_key, 16);
  2324. klv_encode_ber4_length(pb, 35);
  2325. avio_w8(pb, 0x83); // UMID
  2326. avio_wb16(pb, 0x20);
  2327. mxf_write_umid(s, 1);
  2328. }
  2329. static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
  2330. {
  2331. MXFContext *mxf = s->priv_data;
  2332. AVIOContext *pb = s->pb;
  2333. int frame_size = pkt->size / st->codecpar->block_align;
  2334. uint8_t *samples = pkt->data;
  2335. uint8_t *end = pkt->data + pkt->size;
  2336. int i;
  2337. klv_encode_ber4_length(pb, 4 + frame_size*4*8);
  2338. avio_w8(pb, (frame_size == 1920 ? 0 : (mxf->edit_units_count-1) % 5 + 1));
  2339. avio_wl16(pb, frame_size);
  2340. avio_w8(pb, (1<<st->codecpar->channels)-1);
  2341. while (samples < end) {
  2342. for (i = 0; i < st->codecpar->channels; i++) {
  2343. uint32_t sample;
  2344. if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE) {
  2345. sample = AV_RL24(samples)<< 4;
  2346. samples += 3;
  2347. } else {
  2348. sample = AV_RL16(samples)<<12;
  2349. samples += 2;
  2350. }
  2351. avio_wl32(pb, sample | i);
  2352. }
  2353. for (; i < 8; i++)
  2354. avio_wl32(pb, i);
  2355. }
  2356. }
  2357. static int mxf_write_opatom_body_partition(AVFormatContext *s)
  2358. {
  2359. MXFContext *mxf = s->priv_data;
  2360. AVIOContext *pb = s->pb;
  2361. AVStream *st = s->streams[0];
  2362. MXFStreamContext *sc = st->priv_data;
  2363. const uint8_t *key = NULL;
  2364. int err;
  2365. if (!mxf->header_written)
  2366. key = body_partition_key;
  2367. if ((err = mxf_write_partition(s, 1, 0, key, 0)) < 0)
  2368. return err;
  2369. mxf_write_klv_fill(s);
  2370. avio_write(pb, sc->track_essence_element_key, 16);
  2371. klv_encode_ber9_length(pb, mxf->body_offset);
  2372. return 0;
  2373. }
  2374. static int mxf_write_opatom_packet(AVFormatContext *s, AVPacket *pkt, MXFIndexEntry *ie)
  2375. {
  2376. MXFContext *mxf = s->priv_data;
  2377. AVIOContext *pb = s->pb;
  2378. int err;
  2379. if (!mxf->header_written) {
  2380. if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
  2381. return err;
  2382. mxf_write_klv_fill(s);
  2383. if ((err = mxf_write_opatom_body_partition(s)) < 0)
  2384. return err;
  2385. mxf->header_written = 1;
  2386. }
  2387. if (!mxf->edit_unit_byte_count) {
  2388. mxf->index_entries[mxf->edit_units_count].offset = mxf->body_offset;
  2389. mxf->index_entries[mxf->edit_units_count].flags = ie->flags;
  2390. mxf->index_entries[mxf->edit_units_count].temporal_ref = ie->temporal_ref;
  2391. }
  2392. mxf->edit_units_count++;
  2393. avio_write(pb, pkt->data, pkt->size);
  2394. mxf->body_offset += pkt->size;
  2395. return 0;
  2396. }
  2397. static void mxf_compute_edit_unit_byte_count(AVFormatContext *s)
  2398. {
  2399. MXFContext *mxf = s->priv_data;
  2400. int i;
  2401. if (s->oformat == &ff_mxf_opatom_muxer) {
  2402. MXFStreamContext *sc = s->streams[0]->priv_data;
  2403. mxf->edit_unit_byte_count = sc->frame_size;
  2404. return;
  2405. }
  2406. mxf->edit_unit_byte_count = KAG_SIZE; // system element
  2407. for (i = 0; i < s->nb_streams; i++) {
  2408. AVStream *st = s->streams[i];
  2409. MXFStreamContext *sc = st->priv_data;
  2410. sc->slice_offset = mxf->edit_unit_byte_count;
  2411. mxf->edit_unit_byte_count += 16 + 4 + sc->frame_size;
  2412. mxf->edit_unit_byte_count += klv_fill_size(mxf->edit_unit_byte_count);
  2413. }
  2414. }
  2415. static int mxf_write_packet(AVFormatContext *s, AVPacket *pkt)
  2416. {
  2417. MXFContext *mxf = s->priv_data;
  2418. AVIOContext *pb = s->pb;
  2419. AVStream *st = s->streams[pkt->stream_index];
  2420. MXFStreamContext *sc = st->priv_data;
  2421. MXFIndexEntry ie = {0};
  2422. int err;
  2423. if (!mxf->cbr_index && !mxf->edit_unit_byte_count && !(mxf->edit_units_count % EDIT_UNITS_PER_BODY)) {
  2424. if ((err = av_reallocp_array(&mxf->index_entries, mxf->edit_units_count
  2425. + EDIT_UNITS_PER_BODY, sizeof(*mxf->index_entries))) < 0) {
  2426. mxf->edit_units_count = 0;
  2427. av_log(s, AV_LOG_ERROR, "could not allocate index entries\n");
  2428. return err;
  2429. }
  2430. }
  2431. if (st->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
  2432. if (!mxf_parse_mpeg2_frame(s, st, pkt, &ie)) {
  2433. av_log(s, AV_LOG_ERROR, "could not get mpeg2 profile and level\n");
  2434. return -1;
  2435. }
  2436. } else if (st->codecpar->codec_id == AV_CODEC_ID_DNXHD) {
  2437. if (!mxf_parse_dnxhd_frame(s, st, pkt)) {
  2438. av_log(s, AV_LOG_ERROR, "could not get dnxhd profile\n");
  2439. return -1;
  2440. }
  2441. } else if (st->codecpar->codec_id == AV_CODEC_ID_PRORES) {
  2442. if (!mxf_parse_prores_frame(s, st, pkt)) {
  2443. av_log(s, AV_LOG_ERROR, "could not get prores profile\n");
  2444. return -1;
  2445. }
  2446. } else if (st->codecpar->codec_id == AV_CODEC_ID_DVVIDEO) {
  2447. if (!mxf_parse_dv_frame(s, st, pkt)) {
  2448. av_log(s, AV_LOG_ERROR, "could not get dv profile\n");
  2449. return -1;
  2450. }
  2451. } else if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
  2452. if (!mxf_parse_h264_frame(s, st, pkt, &ie)) {
  2453. av_log(s, AV_LOG_ERROR, "could not get h264 profile\n");
  2454. return -1;
  2455. }
  2456. }
  2457. if (mxf->cbr_index) {
  2458. if (pkt->size != sc->frame_size && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  2459. av_log(s, AV_LOG_ERROR, "track %d: frame size does not match index unit size, %d != %d\n",
  2460. st->index, pkt->size, sc->frame_size);
  2461. return -1;
  2462. }
  2463. if (!mxf->header_written)
  2464. mxf_compute_edit_unit_byte_count(s);
  2465. }
  2466. if (s->oformat == &ff_mxf_opatom_muxer)
  2467. return mxf_write_opatom_packet(s, pkt, &ie);
  2468. if (!mxf->header_written) {
  2469. if (mxf->edit_unit_byte_count) {
  2470. if ((err = mxf_write_partition(s, 1, 2, header_open_partition_key, 1)) < 0)
  2471. return err;
  2472. mxf_write_klv_fill(s);
  2473. mxf_write_index_table_segment(s);
  2474. } else {
  2475. if ((err = mxf_write_partition(s, 0, 0, header_open_partition_key, 1)) < 0)
  2476. return err;
  2477. }
  2478. mxf->header_written = 1;
  2479. }
  2480. if (st->index == 0) {
  2481. if (!mxf->edit_unit_byte_count &&
  2482. (!mxf->edit_units_count || mxf->edit_units_count > EDIT_UNITS_PER_BODY) &&
  2483. !(ie.flags & 0x33)) { // I-frame, GOP start
  2484. mxf_write_klv_fill(s);
  2485. if ((err = mxf_write_partition(s, 1, 2, body_partition_key, 0)) < 0)
  2486. return err;
  2487. mxf_write_klv_fill(s);
  2488. mxf_write_index_table_segment(s);
  2489. }
  2490. mxf_write_klv_fill(s);
  2491. mxf_write_system_item(s);
  2492. if (!mxf->edit_unit_byte_count) {
  2493. mxf->index_entries[mxf->edit_units_count].offset = mxf->body_offset;
  2494. mxf->index_entries[mxf->edit_units_count].flags = ie.flags;
  2495. mxf->index_entries[mxf->edit_units_count].temporal_ref = ie.temporal_ref;
  2496. mxf->body_offset += KAG_SIZE; // size of system element
  2497. }
  2498. mxf->edit_units_count++;
  2499. } else if (!mxf->edit_unit_byte_count && st->index == 1) {
  2500. if (!mxf->edit_units_count) {
  2501. av_log(s, AV_LOG_ERROR, "No packets in first stream\n");
  2502. return AVERROR_PATCHWELCOME;
  2503. }
  2504. mxf->index_entries[mxf->edit_units_count-1].slice_offset =
  2505. mxf->body_offset - mxf->index_entries[mxf->edit_units_count-1].offset;
  2506. }
  2507. mxf_write_klv_fill(s);
  2508. avio_write(pb, sc->track_essence_element_key, 16); // write key
  2509. if (s->oformat == &ff_mxf_d10_muxer &&
  2510. st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
  2511. mxf_write_d10_audio_packet(s, st, pkt);
  2512. } else {
  2513. klv_encode_ber4_length(pb, pkt->size); // write length
  2514. avio_write(pb, pkt->data, pkt->size);
  2515. mxf->body_offset += 16+4+pkt->size + klv_fill_size(16+4+pkt->size);
  2516. }
  2517. return 0;
  2518. }
  2519. static void mxf_write_random_index_pack(AVFormatContext *s)
  2520. {
  2521. MXFContext *mxf = s->priv_data;
  2522. AVIOContext *pb = s->pb;
  2523. uint64_t pos = avio_tell(pb);
  2524. int i;
  2525. avio_write(pb, random_index_pack_key, 16);
  2526. klv_encode_ber_length(pb, 28 + 12LL*mxf->body_partitions_count);
  2527. if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer)
  2528. avio_wb32(pb, 1); // BodySID of header partition
  2529. else
  2530. avio_wb32(pb, 0);
  2531. avio_wb64(pb, 0); // offset of header partition
  2532. for (i = 0; i < mxf->body_partitions_count; i++) {
  2533. avio_wb32(pb, 1); // BodySID
  2534. avio_wb64(pb, mxf->body_partition_offset[i]);
  2535. }
  2536. avio_wb32(pb, 0); // BodySID of footer partition
  2537. avio_wb64(pb, mxf->footer_partition_offset);
  2538. avio_wb32(pb, avio_tell(pb) - pos + 4);
  2539. }
  2540. static int mxf_write_footer(AVFormatContext *s)
  2541. {
  2542. MXFContext *mxf = s->priv_data;
  2543. AVIOContext *pb = s->pb;
  2544. int i, err;
  2545. if (!mxf->header_written ||
  2546. (s->oformat == &ff_mxf_opatom_muxer && !mxf->body_partition_offset)) {
  2547. /* reason could be invalid options/not supported codec/out of memory */
  2548. return AVERROR_UNKNOWN;
  2549. }
  2550. mxf->duration = mxf->last_indexed_edit_unit + mxf->edit_units_count;
  2551. mxf_write_klv_fill(s);
  2552. mxf->footer_partition_offset = avio_tell(pb);
  2553. if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer) { // no need to repeat index
  2554. if ((err = mxf_write_partition(s, 0, 0, footer_partition_key, 0)) < 0)
  2555. return err;
  2556. } else {
  2557. if ((err = mxf_write_partition(s, 0, 2, footer_partition_key, 0)) < 0)
  2558. return err;
  2559. mxf_write_klv_fill(s);
  2560. mxf_write_index_table_segment(s);
  2561. }
  2562. mxf_write_klv_fill(s);
  2563. mxf_write_random_index_pack(s);
  2564. if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
  2565. if (s->oformat == &ff_mxf_opatom_muxer) {
  2566. /* rewrite body partition to update lengths */
  2567. avio_seek(pb, mxf->body_partition_offset[0], SEEK_SET);
  2568. if ((err = mxf_write_opatom_body_partition(s)) < 0)
  2569. return err;
  2570. }
  2571. avio_seek(pb, 0, SEEK_SET);
  2572. if (mxf->edit_unit_byte_count && s->oformat != &ff_mxf_opatom_muxer) {
  2573. if ((err = mxf_write_partition(s, 1, 2, header_closed_partition_key, 1)) < 0)
  2574. return err;
  2575. mxf_write_klv_fill(s);
  2576. mxf_write_index_table_segment(s);
  2577. } else {
  2578. if ((err = mxf_write_partition(s, 0, 0, header_closed_partition_key, 1)) < 0)
  2579. return err;
  2580. }
  2581. // update footer partition offset
  2582. for (i = 0; i < mxf->body_partitions_count; i++) {
  2583. avio_seek(pb, mxf->body_partition_offset[i]+44, SEEK_SET);
  2584. avio_wb64(pb, mxf->footer_partition_offset);
  2585. }
  2586. }
  2587. return 0;
  2588. }
  2589. static void mxf_deinit(AVFormatContext *s)
  2590. {
  2591. MXFContext *mxf = s->priv_data;
  2592. ff_audio_interleave_close(s);
  2593. av_freep(&mxf->index_entries);
  2594. av_freep(&mxf->body_partition_offset);
  2595. if (mxf->timecode_track) {
  2596. av_freep(&mxf->timecode_track->priv_data);
  2597. av_freep(&mxf->timecode_track);
  2598. }
  2599. }
  2600. static int mxf_interleave_get_packet(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
  2601. {
  2602. int i, stream_count = 0;
  2603. for (i = 0; i < s->nb_streams; i++)
  2604. stream_count += !!s->streams[i]->last_in_packet_buffer;
  2605. if (stream_count && (s->nb_streams == stream_count || flush)) {
  2606. AVPacketList *pktl = s->internal->packet_buffer;
  2607. if (s->nb_streams != stream_count) {
  2608. AVPacketList *last = NULL;
  2609. // find last packet in edit unit
  2610. while (pktl) {
  2611. if (!stream_count || pktl->pkt.stream_index == 0)
  2612. break;
  2613. // update last packet in packet buffer
  2614. if (s->streams[pktl->pkt.stream_index]->last_in_packet_buffer != pktl)
  2615. s->streams[pktl->pkt.stream_index]->last_in_packet_buffer = pktl;
  2616. last = pktl;
  2617. pktl = pktl->next;
  2618. stream_count--;
  2619. }
  2620. // purge packet queue
  2621. while (pktl) {
  2622. AVPacketList *next = pktl->next;
  2623. av_packet_unref(&pktl->pkt);
  2624. av_freep(&pktl);
  2625. pktl = next;
  2626. }
  2627. if (last)
  2628. last->next = NULL;
  2629. else {
  2630. s->internal->packet_buffer = NULL;
  2631. s->internal->packet_buffer_end= NULL;
  2632. goto out;
  2633. }
  2634. pktl = s->internal->packet_buffer;
  2635. }
  2636. *out = pktl->pkt;
  2637. av_log(s, AV_LOG_TRACE, "out st:%d dts:%"PRId64"\n", (*out).stream_index, (*out).dts);
  2638. s->internal->packet_buffer = pktl->next;
  2639. if(s->streams[pktl->pkt.stream_index]->last_in_packet_buffer == pktl)
  2640. s->streams[pktl->pkt.stream_index]->last_in_packet_buffer= NULL;
  2641. if(!s->internal->packet_buffer)
  2642. s->internal->packet_buffer_end= NULL;
  2643. av_freep(&pktl);
  2644. return 1;
  2645. } else {
  2646. out:
  2647. av_init_packet(out);
  2648. return 0;
  2649. }
  2650. }
  2651. static int mxf_compare_timestamps(AVFormatContext *s, const AVPacket *next,
  2652. const AVPacket *pkt)
  2653. {
  2654. MXFStreamContext *sc = s->streams[pkt ->stream_index]->priv_data;
  2655. MXFStreamContext *sc2 = s->streams[next->stream_index]->priv_data;
  2656. return next->dts > pkt->dts ||
  2657. (next->dts == pkt->dts && sc->order < sc2->order);
  2658. }
  2659. static int mxf_interleave(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
  2660. {
  2661. return ff_audio_rechunk_interleave(s, out, pkt, flush,
  2662. mxf_interleave_get_packet, mxf_compare_timestamps);
  2663. }
  2664. #define MXF_COMMON_OPTIONS \
  2665. { "signal_standard", "Force/set Signal Standard",\
  2666. offsetof(MXFContext, signal_standard), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
  2667. { "bt601", "ITU-R BT.601 and BT.656, also SMPTE 125M (525 and 625 line interlaced)",\
  2668. 0, AV_OPT_TYPE_CONST, {.i64 = 1}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
  2669. { "bt1358", "ITU-R BT.1358 and ITU-R BT.799-3, also SMPTE 293M (525 and 625 line progressive)",\
  2670. 0, AV_OPT_TYPE_CONST, {.i64 = 2}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
  2671. { "smpte347m", "SMPTE 347M (540 Mbps mappings)",\
  2672. 0, AV_OPT_TYPE_CONST, {.i64 = 3}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
  2673. { "smpte274m", "SMPTE 274M (1125 line)",\
  2674. 0, AV_OPT_TYPE_CONST, {.i64 = 4}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
  2675. { "smpte296m", "SMPTE 296M (750 line progressive)",\
  2676. 0, AV_OPT_TYPE_CONST, {.i64 = 5}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
  2677. { "smpte349m", "SMPTE 349M (1485 Mbps mappings)",\
  2678. 0, AV_OPT_TYPE_CONST, {.i64 = 6}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},\
  2679. { "smpte428", "SMPTE 428-1 DCDM",\
  2680. 0, AV_OPT_TYPE_CONST, {.i64 = 7}, -1, 7, AV_OPT_FLAG_ENCODING_PARAM, "signal_standard"},
  2681. static const AVOption mxf_options[] = {
  2682. MXF_COMMON_OPTIONS
  2683. { "store_user_comments", "",
  2684. offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
  2685. { NULL },
  2686. };
  2687. static const AVClass mxf_muxer_class = {
  2688. .class_name = "MXF muxer",
  2689. .item_name = av_default_item_name,
  2690. .option = mxf_options,
  2691. .version = LIBAVUTIL_VERSION_INT,
  2692. };
  2693. static const AVOption d10_options[] = {
  2694. { "d10_channelcount", "Force/set channelcount in generic sound essence descriptor",
  2695. offsetof(MXFContext, channel_count), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 8, AV_OPT_FLAG_ENCODING_PARAM},
  2696. MXF_COMMON_OPTIONS
  2697. { "store_user_comments", "",
  2698. offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
  2699. { NULL },
  2700. };
  2701. static const AVClass mxf_d10_muxer_class = {
  2702. .class_name = "MXF-D10 muxer",
  2703. .item_name = av_default_item_name,
  2704. .option = d10_options,
  2705. .version = LIBAVUTIL_VERSION_INT,
  2706. };
  2707. static const AVOption opatom_options[] = {
  2708. { "mxf_audio_edit_rate", "Audio edit rate for timecode",
  2709. offsetof(MXFContext, audio_edit_rate), AV_OPT_TYPE_RATIONAL, {.dbl=25}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  2710. MXF_COMMON_OPTIONS
  2711. { "store_user_comments", "",
  2712. offsetof(MXFContext, store_user_comments), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
  2713. { NULL },
  2714. };
  2715. static const AVClass mxf_opatom_muxer_class = {
  2716. .class_name = "MXF-OPAtom muxer",
  2717. .item_name = av_default_item_name,
  2718. .option = opatom_options,
  2719. .version = LIBAVUTIL_VERSION_INT,
  2720. };
  2721. AVOutputFormat ff_mxf_muxer = {
  2722. .name = "mxf",
  2723. .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format)"),
  2724. .mime_type = "application/mxf",
  2725. .extensions = "mxf",
  2726. .priv_data_size = sizeof(MXFContext),
  2727. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  2728. .video_codec = AV_CODEC_ID_MPEG2VIDEO,
  2729. .write_header = mxf_write_header,
  2730. .write_packet = mxf_write_packet,
  2731. .write_trailer = mxf_write_footer,
  2732. .deinit = mxf_deinit,
  2733. .flags = AVFMT_NOTIMESTAMPS,
  2734. .interleave_packet = mxf_interleave,
  2735. .priv_class = &mxf_muxer_class,
  2736. };
  2737. AVOutputFormat ff_mxf_d10_muxer = {
  2738. .name = "mxf_d10",
  2739. .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) D-10 Mapping"),
  2740. .mime_type = "application/mxf",
  2741. .priv_data_size = sizeof(MXFContext),
  2742. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  2743. .video_codec = AV_CODEC_ID_MPEG2VIDEO,
  2744. .write_header = mxf_write_header,
  2745. .write_packet = mxf_write_packet,
  2746. .write_trailer = mxf_write_footer,
  2747. .deinit = mxf_deinit,
  2748. .flags = AVFMT_NOTIMESTAMPS,
  2749. .interleave_packet = mxf_interleave,
  2750. .priv_class = &mxf_d10_muxer_class,
  2751. };
  2752. AVOutputFormat ff_mxf_opatom_muxer = {
  2753. .name = "mxf_opatom",
  2754. .long_name = NULL_IF_CONFIG_SMALL("MXF (Material eXchange Format) Operational Pattern Atom"),
  2755. .mime_type = "application/mxf",
  2756. .extensions = "mxf",
  2757. .priv_data_size = sizeof(MXFContext),
  2758. .audio_codec = AV_CODEC_ID_PCM_S16LE,
  2759. .video_codec = AV_CODEC_ID_DNXHD,
  2760. .write_header = mxf_write_header,
  2761. .write_packet = mxf_write_packet,
  2762. .write_trailer = mxf_write_footer,
  2763. .deinit = mxf_deinit,
  2764. .flags = AVFMT_NOTIMESTAMPS,
  2765. .interleave_packet = mxf_interleave,
  2766. .priv_class = &mxf_opatom_muxer_class,
  2767. };