]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/oggenc.c
doxy: Clarify what avpriv_set_pts_info does
[ffmpeg] / libavformat / oggenc.c
index 6ccbd58b17a7f451d86aa0f7e89635af9800b075..c1f22d15cc4daeaccbe97195972aef324ef0683d 100644 (file)
@@ -74,7 +74,7 @@ typedef struct {
 
 static const AVOption options[] = {
     { "pagesize", "preferred page size in bytes",
-        OFFSET(pref_size), AV_OPT_TYPE_INT, { 0 }, 0, MAX_PAGE_SIZE, PARAM },
+        OFFSET(pref_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, MAX_PAGE_SIZE, PARAM },
     { NULL },
 };
 
@@ -202,7 +202,7 @@ static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
     int i, segments, len, flush = 0;
 
     // Handles VFR by flushing page because this frame needs to have a timestamp
-    if (st->codec->codec_id == CODEC_ID_THEORA && !header &&
+    if (st->codec->codec_id == AV_CODEC_ID_THEORA && !header &&
         ogg_granule_to_timestamp(oggstream, granule) >
         ogg_granule_to_timestamp(oggstream, oggstream->last_granule) + 1) {
         if (oggstream->page.granule != -1)
@@ -336,6 +336,35 @@ static int ogg_build_speex_headers(AVCodecContext *avctx,
     return 0;
 }
 
+#define OPUS_HEADER_SIZE 19
+
+static int ogg_build_opus_headers(AVCodecContext *avctx,
+                                  OGGStreamContext *oggstream, int bitexact,
+                                  AVDictionary **m)
+{
+    uint8_t *p;
+
+    if (avctx->extradata_size < OPUS_HEADER_SIZE)
+        return -1;
+
+    /* first packet: Opus header */
+    p = av_mallocz(avctx->extradata_size);
+    if (!p)
+        return AVERROR(ENOMEM);
+    oggstream->header[0] = p;
+    oggstream->header_len[0] = avctx->extradata_size;
+    bytestream_put_buffer(&p, avctx->extradata, avctx->extradata_size);
+
+    /* second packet: VorbisComment */
+    p = ogg_write_vorbiscomment(8, bitexact, &oggstream->header_len[1], m, 0);
+    if (!p)
+        return AVERROR(ENOMEM);
+    oggstream->header[1] = p;
+    bytestream_put_buffer(&p, "OpusTags", 8);
+
+    return 0;
+}
+
 static int ogg_write_header(AVFormatContext *s)
 {
     OGGStreamContext *oggstream;
@@ -346,13 +375,18 @@ static int ogg_write_header(AVFormatContext *s)
         unsigned serial_num = i;
 
         if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
-            avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
+            if (st->codec->codec_id == AV_CODEC_ID_OPUS)
+                /* Opus requires a fixed 48kHz clock */
+                avpriv_set_pts_info(st, 64, 1, 48000);
+            else
+                avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
         else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
             avpriv_set_pts_info(st, 64, st->codec->time_base.num, st->codec->time_base.den);
-        if (st->codec->codec_id != CODEC_ID_VORBIS &&
-            st->codec->codec_id != CODEC_ID_THEORA &&
-            st->codec->codec_id != CODEC_ID_SPEEX  &&
-            st->codec->codec_id != CODEC_ID_FLAC) {
+        if (st->codec->codec_id != AV_CODEC_ID_VORBIS &&
+            st->codec->codec_id != AV_CODEC_ID_THEORA &&
+            st->codec->codec_id != AV_CODEC_ID_SPEEX  &&
+            st->codec->codec_id != AV_CODEC_ID_FLAC   &&
+            st->codec->codec_id != AV_CODEC_ID_OPUS) {
             av_log(s, AV_LOG_ERROR, "Unsupported codec id in stream %d\n", i);
             return -1;
         }
@@ -376,7 +410,7 @@ static int ogg_write_header(AVFormatContext *s)
         oggstream->serial_num = serial_num;
 
         st->priv_data = oggstream;
-        if (st->codec->codec_id == CODEC_ID_FLAC) {
+        if (st->codec->codec_id == AV_CODEC_ID_FLAC) {
             int err = ogg_build_flac_headers(st->codec, oggstream,
                                              st->codec->flags & CODEC_FLAG_BITEXACT,
                                              &s->metadata);
@@ -385,7 +419,7 @@ static int ogg_write_header(AVFormatContext *s)
                 av_freep(&st->priv_data);
                 return err;
             }
-        } else if (st->codec->codec_id == CODEC_ID_SPEEX) {
+        } else if (st->codec->codec_id == AV_CODEC_ID_SPEEX) {
             int err = ogg_build_speex_headers(st->codec, oggstream,
                                               st->codec->flags & CODEC_FLAG_BITEXACT,
                                               &s->metadata);
@@ -394,14 +428,23 @@ static int ogg_write_header(AVFormatContext *s)
                 av_freep(&st->priv_data);
                 return err;
             }
+        } else if (st->codec->codec_id == AV_CODEC_ID_OPUS) {
+            int err = ogg_build_opus_headers(st->codec, oggstream,
+                                             st->codec->flags & CODEC_FLAG_BITEXACT,
+                                             &s->metadata);
+            if (err) {
+                av_log(s, AV_LOG_ERROR, "Error writing Opus headers\n");
+                av_freep(&st->priv_data);
+                return err;
+            }
         } else {
             uint8_t *p;
-            const char *cstr = st->codec->codec_id == CODEC_ID_VORBIS ? "vorbis" : "theora";
-            int header_type = st->codec->codec_id == CODEC_ID_VORBIS ? 3 : 0x81;
-            int framing_bit = st->codec->codec_id == CODEC_ID_VORBIS ? 1 : 0;
+            const char *cstr = st->codec->codec_id == AV_CODEC_ID_VORBIS ? "vorbis" : "theora";
+            int header_type = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 3 : 0x81;
+            int framing_bit = st->codec->codec_id == AV_CODEC_ID_VORBIS ? 1 : 0;
 
             if (avpriv_split_xiph_headers(st->codec->extradata, st->codec->extradata_size,
-                                      st->codec->codec_id == CODEC_ID_VORBIS ? 30 : 42,
+                                      st->codec->codec_id == AV_CODEC_ID_VORBIS ? 30 : 42,
                                       oggstream->header, oggstream->header_len) < 0) {
                 av_log(s, AV_LOG_ERROR, "Extradata corrupted\n");
                 av_freep(&st->priv_data);
@@ -418,7 +461,7 @@ static int ogg_write_header(AVFormatContext *s)
             bytestream_put_byte(&p, header_type);
             bytestream_put_buffer(&p, cstr, 6);
 
-            if (st->codec->codec_id == CODEC_ID_THEORA) {
+            if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
                 /** KFGSHIFT is the width of the less significant section of the granule position
                     The less significant section is the frame count since the last keyframe */
                 oggstream->kfgshift = ((oggstream->header[0][40]&3)<<3)|(oggstream->header[0][41]>>5);
@@ -478,7 +521,7 @@ static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
     int ret;
     int64_t granule;
 
-    if (st->codec->codec_id == CODEC_ID_THEORA) {
+    if (st->codec->codec_id == AV_CODEC_ID_THEORA) {
         int64_t pts = oggstream->vrev < 1 ? pkt->pts : pkt->pts + pkt->duration;
         int pframe_count;
         if (pkt->flags & AV_PKT_FLAG_KEY)
@@ -490,7 +533,9 @@ static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
             pframe_count = 0;
         }
         granule = (oggstream->last_kf_pts<<oggstream->kfgshift) | pframe_count;
-    } else
+    } else if (st->codec->codec_id == AV_CODEC_ID_OPUS)
+        granule = pkt->pts + pkt->duration + av_rescale_q(st->codec->delay, (AVRational){ 1, st->codec->sample_rate }, st->time_base);
+    else
         granule = pkt->pts + pkt->duration;
 
     ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule, 0);
@@ -517,8 +562,9 @@ static int ogg_write_trailer(AVFormatContext *s)
     for (i = 0; i < s->nb_streams; i++) {
         AVStream *st = s->streams[i];
         OGGStreamContext *oggstream = st->priv_data;
-        if (st->codec->codec_id == CODEC_ID_FLAC ||
-            st->codec->codec_id == CODEC_ID_SPEEX) {
+        if (st->codec->codec_id == AV_CODEC_ID_FLAC ||
+            st->codec->codec_id == AV_CODEC_ID_SPEEX ||
+            st->codec->codec_id == AV_CODEC_ID_OPUS) {
             av_free(oggstream->header[0]);
         }
         av_freep(&oggstream->header[1]);
@@ -531,10 +577,10 @@ AVOutputFormat ff_ogg_muxer = {
     .name              = "ogg",
     .long_name         = NULL_IF_CONFIG_SMALL("Ogg"),
     .mime_type         = "application/ogg",
-    .extensions        = "ogg,ogv,spx",
+    .extensions        = "ogg,ogv,spx,opus",
     .priv_data_size    = sizeof(OGGContext),
-    .audio_codec       = CODEC_ID_FLAC,
-    .video_codec       = CODEC_ID_THEORA,
+    .audio_codec       = AV_CODEC_ID_FLAC,
+    .video_codec       = AV_CODEC_ID_THEORA,
     .write_header      = ogg_write_header,
     .write_packet      = ogg_write_packet,
     .write_trailer     = ogg_write_trailer,