oggenc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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 "libavutil/crc.h"
  22. #include "libavutil/opt.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 granule;
  36. int stream_index;
  37. uint8_t flags;
  38. uint8_t segments_count;
  39. uint8_t segments[255];
  40. uint8_t data[MAX_PAGE_SIZE];
  41. uint16_t size;
  42. } OGGPage;
  43. typedef struct {
  44. unsigned page_counter;
  45. uint8_t *header[3];
  46. int header_len[3];
  47. /** for theora granule */
  48. int kfgshift;
  49. int64_t last_kf_pts;
  50. int vrev;
  51. int eos;
  52. unsigned page_count; ///< number of page buffered
  53. OGGPage page; ///< current page
  54. unsigned serial_num; ///< serial number
  55. int64_t last_granule; ///< last packet granule
  56. } OGGStreamContext;
  57. typedef struct OGGPageList {
  58. OGGPage page;
  59. struct OGGPageList *next;
  60. } OGGPageList;
  61. typedef struct {
  62. const AVClass *class;
  63. OGGPageList *page_list;
  64. int pref_size; ///< preferred page size (0 => fill all segments)
  65. } OGGContext;
  66. #define OFFSET(x) offsetof(OGGContext, x)
  67. #define PARAM AV_OPT_FLAG_ENCODING_PARAM
  68. static const AVOption options[] = {
  69. { "oggpagesize", "Set preferred Ogg page size.",
  70. offsetof(OGGContext, pref_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, MAX_PAGE_SIZE, AV_OPT_FLAG_ENCODING_PARAM},
  71. { "pagesize", "preferred page size in bytes",
  72. OFFSET(pref_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, MAX_PAGE_SIZE, PARAM },
  73. { NULL },
  74. };
  75. static const AVClass ogg_muxer_class = {
  76. .class_name = "Ogg muxer",
  77. .item_name = av_default_item_name,
  78. .option = options,
  79. .version = LIBAVUTIL_VERSION_INT,
  80. };
  81. static void ogg_update_checksum(AVFormatContext *s, AVIOContext *pb, int64_t crc_offset)
  82. {
  83. int64_t pos = avio_tell(pb);
  84. uint32_t checksum = ffio_get_checksum(pb);
  85. avio_seek(pb, crc_offset, SEEK_SET);
  86. avio_wb32(pb, checksum);
  87. avio_seek(pb, pos, SEEK_SET);
  88. }
  89. static int ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
  90. {
  91. OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data;
  92. AVIOContext *pb;
  93. int64_t crc_offset;
  94. int ret, size;
  95. uint8_t *buf;
  96. ret = avio_open_dyn_buf(&pb);
  97. if (ret < 0)
  98. return ret;
  99. ffio_init_checksum(pb, ff_crc04C11DB7_update, 0);
  100. ffio_wfourcc(pb, "OggS");
  101. avio_w8(pb, 0);
  102. avio_w8(pb, page->flags | extra_flags);
  103. avio_wl64(pb, page->granule);
  104. avio_wl32(pb, oggstream->serial_num);
  105. avio_wl32(pb, oggstream->page_counter++);
  106. crc_offset = avio_tell(pb);
  107. avio_wl32(pb, 0); // crc
  108. avio_w8(pb, page->segments_count);
  109. avio_write(pb, page->segments, page->segments_count);
  110. avio_write(pb, page->data, page->size);
  111. ogg_update_checksum(s, pb, crc_offset);
  112. avio_flush(pb);
  113. size = avio_close_dyn_buf(pb, &buf);
  114. if (size < 0)
  115. return size;
  116. avio_write(s->pb, buf, size);
  117. avio_flush(s->pb);
  118. av_free(buf);
  119. oggstream->page_count--;
  120. return 0;
  121. }
  122. static int ogg_key_granule(OGGStreamContext *oggstream, int64_t granule)
  123. {
  124. return oggstream->kfgshift && !(granule & ((1<<oggstream->kfgshift)-1));
  125. }
  126. static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, int64_t granule)
  127. {
  128. if (oggstream->kfgshift)
  129. return (granule>>oggstream->kfgshift) +
  130. (granule & ((1<<oggstream->kfgshift)-1));
  131. else
  132. return granule;
  133. }
  134. static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
  135. {
  136. AVStream *st2 = s->streams[next->stream_index];
  137. AVStream *st = s->streams[page->stream_index];
  138. int64_t next_granule, cur_granule;
  139. if (next->granule == -1 || page->granule == -1)
  140. return 0;
  141. next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next->granule),
  142. st2->time_base, AV_TIME_BASE_Q);
  143. cur_granule = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page->granule),
  144. st ->time_base, AV_TIME_BASE_Q);
  145. return next_granule > cur_granule;
  146. }
  147. static int ogg_reset_cur_page(OGGStreamContext *oggstream)
  148. {
  149. oggstream->page.granule = -1;
  150. oggstream->page.flags = 0;
  151. oggstream->page.segments_count = 0;
  152. oggstream->page.size = 0;
  153. return 0;
  154. }
  155. static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
  156. {
  157. OGGContext *ogg = s->priv_data;
  158. OGGPageList **p = &ogg->page_list;
  159. OGGPageList *l = av_mallocz(sizeof(*l));
  160. if (!l)
  161. return AVERROR(ENOMEM);
  162. l->page = oggstream->page;
  163. oggstream->page_count++;
  164. ogg_reset_cur_page(oggstream);
  165. while (*p) {
  166. if (ogg_compare_granule(s, &(*p)->page, &l->page))
  167. break;
  168. p = &(*p)->next;
  169. }
  170. l->next = *p;
  171. *p = l;
  172. return 0;
  173. }
  174. static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
  175. uint8_t *data, unsigned size, int64_t granule,
  176. int header)
  177. {
  178. OGGStreamContext *oggstream = st->priv_data;
  179. OGGContext *ogg = s->priv_data;
  180. int total_segments = size / 255 + 1;
  181. uint8_t *p = data;
  182. int i, segments, len, flush = 0;
  183. // Handles VFR by flushing page because this frame needs to have a timestamp
  184. // For theora, keyframes also need to have a timestamp to correctly mark
  185. // them as such, otherwise seeking will not work correctly at the very
  186. // least with old libogg versions.
  187. // Do not try to flush header packets though, that will create broken files.
  188. if (st->codec->codec_id == AV_CODEC_ID_THEORA && !header &&
  189. (ogg_granule_to_timestamp(oggstream, granule) >
  190. ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1 ||
  191. ogg_key_granule(oggstream, granule))) {
  192. if (oggstream->page.granule != -1)
  193. ogg_buffer_page(s, oggstream);
  194. flush = 1;
  195. }
  196. for (i = 0; i < total_segments; ) {
  197. OGGPage *page = &oggstream->page;
  198. segments = FFMIN(total_segments - i, 255 - page->segments_count);
  199. if (i && !page->segments_count)
  200. page->flags |= 1; // continued packet
  201. memset(page->segments+page->segments_count, 255, segments - 1);
  202. page->segments_count += segments - 1;
  203. len = FFMIN(size, segments*255);
  204. page->segments[page->segments_count++] = len - (segments-1)*255;
  205. memcpy(page->data+page->size, p, len);
  206. p += len;
  207. size -= len;
  208. i += segments;
  209. page->size += len;
  210. if (i == total_segments)
  211. page->granule = granule;
  212. if (!header && (page->segments_count == 255 ||
  213. (ogg->pref_size > 0 && page->size >= ogg->pref_size))) {
  214. ogg_buffer_page(s, oggstream);
  215. }
  216. }
  217. if (flush && oggstream->page.granule != -1)
  218. ogg_buffer_page(s, oggstream);
  219. return 0;
  220. }
  221. static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact,
  222. int *header_len, AVDictionary **m, int framing_bit)
  223. {
  224. const char *vendor = bitexact ? "ffmpeg" : LIBAVFORMAT_IDENT;
  225. int size;
  226. uint8_t *p, *p0;
  227. unsigned int count;
  228. ff_metadata_conv(m, ff_vorbiscomment_metadata_conv, NULL);
  229. size = offset + ff_vorbiscomment_length(*m, vendor, &count) + framing_bit;
  230. p = av_mallocz(size);
  231. if (!p)
  232. return NULL;
  233. p0 = p;
  234. p += offset;
  235. ff_vorbiscomment_write(&p, m, vendor, count);
  236. if (framing_bit)
  237. bytestream_put_byte(&p, 1);
  238. *header_len = size;
  239. return p0;
  240. }
  241. static int ogg_build_flac_headers(AVCodecContext *avctx,
  242. OGGStreamContext *oggstream, int bitexact,
  243. AVDictionary **m)
  244. {
  245. enum FLACExtradataFormat format;
  246. uint8_t *streaminfo;
  247. uint8_t *p;
  248. if (!avpriv_flac_is_extradata_valid(avctx, &format, &streaminfo))
  249. return -1;
  250. // first packet: STREAMINFO
  251. oggstream->header_len[0] = 51;
  252. oggstream->header[0] = av_mallocz(51); // per ogg flac specs
  253. p = oggstream->header[0];
  254. if (!p)
  255. return AVERROR(ENOMEM);
  256. bytestream_put_byte(&p, 0x7F);
  257. bytestream_put_buffer(&p, "FLAC", 4);
  258. bytestream_put_byte(&p, 1); // major version
  259. bytestream_put_byte(&p, 0); // minor version
  260. bytestream_put_be16(&p, 1); // headers packets without this one
  261. bytestream_put_buffer(&p, "fLaC", 4);
  262. bytestream_put_byte(&p, 0x00); // streaminfo
  263. bytestream_put_be24(&p, 34);
  264. bytestream_put_buffer(&p, streaminfo, FLAC_STREAMINFO_SIZE);
  265. // second packet: VorbisComment
  266. p = ogg_write_vorbiscomment(4, bitexact, &oggstream->header_len[1], m, 0);
  267. if (!p)
  268. return AVERROR(ENOMEM);
  269. oggstream->header[1] = p;
  270. bytestream_put_byte(&p, 0x84); // last metadata block and vorbis comment
  271. bytestream_put_be24(&p, oggstream->header_len[1] - 4);
  272. return 0;
  273. }
  274. #define SPEEX_HEADER_SIZE 80
  275. static int ogg_build_speex_headers(AVCodecContext *avctx,
  276. OGGStreamContext *oggstream, int bitexact,
  277. AVDictionary **m)
  278. {
  279. uint8_t *p;
  280. if (avctx->extradata_size < SPEEX_HEADER_SIZE)
  281. return -1;
  282. // first packet: Speex header
  283. p = av_mallocz(SPEEX_HEADER_SIZE);
  284. if (!p)
  285. return AVERROR(ENOMEM);
  286. oggstream->header[0] = p;
  287. oggstream->header_len[0] = SPEEX_HEADER_SIZE;
  288. bytestream_put_buffer(&p, avctx->extradata, SPEEX_HEADER_SIZE);
  289. AV_WL32(&oggstream->header[0][68], 0); // set extra_headers to 0
  290. // second packet: VorbisComment
  291. p = ogg_write_vorbiscomment(0, bitexact, &oggstream->header_len[1], m, 0);
  292. if (!p)
  293. return AVERROR(ENOMEM);
  294. oggstream->header[1] = p;
  295. return 0;
  296. }
  297. static int ogg_write_header(AVFormatContext *s)
  298. {
  299. OGGStreamContext *oggstream;
  300. int i, j;
  301. for (i = 0; i < s->nb_streams; i++) {
  302. AVStream *st = s->streams[i];
  303. unsigned serial_num = i;
  304. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  305. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  306. else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  307. avpriv_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den);
  308. if (st->codec->codec_id != AV_CODEC_ID_VORBIS &&
  309. st->codec->codec_id != AV_CODEC_ID_THEORA &&
  310. st->codec->codec_id != AV_CODEC_ID_SPEEX &&
  311. st->codec->codec_id != AV_CODEC_ID_FLAC) {
  312. av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
  313. return -1;
  314. }
  315. if (!st->codec->extradata || !st->codec->extradata_size) {
  316. av_log(s, AV_LOG_ERROR, "No extradata present\n");
  317. return -1;
  318. }
  319. oggstream = av_mallocz(sizeof(*oggstream));
  320. oggstream->page.stream_index = i;
  321. if (!(st->codec->flags & CODEC_FLAG_BITEXACT))
  322. do {
  323. serial_num = av_get_random_seed();
  324. for (j = 0; j < i; j++) {
  325. OGGStreamContext *sc = s->streams[j]->priv_data;
  326. if (serial_num == sc->serial_num)
  327. break;
  328. }
  329. } while (j < i);
  330. oggstream->serial_num = serial_num;
  331. st->priv_data = oggstream;
  332. if (st->codec->codec_id == AV_CODEC_ID_FLAC) {
  333. int err = ogg_build_flac_headers(st->codec, oggstream,
  334. st->codec->flags & CODEC_FLAG_BITEXACT,
  335. &s->metadata);
  336. if (err) {
  337. av_log(s, AV_LOG_ERROR, "Error writing FLAC headers\n");
  338. av_freep(&st->priv_data);
  339. return err;
  340. }
  341. } else if (st->codec->codec_id == AV_CODEC_ID_SPEEX) {
  342. int err = ogg_build_speex_headers(st->codec, oggstream,
  343. st->codec->flags & CODEC_FLAG_BITEXACT,
  344. &s->metadata);
  345. if (err) {
  346. av_log(s, AV_LOG_ERROR, "Error writing Speex headers\n");
  347. av_freep(&st->priv_data);
  348. return err;
  349. }
  350. } else {
  351. uint8_t *p;
  352. const char *cstr = st->codec->codec_id == AV_CODEC_ID_VORBIS ? "vorbis" : "theora";
  353. int header_type = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 3 : 0x81;
  354. int framing_bit = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 1 : 0;
  355. if (avpriv_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
  356. st->codec->codec_id == AV_CODEC_ID_VORBIS ? 30 : 42,
  357. oggstream->header, oggstream->header_len) < 0) {
  358. av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
  359. av_freep(&st->priv_data);
  360. return -1;
  361. }
  362. p = ogg_write_vorbiscomment(7, st->codec->flags & CODEC_FLAG_BITEXACT,
  363. &oggstream->header_len[1], &s->metadata,
  364. framing_bit);
  365. oggstream->header[1] = p;
  366. if (!p)
  367. return AVERROR(ENOMEM);
  368. bytestream_put_byte(&p, header_type);
  369. bytestream_put_buffer(&p, cstr, 6);
  370. if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
  371. /** KFGSHIFT is the width of the less significant section of the granule position
  372. The less significant section is the frame count since the last keyframe */
  373. oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
  374. oggstream->vrev = oggstream->header[0][9];
  375. av_log(s, AV_LOG_DEBUG, "theora kfgshift %d, vrev %d\n",
  376. oggstream->kfgshift, oggstream->vrev);
  377. }
  378. }
  379. }
  380. for (j = 0; j < s->nb_streams; j++) {
  381. OGGStreamContext *oggstream = s->streams[j]->priv_data;
  382. ogg_buffer_data(s, s->streams[j], oggstream->header[0],
  383. oggstream->header_len[0], 0, 1);
  384. oggstream->page.flags |= 2; // bos
  385. ogg_buffer_page(s, oggstream);
  386. }
  387. for (j = 0; j < s->nb_streams; j++) {
  388. AVStream *st = s->streams[j];
  389. OGGStreamContext *oggstream = st->priv_data;
  390. for (i = 1; i < 3; i++) {
  391. if (oggstream && oggstream->header_len[i])
  392. ogg_buffer_data(s, st, oggstream->header[i],
  393. oggstream->header_len[i], 0, 1);
  394. }
  395. ogg_buffer_page(s, oggstream);
  396. }
  397. return 0;
  398. }
  399. static void ogg_write_pages(AVFormatContext *s, int flush)
  400. {
  401. OGGContext *ogg = s->priv_data;
  402. OGGPageList *next, *p;
  403. if (!ogg->page_list)
  404. return;
  405. for (p = ogg->page_list; p; ) {
  406. OGGStreamContext *oggstream =
  407. s->streams[p->page.stream_index]->priv_data;
  408. if (oggstream->page_count < 2 && !flush)
  409. break;
  410. ogg_write_page(s, &p->page,
  411. flush && oggstream->page_count == 1 ? 4 : 0); // eos
  412. next = p->next;
  413. av_freep(&p);
  414. p = next;
  415. }
  416. ogg->page_list = p;
  417. }
  418. static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
  419. {
  420. AVStream *st = s->streams[pkt->stream_index];
  421. OGGStreamContext *oggstream = st->priv_data;
  422. int ret;
  423. int64_t granule;
  424. if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
  425. int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
  426. int pframe_count;
  427. if (pkt->flags & AV_PKT_FLAG_KEY)
  428. oggstream->last_kf_pts = pts;
  429. pframe_count = pts - oggstream->last_kf_pts;
  430. // prevent frame count from overflow if key frame flag is not set
  431. if (pframe_count >= (1<<oggstream->kfgshift)) {
  432. oggstream->last_kf_pts += pframe_count;
  433. pframe_count = 0;
  434. }
  435. granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
  436. } else
  437. granule = pkt->pts + pkt->duration;
  438. ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0);
  439. if (ret < 0)
  440. return ret;
  441. ogg_write_pages(s, 0);
  442. oggstream->last_granule = granule;
  443. return 0;
  444. }
  445. static int ogg_write_trailer(AVFormatContext *s)
  446. {
  447. int i;
  448. /* flush current page */
  449. for (i = 0; i < s->nb_streams; i++)
  450. ogg_buffer_page(s, s->streams[i]->priv_data);
  451. ogg_write_pages(s, 1);
  452. for (i = 0; i < s->nb_streams; i++) {
  453. AVStream *st = s->streams[i];
  454. OGGStreamContext *oggstream = st->priv_data;
  455. if (st->codec->codec_id == AV_CODEC_ID_FLAC ||
  456. st->codec->codec_id == AV_CODEC_ID_SPEEX) {
  457. av_freep(&oggstream->header[0]);
  458. }
  459. av_freep(&oggstream->header[1]);
  460. av_freep(&st->priv_data);
  461. }
  462. return 0;
  463. }
  464. AVOutputFormat ff_ogg_muxer = {
  465. .name = "ogg",
  466. .long_name = NULL_IF_CONFIG_SMALL("Ogg"),
  467. .mime_type = "application/ogg",
  468. .extensions = "ogg,ogv,spx",
  469. .priv_data_size = sizeof(OGGContext),
  470. .audio_codec = AV_CODEC_ID_FLAC,
  471. .video_codec = AV_CODEC_ID_THEORA,
  472. .write_header = ogg_write_header,
  473. .write_packet = ogg_write_packet,
  474. .write_trailer = ogg_write_trailer,
  475. .priv_class = &ogg_muxer_class,
  476. };