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.

180 lines
5.4KB

  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_binary(ByteIOContext *pb, unsigned int elementid,
  61. uint8_t *buf, int size)
  62. {
  63. put_ebml_id(pb, elementid);
  64. put_ebml_size(pb, size, 0);
  65. put_buffer(pb, buf, size);
  66. }
  67. // XXX: should we do any special checking for valid strings for these 2 functions?
  68. static void put_ebml_string(ByteIOContext *pb, unsigned int elementid, char *str)
  69. {
  70. put_ebml_binary(pb, elementid, str, strlen(str));
  71. }
  72. static void put_ebml_utf8(ByteIOContext *pb, unsigned int elementid, char *str)
  73. {
  74. put_ebml_binary(pb, elementid, str, strlen(str));
  75. }
  76. static offset_t start_ebml_master(ByteIOContext *pb, unsigned int elementid)
  77. {
  78. put_ebml_id(pb, elementid);
  79. // XXX: this always reserves the maximum needed space to store any size value
  80. // we should be smarter (additional parameter for expected size?)
  81. put_ebml_size(pb, (1ULL<<56)-1, 0); // largest unknown size
  82. return url_ftell(pb);
  83. }
  84. static void end_ebml_master(ByteIOContext *pb, offset_t start)
  85. {
  86. offset_t pos = url_ftell(pb);
  87. url_fseek(pb, start - 8, SEEK_SET);
  88. put_ebml_size(pb, pos - start, 7);
  89. url_fseek(pb, pos, SEEK_SET);
  90. }
  91. static int mkv_write_header(AVFormatContext *s)
  92. {
  93. MatroskaMuxContext *mkv = s->priv_data;
  94. ByteIOContext *pb = &s->pb;
  95. offset_t ebml_header, segment_info, tracks;
  96. int i;
  97. ebml_header = start_ebml_master(pb, EBML_ID_HEADER);
  98. put_ebml_uint(pb, EBML_ID_EBMLVERSION, 1);
  99. put_ebml_uint(pb, EBML_ID_EBMLREADVERSION, 1);
  100. put_ebml_uint(pb, EBML_ID_EBMLMAXIDLENGTH, 4);
  101. put_ebml_uint(pb, EBML_ID_EBMLMAXSIZELENGTH, 8);
  102. put_ebml_string(pb, EBML_ID_DOCTYPE, "matroska");
  103. put_ebml_uint(pb, EBML_ID_DOCTYPEVERSION, 1);
  104. put_ebml_uint(pb, EBML_ID_DOCTYPEREADVERSION, 1);
  105. end_ebml_master(pb, ebml_header);
  106. mkv->segment = start_ebml_master(pb, MATROSKA_ID_SEGMENT);
  107. segment_info = start_ebml_master(pb, MATROSKA_ID_INFO);
  108. put_ebml_uint(pb, MATROSKA_ID_TIMECODESCALE, 1000000);
  109. if (strlen(s->title))
  110. put_ebml_utf8(pb, MATROSKA_ID_TITLE, s->title);
  111. if (!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
  112. put_ebml_utf8(pb, MATROSKA_ID_MUXINGAPP, LIBAVFORMAT_IDENT);
  113. // XXX: both are required; something better for writing app?
  114. put_ebml_utf8(pb, MATROSKA_ID_WRITINGAPP, LIBAVFORMAT_IDENT);
  115. }
  116. // XXX: segment UID and duration
  117. end_ebml_master(pb, segment_info);
  118. tracks = start_ebml_master(pb, MATROSKA_ID_TRACKS);
  119. for (i = 0; i < s->nb_streams; i++) {
  120. AVStream *st = s->streams[i];
  121. offset_t track = start_ebml_master(pb, MATROSKA_ID_TRACKENTRY);
  122. end_ebml_master(pb, track);
  123. }
  124. end_ebml_master(pb, tracks);
  125. put_be64(pb, 0xdeadbeef);
  126. return 0;
  127. }
  128. static int mkv_write_packet(AVFormatContext *s, AVPacket *pkt)
  129. {
  130. ByteIOContext *pb = &s->pb;
  131. put_buffer(pb, pkt->data, pkt->size);
  132. return 0;
  133. }
  134. static int mkv_write_trailer(AVFormatContext *s)
  135. {
  136. MatroskaMuxContext *mkv = s->priv_data;
  137. ByteIOContext *pb = &s->pb;
  138. end_ebml_master(pb, mkv->segment);
  139. return 0;
  140. }
  141. AVOutputFormat matroska_muxer = {
  142. "matroska",
  143. "Matroska File Format",
  144. "video/x-matroska",
  145. "mkv",
  146. sizeof(MatroskaMuxContext),
  147. CODEC_ID_MP2,
  148. CODEC_ID_MPEG4,
  149. mkv_write_header,
  150. mkv_write_packet,
  151. mkv_write_trailer,
  152. .codec_tag = (const AVCodecTag*[]){codec_bmp_tags, codec_wav_tags, 0},
  153. };