]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/oggdec.c
avcodec: Remove deprecated old aliases for NVENC encoders
[ffmpeg] / libavformat / oggdec.c
index e0188c7c594d632d7a14d3948580adba44b95995..a456c3df60234656ef23fde54295bb9fac52d859 100644 (file)
@@ -42,7 +42,6 @@
 
 static const struct ogg_codec * const ogg_codecs[] = {
     &ff_skeleton_codec,
-    &ff_daala_codec,
     &ff_dirac_codec,
     &ff_speex_codec,
     &ff_vorbis_codec,
@@ -178,6 +177,7 @@ static int ogg_reset(AVFormatContext *s)
         if (start_pos <= s->internal->data_offset) {
             os->lastpts = 0;
         }
+        os->start_trimming = 0;
         os->end_trimming = 0;
         av_freep(&os->new_metadata);
         os->new_metadata_size = 0;
@@ -206,59 +206,43 @@ static const struct ogg_codec *ogg_find_codec(uint8_t *buf, int size)
  * situation where a new audio stream spawn (identified with a new serial) and
  * must replace the previous one (track switch).
  */
-static int ogg_replace_stream(AVFormatContext *s, uint32_t serial, int size)
+static int ogg_replace_stream(AVFormatContext *s, uint32_t serial, char *magic, int page_size,
+                              int probing)
 {
     struct ogg *ogg = s->priv_data;
     struct ogg_stream *os;
     const struct ogg_codec *codec;
     int i = 0;
 
-    if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
-        uint8_t magic[8];
-        avio_seek(s->pb, -size, SEEK_CUR);
-        if (avio_read(s->pb, magic, sizeof(magic)) != sizeof(magic))
-            return AVERROR_INVALIDDATA;
-        avio_seek(s->pb, size - sizeof(magic), SEEK_CUR);
-        codec = ogg_find_codec(magic, sizeof(magic));
-        if (!codec) {
-            av_log(s, AV_LOG_ERROR, "Cannot identify new stream\n");
-            return AVERROR_INVALIDDATA;
-        }
-        for (i = 0; i < ogg->nstreams; i++) {
-            if (ogg->streams[i].codec == codec)
-                break;
-        }
-        if (i >= ogg->nstreams)
-            return ogg_new_stream(s, serial);
-    } else if (ogg->nstreams != 1) {
+    if (ogg->nstreams != 1) {
         avpriv_report_missing_feature(s, "Changing stream parameters in multistream ogg");
         return AVERROR_PATCHWELCOME;
     }
 
-    os = &ogg->streams[i];
-
-    os->serial  = serial;
-    return i;
-
-#if 0
-    buf     = os->buf;
-    bufsize = os->bufsize;
-    codec   = os->codec;
+    /* Check for codecs */
+    codec = ogg_find_codec(magic, page_size);
+    if (!codec && !probing) {
+        av_log(s, AV_LOG_ERROR, "Cannot identify new stream\n");
+        return AVERROR_INVALIDDATA;
+    }
 
-    if (!ogg->state || ogg->state->streams[i].private != os->private)
-        av_freep(&ogg->streams[i].private);
+    os = &ogg->streams[0];
+    if (os->codec != codec)
+        return AVERROR(EINVAL);
 
-    /* Set Ogg stream settings similar to what is done in ogg_new_stream(). We
-     * also re-use the ogg_stream allocated buffer */
-    memset(os, 0, sizeof(*os));
     os->serial  = serial;
-    os->bufsize = bufsize;
-    os->buf     = buf;
-    os->header  = -1;
     os->codec   = codec;
+    os->serial  = serial;
+    os->lastpts = 0;
+    os->lastdts = 0;
+    os->start_trimming = 0;
+    os->end_trimming = 0;
+
+    /* Chained files have extradata as a new packet */
+    if (codec == &ff_opus_codec)
+        os->header = -1;
 
     return i;
-#endif
 }
 
 static int ogg_new_stream(AVFormatContext *s, uint32_t serial)
@@ -313,7 +297,21 @@ static int data_packets_seen(const struct ogg *ogg)
     return 0;
 }
 
-static int ogg_read_page(AVFormatContext *s, int *sid)
+static int buf_realloc(struct ogg_stream *os, int size)
+{
+    /* Even if invalid guarantee there's enough memory to read the page */
+    if (os->bufsize - os->bufpos < size) {
+        uint8_t *nb = av_realloc(os->buf, 2*os->bufsize + AV_INPUT_BUFFER_PADDING_SIZE);
+        if (!nb)
+            return AVERROR(ENOMEM);
+        os->buf = nb;
+        os->bufsize *= 2;
+    }
+
+    return 0;
+}
+
+static int ogg_read_page(AVFormatContext *s, int *sid, int probing)
 {
     AVIOContext *bc = s->pb;
     struct ogg *ogg = s->priv_data;
@@ -325,6 +323,7 @@ static int ogg_read_page(AVFormatContext *s, int *sid)
     uint32_t crc, crc_tmp;
     int size = 0, idx;
     int64_t version, page_pos;
+    int64_t start_pos;
     uint8_t sync[4];
     uint8_t segments[255];
     uint8_t *readout_buf;
@@ -364,6 +363,10 @@ static int ogg_read_page(AVFormatContext *s, int *sid)
     /* 0x4fa9b05f = av_crc(AV_CRC_32_IEEE, 0x0, "OggS", 4) */
     ffio_init_checksum(bc, ff_crc04C11DB7_update, 0x4fa9b05f);
 
+    /* To rewind if checksum is bad/check magic on switches - this is the max packet size */
+    ffio_ensure_seekback(bc, MAX_PAGE_SIZE);
+    start_pos = avio_tell(bc);
+
     version = avio_r8(bc);
     flags   = avio_r8(bc);
     gp      = avio_rl64(bc);
@@ -389,14 +392,9 @@ static int ogg_read_page(AVFormatContext *s, int *sid)
     if (idx >= 0) {
         os = ogg->streams + idx;
 
-        /* Even if invalid guarantee there's enough memory to read the page */
-        if (os->bufsize - os->bufpos < size) {
-            uint8_t *nb = av_realloc(os->buf, 2*os->bufsize + AV_INPUT_BUFFER_PADDING_SIZE);
-            if (!nb)
-                return AVERROR(ENOMEM);
-            os->buf = nb;
-            os->bufsize *= 2;
-        }
+        ret = buf_realloc(os, size);
+        if (ret < 0)
+            return ret;
 
         readout_buf = os->buf + os->bufpos;
     } else {
@@ -414,7 +412,8 @@ static int ogg_read_page(AVFormatContext *s, int *sid)
         av_log(s, AV_LOG_ERROR, "CRC mismatch!\n");
         if (idx < 0)
             av_free(readout_buf);
-        avio_seek(bc, -size, SEEK_CUR);
+        avio_seek(bc, start_pos, SEEK_SET);
+        *sid = -1;
         return 0;
     }
 
@@ -424,14 +423,15 @@ static int ogg_read_page(AVFormatContext *s, int *sid)
         av_log(s, AV_LOG_ERROR, "Invalid Ogg vers!\n");
         if (idx < 0)
             av_free(readout_buf);
-        avio_seek(bc, -size, SEEK_CUR);
+        avio_seek(bc, start_pos, SEEK_SET);
+        *sid = -1;
         return 0;
     }
 
     /* CRC is correct so we can be 99% sure there's an actual change here */
     if (idx < 0) {
         if (data_packets_seen(ogg))
-            idx = ogg_replace_stream(s, serial, size);
+            idx = ogg_replace_stream(s, serial, readout_buf, size, probing);
         else
             idx = ogg_new_stream(s, serial);
 
@@ -443,6 +443,12 @@ static int ogg_read_page(AVFormatContext *s, int *sid)
 
         os = ogg->streams + idx;
 
+        ret = buf_realloc(os, size);
+        if (ret < 0) {
+            av_free(readout_buf);
+            return ret;
+        }
+
         memcpy(os->buf + os->bufpos, readout_buf, size);
         av_free(readout_buf);
     }
@@ -506,7 +512,7 @@ static int ogg_packet(AVFormatContext *s, int *sid, int *dstart, int *dsize,
         idx = ogg->curidx;
 
         while (idx < 0) {
-            ret = ogg_read_page(s, &idx);
+            ret = ogg_read_page(s, &idx, 0);
             if (ret < 0)
                 return ret;
         }
@@ -657,8 +663,8 @@ static int ogg_get_length(AVFormatContext *s)
     avio_seek(s->pb, end, SEEK_SET);
     ogg->page_pos = -1;
 
-    while (!ogg_read_page(s, &i)) {
-        if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
+    while (!ogg_read_page(s, &i, 1)) {
+        if (i >= 0 && ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
             ogg->streams[i].codec) {
             s->streams[i]->duration =
                 ogg_gptopts(s, i, ogg->streams[i].granule, NULL);
@@ -861,25 +867,25 @@ retry:
     pkt->duration = os->pduration;
     pkt->pos      = fpos;
 
-    if (os->end_trimming) {
+    if (os->start_trimming || os->end_trimming) {
         uint8_t *side_data = av_packet_new_side_data(pkt,
                                                      AV_PKT_DATA_SKIP_SAMPLES,
                                                      10);
         if(!side_data)
             return AVERROR(ENOMEM);
+         AV_WL32(side_data + 0, os->start_trimming);
         AV_WL32(side_data + 4, os->end_trimming);
+        os->start_trimming = 0;
         os->end_trimming = 0;
     }
 
     if (os->new_metadata) {
-        uint8_t *side_data = av_packet_new_side_data(pkt,
-                                                     AV_PKT_DATA_METADATA_UPDATE,
-                                                     os->new_metadata_size);
-        if(!side_data)
-            return AVERROR(ENOMEM);
+        ret = av_packet_add_side_data(pkt, AV_PKT_DATA_METADATA_UPDATE,
+                                      os->new_metadata, os->new_metadata_size);
+        if (ret < 0)
+            return ret;
 
-        memcpy(side_data, os->new_metadata, os->new_metadata_size);
-        av_freep(&os->new_metadata);
+        os->new_metadata      = NULL;
         os->new_metadata_size = 0;
     }