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.

637 lines
21KB

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