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.

743 lines
24KB

  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 <stdint.h>
  22. #include "libavutil/crc.h"
  23. #include "libavutil/mathematics.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/random_seed.h"
  26. #include "libavcodec/xiph.h"
  27. #include "libavcodec/bytestream.h"
  28. #include "libavcodec/flac.h"
  29. #include "avformat.h"
  30. #include "avio_internal.h"
  31. #include "internal.h"
  32. #include "vorbiscomment.h"
  33. #define MAX_PAGE_SIZE 65025
  34. typedef struct {
  35. int64_t start_granule;
  36. int64_t granule;
  37. int stream_index;
  38. uint8_t flags;
  39. uint8_t segments_count;
  40. uint8_t segments[255];
  41. uint8_t data[MAX_PAGE_SIZE];
  42. uint16_t size;
  43. } OGGPage;
  44. typedef struct {
  45. unsigned page_counter;
  46. uint8_t *header[3];
  47. int header_len[3];
  48. /** for theora granule */
  49. int kfgshift;
  50. int64_t last_kf_pts;
  51. int vrev;
  52. int eos;
  53. unsigned page_count; ///< number of page buffered
  54. OGGPage page; ///< current page
  55. unsigned serial_num; ///< serial number
  56. int64_t last_granule; ///< last packet granule
  57. } OGGStreamContext;
  58. typedef struct OGGPageList {
  59. OGGPage page;
  60. struct OGGPageList *next;
  61. } OGGPageList;
  62. typedef struct {
  63. const AVClass *class;
  64. OGGPageList *page_list;
  65. int pref_size; ///< preferred page size (0 => fill all segments)
  66. int64_t pref_duration; ///< preferred page duration (0 => fill all segments)
  67. } OGGContext;
  68. #define OFFSET(x) offsetof(OGGContext, x)
  69. #define PARAM AV_OPT_FLAG_ENCODING_PARAM
  70. static const AVOption options[] = {
  71. { "oggpagesize", "Set preferred Ogg page size.",
  72. offsetof(OGGContext, pref_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, MAX_PAGE_SIZE, AV_OPT_FLAG_ENCODING_PARAM},
  73. { "pagesize", "preferred page size in bytes (deprecated)",
  74. OFFSET(pref_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, MAX_PAGE_SIZE, PARAM },
  75. { "page_duration", "preferred page duration, in microseconds",
  76. OFFSET(pref_duration), AV_OPT_TYPE_INT64, { .i64 = 1000000 }, 0, INT64_MAX, PARAM },
  77. { NULL },
  78. };
  79. static void ogg_update_checksum(AVFormatContext *s, AVIOContext *pb, int64_t crc_offset)
  80. {
  81. int64_t pos = avio_tell(pb);
  82. uint32_t checksum = ffio_get_checksum(pb);
  83. avio_seek(pb, crc_offset, SEEK_SET);
  84. avio_wb32(pb, checksum);
  85. avio_seek(pb, pos, SEEK_SET);
  86. }
  87. static int ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
  88. {
  89. OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data;
  90. AVIOContext *pb;
  91. int64_t crc_offset;
  92. int ret, size;
  93. uint8_t *buf;
  94. ret = avio_open_dyn_buf(&pb);
  95. if (ret < 0)
  96. return ret;
  97. ffio_init_checksum(pb, ff_crc04C11DB7_update, 0);
  98. ffio_wfourcc(pb, "OggS");
  99. avio_w8(pb, 0);
  100. avio_w8(pb, page->flags | extra_flags);
  101. avio_wl64(pb, page->granule);
  102. avio_wl32(pb, oggstream->serial_num);
  103. avio_wl32(pb, oggstream->page_counter++);
  104. crc_offset = avio_tell(pb);
  105. avio_wl32(pb, 0); // crc
  106. avio_w8(pb, page->segments_count);
  107. avio_write(pb, page->segments, page->segments_count);
  108. avio_write(pb, page->data, page->size);
  109. ogg_update_checksum(s, pb, crc_offset);
  110. avio_flush(pb);
  111. size = avio_close_dyn_buf(pb, &buf);
  112. if (size < 0)
  113. return size;
  114. avio_write(s->pb, buf, size);
  115. avio_flush(s->pb);
  116. av_free(buf);
  117. oggstream->page_count--;
  118. return 0;
  119. }
  120. static int ogg_key_granule(OGGStreamContext *oggstream, int64_t granule)
  121. {
  122. return oggstream->kfgshift && !(granule & ((1<<oggstream->kfgshift)-1));
  123. }
  124. static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
  125. {
  126. if (oggstream->kfgshift)
  127. return (granule>>oggstream->kfgshift) +
  128. (granule & ((1<<oggstream->kfgshift)-1));
  129. else
  130. return granule;
  131. }
  132. static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
  133. {
  134. AVStream *st2 = s->streams[next->stream_index];
  135. AVStream *st = s->streams[page->stream_index];
  136. int64_t next_granule, cur_granule;
  137. if (next->granule == -1 || page->granule == -1)
  138. return 0;
  139. next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next->granule),
  140. st2->time_base, AV_TIME_BASE_Q);
  141. cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page->granule),
  142. st ->time_base, AV_TIME_BASE_Q);
  143. return next_granule > cur_granule;
  144. }
  145. static int ogg_reset_cur_page(OGGStreamContext *oggstream)
  146. {
  147. oggstream->page.granule = -1;
  148. oggstream->page.flags = 0;
  149. oggstream->page.segments_count = 0;
  150. oggstream->page.size = 0;
  151. return 0;
  152. }
  153. static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
  154. {
  155. OGGContext *ogg = s->priv_data;
  156. OGGPageList **p = &ogg->page_list;
  157. OGGPageList *l = av_mallocz(sizeof(*l));
  158. if (!l)
  159. return AVERROR(ENOMEM);
  160. l->page = oggstream->page;
  161. oggstream->page.start_granule = oggstream->page.granule;
  162. oggstream->page_count++;
  163. ogg_reset_cur_page(oggstream);
  164. while (*p) {
  165. if (ogg_compare_granule(s, &(*p)->page, &l->page))
  166. break;
  167. p = &(*p)->next;
  168. }
  169. l->next = *p;
  170. *p = l;
  171. return 0;
  172. }
  173. static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
  174. uint8_t *data, unsigned size, int64_t granule,
  175. int header)
  176. {
  177. OGGStreamContext *oggstream = st->priv_data;
  178. OGGContext *ogg = s->priv_data;
  179. int total_segments = size / 255 + 1;
  180. uint8_t *p = data;
  181. int i, segments, len, flush = 0;
  182. // Handles VFR by flushing page because this frame needs to have a timestamp
  183. // For theora, keyframes also need to have a timestamp to correctly mark
  184. // them as such, otherwise seeking will not work correctly at the very
  185. // least with old libogg versions.
  186. // Do not try to flush header packets though, that will create broken files.
  187. if (st->codec->codec_id == AV_CODEC_ID_THEORA && !header &&
  188. (ogg_granule_to_timestamp(oggstream, granule) >
  189. ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1 ||
  190. ogg_key_granule(oggstream, granule))) {
  191. if (oggstream->page.granule != -1)
  192. ogg_buffer_page(s, oggstream);
  193. flush = 1;
  194. }
  195. // avoid a continued page
  196. if (!header && oggstream->page.size > 0 &&
  197. MAX_PAGE_SIZE - oggstream->page.size < size) {
  198. ogg_buffer_page(s, oggstream);
  199. }
  200. for (i = 0; i < total_segments; ) {
  201. OGGPage *page = &oggstream->page;
  202. segments = FFMIN(total_segments - i, 255 - page->segments_count);
  203. if (i && !page->segments_count)
  204. page->flags |= 1; // continued packet
  205. memset(page->segments+page->segments_count, 255, segments - 1);
  206. page->segments_count += segments - 1;
  207. len = FFMIN(size, segments*255);
  208. page->segments[page->segments_count++] = len - (segments-1)*255;
  209. memcpy(page->data+page->size, p, len);
  210. p += len;
  211. size -= len;
  212. i += segments;
  213. page->size += len;
  214. if (i == total_segments)
  215. page->granule = granule;
  216. if (!header) {
  217. AVStream *st = s->streams[page->stream_index];
  218. int64_t start = av_rescale_q(page->start_granule, st->time_base,
  219. AV_TIME_BASE_Q);
  220. int64_t next = av_rescale_q(page->granule, st->time_base,
  221. AV_TIME_BASE_Q);
  222. if (page->segments_count == 255 ||
  223. (ogg->pref_size > 0 && page->size >= ogg->pref_size) ||
  224. (ogg->pref_duration > 0 && next - start >= ogg->pref_duration)) {
  225. ogg_buffer_page(s, oggstream);
  226. }
  227. }
  228. }
  229. if (flush && oggstream->page.granule != -1)
  230. ogg_buffer_page(s, oggstream);
  231. return 0;
  232. }
  233. static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact,
  234. int *header_len, AVDictionary **m, int framing_bit)
  235. {
  236. const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
  237. int size;
  238. uint8_t *p, *p0;
  239. ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
  240. size = offset + ff_vorbiscomment_length(*m, vendor) + framing_bit;
  241. p = av_mallocz(size);
  242. if (!p)
  243. return NULL;
  244. p0 = p;
  245. p += offset;
  246. ff_vorbiscomment_write(&p, m, vendor);
  247. if (framing_bit)
  248. bytestream_put_byte(&p, 1);
  249. *header_len = size;
  250. return p0;
  251. }
  252. static int ogg_build_flac_headers(AVCodecContext *avctx,
  253. OGGStreamContext *oggstream, int bitexact,
  254. AVDictionary **m)
  255. {
  256. enum FLACExtradataFormat format;
  257. uint8_t *streaminfo;
  258. uint8_t *p;
  259. if (!avpriv_flac_is_extradata_valid(avctx, &format, &streaminfo))
  260. return -1;
  261. // first packet: STREAMINFO
  262. oggstream->header_len[0] = 51;
  263. oggstream->header[0] = av_mallocz(51); // per ogg flac specs
  264. p = oggstream->header[0];
  265. if (!p)
  266. return AVERROR(ENOMEM);
  267. bytestream_put_byte(&p, 0x7F);
  268. bytestream_put_buffer(&p, "FLAC", 4);
  269. bytestream_put_byte(&p, 1); // major version
  270. bytestream_put_byte(&p, 0); // minor version
  271. bytestream_put_be16(&p, 1); // headers packets without this one
  272. bytestream_put_buffer(&p, "fLaC", 4);
  273. bytestream_put_byte(&p, 0x00); // streaminfo
  274. bytestream_put_be24(&p, 34);
  275. bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE);
  276. // second packet: VorbisComment
  277. p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m, 0);
  278. if (!p)
  279. return AVERROR(ENOMEM);
  280. oggstream->header[1] = p;
  281. bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
  282. bytestream_put_be24(&p, oggstream->header_len[1] - 4);
  283. return 0;
  284. }
  285. #define SPEEX_HEADER_SIZE 80
  286. static int ogg_build_speex_headers(AVCodecContext *avctx,
  287. OGGStreamContext *oggstream, int bitexact,
  288. AVDictionary **m)
  289. {
  290. uint8_t *p;
  291. if (avctx->extradata_size < SPEEX_HEADER_SIZE)
  292. return -1;
  293. // first packet: Speex header
  294. p = av_mallocz(SPEEX_HEADER_SIZE);
  295. if (!p)
  296. return AVERROR(ENOMEM);
  297. oggstream->header[0] = p;
  298. oggstream->header_len[0] = SPEEX_HEADER_SIZE;
  299. bytestream_put_buffer(&p, avctx->extradata, SPEEX_HEADER_SIZE);
  300. AV_WL32(&oggstream->header[0][68], 0); // set extra_headers to 0
  301. // second packet: VorbisComment
  302. p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m, 0);
  303. if (!p)
  304. return AVERROR(ENOMEM);
  305. oggstream->header[1] = p;
  306. return 0;
  307. }
  308. #define OPUS_HEADER_SIZE 19
  309. static int ogg_build_opus_headers(AVCodecContext *avctx,
  310. OGGStreamContext *oggstream, int bitexact,
  311. AVDictionary **m)
  312. {
  313. uint8_t *p;
  314. if (avctx->extradata_size < OPUS_HEADER_SIZE)
  315. return -1;
  316. /* first packet: Opus header */
  317. p = av_mallocz(avctx->extradata_size);
  318. if (!p)
  319. return AVERROR(ENOMEM);
  320. oggstream->header[0] = p;
  321. oggstream->header_len[0] = avctx->extradata_size;
  322. bytestream_put_buffer(&p, avctx->extradata, avctx->extradata_size);
  323. /* second packet: VorbisComment */
  324. p = ogg_write_vorbiscomment(8, bitexact, &oggstream->header_len[1], m, 0);
  325. if (!p)
  326. return AVERROR(ENOMEM);
  327. oggstream->header[1] = p;
  328. bytestream_put_buffer(&p, "OpusTags", 8);
  329. return 0;
  330. }
  331. static void ogg_write_pages(AVFormatContext *s, int flush)
  332. {
  333. OGGContext *ogg = s->priv_data;
  334. OGGPageList *next, *p;
  335. if (!ogg->page_list)
  336. return;
  337. for (p = ogg->page_list; p; ) {
  338. OGGStreamContext *oggstream =
  339. s->streams[p->page.stream_index]->priv_data;
  340. if (oggstream->page_count < 2 && !flush)
  341. break;
  342. ogg_write_page(s, &p->page,
  343. flush == 1 && oggstream->page_count == 1 ? 4 : 0); // eos
  344. next = p->next;
  345. av_freep(&p);
  346. p = next;
  347. }
  348. ogg->page_list = p;
  349. }
  350. static int ogg_write_header(AVFormatContext *s)
  351. {
  352. OGGContext *ogg = s->priv_data;
  353. OGGStreamContext *oggstream = NULL;
  354. int i, j;
  355. if (ogg->pref_size)
  356. av_log(s, AV_LOG_WARNING, "The pagesize option is deprecated\n");
  357. for (i = 0; i < s->nb_streams; i++) {
  358. AVStream *st = s->streams[i];
  359. unsigned serial_num = i;
  360. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  361. if (st->codec->codec_id == AV_CODEC_ID_OPUS)
  362. /* Opus requires a fixed 48kHz clock */
  363. avpriv_set_pts_info(st, 64, 1, 48000);
  364. else
  365. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  366. }
  367. if (st->codec->codec_id != AV_CODEC_ID_VORBIS &&
  368. st->codec->codec_id != AV_CODEC_ID_THEORA &&
  369. st->codec->codec_id != AV_CODEC_ID_SPEEX &&
  370. st->codec->codec_id != AV_CODEC_ID_FLAC &&
  371. st->codec->codec_id != AV_CODEC_ID_OPUS) {
  372. av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
  373. return -1;
  374. }
  375. if (!st->codec->extradata || !st->codec->extradata_size) {
  376. av_log(s, AV_LOG_ERROR, "No extradata present\n");
  377. return -1;
  378. }
  379. oggstream = av_mallocz(sizeof(*oggstream));
  380. if (!oggstream)
  381. return AVERROR(ENOMEM);
  382. oggstream->page.stream_index = i;
  383. if (!(s->flags & AVFMT_FLAG_BITEXACT))
  384. do {
  385. serial_num = av_get_random_seed();
  386. for (j = 0; j < i; j++) {
  387. OGGStreamContext *sc = s->streams[j]->priv_data;
  388. if (serial_num == sc->serial_num)
  389. break;
  390. }
  391. } while (j < i);
  392. oggstream->serial_num = serial_num;
  393. av_dict_copy(&st->metadata, s->metadata, AV_DICT_DONT_OVERWRITE);
  394. st->priv_data = oggstream;
  395. if (st->codec->codec_id == AV_CODEC_ID_FLAC) {
  396. int err = ogg_build_flac_headers(st->codec, oggstream,
  397. s->flags & AVFMT_FLAG_BITEXACT,
  398. &st->metadata);
  399. if (err) {
  400. av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
  401. av_freep(&st->priv_data);
  402. return err;
  403. }
  404. } else if (st->codec->codec_id == AV_CODEC_ID_SPEEX) {
  405. int err = ogg_build_speex_headers(st->codec, oggstream,
  406. s->flags & AVFMT_FLAG_BITEXACT,
  407. &st->metadata);
  408. if (err) {
  409. av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
  410. av_freep(&st->priv_data);
  411. return err;
  412. }
  413. } else if (st->codec->codec_id == AV_CODEC_ID_OPUS) {
  414. int err = ogg_build_opus_headers(st->codec, oggstream,
  415. s->flags & AVFMT_FLAG_BITEXACT,
  416. &st->metadata);
  417. if (err) {
  418. av_log(s, AV_LOG_ERROR, "Error writing Opus headers\n");
  419. av_freep(&st->priv_data);
  420. return err;
  421. }
  422. } else {
  423. uint8_t *p;
  424. const char *cstr = st->codec->codec_id == AV_CODEC_ID_VORBIS ? "vorbis" : "theora";
  425. int header_type = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 3 : 0x81;
  426. int framing_bit = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 1 : 0;
  427. if (avpriv_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
  428. st->codec->codec_id == AV_CODEC_ID_VORBIS ? 30 : 42,
  429. oggstream->header, oggstream->header_len) < 0) {
  430. av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
  431. av_freep(&st->priv_data);
  432. return -1;
  433. }
  434. p = ogg_write_vorbiscomment(7, s->flags & AVFMT_FLAG_BITEXACT,
  435. &oggstream->header_len[1], &st->metadata,
  436. framing_bit);
  437. oggstream->header[1] = p;
  438. if (!p)
  439. return AVERROR(ENOMEM);
  440. bytestream_put_byte(&p, header_type);
  441. bytestream_put_buffer(&p, cstr, 6);
  442. if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
  443. /** KFGSHIFT is the width of the less significant section of the granule position
  444. The less significant section is the frame count since the last keyframe */
  445. oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
  446. oggstream->vrev = oggstream->header[0][9];
  447. av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
  448. oggstream->kfgshift, oggstream->vrev);
  449. }
  450. }
  451. }
  452. for (j = 0; j < s->nb_streams; j++) {
  453. OGGStreamContext *oggstream = s->streams[j]->priv_data;
  454. ogg_buffer_data(s, s->streams[j], oggstream->header[0],
  455. oggstream->header_len[0], 0, 1);
  456. oggstream->page.flags |= 2; // bos
  457. ogg_buffer_page(s, oggstream);
  458. }
  459. for (j = 0; j < s->nb_streams; j++) {
  460. AVStream *st = s->streams[j];
  461. OGGStreamContext *oggstream = st->priv_data;
  462. for (i = 1; i < 3; i++) {
  463. if (oggstream->header_len[i])
  464. ogg_buffer_data(s, st, oggstream->header[i],
  465. oggstream->header_len[i], 0, 1);
  466. }
  467. ogg_buffer_page(s, oggstream);
  468. }
  469. oggstream->page.start_granule = AV_NOPTS_VALUE;
  470. ogg_write_pages(s, 2);
  471. return 0;
  472. }
  473. static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
  474. {
  475. AVStream *st = s->streams[pkt->stream_index];
  476. OGGStreamContext *oggstream = st->priv_data;
  477. int ret;
  478. int64_t granule;
  479. if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
  480. int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
  481. int pframe_count;
  482. if (pkt->flags & AV_PKT_FLAG_KEY)
  483. oggstream->last_kf_pts = pts;
  484. pframe_count = pts - oggstream->last_kf_pts;
  485. // prevent frame count from overflow if key frame flag is not set
  486. if (pframe_count >= (1<<oggstream->kfgshift)) {
  487. oggstream->last_kf_pts += pframe_count;
  488. pframe_count = 0;
  489. }
  490. granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
  491. } else if (st->codec->codec_id == AV_CODEC_ID_OPUS)
  492. granule = pkt->pts + pkt->duration + av_rescale_q(st->codec->delay, (AVRational){ 1, st->codec->sample_rate }, st->time_base);
  493. else
  494. granule = pkt->pts + pkt->duration;
  495. if (oggstream->page.start_granule == AV_NOPTS_VALUE)
  496. oggstream->page.start_granule = pkt->pts;
  497. ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0);
  498. if (ret < 0)
  499. return ret;
  500. ogg_write_pages(s, 0);
  501. oggstream->last_granule = granule;
  502. return 0;
  503. }
  504. static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
  505. {
  506. int i;
  507. if (pkt)
  508. return ogg_write_packet_internal(s, pkt);
  509. for (i = 0; i < s->nb_streams; i++) {
  510. OGGStreamContext *oggstream = s->streams[i]->priv_data;
  511. if (oggstream->page.segments_count)
  512. ogg_buffer_page(s, oggstream);
  513. }
  514. ogg_write_pages(s, 2);
  515. return 0;
  516. }
  517. static int ogg_write_trailer(AVFormatContext *s)
  518. {
  519. int i;
  520. /* flush current page if needed */
  521. for (i = 0; i < s->nb_streams; i++) {
  522. OGGStreamContext *oggstream = s->streams[i]->priv_data;
  523. if (oggstream->page.size > 0)
  524. ogg_buffer_page(s, oggstream);
  525. }
  526. ogg_write_pages(s, 1);
  527. for (i = 0; i < s->nb_streams; i++) {
  528. AVStream *st = s->streams[i];
  529. OGGStreamContext *oggstream = st->priv_data;
  530. if (st->codec->codec_id == AV_CODEC_ID_FLAC ||
  531. st->codec->codec_id == AV_CODEC_ID_SPEEX ||
  532. st->codec->codec_id == AV_CODEC_ID_OPUS) {
  533. av_freep(&oggstream->header[0]);
  534. }
  535. av_freep(&oggstream->header[1]);
  536. av_freep(&st->priv_data);
  537. }
  538. return 0;
  539. }
  540. #if CONFIG_OGG_MUXER
  541. static const AVClass ogg_muxer_class = {
  542. .class_name = "Ogg muxer",
  543. .item_name = av_default_item_name,
  544. .option = options,
  545. .version = LIBAVUTIL_VERSION_INT,
  546. };
  547. AVOutputFormat ff_ogg_muxer = {
  548. .name = "ogg",
  549. .long_name = NULL_IF_CONFIG_SMALL("Ogg"),
  550. .mime_type = "application/ogg",
  551. .extensions = "ogg,ogv"
  552. #if !CONFIG_SPEEX_MUXER
  553. ",spx"
  554. #endif
  555. #if !CONFIG_OPUS_MUXER
  556. ",opus"
  557. #endif
  558. ,
  559. .priv_data_size = sizeof(OGGContext),
  560. .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
  561. AV_CODEC_ID_VORBIS : AV_CODEC_ID_FLAC,
  562. .video_codec = AV_CODEC_ID_THEORA,
  563. .write_header = ogg_write_header,
  564. .write_packet = ogg_write_packet,
  565. .write_trailer = ogg_write_trailer,
  566. .flags = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
  567. .priv_class = &ogg_muxer_class,
  568. };
  569. #endif
  570. #if CONFIG_OGA_MUXER
  571. static const AVClass oga_muxer_class = {
  572. .class_name = "Ogg audio muxer",
  573. .item_name = av_default_item_name,
  574. .option = options,
  575. .version = LIBAVUTIL_VERSION_INT,
  576. };
  577. AVOutputFormat ff_oga_muxer = {
  578. .name = "oga",
  579. .long_name = NULL_IF_CONFIG_SMALL("Ogg audio"),
  580. .mime_type = "audio/ogg",
  581. .extensions = "oga",
  582. .priv_data_size = sizeof(OGGContext),
  583. .audio_codec = AV_CODEC_ID_VORBIS,
  584. .video_codec = AV_CODEC_ID_NONE,
  585. .write_header = ogg_write_header,
  586. .write_packet = ogg_write_packet,
  587. .write_trailer = ogg_write_trailer,
  588. .flags = AVFMT_TS_NEGATIVE,
  589. .priv_class = &oga_muxer_class,
  590. };
  591. #endif
  592. #if CONFIG_SPEEX_MUXER
  593. static const AVClass speex_muxer_class = {
  594. .class_name = "Speex muxer",
  595. .item_name = av_default_item_name,
  596. .option = options,
  597. .version = LIBAVUTIL_VERSION_INT,
  598. };
  599. AVOutputFormat ff_speex_muxer = {
  600. .name = "speex",
  601. .long_name = NULL_IF_CONFIG_SMALL("Speex"),
  602. .mime_type = "audio/ogg",
  603. .extensions = "spx",
  604. .priv_data_size = sizeof(OGGContext),
  605. .audio_codec = AV_CODEC_ID_SPEEX,
  606. .video_codec = AV_CODEC_ID_NONE,
  607. .write_header = ogg_write_header,
  608. .write_packet = ogg_write_packet,
  609. .write_trailer = ogg_write_trailer,
  610. .flags = AVFMT_TS_NEGATIVE,
  611. .priv_class = &speex_muxer_class,
  612. };
  613. #endif
  614. #if CONFIG_OPUS_MUXER
  615. static const AVClass opus_muxer_class = {
  616. .class_name = "Opus muxer",
  617. .item_name = av_default_item_name,
  618. .option = options,
  619. .version = LIBAVUTIL_VERSION_INT,
  620. };
  621. AVOutputFormat ff_opus_muxer = {
  622. .name = "opus",
  623. .long_name = NULL_IF_CONFIG_SMALL("Opus"),
  624. .mime_type = "audio/ogg",
  625. .extensions = "opus",
  626. .priv_data_size = sizeof(OGGContext),
  627. .audio_codec = AV_CODEC_ID_OPUS,
  628. .video_codec = AV_CODEC_ID_NONE,
  629. .write_header = ogg_write_header,
  630. .write_packet = ogg_write_packet,
  631. .write_trailer = ogg_write_trailer,
  632. .flags = AVFMT_TS_NEGATIVE,
  633. .priv_class = &opus_muxer_class,
  634. };
  635. #endif