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.

228 lines
8.1KB

  1. /*
  2. * Ogg muxer
  3. * Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at free dot fr>
  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 "crc.h"
  23. #include "xiph.h"
  24. #include "bytestream.h"
  25. typedef struct {
  26. int64_t duration;
  27. unsigned page_counter;
  28. uint8_t *header[3];
  29. int header_len[3];
  30. /** for theora granule */
  31. int kfgshift;
  32. int64_t last_kf_pts;
  33. int vrev;
  34. } OGGStreamContext;
  35. static void ogg_update_checksum(AVFormatContext *s, offset_t crc_offset)
  36. {
  37. offset_t pos = url_ftell(s->pb);
  38. uint32_t checksum = get_checksum(s->pb);
  39. url_fseek(s->pb, crc_offset, SEEK_SET);
  40. put_be32(s->pb, checksum);
  41. url_fseek(s->pb, pos, SEEK_SET);
  42. }
  43. static int ogg_write_page(AVFormatContext *s, const uint8_t *data, int size,
  44. int64_t granule, int stream_index, int flags)
  45. {
  46. OGGStreamContext *oggstream = s->streams[stream_index]->priv_data;
  47. offset_t crc_offset;
  48. int page_segments, i;
  49. size = FFMIN(size, 255*255);
  50. page_segments = FFMIN((size/255)+!!size, 255);
  51. init_checksum(s->pb, ff_crc04C11DB7_update, 0);
  52. put_tag(s->pb, "OggS");
  53. put_byte(s->pb, 0);
  54. put_byte(s->pb, flags);
  55. put_le64(s->pb, granule);
  56. put_le32(s->pb, stream_index);
  57. put_le32(s->pb, oggstream->page_counter++);
  58. crc_offset = url_ftell(s->pb);
  59. put_le32(s->pb, 0); // crc
  60. put_byte(s->pb, page_segments);
  61. for (i = 0; i < page_segments-1; i++)
  62. put_byte(s->pb, 255);
  63. if (size) {
  64. put_byte(s->pb, size - (page_segments-1)*255);
  65. put_buffer(s->pb, data, size);
  66. }
  67. ogg_update_checksum(s, crc_offset);
  68. put_flush_packet(s->pb);
  69. return size;
  70. }
  71. static int ogg_build_flac_headers(const uint8_t *extradata, int extradata_size,
  72. OGGStreamContext *oggstream, int bitexact)
  73. {
  74. const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
  75. uint8_t *p;
  76. if (extradata_size != 34)
  77. return -1;
  78. oggstream->header_len[0] = 79;
  79. oggstream->header[0] = av_mallocz(79); // per ogg flac specs
  80. p = oggstream->header[0];
  81. bytestream_put_byte(&p, 0x7F);
  82. bytestream_put_buffer(&p, "FLAC", 4);
  83. bytestream_put_byte(&p, 1); // major version
  84. bytestream_put_byte(&p, 0); // minor version
  85. bytestream_put_be16(&p, 1); // headers packets without this one
  86. bytestream_put_buffer(&p, "fLaC", 4);
  87. bytestream_put_byte(&p, 0x00); // streaminfo
  88. bytestream_put_be24(&p, 34);
  89. bytestream_put_buffer(&p, extradata, 34);
  90. oggstream->header_len[1] = 1+3+4+strlen(vendor)+4;
  91. oggstream->header[1] = av_mallocz(oggstream->header_len[1]);
  92. p = oggstream->header[1];
  93. bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
  94. bytestream_put_be24(&p, oggstream->header_len[1] - 4);
  95. bytestream_put_le32(&p, strlen(vendor));
  96. bytestream_put_buffer(&p, vendor, strlen(vendor));
  97. bytestream_put_le32(&p, 0); // user comment list length
  98. return 0;
  99. }
  100. static int ogg_write_header(AVFormatContext *s)
  101. {
  102. OGGStreamContext *oggstream;
  103. int i, j;
  104. for (i = 0; i < s->nb_streams; i++) {
  105. AVStream *st = s->streams[i];
  106. if (st->codec->codec_type == CODEC_TYPE_AUDIO)
  107. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  108. else if (st->codec->codec_type == CODEC_TYPE_VIDEO)
  109. av_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den);
  110. if (st->codec->codec_id != CODEC_ID_VORBIS &&
  111. st->codec->codec_id != CODEC_ID_THEORA &&
  112. st->codec->codec_id != CODEC_ID_FLAC) {
  113. av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
  114. return -1;
  115. }
  116. if (!st->codec->extradata || !st->codec->extradata_size) {
  117. av_log(s, AV_LOG_ERROR, "No extradata present\n");
  118. return -1;
  119. }
  120. oggstream = av_mallocz(sizeof(*oggstream));
  121. st->priv_data = oggstream;
  122. if (st->codec->codec_id == CODEC_ID_FLAC) {
  123. if (ogg_build_flac_headers(st->codec->extradata, st->codec->extradata_size,
  124. oggstream, st->codec->flags & CODEC_FLAG_BITEXACT) < 0) {
  125. av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
  126. av_freep(&st->priv_data);
  127. }
  128. } else {
  129. if (ff_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
  130. st->codec->codec_id == CODEC_ID_VORBIS ? 30 : 42,
  131. oggstream->header, oggstream->header_len) < 0) {
  132. av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
  133. av_freep(&st->priv_data);
  134. return -1;
  135. }
  136. if (st->codec->codec_id == CODEC_ID_THEORA) {
  137. /** KFGSHIFT is the width of the less significant section of the granule position
  138. The less significant section is the frame count since the last keyframe */
  139. oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
  140. oggstream->vrev = oggstream->header[0][9];
  141. av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
  142. oggstream->kfgshift, oggstream->vrev);
  143. }
  144. }
  145. }
  146. for (i = 0; i < 3; i++) {
  147. for (j = 0; j < s->nb_streams; j++) {
  148. AVStream *st = s->streams[j];
  149. OGGStreamContext *oggstream = st->priv_data;
  150. if (oggstream && oggstream->header_len[i]) {
  151. ogg_write_page(s, oggstream->header[i], oggstream->header_len[i],
  152. 0, st->index, i ? 0 : 2); // bos
  153. }
  154. }
  155. }
  156. return 0;
  157. }
  158. static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
  159. {
  160. AVStream *st = s->streams[pkt->stream_index];
  161. OGGStreamContext *oggstream = st->priv_data;
  162. uint8_t *ptr = pkt->data;
  163. int ret, size = pkt->size;
  164. int64_t granule;
  165. if (st->codec->codec_id == CODEC_ID_THEORA) {
  166. int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
  167. int pframe_count;
  168. if (pkt->flags & PKT_FLAG_KEY)
  169. oggstream->last_kf_pts = pts;
  170. pframe_count = pts - oggstream->last_kf_pts;
  171. // prevent frame count from overflow if key frame flag is not set
  172. if (pframe_count >= (1<<oggstream->kfgshift)) {
  173. oggstream->last_kf_pts += pframe_count;
  174. pframe_count = 0;
  175. }
  176. granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
  177. } else
  178. granule = pkt->pts + pkt->duration;
  179. oggstream->duration = granule;
  180. do {
  181. ret = ogg_write_page(s, ptr, size, granule, pkt->stream_index, ptr != pkt->data);
  182. ptr += ret; size -= ret;
  183. } while (size > 0 || ret == 255*255); // need to output a last nil page
  184. return 0;
  185. }
  186. static int ogg_write_trailer(AVFormatContext *s)
  187. {
  188. int i;
  189. for (i = 0; i < s->nb_streams; i++) {
  190. AVStream *st = s->streams[i];
  191. OGGStreamContext *oggstream = st->priv_data;
  192. ogg_write_page(s, NULL, 0, oggstream->duration, i, 4); // eos
  193. if (st->codec->codec_id == CODEC_ID_FLAC) {
  194. av_free(oggstream->header[0]);
  195. av_free(oggstream->header[1]);
  196. }
  197. av_freep(&st->priv_data);
  198. }
  199. return 0;
  200. }
  201. AVOutputFormat ogg_muxer = {
  202. "ogg",
  203. "Ogg format",
  204. "application/ogg",
  205. "ogg",
  206. 0,
  207. CODEC_ID_FLAC,
  208. CODEC_ID_THEORA,
  209. ogg_write_header,
  210. ogg_write_packet,
  211. ogg_write_trailer,
  212. };