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.

2768 lines
107KB

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