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.

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