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.

360 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)+!!size, 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. if (size) {
  72. put_byte(s->pb, size - (page_segments-1)*255);
  73. put_buffer(s->pb, data, size);
  74. }
  75. ogg_update_checksum(s, crc_offset);
  76. put_flush_packet(s->pb);
  77. return size;
  78. }
  79. static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact,
  80. int *header_len)
  81. {
  82. const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
  83. int size;
  84. uint8_t *p, *p0;
  85. size = offset + 4 + strlen(vendor) + 4;
  86. p = av_mallocz(size);
  87. if (!p)
  88. return NULL;
  89. p0 = p;
  90. p += offset;
  91. bytestream_put_le32(&p, strlen(vendor));
  92. bytestream_put_buffer(&p, vendor, strlen(vendor));
  93. bytestream_put_le32(&p, 0); // user comment list length
  94. *header_len = size;
  95. return p0;
  96. }
  97. static int ogg_build_flac_headers(AVCodecContext *avctx,
  98. OGGStreamContext *oggstream, int bitexact)
  99. {
  100. enum FLACExtradataFormat format;
  101. uint8_t *streaminfo;
  102. uint8_t *p;
  103. if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo))
  104. return -1;
  105. // first packet: STREAMINFO
  106. oggstream->header_len[0] = 51;
  107. oggstream->header[0] = av_mallocz(51); // per ogg flac specs
  108. p = oggstream->header[0];
  109. if (!p)
  110. return AVERROR_NOMEM;
  111. bytestream_put_byte(&p, 0x7F);
  112. bytestream_put_buffer(&p, "FLAC", 4);
  113. bytestream_put_byte(&p, 1); // major version
  114. bytestream_put_byte(&p, 0); // minor version
  115. bytestream_put_be16(&p, 1); // headers packets without this one
  116. bytestream_put_buffer(&p, "fLaC", 4);
  117. bytestream_put_byte(&p, 0x00); // streaminfo
  118. bytestream_put_be24(&p, 34);
  119. bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE);
  120. // second packet: VorbisComment
  121. p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1]);
  122. if (!p)
  123. return AVERROR_NOMEM;
  124. oggstream->header[1] = p;
  125. bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
  126. bytestream_put_be24(&p, oggstream->header_len[1] - 4);
  127. return 0;
  128. }
  129. #define SPEEX_HEADER_SIZE 80
  130. static int ogg_build_speex_headers(AVCodecContext *avctx,
  131. OGGStreamContext *oggstream, int bitexact)
  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]);
  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. if (err) {
  178. av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
  179. av_freep(&st->priv_data);
  180. return err;
  181. }
  182. } else if (st->codec->codec_id == CODEC_ID_SPEEX) {
  183. int err = ogg_build_speex_headers(st->codec, oggstream,
  184. st->codec->flags & CODEC_FLAG_BITEXACT);
  185. if (err) {
  186. av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
  187. av_freep(&st->priv_data);
  188. return err;
  189. }
  190. } else {
  191. if (ff_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
  192. st->codec->codec_id == CODEC_ID_VORBIS ? 30 : 42,
  193. oggstream->header, oggstream->header_len) < 0) {
  194. av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
  195. av_freep(&st->priv_data);
  196. return -1;
  197. }
  198. if (st->codec->codec_id == CODEC_ID_THEORA) {
  199. /** KFGSHIFT is the width of the less significant section of the granule position
  200. The less significant section is the frame count since the last keyframe */
  201. oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
  202. oggstream->vrev = oggstream->header[0][9];
  203. av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
  204. oggstream->kfgshift, oggstream->vrev);
  205. }
  206. }
  207. }
  208. for (i = 0; i < 3; i++) {
  209. for (j = 0; j < s->nb_streams; j++) {
  210. AVStream *st = s->streams[j];
  211. OGGStreamContext *oggstream = st->priv_data;
  212. if (oggstream && oggstream->header_len[i]) {
  213. ogg_write_page(s, oggstream->header[i], oggstream->header_len[i],
  214. 0, st->index, i ? 0 : 2); // bos
  215. }
  216. }
  217. }
  218. return 0;
  219. }
  220. static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
  221. {
  222. AVStream *st = s->streams[pkt->stream_index];
  223. OGGStreamContext *oggstream = st->priv_data;
  224. uint8_t *ptr = pkt->data;
  225. int ret, size = pkt->size;
  226. int64_t granule;
  227. if (st->codec->codec_id == CODEC_ID_THEORA) {
  228. int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
  229. int pframe_count;
  230. if (pkt->flags & PKT_FLAG_KEY)
  231. oggstream->last_kf_pts = pts;
  232. pframe_count = pts - oggstream->last_kf_pts;
  233. // prevent frame count from overflow if key frame flag is not set
  234. if (pframe_count >= (1<<oggstream->kfgshift)) {
  235. oggstream->last_kf_pts += pframe_count;
  236. pframe_count = 0;
  237. }
  238. granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
  239. } else
  240. granule = pkt->pts + pkt->duration;
  241. oggstream->duration = granule;
  242. do {
  243. ret = ogg_write_page(s, ptr, size, granule, pkt->stream_index, ptr != pkt->data);
  244. ptr += ret; size -= ret;
  245. } while (size > 0 || ret == 255*255); // need to output a last nil page
  246. return 0;
  247. }
  248. static int ogg_compare_granule(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
  249. {
  250. AVStream *st2 = s->streams[next->stream_index];
  251. AVStream *st = s->streams[pkt ->stream_index];
  252. int64_t next_granule = av_rescale_q(next->pts + next->duration,
  253. st2->time_base, AV_TIME_BASE_Q);
  254. int64_t cur_granule = av_rescale_q(pkt ->pts + pkt ->duration,
  255. st ->time_base, AV_TIME_BASE_Q);
  256. return next_granule > cur_granule;
  257. }
  258. static int ogg_interleave_per_granule(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
  259. {
  260. OGGStreamContext *ogg;
  261. int i, stream_count = 0;
  262. int interleaved = 0;
  263. if (pkt) {
  264. ff_interleave_add_packet(s, pkt, ogg_compare_granule);
  265. ogg = s->streams[pkt->stream_index]->priv_data;
  266. ogg->packet_count++;
  267. }
  268. for (i = 0; i < s->nb_streams; i++) {
  269. ogg = s->streams[i]->priv_data;
  270. stream_count += !!ogg->packet_count;
  271. interleaved += ogg->packet_count > 1;
  272. }
  273. if ((s->nb_streams == stream_count && interleaved == stream_count) ||
  274. (flush && stream_count)) {
  275. AVPacketList *pktl= s->packet_buffer;
  276. *out= pktl->pkt;
  277. s->packet_buffer = pktl->next;
  278. ogg = s->streams[out->stream_index]->priv_data;
  279. if (flush && ogg->packet_count == 1)
  280. ogg->eos = 1;
  281. ogg->packet_count--;
  282. if(!s->packet_buffer)
  283. s->packet_buffer_end= NULL;
  284. if(s->streams[out->stream_index]->last_in_packet_buffer == pktl)
  285. s->streams[out->stream_index]->last_in_packet_buffer= NULL;
  286. av_freep(&pktl);
  287. return 1;
  288. } else {
  289. av_init_packet(out);
  290. return 0;
  291. }
  292. }
  293. static int ogg_write_trailer(AVFormatContext *s)
  294. {
  295. int i;
  296. for (i = 0; i < s->nb_streams; i++) {
  297. AVStream *st = s->streams[i];
  298. OGGStreamContext *oggstream = st->priv_data;
  299. if (st->codec->codec_id == CODEC_ID_FLAC ||
  300. st->codec->codec_id == CODEC_ID_SPEEX) {
  301. av_free(oggstream->header[0]);
  302. av_free(oggstream->header[1]);
  303. }
  304. av_freep(&st->priv_data);
  305. }
  306. return 0;
  307. }
  308. AVOutputFormat ogg_muxer = {
  309. "ogg",
  310. NULL_IF_CONFIG_SMALL("Ogg"),
  311. "application/ogg",
  312. "ogg,ogv,spx",
  313. 0,
  314. CODEC_ID_FLAC,
  315. CODEC_ID_THEORA,
  316. ogg_write_header,
  317. ogg_write_packet,
  318. ogg_write_trailer,
  319. .interleave_packet = ogg_interleave_per_granule,
  320. };