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.

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