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