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.

359 lines
12KB

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