]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/mvdec.c
Merge commit '5fccedaa67390ccddd6347c8e1c71b7664558bcd'
[ffmpeg] / libavformat / mvdec.c
index 5525233db56a534033256bf740ecd02221fed424..6e7c3ffd113cbc16225ccb9106d4e173bcada4a2 100644 (file)
  * Silicon Graphics Movie demuxer
  */
 
+#include "libavutil/channel_layout.h"
 #include "libavutil/eval.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/rational.h"
+
 #include "avformat.h"
 #include "internal.h"
 
-typedef struct {
+typedef struct MvContext {
     int nb_video_tracks;
     int nb_audio_tracks;
 
-    int eof_count;        /**< number of streams that have finished */
-    int stream_index;     /**< current stream index */
-    int frame[2];         /**< frame nb for current stream */
+    int eof_count;        ///< number of streams that have finished
+    int stream_index;     ///< current stream index
+    int frame[2];         ///< frame nb for current stream
+
+    int acompression;     ///< compression level for audio stream
+    int aformat;          ///< audio format
 } MvContext;
 
 #define AUDIO_FORMAT_SIGNED 401
 
 static int mv_probe(AVProbeData *p)
 {
-    if (AV_RB32(p->buf) == MKBETAG('M','O','V','I') && AV_RB16(p->buf + 4) < 3)
+    if (AV_RB32(p->buf) == MKBETAG('M', 'O', 'V', 'I') &&
+        AV_RB16(p->buf + 4) < 3)
         return AVPROBE_SCORE_MAX;
     return 0;
 }
 
-static char * var_read_string(AVIOContext *pb, int size)
+static char *var_read_string(AVIOContext *pb, int size)
 {
-    char *str = av_malloc(size + 1);
     int n;
+    char *str = av_malloc(size + 1);
     if (!str)
         return NULL;
     n = avio_get_str(pb, size, str, size + 1);
     if (n < size)
-         avio_skip(pb, size - n);
+        avio_skip(pb, size - n);
     return str;
 }
 
 static int var_read_int(AVIOContext *pb, int size)
 {
     int v;
-    char * s = var_read_string(pb, size);
-    if (!s || sscanf(s, "%d", &v) != 1)
-        v = 0;
+    char *s = var_read_string(pb, size);
+    if (!s)
+        return 0;
+    v = strtol(s, NULL, 10);
     av_free(s);
     return v;
 }
@@ -73,9 +80,9 @@ static int var_read_int(AVIOContext *pb, int size)
 static AVRational var_read_float(AVIOContext *pb, int size)
 {
     AVRational v;
-    char * s = var_read_string(pb, size);
+    char *s = var_read_string(pb, size);
     if (!s)
-        return (AVRational){0, 0};
+        return (AVRational) { 0, 0 };
     v = av_d2q(av_strtod(s, NULL), INT_MAX);
     av_free(s);
     return v;
@@ -88,13 +95,15 @@ static void var_read_metadata(AVFormatContext *avctx, const char *tag, int size)
         av_dict_set(&avctx->metadata, tag, value, AV_DICT_DONT_STRDUP_VAL);
 }
 
-static int set_channels(AVFormatContext *avctx, AVStream *st, int channels) {
+static int set_channels(AVFormatContext *avctx, AVStream *st, int channels)
+{
     if (channels <= 0) {
-        av_log(avctx, AV_LOG_ERROR, "Channel count %d invalid\n", channels);
+        av_log(avctx, AV_LOG_ERROR, "Channel count %d invalid.\n", channels);
         return AVERROR_INVALIDDATA;
     }
-    st->codec->channels = channels;
-    st->codec->channel_layout = (st->codec->channels == 1) ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
+    st->codec->channels       = channels;
+    st->codec->channel_layout = (st->codec->channels == 1) ? AV_CH_LAYOUT_MONO
+                                                           : AV_CH_LAYOUT_STEREO;
     return 0;
 }
 
@@ -102,7 +111,8 @@ static int set_channels(AVFormatContext *avctx, AVStream *st, int channels) {
  * Parse global variable
  * @return < 0 if unknown
  */
-static int parse_global_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
+static int parse_global_var(AVFormatContext *avctx, AVStream *st,
+                            const char *name, int size)
 {
     MvContext *mv = avctx->priv_data;
     AVIOContext *pb = avctx->pb;
@@ -112,10 +122,11 @@ static int parse_global_var(AVFormatContext *avctx, AVStream *st, const char *na
         mv->nb_audio_tracks = var_read_int(pb, size);
     } else if (!strcmp(name, "COMMENT") || !strcmp(name, "TITLE")) {
         var_read_metadata(avctx, name, size);
-    } else if (!strcmp(name, "LOOP_MODE") || !strcmp(name, "NUM_LOOPS") || !strcmp(name, "OPTIMIZED")) {
+    } else if (!strcmp(name, "LOOP_MODE") || !strcmp(name, "NUM_LOOPS") ||
+               !strcmp(name, "OPTIMIZED")) {
         avio_skip(pb, size); // ignore
     } else
-        return -1;
+        return AVERROR_INVALIDDATA;
 
     return 0;
 }
@@ -124,15 +135,17 @@ static int parse_global_var(AVFormatContext *avctx, AVStream *st, const char *na
  * Parse audio variable
  * @return < 0 if unknown
  */
-static int parse_audio_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
+static int parse_audio_var(AVFormatContext *avctx, AVStream *st,
+                           const char *name, int size)
 {
+    MvContext *mv = avctx->priv_data;
     AVIOContext *pb = avctx->pb;
     if (!strcmp(name, "__DIR_COUNT")) {
         st->nb_frames = var_read_int(pb, size);
     } else if (!strcmp(name, "AUDIO_FORMAT")) {
-        st->codec->codec_id = var_read_int(pb, size);
+        mv->aformat = var_read_int(pb, size);
     } else if (!strcmp(name, "COMPRESSION")) {
-        st->codec->codec_tag = var_read_int(pb, size);
+        mv->acompression = var_read_int(pb, size);
     } else if (!strcmp(name, "DEFAULT_VOL")) {
         var_read_metadata(avctx, name, size);
     } else if (!strcmp(name, "NUM_CHANNELS")) {
@@ -143,7 +156,8 @@ static int parse_audio_var(AVFormatContext *avctx, AVStream *st, const char *nam
     } else if (!strcmp(name, "SAMPLE_WIDTH")) {
         st->codec->bits_per_coded_sample = var_read_int(pb, size) * 8;
     } else
-        return -1;
+        return AVERROR_INVALIDDATA;
+
     return 0;
 }
 
@@ -151,13 +165,14 @@ static int parse_audio_var(AVFormatContext *avctx, AVStream *st, const char *nam
  * Parse video variable
  * @return < 0 if unknown
  */
-static int parse_video_var(AVFormatContext *avctx, AVStream *st, const char *name, int size)
+static int parse_video_var(AVFormatContext *avctx, AVStream *st,
+                           const char *name, int size)
 {
     AVIOContext *pb = avctx->pb;
     if (!strcmp(name, "__DIR_COUNT")) {
         st->nb_frames = st->duration = var_read_int(pb, size);
     } else if (!strcmp(name, "COMPRESSION")) {
-        char * str = var_read_string(pb, size);
+        char *str = var_read_string(pb, size);
         if (!str)
             return AVERROR_INVALIDDATA;
         if (!strcmp(str, "1")) {
@@ -172,35 +187,40 @@ static int parse_video_var(AVFormatContext *avctx, AVStream *st, const char *nam
         } else if (!strcmp(str, "MVC2")) {
             st->codec->codec_id = AV_CODEC_ID_MVC2;
         } else {
-            avpriv_request_sample(avctx, "video compression %s", str);
+            avpriv_request_sample(avctx, "Video compression %s", str);
         }
         av_free(str);
     } else if (!strcmp(name, "FPS")) {
         AVRational fps = var_read_float(pb, size);
         avpriv_set_pts_info(st, 64, fps.den, fps.num);
+        st->avg_frame_rate = fps;
     } else if (!strcmp(name, "HEIGHT")) {
         st->codec->height = var_read_int(pb, size);
     } else if (!strcmp(name, "PIXEL_ASPECT")) {
         st->sample_aspect_ratio = var_read_float(pb, size);
         av_reduce(&st->sample_aspect_ratio.num, &st->sample_aspect_ratio.den,
-                  st->sample_aspect_ratio.num, st->sample_aspect_ratio.den, INT_MAX);
+                  st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
+                  INT_MAX);
     } else if (!strcmp(name, "WIDTH")) {
         st->codec->width = var_read_int(pb, size);
     } else if (!strcmp(name, "ORIENTATION")) {
         if (var_read_int(pb, size) == 1101) {
-            st->codec->extradata       = av_strdup("BottomUp");
-            st->codec->extradata_size  = 9;
+            st->codec->extradata      = av_strdup("BottomUp");
+            st->codec->extradata_size = 9;
         }
     } else if (!strcmp(name, "Q_SPATIAL") || !strcmp(name, "Q_TEMPORAL")) {
         var_read_metadata(avctx, name, size);
     } else if (!strcmp(name, "INTERLACING") || !strcmp(name, "PACKING")) {
         avio_skip(pb, size); // ignore
     } else
-        return -1;
+        return AVERROR_INVALIDDATA;
+
     return 0;
 }
 
-static void read_table(AVFormatContext *avctx, AVStream *st, int (*parse)(AVFormatContext *avctx, AVStream *st, const char *name, int size))
+static void read_table(AVFormatContext *avctx, AVStream *st,
+                       int (*parse)(AVFormatContext *avctx, AVStream *st,
+                                    const char *name, int size))
 {
     int count, i;
     AVIOContext *pb = avctx->pb;
@@ -214,7 +234,7 @@ static void read_table(AVFormatContext *avctx, AVStream *st, int (*parse)(AVForm
         name[sizeof(name) - 1] = 0;
         size = avio_rb32(pb);
         if (parse(avctx, st, name, size) < 0) {
-            avpriv_request_sample(avctx, "variable %s", name);
+            avpriv_request_sample(avctx, "Variable %s", name);
             avio_skip(pb, size);
         }
     }
@@ -253,7 +273,7 @@ static int mv_read_header(AVFormatContext *avctx)
         avio_skip(pb, 22);
 
         /* allocate audio track first to prevent unnecessary seeking
-           (audio packet always precede video packet for a given frame) */
+         * (audio packet always precede video packet for a given frame) */
         ast = avformat_new_stream(avctx, NULL);
         if (!ast)
             return AVERROR(ENOMEM);
@@ -261,9 +281,10 @@ static int mv_read_header(AVFormatContext *avctx)
         vst = avformat_new_stream(avctx, NULL);
         if (!vst)
             return AVERROR(ENOMEM);
-        vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
         avpriv_set_pts_info(vst, 64, 1, 15);
-        vst->nb_frames = avio_rb32(pb);
+        vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
+        vst->avg_frame_rate    = av_inv_q(vst->time_base);
+        vst->nb_frames         = avio_rb32(pb);
         v = avio_rb32(pb);
         switch (v) {
         case 1:
@@ -274,7 +295,7 @@ static int mv_read_header(AVFormatContext *avctx)
             vst->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
             break;
         default:
-            avpriv_request_sample(avctx, "video compression %i", v);
+            avpriv_request_sample(avctx, "Video compression %i", v);
             break;
         }
         vst->codec->codec_tag = 0;
@@ -282,9 +303,9 @@ static int mv_read_header(AVFormatContext *avctx)
         vst->codec->height    = avio_rb32(pb);
         avio_skip(pb, 12);
 
-        ast->codec->codec_type     = AVMEDIA_TYPE_AUDIO;
-        ast->nb_frames             = vst->nb_frames;
-        ast->codec->sample_rate    = avio_rb32(pb);
+        ast->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
+        ast->nb_frames          = vst->nb_frames;
+        ast->codec->sample_rate = avio_rb32(pb);
         avpriv_set_pts_info(ast, 33, 1, ast->codec->sample_rate);
         if (set_channels(avctx, ast, avio_rb32(pb)) < 0)
             return AVERROR_INVALIDDATA;
@@ -293,7 +314,7 @@ static int mv_read_header(AVFormatContext *avctx)
         if (v == AUDIO_FORMAT_SIGNED) {
             ast->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
         } else {
-            avpriv_request_sample(avctx, "audio compression (format %i)", v);
+            avpriv_request_sample(avctx, "Audio compression (format %i)", v);
         }
 
         avio_skip(pb, 12);
@@ -307,8 +328,8 @@ static int mv_read_header(AVFormatContext *avctx)
             uint32_t asize = avio_rb32(pb);
             uint32_t vsize = avio_rb32(pb);
             avio_skip(pb, 8);
-            av_add_index_entry(ast, pos,         timestamp, asize, 0, AVINDEX_KEYFRAME);
-            av_add_index_entry(vst, pos + asize, i,         vsize, 0, AVINDEX_KEYFRAME);
+            av_add_index_entry(ast, pos, timestamp, asize, 0, AVINDEX_KEYFRAME);
+            av_add_index_entry(vst, pos + asize, i, vsize, 0, AVINDEX_KEYFRAME);
             timestamp += asize / (ast->codec->channels * 2);
         }
     } else if (!version && avio_rb16(pb) == 3) {
@@ -317,31 +338,33 @@ static int mv_read_header(AVFormatContext *avctx)
         read_table(avctx, NULL, parse_global_var);
 
         if (mv->nb_audio_tracks > 1) {
-            avpriv_request_sample(avctx, "multiple audio streams support");
+            avpriv_request_sample(avctx, "Multiple audio streams support");
             return AVERROR_PATCHWELCOME;
         } else if (mv->nb_audio_tracks) {
             ast = avformat_new_stream(avctx, NULL);
             if (!ast)
                 return AVERROR(ENOMEM);
             ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
-            /* temporarily store compression value in codec_tag; format value in codec_id */
             read_table(avctx, ast, parse_audio_var);
-            if (ast->codec->codec_tag == 100 && ast->codec->codec_id == AUDIO_FORMAT_SIGNED && ast->codec->bits_per_coded_sample == 16) {
+            if (mv->acompression == 100 &&
+                mv->aformat == AUDIO_FORMAT_SIGNED &&
+                ast->codec->bits_per_coded_sample == 16) {
                 ast->codec->codec_id = AV_CODEC_ID_PCM_S16BE;
             } else {
-                avpriv_request_sample(avctx, "audio compression %i (format %i, width %i)",
-                    ast->codec->codec_tag, ast->codec->codec_id, ast->codec->bits_per_coded_sample);
+                avpriv_request_sample(avctx,
+                                      "Audio compression %i (format %i, sr %i)",
+                                      mv->acompression, mv->aformat,
+                                      ast->codec->bits_per_coded_sample);
                 ast->codec->codec_id = AV_CODEC_ID_NONE;
             }
-            ast->codec->codec_tag = 0;
             if (ast->codec->channels <= 0) {
-                av_log(avctx, AV_LOG_ERROR, "No valid channel count found\n");
+                av_log(avctx, AV_LOG_ERROR, "No valid channel count found.\n");
                 return AVERROR_INVALIDDATA;
             }
         }
 
         if (mv->nb_video_tracks > 1) {
-            avpriv_request_sample(avctx, "multiple video streams support");
+            avpriv_request_sample(avctx, "Multiple video streams support");
             return AVERROR_PATCHWELCOME;
         } else if (mv->nb_video_tracks) {
             vst = avformat_new_stream(avctx, NULL);
@@ -357,7 +380,7 @@ static int mv_read_header(AVFormatContext *avctx)
         if (mv->nb_video_tracks)
             read_index(pb, vst);
     } else {
-        avpriv_request_sample(avctx, "version %i", version);
+        avpriv_request_sample(avctx, "Version %i", version);
         return AVERROR_PATCHWELCOME;
     }
 
@@ -374,9 +397,9 @@ static int mv_read_packet(AVFormatContext *avctx, AVPacket *pkt)
     int ret;
     uint64_t pos;
 
-    if (frame  < st->nb_index_entries) {
+    if (frame < st->nb_index_entries) {
         index = &st->index_entries[frame];
-        pos = avio_tell(pb);
+        pos   = avio_tell(pb);
         if (index->pos > pos)
             avio_skip(pb, index->pos - pos);
         else if (index->pos < pos) {
@@ -391,8 +414,8 @@ static int mv_read_packet(AVFormatContext *avctx, AVPacket *pkt)
             return ret;
 
         pkt->stream_index = mv->stream_index;
-        pkt->pts = index->timestamp;
-        pkt->flags |= AV_PKT_FLAG_KEY;
+        pkt->pts          = index->timestamp;
+        pkt->flags       |= AV_PKT_FLAG_KEY;
 
         mv->frame[mv->stream_index]++;
         mv->eof_count = 0;
@@ -400,6 +423,9 @@ static int mv_read_packet(AVFormatContext *avctx, AVPacket *pkt)
         mv->eof_count++;
         if (mv->eof_count >= avctx->nb_streams)
             return AVERROR_EOF;
+
+        // avoid returning 0 without a packet
+        return AVERROR(EAGAIN);
     }
 
     mv->stream_index++;
@@ -409,7 +435,8 @@ static int mv_read_packet(AVFormatContext *avctx, AVPacket *pkt)
     return 0;
 }
 
-static int mv_read_seek(AVFormatContext *avctx, int stream_index, int64_t timestamp, int flags)
+static int mv_read_seek(AVFormatContext *avctx, int stream_index,
+                        int64_t timestamp, int flags)
 {
     MvContext *mv = avctx->priv_data;
     AVStream *st = avctx->streams[stream_index];
@@ -423,7 +450,7 @@ static int mv_read_seek(AVFormatContext *avctx, int stream_index, int64_t timest
 
     frame = av_index_search_timestamp(st, timestamp, flags);
     if (frame < 0)
-        return -1;
+        return AVERROR_INVALIDDATA;
 
     for (i = 0; i < avctx->nb_streams; i++)
         mv->frame[i] = frame;