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.

2739 lines
106KB

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