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.

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