yading@10: /* yading@10: * Copyright (c) 2012 Stefano Sabatini yading@10: * yading@10: * Permission is hereby granted, free of charge, to any person obtaining a copy yading@10: * of this software and associated documentation files (the "Software"), to deal yading@10: * in the Software without restriction, including without limitation the rights yading@10: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell yading@10: * copies of the Software, and to permit persons to whom the Software is yading@10: * furnished to do so, subject to the following conditions: yading@10: * yading@10: * The above copyright notice and this permission notice shall be included in yading@10: * all copies or substantial portions of the Software. yading@10: * yading@10: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR yading@10: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, yading@10: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL yading@10: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER yading@10: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, yading@10: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN yading@10: * THE SOFTWARE. yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * libavformat demuxing API use example. yading@10: * yading@10: * Show how to use the libavformat and libavcodec API to demux and yading@10: * decode audio and video data. yading@10: * @example doc/examples/demuxing.c yading@10: */ yading@10: yading@10: #include yading@10: #include yading@10: #include yading@10: #include yading@10: yading@10: static AVFormatContext *fmt_ctx = NULL; yading@10: static AVCodecContext *video_dec_ctx = NULL, *audio_dec_ctx; yading@10: static AVStream *video_stream = NULL, *audio_stream = NULL; yading@10: static const char *src_filename = NULL; yading@10: static const char *video_dst_filename = NULL; yading@10: static const char *audio_dst_filename = NULL; yading@10: static FILE *video_dst_file = NULL; yading@10: static FILE *audio_dst_file = NULL; yading@10: yading@10: static uint8_t *video_dst_data[4] = {NULL}; yading@10: static int video_dst_linesize[4]; yading@10: static int video_dst_bufsize; yading@10: yading@10: static uint8_t **audio_dst_data = NULL; yading@10: static int audio_dst_linesize; yading@10: static int audio_dst_bufsize; yading@10: yading@10: static int video_stream_idx = -1, audio_stream_idx = -1; yading@10: static AVFrame *frame = NULL; yading@10: static AVPacket pkt; yading@10: static int video_frame_count = 0; yading@10: static int audio_frame_count = 0; yading@10: yading@10: static int decode_packet(int *got_frame, int cached) yading@10: { yading@10: int ret = 0; yading@10: yading@10: if (pkt.stream_index == video_stream_idx) { yading@10: /* decode video frame */ yading@10: ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, &pkt); yading@10: if (ret < 0) { yading@10: fprintf(stderr, "Error decoding video frame\n"); yading@10: return ret; yading@10: } yading@10: yading@10: if (*got_frame) { yading@10: printf("video_frame%s n:%d coded_n:%d pts:%s\n", yading@10: cached ? "(cached)" : "", yading@10: video_frame_count++, frame->coded_picture_number, yading@10: av_ts2timestr(frame->pts, &video_dec_ctx->time_base)); yading@10: yading@10: /* copy decoded frame to destination buffer: yading@10: * this is required since rawvideo expects non aligned data */ yading@10: av_image_copy(video_dst_data, video_dst_linesize, yading@10: (const uint8_t **)(frame->data), frame->linesize, yading@10: video_dec_ctx->pix_fmt, video_dec_ctx->width, video_dec_ctx->height); yading@10: yading@10: /* write to rawvideo file */ yading@10: fwrite(video_dst_data[0], 1, video_dst_bufsize, video_dst_file); yading@10: } yading@10: } else if (pkt.stream_index == audio_stream_idx) { yading@10: /* decode audio frame */ yading@10: ret = avcodec_decode_audio4(audio_dec_ctx, frame, got_frame, &pkt); yading@10: if (ret < 0) { yading@10: fprintf(stderr, "Error decoding audio frame\n"); yading@10: return ret; yading@10: } yading@10: yading@10: if (*got_frame) { yading@10: printf("audio_frame%s n:%d nb_samples:%d pts:%s\n", yading@10: cached ? "(cached)" : "", yading@10: audio_frame_count++, frame->nb_samples, yading@10: av_ts2timestr(frame->pts, &audio_dec_ctx->time_base)); yading@10: yading@10: ret = av_samples_alloc(audio_dst_data, &audio_dst_linesize, av_frame_get_channels(frame), yading@10: frame->nb_samples, frame->format, 1); yading@10: if (ret < 0) { yading@10: fprintf(stderr, "Could not allocate audio buffer\n"); yading@10: return AVERROR(ENOMEM); yading@10: } yading@10: yading@10: /* TODO: extend return code of the av_samples_* functions so that this call is not needed */ yading@10: audio_dst_bufsize = yading@10: av_samples_get_buffer_size(NULL, av_frame_get_channels(frame), yading@10: frame->nb_samples, frame->format, 1); yading@10: yading@10: /* copy audio data to destination buffer: yading@10: * this is required since rawaudio expects non aligned data */ yading@10: av_samples_copy(audio_dst_data, frame->data, 0, 0, yading@10: frame->nb_samples, av_frame_get_channels(frame), frame->format); yading@10: yading@10: /* write to rawaudio file */ yading@10: fwrite(audio_dst_data[0], 1, audio_dst_bufsize, audio_dst_file); yading@10: av_freep(&audio_dst_data[0]); yading@10: } yading@10: } yading@10: yading@10: return ret; yading@10: } yading@10: yading@10: static int open_codec_context(int *stream_idx, yading@10: AVFormatContext *fmt_ctx, enum AVMediaType type) yading@10: { yading@10: int ret; yading@10: AVStream *st; yading@10: AVCodecContext *dec_ctx = NULL; yading@10: AVCodec *dec = NULL; yading@10: yading@10: ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0); yading@10: if (ret < 0) { yading@10: fprintf(stderr, "Could not find %s stream in input file '%s'\n", yading@10: av_get_media_type_string(type), src_filename); yading@10: return ret; yading@10: } else { yading@10: *stream_idx = ret; yading@10: st = fmt_ctx->streams[*stream_idx]; yading@10: yading@10: /* find decoder for the stream */ yading@10: dec_ctx = st->codec; yading@10: dec = avcodec_find_decoder(dec_ctx->codec_id); yading@10: if (!dec) { yading@10: fprintf(stderr, "Failed to find %s codec\n", yading@10: av_get_media_type_string(type)); yading@10: return ret; yading@10: } yading@10: yading@10: if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) { yading@10: fprintf(stderr, "Failed to open %s codec\n", yading@10: av_get_media_type_string(type)); yading@10: return ret; yading@10: } yading@10: } yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int get_format_from_sample_fmt(const char **fmt, yading@10: enum AVSampleFormat sample_fmt) yading@10: { yading@10: int i; yading@10: struct sample_fmt_entry { yading@10: enum AVSampleFormat sample_fmt; const char *fmt_be, *fmt_le; yading@10: } sample_fmt_entries[] = { yading@10: { AV_SAMPLE_FMT_U8, "u8", "u8" }, yading@10: { AV_SAMPLE_FMT_S16, "s16be", "s16le" }, yading@10: { AV_SAMPLE_FMT_S32, "s32be", "s32le" }, yading@10: { AV_SAMPLE_FMT_FLT, "f32be", "f32le" }, yading@10: { AV_SAMPLE_FMT_DBL, "f64be", "f64le" }, yading@10: }; yading@10: *fmt = NULL; yading@10: yading@10: for (i = 0; i < FF_ARRAY_ELEMS(sample_fmt_entries); i++) { yading@10: struct sample_fmt_entry *entry = &sample_fmt_entries[i]; yading@10: if (sample_fmt == entry->sample_fmt) { yading@10: *fmt = AV_NE(entry->fmt_be, entry->fmt_le); yading@10: return 0; yading@10: } yading@10: } yading@10: yading@10: fprintf(stderr, yading@10: "sample format %s is not supported as output format\n", yading@10: av_get_sample_fmt_name(sample_fmt)); yading@10: return -1; yading@10: } yading@10: yading@10: int main (int argc, char **argv) yading@10: { yading@10: int ret = 0, got_frame; yading@10: yading@10: if (argc != 4) { yading@10: fprintf(stderr, "usage: %s input_file video_output_file audio_output_file\n" yading@10: "API example program to show how to read frames from an input file.\n" yading@10: "This program reads frames from a file, decodes them, and writes decoded\n" yading@10: "video frames to a rawvideo file named video_output_file, and decoded\n" yading@10: "audio frames to a rawaudio file named audio_output_file.\n" yading@10: "\n", argv[0]); yading@10: exit(1); yading@10: } yading@10: src_filename = argv[1]; yading@10: video_dst_filename = argv[2]; yading@10: audio_dst_filename = argv[3]; yading@10: yading@10: /* register all formats and codecs */ yading@10: av_register_all(); yading@10: yading@10: /* open input file, and allocate format context */ yading@10: if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) { yading@10: fprintf(stderr, "Could not open source file %s\n", src_filename); yading@10: exit(1); yading@10: } yading@10: yading@10: /* retrieve stream information */ yading@10: if (avformat_find_stream_info(fmt_ctx, NULL) < 0) { yading@10: fprintf(stderr, "Could not find stream information\n"); yading@10: exit(1); yading@10: } yading@10: yading@10: if (open_codec_context(&video_stream_idx, fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) { yading@10: video_stream = fmt_ctx->streams[video_stream_idx]; yading@10: video_dec_ctx = video_stream->codec; yading@10: yading@10: video_dst_file = fopen(video_dst_filename, "wb"); yading@10: if (!video_dst_file) { yading@10: fprintf(stderr, "Could not open destination file %s\n", video_dst_filename); yading@10: ret = 1; yading@10: goto end; yading@10: } yading@10: yading@10: /* allocate image where the decoded image will be put */ yading@10: ret = av_image_alloc(video_dst_data, video_dst_linesize, yading@10: video_dec_ctx->width, video_dec_ctx->height, yading@10: video_dec_ctx->pix_fmt, 1); yading@10: if (ret < 0) { yading@10: fprintf(stderr, "Could not allocate raw video buffer\n"); yading@10: goto end; yading@10: } yading@10: video_dst_bufsize = ret; yading@10: } yading@10: yading@10: if (open_codec_context(&audio_stream_idx, fmt_ctx, AVMEDIA_TYPE_AUDIO) >= 0) { yading@10: int nb_planes; yading@10: yading@10: audio_stream = fmt_ctx->streams[audio_stream_idx]; yading@10: audio_dec_ctx = audio_stream->codec; yading@10: audio_dst_file = fopen(audio_dst_filename, "wb"); yading@10: if (!audio_dst_file) { yading@10: fprintf(stderr, "Could not open destination file %s\n", video_dst_filename); yading@10: ret = 1; yading@10: goto end; yading@10: } yading@10: yading@10: nb_planes = av_sample_fmt_is_planar(audio_dec_ctx->sample_fmt) ? yading@10: audio_dec_ctx->channels : 1; yading@10: audio_dst_data = av_mallocz(sizeof(uint8_t *) * nb_planes); yading@10: if (!audio_dst_data) { yading@10: fprintf(stderr, "Could not allocate audio data buffers\n"); yading@10: ret = AVERROR(ENOMEM); yading@10: goto end; yading@10: } yading@10: } yading@10: yading@10: /* dump input information to stderr */ yading@10: av_dump_format(fmt_ctx, 0, src_filename, 0); yading@10: yading@10: if (!audio_stream && !video_stream) { yading@10: fprintf(stderr, "Could not find audio or video stream in the input, aborting\n"); yading@10: ret = 1; yading@10: goto end; yading@10: } yading@10: yading@10: frame = avcodec_alloc_frame(); yading@10: if (!frame) { yading@10: fprintf(stderr, "Could not allocate frame\n"); yading@10: ret = AVERROR(ENOMEM); yading@10: goto end; yading@10: } yading@10: yading@10: /* initialize packet, set data to NULL, let the demuxer fill it */ yading@10: av_init_packet(&pkt); yading@10: pkt.data = NULL; yading@10: pkt.size = 0; yading@10: yading@10: if (video_stream) yading@10: printf("Demuxing video from file '%s' into '%s'\n", src_filename, video_dst_filename); yading@10: if (audio_stream) yading@10: printf("Demuxing audio from file '%s' into '%s'\n", src_filename, audio_dst_filename); yading@10: yading@10: /* read frames from the file */ yading@10: while (av_read_frame(fmt_ctx, &pkt) >= 0) { yading@10: decode_packet(&got_frame, 0); yading@10: av_free_packet(&pkt); yading@10: } yading@10: yading@10: /* flush cached frames */ yading@10: pkt.data = NULL; yading@10: pkt.size = 0; yading@10: do { yading@10: decode_packet(&got_frame, 1); yading@10: } while (got_frame); yading@10: yading@10: printf("Demuxing succeeded.\n"); yading@10: yading@10: if (video_stream) { yading@10: printf("Play the output video file with the command:\n" yading@10: "ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n", yading@10: av_get_pix_fmt_name(video_dec_ctx->pix_fmt), video_dec_ctx->width, video_dec_ctx->height, yading@10: video_dst_filename); yading@10: } yading@10: yading@10: if (audio_stream) { yading@10: const char *fmt; yading@10: yading@10: if ((ret = get_format_from_sample_fmt(&fmt, audio_dec_ctx->sample_fmt)) < 0) yading@10: goto end; yading@10: printf("Play the output audio file with the command:\n" yading@10: "ffplay -f %s -ac %d -ar %d %s\n", yading@10: fmt, audio_dec_ctx->channels, audio_dec_ctx->sample_rate, yading@10: audio_dst_filename); yading@10: } yading@10: yading@10: end: yading@10: if (video_dec_ctx) yading@10: avcodec_close(video_dec_ctx); yading@10: if (audio_dec_ctx) yading@10: avcodec_close(audio_dec_ctx); yading@10: avformat_close_input(&fmt_ctx); yading@10: if (video_dst_file) yading@10: fclose(video_dst_file); yading@10: if (audio_dst_file) yading@10: fclose(audio_dst_file); yading@10: av_free(frame); yading@10: av_free(video_dst_data[0]); yading@10: av_free(audio_dst_data); yading@10: yading@10: return ret < 0; yading@10: }