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.

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