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.

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