Browse code

lavf: add avformat_find_stream_info()

It supports passing options to codecs.
(cherry picked from commit a67c061e0f3b55ffcc96f336fc0998e44b86c8e4)

Conflicts:

libavformat/utils.c

Signed-off-by: Anton Khirnov <anton@khirnov.net>

Anton Khirnov authored on 2011/05/23 02:24:59
Showing 3 changed files
... ...
@@ -96,7 +96,7 @@ static int movie_init(AVFilterContext *ctx)
96 96
                "Failed to avformat_open_input '%s'\n", movie->file_name);
97 97
         return ret;
98 98
     }
99
-    if ((ret = av_find_stream_info(movie->format_ctx)) < 0)
99
+    if ((ret = avformat_find_stream_info(movie->format_ctx, NULL)) < 0)
100 100
         av_log(ctx, AV_LOG_WARNING, "Failed to find stream info\n");
101 101
 
102 102
     // if seeking requested, we execute it
... ...
@@ -1081,6 +1081,7 @@ int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputForma
1081 1081
  */
1082 1082
 AVFormatContext *avformat_alloc_context(void);
1083 1083
 
1084
+#if FF_API_FORMAT_PARAMETERS
1084 1085
 /**
1085 1086
  * Read packets of a media file to get stream information. This
1086 1087
  * is useful for file formats with no headers such as MPEG. This
... ...
@@ -1093,8 +1094,34 @@ AVFormatContext *avformat_alloc_context(void);
1093 1093
  * @return >=0 if OK, AVERROR_xxx on error
1094 1094
  * @todo Let the user decide somehow what information is needed so that
1095 1095
  *       we do not waste time getting stuff the user does not need.
1096
+ *
1097
+ * @deprecated use avformat_find_stream_info.
1096 1098
  */
1097 1099
 int av_find_stream_info(AVFormatContext *ic);
1100
+#endif
1101
+
1102
+/**
1103
+ * Read packets of a media file to get stream information. This
1104
+ * is useful for file formats with no headers such as MPEG. This
1105
+ * function also computes the real framerate in case of MPEG-2 repeat
1106
+ * frame mode.
1107
+ * The logical file position is not changed by this function;
1108
+ * examined packets may be buffered for later processing.
1109
+ *
1110
+ * @param ic media file handle
1111
+ * @param options  If non-NULL, an ic.nb_streams long array of pointers to
1112
+ *                 dictionaries, where i-th member contains options for
1113
+ *                 codec corresponding to i-th stream.
1114
+ *                 On return each dictionary will be filled with options that were not found.
1115
+ * @return >=0 if OK, AVERROR_xxx on error
1116
+ *
1117
+ * @note this function isn't guaranteed to open all the codecs, so
1118
+ *       options being non-empty at return is a perfectly normal behavior.
1119
+ *
1120
+ * @todo Let the user decide somehow what information is needed so that
1121
+ *       we do not waste time getting stuff the user does not need.
1122
+ */
1123
+int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);
1098 1124
 
1099 1125
 /**
1100 1126
  * Find the "best" stream in the file.
... ...
@@ -2074,7 +2074,7 @@ static int has_decode_delay_been_guessed(AVStream *st)
2074 2074
         st->codec_info_nb_frames >= 6 + st->codec->has_b_frames;
2075 2075
 }
2076 2076
 
2077
-static int try_decode_frame(AVStream *st, AVPacket *avpkt)
2077
+static int try_decode_frame(AVStream *st, AVPacket *avpkt, AVDictionary **options)
2078 2078
 {
2079 2079
     int16_t *samples;
2080 2080
     AVCodec *codec;
... ...
@@ -2085,7 +2085,7 @@ static int try_decode_frame(AVStream *st, AVPacket *avpkt)
2085 2085
         codec = avcodec_find_decoder(st->codec->codec_id);
2086 2086
         if (!codec)
2087 2087
             return -1;
2088
-        ret = avcodec_open(st->codec, codec);
2088
+        ret = avcodec_open2(st->codec, codec, options);
2089 2089
         if (ret < 0)
2090 2090
             return ret;
2091 2091
     }
... ...
@@ -2204,12 +2204,20 @@ static int tb_unreliable(AVCodecContext *c){
2204 2204
     return 0;
2205 2205
 }
2206 2206
 
2207
+#if FF_API_FORMAT_PARAMETERS
2207 2208
 int av_find_stream_info(AVFormatContext *ic)
2208 2209
 {
2210
+    return avformat_find_stream_info(ic, NULL);
2211
+}
2212
+#endif
2213
+
2214
+int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2215
+{
2209 2216
     int i, count, ret, read_size, j;
2210 2217
     AVStream *st;
2211 2218
     AVPacket pkt1, *pkt;
2212 2219
     int64_t old_offset = avio_tell(ic->pb);
2220
+    int orig_nb_streams = ic->nb_streams;        // new streams might appear, no options for those
2213 2221
 
2214 2222
     for(i=0;i<ic->nb_streams;i++) {
2215 2223
         AVCodec *codec;
... ...
@@ -2246,12 +2254,12 @@ int av_find_stream_info(AVFormatContext *ic)
2246 2246
         /* Ensure that subtitle_header is properly set. */
2247 2247
         if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
2248 2248
             && codec && !st->codec->codec)
2249
-            avcodec_open(st->codec, codec);
2249
+            avcodec_open2(st->codec, codec, options ? &options[i] : NULL);
2250 2250
 
2251 2251
         //try to just open decoders, in case this is enough to get parameters
2252 2252
         if(!has_codec_parameters(st->codec)){
2253 2253
             if (codec && !st->codec->codec)
2254
-                avcodec_open(st->codec, codec);
2254
+                avcodec_open2(st->codec, codec, options ? &options[i] : NULL);
2255 2255
         }
2256 2256
     }
2257 2257
 
... ...
@@ -2386,7 +2394,7 @@ int av_find_stream_info(AVFormatContext *ic)
2386 2386
            it takes longer and uses more memory. For MPEG-4, we need to
2387 2387
            decompress for QuickTime. */
2388 2388
         if (!has_codec_parameters(st->codec) || !has_decode_delay_been_guessed(st))
2389
-            try_decode_frame(st, pkt);
2389
+            try_decode_frame(st, pkt, (options && i <= orig_nb_streams )? &options[i] : NULL);
2390 2390
 
2391 2391
         st->codec_info_nb_frames++;
2392 2392
         count++;