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.

726 lines
24KB

  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. uint8_t *p;
  264. if (avctx->extradata_size < FLAC_STREAMINFO_SIZE)
  265. return AVERROR(EINVAL);
  266. // first packet: STREAMINFO
  267. oggstream->header_len[0] = 51;
  268. oggstream->header[0] = av_mallocz(51); // per ogg flac specs
  269. p = oggstream->header[0];
  270. if (!p)
  271. return AVERROR(ENOMEM);
  272. bytestream_put_byte(&p, 0x7F);
  273. bytestream_put_buffer(&p, "FLAC", 4);
  274. bytestream_put_byte(&p, 1); // major version
  275. bytestream_put_byte(&p, 0); // minor version
  276. bytestream_put_be16(&p, 1); // headers packets without this one
  277. bytestream_put_buffer(&p, "fLaC", 4);
  278. bytestream_put_byte(&p, 0x00); // streaminfo
  279. bytestream_put_be24(&p, 34);
  280. bytestream_put_buffer(&p, avctx->extradata, FLAC_STREAMINFO_SIZE);
  281. // second packet: VorbisComment
  282. p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m, 0);
  283. if (!p)
  284. return AVERROR(ENOMEM);
  285. oggstream->header[1] = p;
  286. bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
  287. bytestream_put_be24(&p, oggstream->header_len[1] - 4);
  288. return 0;
  289. }
  290. #define SPEEX_HEADER_SIZE 80
  291. static int ogg_build_speex_headers(AVCodecContext *avctx,
  292. OGGStreamContext *oggstream, int bitexact,
  293. AVDictionary **m)
  294. {
  295. uint8_t *p;
  296. if (avctx->extradata_size < SPEEX_HEADER_SIZE)
  297. return AVERROR_INVALIDDATA;
  298. // first packet: Speex header
  299. p = av_mallocz(SPEEX_HEADER_SIZE);
  300. if (!p)
  301. return AVERROR(ENOMEM);
  302. oggstream->header[0] = p;
  303. oggstream->header_len[0] = SPEEX_HEADER_SIZE;
  304. bytestream_put_buffer(&p, avctx->extradata, SPEEX_HEADER_SIZE);
  305. AV_WL32(&oggstream->header[0][68], 0); // set extra_headers to 0
  306. // second packet: VorbisComment
  307. p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m, 0);
  308. if (!p)
  309. return AVERROR(ENOMEM);
  310. oggstream->header[1] = p;
  311. return 0;
  312. }
  313. #define OPUS_HEADER_SIZE 19
  314. static int ogg_build_opus_headers(AVCodecContext *avctx,
  315. OGGStreamContext *oggstream, int bitexact,
  316. AVDictionary **m)
  317. {
  318. uint8_t *p;
  319. if (avctx->extradata_size < OPUS_HEADER_SIZE)
  320. return AVERROR_INVALIDDATA;
  321. /* first packet: Opus header */
  322. p = av_mallocz(avctx->extradata_size);
  323. if (!p)
  324. return AVERROR(ENOMEM);
  325. oggstream->header[0] = p;
  326. oggstream->header_len[0] = avctx->extradata_size;
  327. bytestream_put_buffer(&p, avctx->extradata, avctx->extradata_size);
  328. /* second packet: VorbisComment */
  329. p = ogg_write_vorbiscomment(8, bitexact, &oggstream->header_len[1], m, 0);
  330. if (!p)
  331. return AVERROR(ENOMEM);
  332. oggstream->header[1] = p;
  333. bytestream_put_buffer(&p, "OpusTags", 8);
  334. return 0;
  335. }
  336. static void ogg_write_pages(AVFormatContext *s, int flush)
  337. {
  338. OGGContext *ogg = s->priv_data;
  339. OGGPageList *next, *p;
  340. if (!ogg->page_list)
  341. return;
  342. for (p = ogg->page_list; p; ) {
  343. OGGStreamContext *oggstream =
  344. s->streams[p->page.stream_index]->priv_data;
  345. if (oggstream->page_count < 2 && !flush)
  346. break;
  347. ogg_write_page(s, &p->page,
  348. flush == 1 && oggstream->page_count == 1 ? 4 : 0); // eos
  349. next = p->next;
  350. av_freep(&p);
  351. p = next;
  352. }
  353. ogg->page_list = p;
  354. }
  355. static int ogg_write_header(AVFormatContext *s)
  356. {
  357. OGGContext *ogg = s->priv_data;
  358. OGGStreamContext *oggstream = NULL;
  359. int i, j;
  360. if (ogg->pref_size)
  361. av_log(s, AV_LOG_WARNING, "The pagesize option is deprecated\n");
  362. for (i = 0; i < s->nb_streams; i++) {
  363. AVStream *st = s->streams[i];
  364. unsigned serial_num = i;
  365. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  366. if (st->codec->codec_id == AV_CODEC_ID_OPUS)
  367. /* Opus requires a fixed 48kHz clock */
  368. avpriv_set_pts_info(st, 64, 1, 48000);
  369. else
  370. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  371. }
  372. if (st->codec->codec_id != AV_CODEC_ID_VORBIS &&
  373. st->codec->codec_id != AV_CODEC_ID_THEORA &&
  374. st->codec->codec_id != AV_CODEC_ID_SPEEX &&
  375. st->codec->codec_id != AV_CODEC_ID_FLAC &&
  376. st->codec->codec_id != AV_CODEC_ID_OPUS) {
  377. av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
  378. return AVERROR(EINVAL);
  379. }
  380. if (!st->codec->extradata || !st->codec->extradata_size) {
  381. av_log(s, AV_LOG_ERROR, "No extradata present\n");
  382. return AVERROR_INVALIDDATA;
  383. }
  384. oggstream = av_mallocz(sizeof(*oggstream));
  385. if (!oggstream)
  386. return AVERROR(ENOMEM);
  387. oggstream->page.stream_index = i;
  388. if (!(s->flags & AVFMT_FLAG_BITEXACT))
  389. do {
  390. serial_num = av_get_random_seed();
  391. for (j = 0; j < i; j++) {
  392. OGGStreamContext *sc = s->streams[j]->priv_data;
  393. if (serial_num == sc->serial_num)
  394. break;
  395. }
  396. } while (j < i);
  397. oggstream->serial_num = serial_num;
  398. av_dict_copy(&st->metadata, s->metadata, AV_DICT_DONT_OVERWRITE);
  399. st->priv_data = oggstream;
  400. if (st->codec->codec_id == AV_CODEC_ID_FLAC) {
  401. int err = ogg_build_flac_headers(st->codec, oggstream,
  402. s->flags & AVFMT_FLAG_BITEXACT,
  403. &st->metadata);
  404. if (err) {
  405. av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
  406. av_freep(&st->priv_data);
  407. return err;
  408. }
  409. } else if (st->codec->codec_id == AV_CODEC_ID_SPEEX) {
  410. int err = ogg_build_speex_headers(st->codec, oggstream,
  411. s->flags & AVFMT_FLAG_BITEXACT,
  412. &st->metadata);
  413. if (err) {
  414. av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
  415. av_freep(&st->priv_data);
  416. return err;
  417. }
  418. } else if (st->codec->codec_id == AV_CODEC_ID_OPUS) {
  419. int err = ogg_build_opus_headers(st->codec, oggstream,
  420. s->flags & AVFMT_FLAG_BITEXACT,
  421. &st->metadata);
  422. if (err) {
  423. av_log(s, AV_LOG_ERROR, "Error writing Opus headers\n");
  424. av_freep(&st->priv_data);
  425. return err;
  426. }
  427. } else {
  428. uint8_t *p;
  429. const char *cstr = st->codec->codec_id == AV_CODEC_ID_VORBIS ? "vorbis" : "theora";
  430. int header_type = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 3 : 0x81;
  431. int framing_bit = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 1 : 0;
  432. if (avpriv_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
  433. st->codec->codec_id == AV_CODEC_ID_VORBIS ? 30 : 42,
  434. (const uint8_t**)oggstream->header, oggstream->header_len) < 0) {
  435. av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
  436. av_freep(&st->priv_data);
  437. return AVERROR_INVALIDDATA;
  438. }
  439. p = ogg_write_vorbiscomment(7, s->flags & AVFMT_FLAG_BITEXACT,
  440. &oggstream->header_len[1], &st->metadata,
  441. framing_bit);
  442. oggstream->header[1] = p;
  443. if (!p)
  444. return AVERROR(ENOMEM);
  445. bytestream_put_byte(&p, header_type);
  446. bytestream_put_buffer(&p, cstr, 6);
  447. if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
  448. /** KFGSHIFT is the width of the less significant section of the granule position
  449. The less significant section is the frame count since the last keyframe */
  450. oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
  451. oggstream->vrev = oggstream->header[0][9];
  452. av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
  453. oggstream->kfgshift, oggstream->vrev);
  454. }
  455. }
  456. }
  457. for (j = 0; j < s->nb_streams; j++) {
  458. OGGStreamContext *oggstream = s->streams[j]->priv_data;
  459. ogg_buffer_data(s, s->streams[j], oggstream->header[0],
  460. oggstream->header_len[0], 0, 1);
  461. oggstream->page.flags |= 2; // bos
  462. ogg_buffer_page(s, oggstream);
  463. }
  464. for (j = 0; j < s->nb_streams; j++) {
  465. AVStream *st = s->streams[j];
  466. OGGStreamContext *oggstream = st->priv_data;
  467. for (i = 1; i < 3; i++) {
  468. if (oggstream->header_len[i])
  469. ogg_buffer_data(s, st, oggstream->header[i],
  470. oggstream->header_len[i], 0, 1);
  471. }
  472. ogg_buffer_page(s, oggstream);
  473. }
  474. oggstream->page.start_granule = AV_NOPTS_VALUE;
  475. ogg_write_pages(s, 2);
  476. return 0;
  477. }
  478. static int ogg_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
  479. {
  480. AVStream *st = s->streams[pkt->stream_index];
  481. OGGStreamContext *oggstream = st->priv_data;
  482. int ret;
  483. int64_t granule;
  484. if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
  485. int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
  486. int pframe_count;
  487. if (pkt->flags & AV_PKT_FLAG_KEY)
  488. oggstream->last_kf_pts = pts;
  489. pframe_count = pts - oggstream->last_kf_pts;
  490. // prevent frame count from overflow if key frame flag is not set
  491. if (pframe_count >= (1<<oggstream->kfgshift)) {
  492. oggstream->last_kf_pts += pframe_count;
  493. pframe_count = 0;
  494. }
  495. granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
  496. } else if (st->codec->codec_id == AV_CODEC_ID_OPUS)
  497. granule = pkt->pts + pkt->duration +
  498. av_rescale_q(st->codec->initial_padding,
  499. (AVRational){ 1, st->codec->sample_rate },
  500. st->time_base);
  501. else
  502. granule = pkt->pts + pkt->duration;
  503. if (oggstream->page.start_granule == AV_NOPTS_VALUE)
  504. oggstream->page.start_granule = pkt->pts;
  505. ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0);
  506. if (ret < 0)
  507. return ret;
  508. ogg_write_pages(s, 0);
  509. oggstream->last_granule = granule;
  510. return 0;
  511. }
  512. static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
  513. {
  514. int i;
  515. if (pkt)
  516. return ogg_write_packet_internal(s, pkt);
  517. for (i = 0; i < s->nb_streams; i++) {
  518. OGGStreamContext *oggstream = s->streams[i]->priv_data;
  519. if (oggstream->page.segments_count)
  520. ogg_buffer_page(s, oggstream);
  521. }
  522. ogg_write_pages(s, 2);
  523. return 0;
  524. }
  525. static int ogg_write_trailer(AVFormatContext *s)
  526. {
  527. int i;
  528. /* flush current page if needed */
  529. for (i = 0; i < s->nb_streams; i++) {
  530. OGGStreamContext *oggstream = s->streams[i]->priv_data;
  531. if (oggstream->page.size > 0)
  532. ogg_buffer_page(s, oggstream);
  533. }
  534. ogg_write_pages(s, 1);
  535. for (i = 0; i < s->nb_streams; i++) {
  536. AVStream *st = s->streams[i];
  537. OGGStreamContext *oggstream = st->priv_data;
  538. if (st->codec->codec_id == AV_CODEC_ID_FLAC ||
  539. st->codec->codec_id == AV_CODEC_ID_SPEEX ||
  540. st->codec->codec_id == AV_CODEC_ID_OPUS) {
  541. av_freep(&oggstream->header[0]);
  542. }
  543. av_freep(&oggstream->header[1]);
  544. av_freep(&st->priv_data);
  545. }
  546. return 0;
  547. }
  548. #if CONFIG_OGG_MUXER
  549. OGG_CLASS(ogg, Ogg)
  550. AVOutputFormat ff_ogg_muxer = {
  551. .name = "ogg",
  552. .long_name = NULL_IF_CONFIG_SMALL("Ogg"),
  553. .mime_type = "application/ogg",
  554. .extensions = "ogg,ogv"
  555. #if !CONFIG_SPX_MUXER
  556. ",spx"
  557. #endif
  558. #if !CONFIG_OPUS_MUXER
  559. ",opus"
  560. #endif
  561. ,
  562. .priv_data_size = sizeof(OGGContext),
  563. .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
  564. AV_CODEC_ID_VORBIS : AV_CODEC_ID_FLAC,
  565. .video_codec = AV_CODEC_ID_THEORA,
  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 = &ogg_muxer_class,
  571. };
  572. #endif
  573. #if CONFIG_OGA_MUXER
  574. OGG_CLASS(oga, Ogg audio)
  575. AVOutputFormat ff_oga_muxer = {
  576. .name = "oga",
  577. .long_name = NULL_IF_CONFIG_SMALL("Ogg Audio"),
  578. .mime_type = "audio/ogg",
  579. .extensions = "oga",
  580. .priv_data_size = sizeof(OGGContext),
  581. .audio_codec = CONFIG_LIBVORBIS_ENCODER ?
  582. AV_CODEC_ID_VORBIS : AV_CODEC_ID_FLAC,
  583. .write_header = ogg_write_header,
  584. .write_packet = ogg_write_packet,
  585. .write_trailer = ogg_write_trailer,
  586. .flags = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
  587. .priv_class = &oga_muxer_class,
  588. };
  589. #endif
  590. #if CONFIG_SPX_MUXER
  591. OGG_CLASS(spx, Ogg Speex)
  592. AVOutputFormat ff_spx_muxer = {
  593. .name = "spx",
  594. .long_name = NULL_IF_CONFIG_SMALL("Ogg Speex"),
  595. .mime_type = "audio/ogg",
  596. .extensions = "spx",
  597. .priv_data_size = sizeof(OGGContext),
  598. .audio_codec = AV_CODEC_ID_SPEEX,
  599. .write_header = ogg_write_header,
  600. .write_packet = ogg_write_packet,
  601. .write_trailer = ogg_write_trailer,
  602. .flags = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
  603. .priv_class = &spx_muxer_class,
  604. };
  605. #endif
  606. #if CONFIG_OPUS_MUXER
  607. OGG_CLASS(opus, Ogg Opus)
  608. AVOutputFormat ff_opus_muxer = {
  609. .name = "opus",
  610. .long_name = NULL_IF_CONFIG_SMALL("Ogg Opus"),
  611. .mime_type = "audio/ogg",
  612. .extensions = "opus",
  613. .priv_data_size = sizeof(OGGContext),
  614. .audio_codec = AV_CODEC_ID_OPUS,
  615. .write_header = ogg_write_header,
  616. .write_packet = ogg_write_packet,
  617. .write_trailer = ogg_write_trailer,
  618. .flags = AVFMT_TS_NEGATIVE | AVFMT_ALLOW_FLUSH,
  619. .priv_class = &opus_muxer_class,
  620. };
  621. #endif