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.

725 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 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. #define OGG_CLASS(flavor, name)\
  80. static const AVClass flavor ## _muxer_class = {\
  81. .class_name = #name " muxer",\
  82. .item_name = av_default_item_name,\
  83. .option = options,\
  84. .version = LIBAVUTIL_VERSION_INT,\
  85. };
  86. static void ogg_update_checksum(AVFormatContext *s, AVIOContext *pb, int64_t crc_offset)
  87. {
  88. int64_t pos = avio_tell(pb);
  89. uint32_t checksum = ffio_get_checksum(pb);
  90. avio_seek(pb, crc_offset, SEEK_SET);
  91. avio_wb32(pb, checksum);
  92. avio_seek(pb, pos, SEEK_SET);
  93. }
  94. static int ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
  95. {
  96. OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data;
  97. AVIOContext *pb;
  98. int64_t crc_offset;
  99. int ret, size;
  100. uint8_t *buf;
  101. ret = avio_open_dyn_buf(&pb);
  102. if (ret < 0)
  103. return ret;
  104. ffio_init_checksum(pb, ff_crc04C11DB7_update, 0);
  105. ffio_wfourcc(pb, "OggS");
  106. avio_w8(pb, 0);
  107. avio_w8(pb, page->flags | extra_flags);
  108. avio_wl64(pb, page->granule);
  109. avio_wl32(pb, oggstream->serial_num);
  110. avio_wl32(pb, oggstream->page_counter++);
  111. crc_offset = avio_tell(pb);
  112. avio_wl32(pb, 0); // crc
  113. avio_w8(pb, page->segments_count);
  114. avio_write(pb, page->segments, page->segments_count);
  115. avio_write(pb, page->data, page->size);
  116. ogg_update_checksum(s, pb, crc_offset);
  117. avio_flush(pb);
  118. size = avio_close_dyn_buf(pb, &buf);
  119. if (size < 0)
  120. return size;
  121. avio_write(s->pb, buf, size);
  122. avio_flush(s->pb);
  123. av_free(buf);
  124. oggstream->page_count--;
  125. return 0;
  126. }
  127. static int ogg_key_granule(OGGStreamContext *oggstream, int64_t granule)
  128. {
  129. return oggstream->kfgshift && !(granule & ((1<<oggstream->kfgshift)-1));
  130. }
  131. static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
  132. {
  133. if (oggstream->kfgshift)
  134. return (granule>>oggstream->kfgshift) +
  135. (granule & ((1<<oggstream->kfgshift)-1));
  136. else
  137. return granule;
  138. }
  139. static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
  140. {
  141. AVStream *st2 = s->streams[next->stream_index];
  142. AVStream *st = s->streams[page->stream_index];
  143. int64_t next_granule, cur_granule;
  144. if (next->granule == -1 || page->granule == -1)
  145. return 0;
  146. next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next->granule),
  147. st2->time_base, AV_TIME_BASE_Q);
  148. cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page->granule),
  149. st ->time_base, AV_TIME_BASE_Q);
  150. return next_granule > cur_granule;
  151. }
  152. static int ogg_reset_cur_page(OGGStreamContext *oggstream)
  153. {
  154. oggstream->page.granule = -1;
  155. oggstream->page.flags = 0;
  156. oggstream->page.segments_count = 0;
  157. oggstream->page.size = 0;
  158. return 0;
  159. }
  160. static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
  161. {
  162. OGGContext *ogg = s->priv_data;
  163. OGGPageList **p = &ogg->page_list;
  164. OGGPageList *l = av_mallocz(sizeof(*l));
  165. if (!l)
  166. return AVERROR(ENOMEM);
  167. l->page = oggstream->page;
  168. oggstream->page.start_granule = oggstream->page.granule;
  169. oggstream->page_count++;
  170. ogg_reset_cur_page(oggstream);
  171. while (*p) {
  172. if (ogg_compare_granule(s, &(*p)->page, &l->page))
  173. break;
  174. p = &(*p)->next;
  175. }
  176. l->next = *p;
  177. *p = l;
  178. return 0;
  179. }
  180. static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
  181. uint8_t *data, unsigned size, int64_t granule,
  182. int header)
  183. {
  184. OGGStreamContext *oggstream = st->priv_data;
  185. OGGContext *ogg = s->priv_data;
  186. int total_segments = size / 255 + 1;
  187. uint8_t *p = data;
  188. int i, segments, len, flush = 0;
  189. // Handles VFR by flushing page because this frame needs to have a timestamp
  190. // For theora, keyframes also need to have a timestamp to correctly mark
  191. // them as such, otherwise seeking will not work correctly at the very
  192. // least with old libogg versions.
  193. // Do not try to flush header packets though, that will create broken files.
  194. if (st->codec->codec_id == AV_CODEC_ID_THEORA && !header &&
  195. (ogg_granule_to_timestamp(oggstream, granule) >
  196. ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1 ||
  197. ogg_key_granule(oggstream, granule))) {
  198. if (oggstream->page.granule != -1)
  199. ogg_buffer_page(s, oggstream);
  200. flush = 1;
  201. }
  202. // avoid a continued page
  203. if (!header && oggstream->page.size > 0 &&
  204. MAX_PAGE_SIZE - oggstream->page.size < size) {
  205. ogg_buffer_page(s, oggstream);
  206. }
  207. for (i = 0; i < total_segments; ) {
  208. OGGPage *page = &oggstream->page;
  209. segments = FFMIN(total_segments - i, 255 - page->segments_count);
  210. if (i && !page->segments_count)
  211. page->flags |= 1; // continued packet
  212. memset(page->segments+page->segments_count, 255, segments - 1);
  213. page->segments_count += segments - 1;
  214. len = FFMIN(size, segments*255);
  215. page->segments[page->segments_count++] = len - (segments-1)*255;
  216. memcpy(page->data+page->size, p, len);
  217. p += len;
  218. size -= len;
  219. i += segments;
  220. page->size += len;
  221. if (i == total_segments)
  222. page->granule = granule;
  223. if (!header) {
  224. AVStream *st = s->streams[page->stream_index];
  225. int64_t start = av_rescale_q(page->start_granule, st->time_base,
  226. AV_TIME_BASE_Q);
  227. int64_t next = av_rescale_q(page->granule, st->time_base,
  228. AV_TIME_BASE_Q);
  229. if (page->segments_count == 255 ||
  230. (ogg->pref_size > 0 && page->size >= ogg->pref_size) ||
  231. (ogg->pref_duration > 0 && next - start >= ogg->pref_duration)) {
  232. ogg_buffer_page(s, oggstream);
  233. }
  234. }
  235. }
  236. if (flush && oggstream->page.granule != -1)
  237. ogg_buffer_page(s, oggstream);
  238. return 0;
  239. }
  240. static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact,
  241. int *header_len, AVDictionary **m, int framing_bit)
  242. {
  243. const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
  244. int size;
  245. uint8_t *p, *p0;
  246. ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
  247. size = offset + ff_vorbiscomment_length(*m, vendor) + 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);
  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 void ogg_write_pages(AVFormatContext *s, int flush)
  339. {
  340. OGGContext *ogg = s->priv_data;
  341. OGGPageList *next, *p;
  342. if (!ogg->page_list)
  343. return;
  344. for (p = ogg->page_list; p; ) {
  345. OGGStreamContext *oggstream =
  346. s->streams[p->page.stream_index]->priv_data;
  347. if (oggstream->page_count < 2 && !flush)
  348. break;
  349. ogg_write_page(s, &p->page,
  350. flush == 1 && oggstream->page_count == 1 ? 4 : 0); // eos
  351. next = p->next;
  352. av_freep(&p);
  353. p = next;
  354. }
  355. ogg->page_list = p;
  356. }
  357. static int ogg_write_header(AVFormatContext *s)
  358. {
  359. OGGContext *ogg = s->priv_data;
  360. OGGStreamContext *oggstream = NULL;
  361. int i, j;
  362. if (ogg->pref_size)
  363. av_log(s, AV_LOG_WARNING, "The pagesize option is deprecated\n");
  364. for (i = 0; i < s->nb_streams; i++) {
  365. AVStream *st = s->streams[i];
  366. unsigned serial_num = i;
  367. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  368. if (st->codec->codec_id == AV_CODEC_ID_OPUS)
  369. /* Opus requires a fixed 48kHz clock */
  370. avpriv_set_pts_info(st, 64, 1, 48000);
  371. else
  372. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  373. }
  374. if (st->codec->codec_id != AV_CODEC_ID_VORBIS &&
  375. st->codec->codec_id != AV_CODEC_ID_THEORA &&
  376. st->codec->codec_id != AV_CODEC_ID_SPEEX &&
  377. st->codec->codec_id != AV_CODEC_ID_FLAC &&
  378. st->codec->codec_id != AV_CODEC_ID_OPUS) {
  379. av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
  380. return -1;
  381. }
  382. if (!st->codec->extradata || !st->codec->extradata_size) {
  383. av_log(s, AV_LOG_ERROR, "No extradata present\n");
  384. return -1;
  385. }
  386. oggstream = av_mallocz(sizeof(*oggstream));
  387. if (!oggstream)
  388. return AVERROR(ENOMEM);
  389. oggstream->page.stream_index = i;
  390. if (!(s->flags & AVFMT_FLAG_BITEXACT))
  391. do {
  392. serial_num = av_get_random_seed();
  393. for (j = 0; j < i; j++) {
  394. OGGStreamContext *sc = s->streams[j]->priv_data;
  395. if (serial_num == sc->serial_num)
  396. break;
  397. }
  398. } while (j < i);
  399. oggstream->serial_num = serial_num;
  400. av_dict_copy(&st->metadata, s->metadata, AV_DICT_DONT_OVERWRITE);
  401. st->priv_data = oggstream;
  402. if (st->codec->codec_id == AV_CODEC_ID_FLAC) {
  403. int err = ogg_build_flac_headers(st->codec, oggstream,
  404. s->flags & AVFMT_FLAG_BITEXACT,
  405. &st->metadata);
  406. if (err) {
  407. av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
  408. av_freep(&st->priv_data);
  409. return err;
  410. }
  411. } else if (st->codec->codec_id == AV_CODEC_ID_SPEEX) {
  412. int err = ogg_build_speex_headers(st->codec, oggstream,
  413. s->flags & AVFMT_FLAG_BITEXACT,
  414. &st->metadata);
  415. if (err) {
  416. av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
  417. av_freep(&st->priv_data);
  418. return err;
  419. }
  420. } else if (st->codec->codec_id == AV_CODEC_ID_OPUS) {
  421. int err = ogg_build_opus_headers(st->codec, oggstream,
  422. s->flags & AVFMT_FLAG_BITEXACT,
  423. &st->metadata);
  424. if (err) {
  425. av_log(s, AV_LOG_ERROR, "Error writing Opus headers\n");
  426. av_freep(&st->priv_data);
  427. return err;
  428. }
  429. } else {
  430. uint8_t *p;
  431. const char *cstr = st->codec->codec_id == AV_CODEC_ID_VORBIS ? "vorbis" : "theora";
  432. int header_type = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 3 : 0x81;
  433. int framing_bit = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 1 : 0;
  434. if (avpriv_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
  435. st->codec->codec_id == AV_CODEC_ID_VORBIS ? 30 : 42,
  436. oggstream->header, oggstream->header_len) < 0) {
  437. av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
  438. av_freep(&st->priv_data);
  439. return -1;
  440. }
  441. p = ogg_write_vorbiscomment(7, s->flags & AVFMT_FLAG_BITEXACT,
  442. &oggstream->header_len[1], &st->metadata,
  443. framing_bit);
  444. oggstream->header[1] = p;
  445. if (!p)
  446. return AVERROR(ENOMEM);
  447. bytestream_put_byte(&p, header_type);
  448. bytestream_put_buffer(&p, cstr, 6);
  449. if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
  450. /** KFGSHIFT is the width of the less significant section of the granule position
  451. The less significant section is the frame count since the last keyframe */
  452. oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
  453. oggstream->vrev = oggstream->header[0][9];
  454. av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
  455. oggstream->kfgshift, oggstream->vrev);
  456. }
  457. }
  458. }
  459. for (j = 0; j < s->nb_streams; j++) {
  460. OGGStreamContext *oggstream = s->streams[j]->priv_data;
  461. ogg_buffer_data(s, s->streams[j], oggstream->header[0],
  462. oggstream->header_len[0], 0, 1);
  463. oggstream->page.flags |= 2; // bos
  464. ogg_buffer_page(s, oggstream);
  465. }
  466. for (j = 0; j < s->nb_streams; j++) {
  467. AVStream *st = s->streams[j];
  468. OGGStreamContext *oggstream = st->priv_data;
  469. for (i = 1; i < 3; i++) {
  470. if (oggstream->header_len[i])
  471. ogg_buffer_data(s, st, oggstream->header[i],
  472. oggstream->header_len[i], 0, 1);
  473. }
  474. ogg_buffer_page(s, oggstream);
  475. }
  476. oggstream->page.start_granule = AV_NOPTS_VALUE;
  477. ogg_write_pages(s, 2);
  478. return 0;
  479. }
  480. static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
  481. {
  482. AVStream *st = s->streams[pkt->stream_index];
  483. OGGStreamContext *oggstream = st->priv_data;
  484. int ret;
  485. int64_t granule;
  486. if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
  487. int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
  488. int pframe_count;
  489. if (pkt->flags & AV_PKT_FLAG_KEY)
  490. oggstream->last_kf_pts = pts;
  491. pframe_count = pts - oggstream->last_kf_pts;
  492. // prevent frame count from overflow if key frame flag is not set
  493. if (pframe_count >= (1<<oggstream->kfgshift)) {
  494. oggstream->last_kf_pts += pframe_count;
  495. pframe_count = 0;
  496. }
  497. granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
  498. } else if (st->codec->codec_id == AV_CODEC_ID_OPUS)
  499. granule = pkt->pts + pkt->duration + av_rescale_q(st->codec->delay, (AVRational){ 1, st->codec->sample_rate }, st->time_base);
  500. else
  501. granule = pkt->pts + pkt->duration;
  502. if (oggstream->page.start_granule == AV_NOPTS_VALUE)
  503. oggstream->page.start_granule = pkt->pts;
  504. ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0);
  505. if (ret < 0)
  506. return ret;
  507. ogg_write_pages(s, 0);
  508. oggstream->last_granule = granule;
  509. return 0;
  510. }
  511. static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
  512. {
  513. int i;
  514. if (pkt)
  515. return ogg_write_packet_internal(s, pkt);
  516. for (i = 0; i < s->nb_streams; i++) {
  517. OGGStreamContext *oggstream = s->streams[i]->priv_data;
  518. if (oggstream->page.segments_count)
  519. ogg_buffer_page(s, oggstream);
  520. }
  521. ogg_write_pages(s, 2);
  522. return 0;
  523. }
  524. static int ogg_write_trailer(AVFormatContext *s)
  525. {
  526. int i;
  527. /* flush current page if needed */
  528. for (i = 0; i < s->nb_streams; i++) {
  529. OGGStreamContext *oggstream = s->streams[i]->priv_data;
  530. if (oggstream->page.size > 0)
  531. ogg_buffer_page(s, oggstream);
  532. }
  533. ogg_write_pages(s, 1);
  534. for (i = 0; i < s->nb_streams; i++) {
  535. AVStream *st = s->streams[i];
  536. OGGStreamContext *oggstream = st->priv_data;
  537. if (st->codec->codec_id == AV_CODEC_ID_FLAC ||
  538. st->codec->codec_id == AV_CODEC_ID_SPEEX ||
  539. st->codec->codec_id == AV_CODEC_ID_OPUS) {
  540. av_freep(&oggstream->header[0]);
  541. }
  542. av_freep(&oggstream->header[1]);
  543. av_freep(&st->priv_data);
  544. }
  545. return 0;
  546. }
  547. #if CONFIG_OGG_MUXER
  548. OGG_CLASS(ogg, Ogg)
  549. AVOutputFormat ff_ogg_muxer = {
  550. .name = "ogg",
  551. .long_name = NULL_IF_CONFIG_SMALL("Ogg"),
  552. .mime_type = "application/ogg",
  553. .extensions = "ogg,ogv"
  554. #if !CONFIG_SPX_MUXER
  555. ",spx"
  556. #endif
  557. #if !CONFIG_OPUS_MUXER
  558. ",opus"
  559. #endif
  560. ,
  561. .priv_data_size = sizeof(OGGContext),
  562. .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
  563. AV_CODEC_ID_VORBIS : AV_CODEC_ID_FLAC,
  564. .video_codec = AV_CODEC_ID_THEORA,
  565. .write_header = ogg_write_header,
  566. .write_packet = ogg_write_packet,
  567. .write_trailer = ogg_write_trailer,
  568. .flags = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
  569. .priv_class = &ogg_muxer_class,
  570. };
  571. #endif
  572. #if CONFIG_OGA_MUXER
  573. OGG_CLASS(oga, Ogg audio)
  574. AVOutputFormat ff_oga_muxer = {
  575. .name = "oga",
  576. .long_name = NULL_IF_CONFIG_SMALL("Ogg Audio"),
  577. .mime_type = "audio/ogg",
  578. .extensions = "oga",
  579. .priv_data_size = sizeof(OGGContext),
  580. .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
  581. AV_CODEC_ID_VORBIS : AV_CODEC_ID_FLAC,
  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 = &oga_muxer_class,
  587. };
  588. #endif
  589. #if CONFIG_SPX_MUXER
  590. OGG_CLASS(spx, Ogg Speex)
  591. AVOutputFormat ff_spx_muxer = {
  592. .name = "spx",
  593. .long_name = NULL_IF_CONFIG_SMALL("Ogg Speex"),
  594. .mime_type = "audio/ogg",
  595. .extensions = "spx",
  596. .priv_data_size = sizeof(OGGContext),
  597. .audio_codec = AV_CODEC_ID_SPEEX,
  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 = &spx_muxer_class,
  603. };
  604. #endif
  605. #if CONFIG_OPUS_MUXER
  606. OGG_CLASS(opus, Ogg Opus)
  607. AVOutputFormat ff_opus_muxer = {
  608. .name = "opus",
  609. .long_name = NULL_IF_CONFIG_SMALL("Ogg Opus"),
  610. .mime_type = "audio/ogg",
  611. .extensions = "opus",
  612. .priv_data_size = sizeof(OGGContext),
  613. .audio_codec = AV_CODEC_ID_OPUS,
  614. .write_header = ogg_write_header,
  615. .write_packet = ogg_write_packet,
  616. .write_trailer = ogg_write_trailer,
  617. .flags = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
  618. .priv_class = &opus_muxer_class,
  619. };
  620. #endif