oggenc.c 10.0 KB

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