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.

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