00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00031 #include <libavutil/imgutils.h>
00032 #include <libavutil/samplefmt.h>
00033 #include <libavutil/timestamp.h>
00034 #include <libavformat/avformat.h>
00035
00036 static AVFormatContext *fmt_ctx = NULL;
00037 static AVCodecContext *video_dec_ctx = NULL, *audio_dec_ctx;
00038 static AVStream *video_stream = NULL, *audio_stream = NULL;
00039 static const char *src_filename = NULL;
00040 static const char *video_dst_filename = NULL;
00041 static const char *audio_dst_filename = NULL;
00042 static FILE *video_dst_file = NULL;
00043 static FILE *audio_dst_file = NULL;
00044
00045 static uint8_t *video_dst_data[4] = {NULL};
00046 static int video_dst_linesize[4];
00047 static int video_dst_bufsize;
00048
00049 static uint8_t **audio_dst_data = NULL;
00050 static int audio_dst_linesize;
00051 static int audio_dst_bufsize;
00052
00053 static int video_stream_idx = -1, audio_stream_idx = -1;
00054 static AVFrame *frame = NULL;
00055 static AVPacket pkt;
00056 static int video_frame_count = 0;
00057 static int audio_frame_count = 0;
00058
00059 static int decode_packet(int *got_frame, int cached)
00060 {
00061 int ret = 0;
00062
00063 if (pkt.stream_index == video_stream_idx) {
00064
00065 ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, &pkt);
00066 if (ret < 0) {
00067 fprintf(stderr, "Error decoding video frame\n");
00068 return ret;
00069 }
00070
00071 if (*got_frame) {
00072 printf("video_frame%s n:%d coded_n:%d pts:%s\n",
00073 cached ? "(cached)" : "",
00074 video_frame_count++, frame->coded_picture_number,
00075 av_ts2timestr(frame->pts, &video_dec_ctx->time_base));
00076
00077
00078
00079 av_image_copy(video_dst_data, video_dst_linesize,
00080 (const uint8_t **)(frame->data), frame->linesize,
00081 video_dec_ctx->pix_fmt, video_dec_ctx->width, video_dec_ctx->height);
00082
00083
00084 fwrite(video_dst_data[0], 1, video_dst_bufsize, video_dst_file);
00085 }
00086 } else if (pkt.stream_index == audio_stream_idx) {
00087
00088 ret = avcodec_decode_audio4(audio_dec_ctx, frame, got_frame, &pkt);
00089 if (ret < 0) {
00090 fprintf(stderr, "Error decoding audio frame\n");
00091 return ret;
00092 }
00093
00094 if (*got_frame) {
00095 printf("audio_frame%s n:%d nb_samples:%d pts:%s\n",
00096 cached ? "(cached)" : "",
00097 audio_frame_count++, frame->nb_samples,
00098 av_ts2timestr(frame->pts, &audio_dec_ctx->time_base));
00099
00100 ret = av_samples_alloc(audio_dst_data, &audio_dst_linesize, frame->channels,
00101 frame->nb_samples, frame->format, 1);
00102 if (ret < 0) {
00103 fprintf(stderr, "Could not allocate audio buffer\n");
00104 return AVERROR(ENOMEM);
00105 }
00106
00107
00108 audio_dst_bufsize =
00109 av_samples_get_buffer_size(NULL, frame->channels,
00110 frame->nb_samples, frame->format, 1);
00111
00112
00113
00114 av_samples_copy(audio_dst_data, frame->data, 0, 0,
00115 frame->nb_samples, frame->channels, frame->format);
00116
00117
00118 fwrite(audio_dst_data[0], 1, audio_dst_bufsize, audio_dst_file);
00119 av_freep(&audio_dst_data[0]);
00120 }
00121 }
00122
00123 return ret;
00124 }
00125
00126 static int open_codec_context(int *stream_idx,
00127 AVFormatContext *fmt_ctx, enum AVMediaType type)
00128 {
00129 int ret;
00130 AVStream *st;
00131 AVCodecContext *dec_ctx = NULL;
00132 AVCodec *dec = NULL;
00133
00134 ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
00135 if (ret < 0) {
00136 fprintf(stderr, "Could not find %s stream in input file '%s'\n",
00137 av_get_media_type_string(type), src_filename);
00138 return ret;
00139 } else {
00140 *stream_idx = ret;
00141 st = fmt_ctx->streams[*stream_idx];
00142
00143
00144 dec_ctx = st->codec;
00145 dec = avcodec_find_decoder(dec_ctx->codec_id);
00146 if (!dec) {
00147 fprintf(stderr, "Failed to find %s codec\n",
00148 av_get_media_type_string(type));
00149 return ret;
00150 }
00151
00152 if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
00153 fprintf(stderr, "Failed to open %s codec\n",
00154 av_get_media_type_string(type));
00155 return ret;
00156 }
00157 }
00158
00159 return 0;
00160 }
00161
00162 static int get_format_from_sample_fmt(const char **fmt,
00163 enum AVSampleFormat sample_fmt)
00164 {
00165 int i;
00166 struct sample_fmt_entry {
00167 enum AVSampleFormat sample_fmt; const char *fmt_be, *fmt_le;
00168 } sample_fmt_entries[] = {
00169 { AV_SAMPLE_FMT_U8, "u8", "u8" },
00170 { AV_SAMPLE_FMT_S16, "s16be", "s16le" },
00171 { AV_SAMPLE_FMT_S32, "s32be", "s32le" },
00172 { AV_SAMPLE_FMT_FLT, "f32be", "f32le" },
00173 { AV_SAMPLE_FMT_DBL, "f64be", "f64le" },
00174 };
00175 *fmt = NULL;
00176
00177 for (i = 0; i < FF_ARRAY_ELEMS(sample_fmt_entries); i++) {
00178 struct sample_fmt_entry *entry = &sample_fmt_entries[i];
00179 if (sample_fmt == entry->sample_fmt) {
00180 *fmt = AV_NE(entry->fmt_be, entry->fmt_le);
00181 return 0;
00182 }
00183 }
00184
00185 fprintf(stderr,
00186 "sample format %s is not supported as output format\n",
00187 av_get_sample_fmt_name(sample_fmt));
00188 return -1;
00189 }
00190
00191 int main (int argc, char **argv)
00192 {
00193 int ret = 0, got_frame;
00194
00195 if (argc != 4) {
00196 fprintf(stderr, "usage: %s input_file video_output_file audio_output_file\n"
00197 "API example program to show how to read frames from an input file.\n"
00198 "This program reads frames from a file, decodes them, and writes decoded\n"
00199 "video frames to a rawvideo file named video_output_file, and decoded\n"
00200 "audio frames to a rawaudio file named audio_output_file.\n"
00201 "\n", argv[0]);
00202 exit(1);
00203 }
00204 src_filename = argv[1];
00205 video_dst_filename = argv[2];
00206 audio_dst_filename = argv[3];
00207
00208
00209 av_register_all();
00210
00211
00212 if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) {
00213 fprintf(stderr, "Could not open source file %s\n", src_filename);
00214 exit(1);
00215 }
00216
00217
00218 if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
00219 fprintf(stderr, "Could not find stream information\n");
00220 exit(1);
00221 }
00222
00223 if (open_codec_context(&video_stream_idx, fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) {
00224 video_stream = fmt_ctx->streams[video_stream_idx];
00225 video_dec_ctx = video_stream->codec;
00226
00227 video_dst_file = fopen(video_dst_filename, "wb");
00228 if (!video_dst_file) {
00229 fprintf(stderr, "Could not open destination file %s\n", video_dst_filename);
00230 ret = 1;
00231 goto end;
00232 }
00233
00234
00235 ret = av_image_alloc(video_dst_data, video_dst_linesize,
00236 video_dec_ctx->width, video_dec_ctx->height,
00237 video_dec_ctx->pix_fmt, 1);
00238 if (ret < 0) {
00239 fprintf(stderr, "Could not allocate raw video buffer\n");
00240 goto end;
00241 }
00242 video_dst_bufsize = ret;
00243 }
00244
00245
00246 av_dump_format(fmt_ctx, 0, src_filename, 0);
00247
00248 if (open_codec_context(&audio_stream_idx, fmt_ctx, AVMEDIA_TYPE_AUDIO) >= 0) {
00249 int nb_planes;
00250
00251 audio_stream = fmt_ctx->streams[audio_stream_idx];
00252 audio_dec_ctx = audio_stream->codec;
00253 audio_dst_file = fopen(audio_dst_filename, "wb");
00254 if (!audio_dst_file) {
00255 fprintf(stderr, "Could not open destination file %s\n", video_dst_filename);
00256 ret = 1;
00257 goto end;
00258 }
00259
00260 nb_planes = av_sample_fmt_is_planar(audio_dec_ctx->sample_fmt) ?
00261 audio_dec_ctx->channels : 1;
00262 audio_dst_data = av_mallocz(sizeof(uint8_t *) * nb_planes);
00263 if (!audio_dst_data) {
00264 fprintf(stderr, "Could not allocate audio data buffers\n");
00265 ret = AVERROR(ENOMEM);
00266 goto end;
00267 }
00268 }
00269
00270 if (!audio_stream && !video_stream) {
00271 fprintf(stderr, "Could not find audio or video stream in the input, aborting\n");
00272 ret = 1;
00273 goto end;
00274 }
00275
00276 frame = avcodec_alloc_frame();
00277 if (!frame) {
00278 fprintf(stderr, "Could not allocate frame\n");
00279 ret = AVERROR(ENOMEM);
00280 goto end;
00281 }
00282
00283
00284 av_init_packet(&pkt);
00285 pkt.data = NULL;
00286 pkt.size = 0;
00287
00288 if (video_stream)
00289 printf("Demuxing video from file '%s' into '%s'\n", src_filename, video_dst_filename);
00290 if (audio_stream)
00291 printf("Demuxing video from file '%s' into '%s'\n", src_filename, audio_dst_filename);
00292
00293
00294 while (av_read_frame(fmt_ctx, &pkt) >= 0)
00295 decode_packet(&got_frame, 0);
00296
00297
00298 pkt.data = NULL;
00299 pkt.size = 0;
00300 do {
00301 decode_packet(&got_frame, 1);
00302 } while (got_frame);
00303
00304 printf("Demuxing succeeded.\n");
00305
00306 if (video_stream) {
00307 printf("Play the output video file with the command:\n"
00308 "ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
00309 av_get_pix_fmt_name(video_dec_ctx->pix_fmt), video_dec_ctx->width, video_dec_ctx->height,
00310 video_dst_filename);
00311 }
00312
00313 if (audio_stream) {
00314 const char *fmt;
00315
00316 if ((ret = get_format_from_sample_fmt(&fmt, audio_dec_ctx->sample_fmt) < 0))
00317 goto end;
00318 printf("Play the output audio file with the command:\n"
00319 "ffplay -f %s -ac %d -ar %d %s\n",
00320 fmt, audio_dec_ctx->channels, audio_dec_ctx->sample_rate,
00321 audio_dst_filename);
00322 }
00323
00324 end:
00325 if (video_dec_ctx)
00326 avcodec_close(video_dec_ctx);
00327 if (audio_dec_ctx)
00328 avcodec_close(audio_dec_ctx);
00329 avformat_close_input(&fmt_ctx);
00330 if (video_dst_file)
00331 fclose(video_dst_file);
00332 if (audio_dst_file)
00333 fclose(audio_dst_file);
00334 av_free(frame);
00335 av_free(video_dst_data[0]);
00336 av_free(audio_dst_data);
00337
00338 return ret < 0;
00339 }