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.

639 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/mathematics.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/random_seed.h"
  25. #include "libavcodec/xiph.h"
  26. #include "libavcodec/bytestream.h"
  27. #include "libavcodec/flac.h"
  28. #include "avformat.h"
  29. #include "avio_internal.h"
  30. #include "internal.h"
  31. #include "vorbiscomment.h"
  32. #define MAX_PAGE_SIZE 65025
  33. typedef struct {
  34. int64_t start_granule;
  35. int64_t granule;
  36. int stream_index;
  37. uint8_t flags;
  38. uint8_t segments_count;
  39. uint8_t segments[255];
  40. uint8_t data[MAX_PAGE_SIZE];
  41. uint16_t size;
  42. } OGGPage;
  43. typedef struct {
  44. unsigned page_counter;
  45. uint8_t *header[3];
  46. int header_len[3];
  47. /** for theora granule */
  48. int kfgshift;
  49. int64_t last_kf_pts;
  50. int vrev;
  51. int eos;
  52. unsigned page_count; ///< number of page buffered
  53. OGGPage page; ///< current page
  54. unsigned serial_num; ///< serial number
  55. int64_t last_granule; ///< last packet granule
  56. } OGGStreamContext;
  57. typedef struct OGGPageList {
  58. OGGPage page;
  59. struct OGGPageList *next;
  60. } OGGPageList;
  61. typedef struct {
  62. const AVClass *class;
  63. OGGPageList *page_list;
  64. int pref_size; ///< preferred page size (0 => fill all segments)
  65. int64_t pref_duration; ///< preferred page duration (0 => fill all segments)
  66. } OGGContext;
  67. #define OFFSET(x) offsetof(OGGContext, x)
  68. #define PARAM AV_OPT_FLAG_ENCODING_PARAM
  69. static const AVOption options[] = {
  70. { "oggpagesize", "Set preferred Ogg page size.",
  71. offsetof(OGGContext, pref_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, MAX_PAGE_SIZE, AV_OPT_FLAG_ENCODING_PARAM},
  72. { "pagesize", "preferred page size in bytes (deprecated)",
  73. OFFSET(pref_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, MAX_PAGE_SIZE, PARAM },
  74. { "page_duration", "preferred page duration, in microseconds",
  75. OFFSET(pref_duration), AV_OPT_TYPE_INT64, { .i64 = 1000000 }, 0, INT64_MAX, PARAM },
  76. { NULL },
  77. };
  78. static const AVClass ogg_muxer_class = {
  79. .class_name = "Ogg muxer",
  80. .item_name = av_default_item_name,
  81. .option = options,
  82. .version = LIBAVUTIL_VERSION_INT,
  83. };
  84. static void ogg_update_checksum(AVFormatContext *s, AVIOContext *pb, int64_t crc_offset)
  85. {
  86. int64_t pos = avio_tell(pb);
  87. uint32_t checksum = ffio_get_checksum(pb);
  88. avio_seek(pb, crc_offset, SEEK_SET);
  89. avio_wb32(pb, checksum);
  90. avio_seek(pb, pos, SEEK_SET);
  91. }
  92. static int ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
  93. {
  94. OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data;
  95. AVIOContext *pb;
  96. int64_t crc_offset;
  97. int ret, size;
  98. uint8_t *buf;
  99. ret = avio_open_dyn_buf(&pb);
  100. if (ret < 0)
  101. return ret;
  102. ffio_init_checksum(pb, ff_crc04C11DB7_update, 0);
  103. ffio_wfourcc(pb, "OggS");
  104. avio_w8(pb, 0);
  105. avio_w8(pb, page->flags | extra_flags);
  106. avio_wl64(pb, page->granule);
  107. avio_wl32(pb, oggstream->serial_num);
  108. avio_wl32(pb, oggstream->page_counter++);
  109. crc_offset = avio_tell(pb);
  110. avio_wl32(pb, 0); // crc
  111. avio_w8(pb, page->segments_count);
  112. avio_write(pb, page->segments, page->segments_count);
  113. avio_write(pb, page->data, page->size);
  114. ogg_update_checksum(s, pb, crc_offset);
  115. avio_flush(pb);
  116. size = avio_close_dyn_buf(pb, &buf);
  117. if (size < 0)
  118. return size;
  119. avio_write(s->pb, buf, size);
  120. avio_flush(s->pb);
  121. av_free(buf);
  122. oggstream->page_count--;
  123. return 0;
  124. }
  125. static int ogg_key_granule(OGGStreamContext *oggstream, int64_t granule)
  126. {
  127. return oggstream->kfgshift && !(granule & ((1<<oggstream->kfgshift)-1));
  128. }
  129. static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
  130. {
  131. if (oggstream->kfgshift)
  132. return (granule>>oggstream->kfgshift) +
  133. (granule & ((1<<oggstream->kfgshift)-1));
  134. else
  135. return granule;
  136. }
  137. static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
  138. {
  139. AVStream *st2 = s->streams[next->stream_index];
  140. AVStream *st = s->streams[page->stream_index];
  141. int64_t next_granule, cur_granule;
  142. if (next->granule == -1 || page->granule == -1)
  143. return 0;
  144. next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next->granule),
  145. st2->time_base, AV_TIME_BASE_Q);
  146. cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page->granule),
  147. st ->time_base, AV_TIME_BASE_Q);
  148. return next_granule > cur_granule;
  149. }
  150. static int ogg_reset_cur_page(OGGStreamContext *oggstream)
  151. {
  152. oggstream->page.granule = -1;
  153. oggstream->page.flags = 0;
  154. oggstream->page.segments_count = 0;
  155. oggstream->page.size = 0;
  156. return 0;
  157. }
  158. static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
  159. {
  160. OGGContext *ogg = s->priv_data;
  161. OGGPageList **p = &ogg->page_list;
  162. OGGPageList *l = av_mallocz(sizeof(*l));
  163. if (!l)
  164. return AVERROR(ENOMEM);
  165. l->page = oggstream->page;
  166. oggstream->page.start_granule = oggstream->page.granule;
  167. oggstream->page_count++;
  168. ogg_reset_cur_page(oggstream);
  169. while (*p) {
  170. if (ogg_compare_granule(s, &(*p)->page, &l->page))
  171. break;
  172. p = &(*p)->next;
  173. }
  174. l->next = *p;
  175. *p = l;
  176. return 0;
  177. }
  178. static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
  179. uint8_t *data, unsigned size, int64_t granule,
  180. int header)
  181. {
  182. OGGStreamContext *oggstream = st->priv_data;
  183. OGGContext *ogg = s->priv_data;
  184. int total_segments = size / 255 + 1;
  185. uint8_t *p = data;
  186. int i, segments, len, flush = 0;
  187. // Handles VFR by flushing page because this frame needs to have a timestamp
  188. // For theora, keyframes also need to have a timestamp to correctly mark
  189. // them as such, otherwise seeking will not work correctly at the very
  190. // least with old libogg versions.
  191. // Do not try to flush header packets though, that will create broken files.
  192. if (st->codec->codec_id == AV_CODEC_ID_THEORA && !header &&
  193. (ogg_granule_to_timestamp(oggstream, granule) >
  194. ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1 ||
  195. ogg_key_granule(oggstream, granule))) {
  196. if (oggstream->page.granule != -1)
  197. ogg_buffer_page(s, oggstream);
  198. flush = 1;
  199. }
  200. // avoid a continued page
  201. if (!header && oggstream->page.size > 0 &&
  202. MAX_PAGE_SIZE - oggstream->page.size < size) {
  203. ogg_buffer_page(s, oggstream);
  204. }
  205. for (i = 0; i < total_segments; ) {
  206. OGGPage *page = &oggstream->page;
  207. segments = FFMIN(total_segments - i, 255 - page->segments_count);
  208. if (i && !page->segments_count)
  209. page->flags |= 1; // continued packet
  210. memset(page->segments+page->segments_count, 255, segments - 1);
  211. page->segments_count += segments - 1;
  212. len = FFMIN(size, segments*255);
  213. page->segments[page->segments_count++] = len - (segments-1)*255;
  214. memcpy(page->data+page->size, p, len);
  215. p += len;
  216. size -= len;
  217. i += segments;
  218. page->size += len;
  219. if (i == total_segments)
  220. page->granule = granule;
  221. if (!header) {
  222. AVStream *st = s->streams[page->stream_index];
  223. int64_t start = av_rescale_q(page->start_granule, st->time_base,
  224. AV_TIME_BASE_Q);
  225. int64_t next = av_rescale_q(page->granule, st->time_base,
  226. AV_TIME_BASE_Q);
  227. if (page->segments_count == 255 ||
  228. (ogg->pref_size > 0 && page->size >= ogg->pref_size) ||
  229. (ogg->pref_duration > 0 && next - start >= ogg->pref_duration)) {
  230. ogg_buffer_page(s, oggstream);
  231. }
  232. }
  233. }
  234. if (flush && oggstream->page.granule != -1)
  235. ogg_buffer_page(s, oggstream);
  236. return 0;
  237. }
  238. static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact,
  239. int *header_len, AVDictionary **m, int framing_bit)
  240. {
  241. const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
  242. int size;
  243. uint8_t *p, *p0;
  244. unsigned int count;
  245. ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
  246. size = offset + ff_vorbiscomment_length(*m, vendor, &count) + framing_bit;
  247. p = av_mallocz(size);
  248. if (!p)
  249. return NULL;
  250. p0 = p;
  251. p += offset;
  252. ff_vorbiscomment_write(&p, m, vendor, count);
  253. if (framing_bit)
  254. bytestream_put_byte(&p, 1);
  255. *header_len = size;
  256. return p0;
  257. }
  258. static int ogg_build_flac_headers(AVCodecContext *avctx,
  259. OGGStreamContext *oggstream, int bitexact,
  260. AVDictionary **m)
  261. {
  262. enum FLACExtradataFormat format;
  263. uint8_t *streaminfo;
  264. uint8_t *p;
  265. if (!avpriv_flac_is_extradata_valid(avctx, &format, &streaminfo))
  266. return -1;
  267. // first packet: STREAMINFO
  268. oggstream->header_len[0] = 51;
  269. oggstream->header[0] = av_mallocz(51); // per ogg flac specs
  270. p = oggstream->header[0];
  271. if (!p)
  272. return AVERROR(ENOMEM);
  273. bytestream_put_byte(&p, 0x7F);
  274. bytestream_put_buffer(&p, "FLAC", 4);
  275. bytestream_put_byte(&p, 1); // major version
  276. bytestream_put_byte(&p, 0); // minor version
  277. bytestream_put_be16(&p, 1); // headers packets without this one
  278. bytestream_put_buffer(&p, "fLaC", 4);
  279. bytestream_put_byte(&p, 0x00); // streaminfo
  280. bytestream_put_be24(&p, 34);
  281. bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE);
  282. // second packet: VorbisComment
  283. p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m, 0);
  284. if (!p)
  285. return AVERROR(ENOMEM);
  286. oggstream->header[1] = p;
  287. bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
  288. bytestream_put_be24(&p, oggstream->header_len[1] - 4);
  289. return 0;
  290. }
  291. #define SPEEX_HEADER_SIZE 80
  292. static int ogg_build_speex_headers(AVCodecContext *avctx,
  293. OGGStreamContext *oggstream, int bitexact,
  294. AVDictionary **m)
  295. {
  296. uint8_t *p;
  297. if (avctx->extradata_size < SPEEX_HEADER_SIZE)
  298. return -1;
  299. // first packet: Speex header
  300. p = av_mallocz(SPEEX_HEADER_SIZE);
  301. if (!p)
  302. return AVERROR(ENOMEM);
  303. oggstream->header[0] = p;
  304. oggstream->header_len[0] = SPEEX_HEADER_SIZE;
  305. bytestream_put_buffer(&p, avctx->extradata, SPEEX_HEADER_SIZE);
  306. AV_WL32(&oggstream->header[0][68], 0); // set extra_headers to 0
  307. // second packet: VorbisComment
  308. p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m, 0);
  309. if (!p)
  310. return AVERROR(ENOMEM);
  311. oggstream->header[1] = p;
  312. return 0;
  313. }
  314. #define OPUS_HEADER_SIZE 19
  315. static int ogg_build_opus_headers(AVCodecContext *avctx,
  316. OGGStreamContext *oggstream, int bitexact,
  317. AVDictionary **m)
  318. {
  319. uint8_t *p;
  320. if (avctx->extradata_size < OPUS_HEADER_SIZE)
  321. return -1;
  322. /* first packet: Opus header */
  323. p = av_mallocz(avctx->extradata_size);
  324. if (!p)
  325. return AVERROR(ENOMEM);
  326. oggstream->header[0] = p;
  327. oggstream->header_len[0] = avctx->extradata_size;
  328. bytestream_put_buffer(&p, avctx->extradata, avctx->extradata_size);
  329. /* second packet: VorbisComment */
  330. p = ogg_write_vorbiscomment(8, bitexact, &oggstream->header_len[1], m, 0);
  331. if (!p)
  332. return AVERROR(ENOMEM);
  333. oggstream->header[1] = p;
  334. bytestream_put_buffer(&p, "OpusTags", 8);
  335. return 0;
  336. }
  337. static int ogg_write_header(AVFormatContext *s)
  338. {
  339. OGGContext *ogg = s->priv_data;
  340. OGGStreamContext *oggstream = NULL;
  341. int i, j;
  342. if (ogg->pref_size)
  343. av_log(s, AV_LOG_WARNING, "The pagesize option is deprecated\n");
  344. for (i = 0; i < s->nb_streams; i++) {
  345. AVStream *st = s->streams[i];
  346. unsigned serial_num = i;
  347. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  348. if (st->codec->codec_id == AV_CODEC_ID_OPUS)
  349. /* Opus requires a fixed 48kHz clock */
  350. avpriv_set_pts_info(st, 64, 1, 48000);
  351. else
  352. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  353. } else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  354. avpriv_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den);
  355. if (st->codec->codec_id != AV_CODEC_ID_VORBIS &&
  356. st->codec->codec_id != AV_CODEC_ID_THEORA &&
  357. st->codec->codec_id != AV_CODEC_ID_SPEEX &&
  358. st->codec->codec_id != AV_CODEC_ID_FLAC &&
  359. st->codec->codec_id != AV_CODEC_ID_OPUS) {
  360. av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
  361. return -1;
  362. }
  363. if (!st->codec->extradata || !st->codec->extradata_size) {
  364. av_log(s, AV_LOG_ERROR, "No extradata present\n");
  365. return -1;
  366. }
  367. oggstream = av_mallocz(sizeof(*oggstream));
  368. oggstream->page.stream_index = i;
  369. if (!(st->codec->flags & CODEC_FLAG_BITEXACT))
  370. do {
  371. serial_num = av_get_random_seed();
  372. for (j = 0; j < i; j++) {
  373. OGGStreamContext *sc = s->streams[j]->priv_data;
  374. if (serial_num == sc->serial_num)
  375. break;
  376. }
  377. } while (j < i);
  378. oggstream->serial_num = serial_num;
  379. av_dict_copy(&st->metadata, s->metadata, AV_DICT_DONT_OVERWRITE);
  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. &st->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. &st->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. &st->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], &st->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. .flags = AVFMT_TS_NEGATIVE,
  543. .priv_class = &ogg_muxer_class,
  544. };