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.

167 lines
4.8KB

  1. /*
  2. * MXF muxer
  3. * Copyright (c) 2008 GUCAS, Zhentan Feng <spyfeng at gmail dot com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /*
  22. * References
  23. * SMPTE 336M KLV Data Encoding Protocol Using Key-Length-Value
  24. * SMPTE 377M MXF File Format Specifications
  25. * SMPTE 379M MXF Generic Container
  26. * SMPTE 381M Mapping MPEG Streams into the MXF Generic Container
  27. * SMPTE RP210: SMPTE Metadata Dictionary
  28. * SMPTE RP224: Registry of SMPTE Universal Labels
  29. */
  30. //#define DEBUG
  31. #include "mxf.h"
  32. typedef struct {
  33. int local_tag;
  34. UID uid;
  35. } MXFLocalTagPair;
  36. static const uint8_t uuid_base[] = { 0xAD,0xAB,0x44,0x24,0x2f,0x25,0x4d,0xc7,0x92,0xff,0x29,0xbd };
  37. static const uint8_t umid_base[] = { 0x06,0x0A,0x2B,0x34,0x01,0x01,0x01,0x01,0x01,0x01,0x0F,0x00,0x13,0x00,0x00,0x00 };
  38. /**
  39. * complete key for operation pattern, partitions, and primer pack
  40. */
  41. static const uint8_t op1a_ul[] = { 0x06,0x0E,0x2B,0x34,0x04,0x01,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x01,0x01,0x00 };
  42. static const uint8_t header_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x02,0x04,0x00 }; // ClosedComplete
  43. static const uint8_t footer_partition_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x04,0x04,0x00 }; // ClosedComplete
  44. static const uint8_t primer_pack_key[] = { 0x06,0x0E,0x2B,0x34,0x02,0x05,0x01,0x01,0x0D,0x01,0x02,0x01,0x01,0x05,0x01,0x00 };
  45. static void mxf_write_uuid(ByteIOContext *pb, enum CodecID type, int value)
  46. {
  47. put_buffer(pb, uuid_base, 12);
  48. put_be16(pb, type);
  49. put_be16(pb, value);
  50. }
  51. static int klv_encode_ber_length(ByteIOContext *pb, uint64_t len)
  52. {
  53. // Determine the best BER size
  54. int size;
  55. if (len < 128) {
  56. //short form
  57. put_byte(pb, len);
  58. return 1;
  59. }
  60. size = (av_log2(len) >> 3) + 1;
  61. // long form
  62. put_byte(pb, 0x80 + size);
  63. while(size) {
  64. size --;
  65. put_byte(pb, len >> 8 * size & 0xff);
  66. }
  67. return 0;
  68. }
  69. static const MXFCodecUL *mxf_get_essence_container_ul(enum CodecID type)
  70. {
  71. const MXFCodecUL *uls = ff_mxf_essence_container_uls;
  72. while (uls->id != CODEC_ID_NONE) {
  73. if (uls->id == type)
  74. break;
  75. uls++;
  76. }
  77. return uls;
  78. }
  79. static int mxf_write_primer_pack(AVFormatContext *s)
  80. {
  81. ByteIOContext *pb = s->pb;
  82. int local_tag_number, i = 0;
  83. local_tag_number = sizeof(mxf_local_tag_batch) / sizeof(MXFLocalTagPair);
  84. put_buffer(pb, primer_pack_key, 16);
  85. klv_encode_ber_length(pb, local_tag_number * 18 + 8);
  86. put_be32(pb, local_tag_number); // local_tag num
  87. put_be32(pb, 18); // item size, always 18 according to the specs
  88. for (i = 0; i < local_tag_number; i++) {
  89. put_be16(pb, mxf_local_tag_batch[i].local_tag);
  90. put_buffer(pb, mxf_local_tag_batch[i].uid, 16);
  91. }
  92. return 0;
  93. }
  94. static void mxf_write_local_tag(ByteIOContext *pb, int value_size, int tag)
  95. {
  96. put_be16(pb, tag);
  97. put_be16(pb, value_size);
  98. }
  99. static void mxf_free(AVFormatContext *s)
  100. {
  101. MXFContext *mxf = s->priv_data;
  102. AVStream *st;
  103. int i;
  104. for (i = 0; i < s->nb_streams; i++) {
  105. st = s->streams[i];
  106. av_freep(&st->priv_data);
  107. }
  108. av_freep(&mxf->essence_container_uls);
  109. }
  110. static const MXFDataDefinitionUL *mxf_get_data_definition_ul(enum CodecType type)
  111. {
  112. const MXFDataDefinitionUL *uls = ff_mxf_data_definition_uls;
  113. while (uls->type != CODEC_TYPE_DATA) {
  114. if (type == uls->type)
  115. break;
  116. uls ++;
  117. }
  118. return uls;
  119. }
  120. static int mux_write_packet(AVFormatContext *s, AVPacket *pkt)
  121. {
  122. ByteIOContext *pb = s->pb;
  123. AVStream *st = s->streams[pkt->stream_index];
  124. MXFStreamContext *sc = st->priv_data;
  125. put_buffer(pb, sc->track_essence_element_key, 16); // write key
  126. klv_encode_ber_length(pb, pkt->size); // write length
  127. put_buffer(pb, pkt->data, pkt->size); // write value
  128. put_flush_packet(pb);
  129. return 0;
  130. }
  131. AVOutputFormat mxf_muxer = {
  132. "mxf",
  133. NULL_IF_CONFIG_SMALL("Material eXchange Format"),
  134. NULL,
  135. "mxf",
  136. sizeof(MXFContext),
  137. CODEC_ID_PCM_S16LE,
  138. CODEC_ID_MPEG2VIDEO,
  139. mux_write_header,
  140. mux_write_packet,
  141. mux_write_footer,
  142. };