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.

181 lines
5.5KB

  1. /*
  2. * Matroska file muxer
  3. * Copyright (c) 2007 David Conrad
  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. #include "avformat.h"
  22. #include "riff.h"
  23. #include "matroska.h"
  24. typedef struct MatroskaMuxContext {
  25. offset_t segment;
  26. } MatroskaMuxContext;
  27. static void put_ebml_id(ByteIOContext *pb, unsigned int id)
  28. {
  29. if (id >= 0x3fffff)
  30. put_byte(pb, id >> 24);
  31. if (id >= 0x7fff)
  32. put_byte(pb, id >> 16);
  33. if (id >= 0xff)
  34. put_byte(pb, id >> 8);
  35. put_byte(pb, id);
  36. }
  37. // XXX: test this thoroughly and get rid of minbytes hack (currently needed to
  38. // use up all of the space reserved in start_ebml_master)
  39. static void put_ebml_size(ByteIOContext *pb, uint64_t size, int minbytes)
  40. {
  41. int bytes = minbytes;
  42. while (size >> (bytes*7 + 7)) bytes++;
  43. // sizes larger than this are currently undefined in EBML
  44. // XXX: error condition?
  45. if (size > (1ULL<<56)-1) return;
  46. put_byte(pb, (0x80 >> bytes) | (size >> bytes*8));
  47. for (bytes -= 1; bytes >= 0; bytes--)
  48. put_byte(pb, size >> bytes*8);
  49. }
  50. static void put_ebml_uint(ByteIOContext *pb, unsigned int elementid, uint64_t val)
  51. {
  52. int bytes = 1;
  53. while (val >> bytes*8) bytes++;
  54. put_ebml_id(pb, elementid);
  55. put_ebml_size(pb, bytes, 0);
  56. for (bytes -= 1; bytes >= 0; bytes--)
  57. put_byte(pb, val >> bytes*8);
  58. }
  59. //static void put_ebml_sint(ByteIOContext *pb, unsigned int elementid, int64_t val)
  60. static void put_ebml_float(ByteIOContext *pb, unsigned int elementid, double val)
  61. {
  62. // XXX: single-precision floats?
  63. put_ebml_id(pb, elementid);
  64. put_ebml_size(pb, 8, 0);
  65. put_be64(pb, av_dbl2int(val));
  66. }
  67. static void put_ebml_binary(ByteIOContext *pb, unsigned int elementid,
  68. const uint8_t *buf, int size)
  69. {
  70. put_ebml_id(pb, elementid);
  71. put_ebml_size(pb, size, 0);
  72. put_buffer(pb, buf, size);
  73. }
  74. static void put_ebml_string(ByteIOContext *pb, unsigned int elementid, const char *str)
  75. {
  76. put_ebml_binary(pb, elementid, str, strlen(str));
  77. }
  78. static offset_t start_ebml_master(ByteIOContext *pb, unsigned int elementid)
  79. {
  80. put_ebml_id(pb, elementid);
  81. // XXX: this always reserves the maximum needed space to store any size value
  82. // we should be smarter (additional parameter for expected size?)
  83. put_ebml_size(pb, (1ULL<<56)-1, 0); // largest unknown size
  84. return url_ftell(pb);
  85. }
  86. static void end_ebml_master(ByteIOContext *pb, offset_t start)
  87. {
  88. offset_t pos = url_ftell(pb);
  89. url_fseek(pb, start - 8, SEEK_SET);
  90. put_ebml_size(pb, pos - start, 7);
  91. url_fseek(pb, pos, SEEK_SET);
  92. }
  93. static int mkv_write_header(AVFormatContext *s)
  94. {
  95. MatroskaMuxContext *mkv = s->priv_data;
  96. ByteIOContext *pb = &s->pb;
  97. offset_t ebml_header, segment_info, tracks;
  98. int i;
  99. ebml_header = start_ebml_master(pb, EBML_ID_HEADER);
  100. put_ebml_uint (pb, EBML_ID_EBMLVERSION , 1);
  101. put_ebml_uint (pb, EBML_ID_EBMLREADVERSION , 1);
  102. put_ebml_uint (pb, EBML_ID_EBMLMAXIDLENGTH , 4);
  103. put_ebml_uint (pb, EBML_ID_EBMLMAXSIZELENGTH , 8);
  104. put_ebml_string (pb, EBML_ID_DOCTYPE , "matroska");
  105. put_ebml_uint (pb, EBML_ID_DOCTYPEVERSION , 1);
  106. put_ebml_uint (pb, EBML_ID_DOCTYPEREADVERSION , 1);
  107. end_ebml_master(pb, ebml_header);
  108. mkv->segment = start_ebml_master(pb, MATROSKA_ID_SEGMENT);
  109. segment_info = start_ebml_master(pb, MATROSKA_ID_INFO);
  110. put_ebml_uint(pb, MATROSKA_ID_TIMECODESCALE, 1000000);
  111. if (strlen(s->title))
  112. put_ebml_string(pb, MATROSKA_ID_TITLE, s->title);
  113. if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
  114. put_ebml_string(pb, MATROSKA_ID_MUXINGAPP, LIBAVFORMAT_IDENT);
  115. // XXX: both are required; something better for writing app?
  116. put_ebml_string(pb, MATROSKA_ID_WRITINGAPP, LIBAVFORMAT_IDENT);
  117. }
  118. // XXX: segment UID and duration
  119. end_ebml_master(pb, segment_info);
  120. tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS);
  121. for (i = 0; i < s->nb_streams; i++) {
  122. AVStream *st = s->streams[i];
  123. offset_t track = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY);
  124. end_ebml_master(pb, track);
  125. }
  126. end_ebml_master(pb, tracks);
  127. return 0;
  128. }
  129. static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
  130. {
  131. ByteIOContext *pb = &s->pb;
  132. put_buffer(pb, pkt->data, pkt->size);
  133. return 0;
  134. }
  135. static int mkv_write_trailer(AVFormatContext *s)
  136. {
  137. MatroskaMuxContext *mkv = s->priv_data;
  138. ByteIOContext *pb = &s->pb;
  139. end_ebml_master(pb, mkv->segment);
  140. return 0;
  141. }
  142. AVOutputFormat matroska_muxer = {
  143. "matroska",
  144. "Matroska File Format",
  145. "video/x-matroska",
  146. "mkv",
  147. sizeof(MatroskaMuxContext),
  148. CODEC_ID_MP2,
  149. CODEC_ID_MPEG4,
  150. mkv_write_header,
  151. mkv_write_packet,
  152. mkv_write_trailer,
  153. .codec_tag = (const AVCodecTag*[]){codec_bmp_tags, codec_wav_tags, 0},
  154. };