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.

703 lines
23KB

  1. /*
  2. * Ogg muxer
  3. * Copyright (c) 2007 Baptiste Coudurier <baptiste dot coudurier at free dot fr>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. { "pagesize", "preferred page size in bytes (deprecated)",
  72. OFFSET(pref_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, MAX_PAGE_SIZE, PARAM },
  73. { "page_duration", "preferred page duration, in microseconds",
  74. OFFSET(pref_duration), AV_OPT_TYPE_INT64, { .i64 = 1000000 }, 0, INT64_MAX, PARAM },
  75. { NULL },
  76. };
  77. #define OGG_CLASS(flavor)\
  78. static const AVClass flavor ## _muxer_class = {\
  79. .class_name = #flavor " 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 int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
  126. {
  127. if (oggstream->kfgshift)
  128. return (granule>>oggstream->kfgshift) +
  129. (granule & ((1<<oggstream->kfgshift)-1));
  130. else
  131. return granule;
  132. }
  133. static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
  134. {
  135. AVStream *st2 = s->streams[next->stream_index];
  136. AVStream *st = s->streams[page->stream_index];
  137. int64_t next_granule, cur_granule;
  138. if (next->granule == -1 || page->granule == -1)
  139. return 0;
  140. next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next->granule),
  141. st2->time_base, AV_TIME_BASE_Q);
  142. cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page->granule),
  143. st ->time_base, AV_TIME_BASE_Q);
  144. return next_granule > cur_granule;
  145. }
  146. static int ogg_reset_cur_page(OGGStreamContext *oggstream)
  147. {
  148. oggstream->page.granule = -1;
  149. oggstream->page.flags = 0;
  150. oggstream->page.segments_count = 0;
  151. oggstream->page.size = 0;
  152. return 0;
  153. }
  154. static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
  155. {
  156. OGGContext *ogg = s->priv_data;
  157. OGGPageList **p = &ogg->page_list;
  158. OGGPageList *l = av_mallocz(sizeof(*l));
  159. if (!l)
  160. return AVERROR(ENOMEM);
  161. l->page = oggstream->page;
  162. oggstream->page.start_granule = oggstream->page.granule;
  163. oggstream->page_count++;
  164. ogg_reset_cur_page(oggstream);
  165. while (*p) {
  166. if (ogg_compare_granule(s, &(*p)->page, &l->page))
  167. break;
  168. p = &(*p)->next;
  169. }
  170. l->next = *p;
  171. *p = l;
  172. return 0;
  173. }
  174. static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
  175. uint8_t *data, unsigned size, int64_t granule,
  176. int header)
  177. {
  178. OGGStreamContext *oggstream = st->priv_data;
  179. OGGContext *ogg = s->priv_data;
  180. int total_segments = size / 255 + 1;
  181. uint8_t *p = data;
  182. int i, segments, len, flush = 0;
  183. // Handles VFR by flushing page because this frame needs to have a timestamp
  184. if (st->codec->codec_id == AV_CODEC_ID_THEORA && !header &&
  185. ogg_granule_to_timestamp(oggstream, granule) >
  186. ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1) {
  187. if (oggstream->page.granule != -1)
  188. ogg_buffer_page(s, oggstream);
  189. flush = 1;
  190. }
  191. // avoid a continued page
  192. if (!header && oggstream->page.size > 0 &&
  193. MAX_PAGE_SIZE - oggstream->page.size < size) {
  194. ogg_buffer_page(s, oggstream);
  195. }
  196. for (i = 0; i < total_segments; ) {
  197. OGGPage *page = &oggstream->page;
  198. segments = FFMIN(total_segments - i, 255 - page->segments_count);
  199. if (i && !page->segments_count)
  200. page->flags |= 1; // continued packet
  201. memset(page->segments+page->segments_count, 255, segments - 1);
  202. page->segments_count += segments - 1;
  203. len = FFMIN(size, segments*255);
  204. page->segments[page->segments_count++] = len - (segments-1)*255;
  205. memcpy(page->data+page->size, p, len);
  206. p += len;
  207. size -= len;
  208. i += segments;
  209. page->size += len;
  210. if (i == total_segments)
  211. page->granule = granule;
  212. if (!header) {
  213. AVStream *st = s->streams[page->stream_index];
  214. int64_t start = av_rescale_q(page->start_granule, st->time_base,
  215. AV_TIME_BASE_Q);
  216. int64_t next = av_rescale_q(page->granule, st->time_base,
  217. AV_TIME_BASE_Q);
  218. if (page->segments_count == 255 ||
  219. (ogg->pref_size > 0 && page->size >= ogg->pref_size) ||
  220. (ogg->pref_duration > 0 && next - start >= ogg->pref_duration)) {
  221. ogg_buffer_page(s, oggstream);
  222. }
  223. }
  224. }
  225. if (flush && oggstream->page.granule != -1)
  226. ogg_buffer_page(s, oggstream);
  227. return 0;
  228. }
  229. static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact,
  230. int *header_len, AVDictionary **m, int framing_bit)
  231. {
  232. const char *vendor = bitexact ? "Libav" : LIBAVFORMAT_IDENT;
  233. int size;
  234. uint8_t *p, *p0;
  235. ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
  236. size = offset + ff_vorbiscomment_length(*m, vendor) + framing_bit;
  237. p = av_mallocz(size);
  238. if (!p)
  239. return NULL;
  240. p0 = p;
  241. p += offset;
  242. ff_vorbiscomment_write(&p, m, vendor);
  243. if (framing_bit)
  244. bytestream_put_byte(&p, 1);
  245. *header_len = size;
  246. return p0;
  247. }
  248. static int ogg_build_flac_headers(AVCodecContext *avctx,
  249. OGGStreamContext *oggstream, int bitexact,
  250. AVDictionary **m)
  251. {
  252. enum FLACExtradataFormat format;
  253. uint8_t *streaminfo;
  254. uint8_t *p;
  255. if (!avpriv_flac_is_extradata_valid(avctx, &format, &streaminfo))
  256. return -1;
  257. // first packet: STREAMINFO
  258. oggstream->header_len[0] = 51;
  259. oggstream->header[0] = av_mallocz(51); // per ogg flac specs
  260. p = oggstream->header[0];
  261. if (!p)
  262. return AVERROR(ENOMEM);
  263. bytestream_put_byte(&p, 0x7F);
  264. bytestream_put_buffer(&p, "FLAC", 4);
  265. bytestream_put_byte(&p, 1); // major version
  266. bytestream_put_byte(&p, 0); // minor version
  267. bytestream_put_be16(&p, 1); // headers packets without this one
  268. bytestream_put_buffer(&p, "fLaC", 4);
  269. bytestream_put_byte(&p, 0x00); // streaminfo
  270. bytestream_put_be24(&p, 34);
  271. bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE);
  272. // second packet: VorbisComment
  273. p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m, 0);
  274. if (!p)
  275. return AVERROR(ENOMEM);
  276. oggstream->header[1] = p;
  277. bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
  278. bytestream_put_be24(&p, oggstream->header_len[1] - 4);
  279. return 0;
  280. }
  281. #define SPEEX_HEADER_SIZE 80
  282. static int ogg_build_speex_headers(AVCodecContext *avctx,
  283. OGGStreamContext *oggstream, int bitexact,
  284. AVDictionary **m)
  285. {
  286. uint8_t *p;
  287. if (avctx->extradata_size < SPEEX_HEADER_SIZE)
  288. return -1;
  289. // first packet: Speex header
  290. p = av_mallocz(SPEEX_HEADER_SIZE);
  291. if (!p)
  292. return AVERROR(ENOMEM);
  293. oggstream->header[0] = p;
  294. oggstream->header_len[0] = SPEEX_HEADER_SIZE;
  295. bytestream_put_buffer(&p, avctx->extradata, SPEEX_HEADER_SIZE);
  296. AV_WL32(&oggstream->header[0][68], 0); // set extra_headers to 0
  297. // second packet: VorbisComment
  298. p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m, 0);
  299. if (!p)
  300. return AVERROR(ENOMEM);
  301. oggstream->header[1] = p;
  302. return 0;
  303. }
  304. #define OPUS_HEADER_SIZE 19
  305. static int ogg_build_opus_headers(AVCodecContext *avctx,
  306. OGGStreamContext *oggstream, int bitexact,
  307. AVDictionary **m)
  308. {
  309. uint8_t *p;
  310. if (avctx->extradata_size < OPUS_HEADER_SIZE)
  311. return -1;
  312. /* first packet: Opus header */
  313. p = av_mallocz(avctx->extradata_size);
  314. if (!p)
  315. return AVERROR(ENOMEM);
  316. oggstream->header[0] = p;
  317. oggstream->header_len[0] = avctx->extradata_size;
  318. bytestream_put_buffer(&p, avctx->extradata, avctx->extradata_size);
  319. /* second packet: VorbisComment */
  320. p = ogg_write_vorbiscomment(8, bitexact, &oggstream->header_len[1], m, 0);
  321. if (!p)
  322. return AVERROR(ENOMEM);
  323. oggstream->header[1] = p;
  324. bytestream_put_buffer(&p, "OpusTags", 8);
  325. return 0;
  326. }
  327. static void ogg_write_pages(AVFormatContext *s, int flush)
  328. {
  329. OGGContext *ogg = s->priv_data;
  330. OGGPageList *next, *p;
  331. if (!ogg->page_list)
  332. return;
  333. for (p = ogg->page_list; p; ) {
  334. OGGStreamContext *oggstream =
  335. s->streams[p->page.stream_index]->priv_data;
  336. if (oggstream->page_count < 2 && !flush)
  337. break;
  338. ogg_write_page(s, &p->page,
  339. flush == 1 && oggstream->page_count == 1 ? 4 : 0); // eos
  340. next = p->next;
  341. av_freep(&p);
  342. p = next;
  343. }
  344. ogg->page_list = p;
  345. }
  346. static int ogg_write_header(AVFormatContext *s)
  347. {
  348. OGGContext *ogg = s->priv_data;
  349. OGGStreamContext *oggstream;
  350. int i, j;
  351. if (ogg->pref_size)
  352. av_log(s, AV_LOG_WARNING, "The pagesize option is deprecated\n");
  353. for (i = 0; i < s->nb_streams; i++) {
  354. AVStream *st = s->streams[i];
  355. unsigned serial_num = i;
  356. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  357. if (st->codec->codec_id == AV_CODEC_ID_OPUS)
  358. /* Opus requires a fixed 48kHz clock */
  359. avpriv_set_pts_info(st, 64, 1, 48000);
  360. else
  361. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  362. if (st->codec->codec_id != AV_CODEC_ID_VORBIS &&
  363. st->codec->codec_id != AV_CODEC_ID_THEORA &&
  364. st->codec->codec_id != AV_CODEC_ID_SPEEX &&
  365. st->codec->codec_id != AV_CODEC_ID_FLAC &&
  366. st->codec->codec_id != AV_CODEC_ID_OPUS) {
  367. av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
  368. return -1;
  369. }
  370. if (!st->codec->extradata || !st->codec->extradata_size) {
  371. av_log(s, AV_LOG_ERROR, "No extradata present\n");
  372. return -1;
  373. }
  374. oggstream = av_mallocz(sizeof(*oggstream));
  375. oggstream->page.stream_index = i;
  376. if (!(s->flags & AVFMT_FLAG_BITEXACT))
  377. do {
  378. serial_num = av_get_random_seed();
  379. for (j = 0; j < i; j++) {
  380. OGGStreamContext *sc = s->streams[j]->priv_data;
  381. if (serial_num == sc->serial_num)
  382. break;
  383. }
  384. } while (j < i);
  385. oggstream->serial_num = serial_num;
  386. st->priv_data = oggstream;
  387. if (st->codec->codec_id == AV_CODEC_ID_FLAC) {
  388. int err = ogg_build_flac_headers(st->codec, oggstream,
  389. s->flags & AVFMT_FLAG_BITEXACT,
  390. &s->metadata);
  391. if (err) {
  392. av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
  393. av_freep(&st->priv_data);
  394. return err;
  395. }
  396. } else if (st->codec->codec_id == AV_CODEC_ID_SPEEX) {
  397. int err = ogg_build_speex_headers(st->codec, oggstream,
  398. s->flags & AVFMT_FLAG_BITEXACT,
  399. &s->metadata);
  400. if (err) {
  401. av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
  402. av_freep(&st->priv_data);
  403. return err;
  404. }
  405. } else if (st->codec->codec_id == AV_CODEC_ID_OPUS) {
  406. int err = ogg_build_opus_headers(st->codec, oggstream,
  407. s->flags & AVFMT_FLAG_BITEXACT,
  408. &s->metadata);
  409. if (err) {
  410. av_log(s, AV_LOG_ERROR, "Error writing Opus headers\n");
  411. av_freep(&st->priv_data);
  412. return err;
  413. }
  414. } else {
  415. uint8_t *p;
  416. const char *cstr = st->codec->codec_id == AV_CODEC_ID_VORBIS ? "vorbis" : "theora";
  417. int header_type = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 3 : 0x81;
  418. int framing_bit = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 1 : 0;
  419. if (avpriv_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
  420. st->codec->codec_id == AV_CODEC_ID_VORBIS ? 30 : 42,
  421. oggstream->header, oggstream->header_len) < 0) {
  422. av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
  423. av_freep(&st->priv_data);
  424. return -1;
  425. }
  426. p = ogg_write_vorbiscomment(7, s->flags & AVFMT_FLAG_BITEXACT,
  427. &oggstream->header_len[1], &s->metadata,
  428. framing_bit);
  429. oggstream->header[1] = p;
  430. if (!p)
  431. return AVERROR(ENOMEM);
  432. bytestream_put_byte(&p, header_type);
  433. bytestream_put_buffer(&p, cstr, 6);
  434. if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
  435. /** KFGSHIFT is the width of the less significant section of the granule position
  436. The less significant section is the frame count since the last keyframe */
  437. oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
  438. oggstream->vrev = oggstream->header[0][9];
  439. av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
  440. oggstream->kfgshift, oggstream->vrev);
  441. }
  442. }
  443. }
  444. for (j = 0; j < s->nb_streams; j++) {
  445. OGGStreamContext *oggstream = s->streams[j]->priv_data;
  446. ogg_buffer_data(s, s->streams[j], oggstream->header[0],
  447. oggstream->header_len[0], 0, 1);
  448. oggstream->page.flags |= 2; // bos
  449. ogg_buffer_page(s, oggstream);
  450. }
  451. for (j = 0; j < s->nb_streams; j++) {
  452. AVStream *st = s->streams[j];
  453. OGGStreamContext *oggstream = st->priv_data;
  454. for (i = 1; i < 3; i++) {
  455. if (oggstream && oggstream->header_len[i])
  456. ogg_buffer_data(s, st, oggstream->header[i],
  457. oggstream->header_len[i], 0, 1);
  458. }
  459. ogg_buffer_page(s, oggstream);
  460. }
  461. oggstream->page.start_granule = AV_NOPTS_VALUE;
  462. ogg_write_pages(s, 2);
  463. return 0;
  464. }
  465. static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
  466. {
  467. AVStream *st = s->streams[pkt->stream_index];
  468. OGGStreamContext *oggstream = st->priv_data;
  469. int ret;
  470. int64_t granule;
  471. if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
  472. int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
  473. int pframe_count;
  474. if (pkt->flags & AV_PKT_FLAG_KEY)
  475. oggstream->last_kf_pts = pts;
  476. pframe_count = pts - oggstream->last_kf_pts;
  477. // prevent frame count from overflow if key frame flag is not set
  478. if (pframe_count >= (1<<oggstream->kfgshift)) {
  479. oggstream->last_kf_pts += pframe_count;
  480. pframe_count = 0;
  481. }
  482. granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
  483. } else if (st->codec->codec_id == AV_CODEC_ID_OPUS)
  484. granule = pkt->pts + pkt->duration +
  485. av_rescale_q(st->codec->initial_padding,
  486. (AVRational){ 1, st->codec->sample_rate },
  487. st->time_base);
  488. else
  489. granule = pkt->pts + pkt->duration;
  490. if (oggstream->page.start_granule == AV_NOPTS_VALUE)
  491. oggstream->page.start_granule = pkt->pts;
  492. ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0);
  493. if (ret < 0)
  494. return ret;
  495. ogg_write_pages(s, 0);
  496. oggstream->last_granule = granule;
  497. return 0;
  498. }
  499. static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
  500. {
  501. int i;
  502. if (pkt)
  503. return ogg_write_packet_internal(s, pkt);
  504. for (i = 0; i < s->nb_streams; i++) {
  505. OGGStreamContext *oggstream = s->streams[i]->priv_data;
  506. if (oggstream->page.segments_count)
  507. ogg_buffer_page(s, oggstream);
  508. }
  509. ogg_write_pages(s, 2);
  510. return 0;
  511. }
  512. static int ogg_write_trailer(AVFormatContext *s)
  513. {
  514. int i;
  515. /* flush current page if needed */
  516. for (i = 0; i < s->nb_streams; i++) {
  517. OGGStreamContext *oggstream = s->streams[i]->priv_data;
  518. if (oggstream->page.size > 0)
  519. ogg_buffer_page(s, oggstream);
  520. }
  521. ogg_write_pages(s, 1);
  522. for (i = 0; i < s->nb_streams; i++) {
  523. AVStream *st = s->streams[i];
  524. OGGStreamContext *oggstream = st->priv_data;
  525. if (st->codec->codec_id == AV_CODEC_ID_FLAC ||
  526. st->codec->codec_id == AV_CODEC_ID_SPEEX ||
  527. st->codec->codec_id == AV_CODEC_ID_OPUS) {
  528. av_free(oggstream->header[0]);
  529. }
  530. av_freep(&oggstream->header[1]);
  531. av_freep(&st->priv_data);
  532. }
  533. return 0;
  534. }
  535. #if CONFIG_OGG_MUXER
  536. OGG_CLASS(ogg)
  537. AVOutputFormat ff_ogg_muxer = {
  538. .name = "ogg",
  539. .long_name = NULL_IF_CONFIG_SMALL("Ogg"),
  540. .mime_type = "application/ogg",
  541. .extensions = "ogg,ogv",
  542. .priv_data_size = sizeof(OGGContext),
  543. .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
  544. AV_CODEC_ID_VORBIS : AV_CODEC_ID_FLAC,
  545. .video_codec = AV_CODEC_ID_THEORA,
  546. .write_header = ogg_write_header,
  547. .write_packet = ogg_write_packet,
  548. .write_trailer = ogg_write_trailer,
  549. .flags = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
  550. .priv_class = &ogg_muxer_class,
  551. };
  552. #endif
  553. #if CONFIG_OGA_MUXER
  554. OGG_CLASS(oga)
  555. AVOutputFormat ff_oga_muxer = {
  556. .name = "oga",
  557. .long_name = NULL_IF_CONFIG_SMALL("Ogg Audio"),
  558. .mime_type = "audio/ogg",
  559. .extensions = "oga",
  560. .priv_data_size = sizeof(OGGContext),
  561. .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
  562. AV_CODEC_ID_VORBIS : AV_CODEC_ID_FLAC,
  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 = &oga_muxer_class,
  568. };
  569. #endif
  570. #if CONFIG_SPX_MUXER
  571. OGG_CLASS(spx)
  572. AVOutputFormat ff_spx_muxer = {
  573. .name = "spx",
  574. .long_name = NULL_IF_CONFIG_SMALL("Ogg Speex"),
  575. .mime_type = "audio/ogg",
  576. .extensions = "spx",
  577. .priv_data_size = sizeof(OGGContext),
  578. .audio_codec = AV_CODEC_ID_SPEEX,
  579. .write_header = ogg_write_header,
  580. .write_packet = ogg_write_packet,
  581. .write_trailer = ogg_write_trailer,
  582. .flags = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
  583. .priv_class = &spx_muxer_class,
  584. };
  585. #endif
  586. #if CONFIG_OPUS_MUXER
  587. OGG_CLASS(opus)
  588. AVOutputFormat ff_opus_muxer = {
  589. .name = "opus",
  590. .long_name = NULL_IF_CONFIG_SMALL("Ogg Opus"),
  591. .mime_type = "audio/ogg",
  592. .extensions = "opus",
  593. .priv_data_size = sizeof(OGGContext),
  594. .audio_codec = AV_CODEC_ID_OPUS,
  595. .write_header = ogg_write_header,
  596. .write_packet = ogg_write_packet,
  597. .write_trailer = ogg_write_trailer,
  598. .flags = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
  599. .priv_class = &opus_muxer_class,
  600. };
  601. #endif