[FFmpeg-devel] [PATCH] examples: add demuxing example
Stefano Sabatini
stefasab at gmail.com
Wed Aug 29 23:44:19 CEST 2012
---
doc/examples/Makefile | 3 +-
doc/examples/demuxing.c | 199 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 201 insertions(+), 1 deletions(-)
create mode 100644 doc/examples/demuxing.c
diff --git a/doc/examples/Makefile b/doc/examples/Makefile
index 287dfc4..dab12c1 100644
--- a/doc/examples/Makefile
+++ b/doc/examples/Makefile
@@ -12,6 +12,7 @@ CFLAGS := $(shell pkg-config --cflags $(FFMPEG_LIBS)) $(CFLAGS)
LDLIBS := $(shell pkg-config --libs $(FFMPEG_LIBS)) $(LDLIBS)
EXAMPLES= decoding_encoding \
+ demuxing \
filtering_video \
filtering_audio \
metadata \
@@ -29,7 +30,7 @@ muxing: LDLIBS += -lm
all: $(OBJS) $(EXAMPLES)
clean-test:
- $(RM) test*.pgm test.h264 test.mp2 test.sw test.mpg outscale*.pgm
+ $(RM) test*.pgm test.h264 test.mp2 test.sw test.mpg outscale*.pgm outdemux.raw
clean: clean-test
$(RM) $(EXAMPLES) $(OBJS)
diff --git a/doc/examples/demuxing.c b/doc/examples/demuxing.c
new file mode 100644
index 0000000..621181f
--- /dev/null
+++ b/doc/examples/demuxing.c
@@ -0,0 +1,199 @@
+/*
+ * Copyright (c) 2012 Stefano Sabatini
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+/**
+ * @file
+ * libavformat demuxing API use example.
+ *
+ * Show how to use the libavformat and libavcodec API for demuxing and
+ * decoding video data.
+ */
+
+#include <libavutil/imgutils.h>
+#include <libavutil/timestamp.h>
+#include <libavformat/avformat.h>
+
+
+int main (int argc, char **argv)
+{
+ AVFormatContext *fmtctx = NULL;
+ AVCodecContext *decctx = NULL;
+ AVCodec *dec = NULL;
+ AVStream *stream = NULL;
+ const char *src_filename = NULL, *dst_filename = "outdemux.raw";
+ FILE *dst_file = NULL;
+ uint8_t *dst_data[4] = {NULL};
+ int dst_linesize[4];
+ int dst_bufsize;
+ AVPacket pkt;
+ int stream_idx;
+ AVFrame *frame = NULL;
+ int got_frame, ret, frame_count = 0;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s inputfile\n"
+ "API example program to show how to read frames from an input file.\n"
+ "This program reads frames from a file, decode them, and write them "
+ "to a rawvideo file named like outdemux.raw."
+ "\n", argv[0]);
+ exit(1);
+ }
+ src_filename = argv[1];
+
+ /* register all formats and codecs */
+ av_register_all();
+
+ /* open input file, and allocated format context */
+ if (avformat_open_input(&fmtctx, src_filename, NULL, NULL) < 0) {
+ fprintf(stderr, "Cannot open source file %s\n", src_filename);
+ exit(1);
+ }
+
+ /* retrieve stream information */
+ if (avformat_find_stream_info(fmtctx, NULL) < 0) {
+ fprintf(stderr, "Cannot find stream information\n");
+ exit(1);
+ }
+
+ ret = av_find_best_stream(fmtctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
+ if (ret < 0) {
+ fprintf(stderr, "Cannot find video stream in file\n");
+ goto end;
+ }
+ stream_idx = ret;
+ stream = fmtctx->streams[stream_idx];
+
+ /* find decoder for the stream */
+ decctx = stream->codec;
+ dec = avcodec_find_decoder(decctx->codec_id);
+ if (!dec) {
+ fprintf(stderr, "Failed to find any codec\n");
+ ret = 1;
+ goto end;
+ }
+
+ if ((ret = avcodec_open2(decctx, dec, NULL)) < 0) {
+ fprintf(stderr, "Failed to open codec\n");
+ goto end;
+ }
+
+ /* dump input information to stderr */
+ av_dump_format(fmtctx, 0, src_filename, 0);
+
+ dst_file = fopen(dst_filename, "wb");
+ if (!dst_file) {
+ fprintf(stderr, "Could not open destination file %s\n", dst_filename);
+ ret = 1;
+ goto end;
+ }
+
+ frame = avcodec_alloc_frame();
+ if (!frame) {
+ fprintf(stderr, "Could not allocate video frame\n");
+ ret = 1;
+ goto end;
+ }
+
+ /* allocate image where the decoded image will be put */
+ ret = av_image_alloc(dst_data, dst_linesize,
+ decctx->width, decctx->height, decctx->pix_fmt, 1);
+ if (ret < 0) {
+ fprintf(stderr, "Could not alloc raw video buffer\n");
+ goto end;
+ }
+ dst_bufsize = ret;
+
+ /* initialize packet, set data to NULL, let the demuxer fill it */
+ av_init_packet(&pkt);
+ pkt.size = 0;
+ pkt.data = NULL;
+
+ printf("Demuxing file '%s' to '%s'\n", src_filename, dst_filename);
+
+ /* read frames from the file */
+ while (av_read_frame(fmtctx, &pkt) >= 0) {
+ if (pkt.stream_index != stream_idx)
+ continue;
+
+ /* decode video frame */
+ ret = avcodec_decode_video2(decctx, frame, &got_frame, &pkt);
+ if (ret < 0) {
+ fprintf(stderr, "Error decoding video frame\n");
+ goto end;
+ }
+
+ if (got_frame) {
+ printf("frame n:%d coded_n:%d pts:%s\n",
+ frame_count++, frame->coded_picture_number,
+ av_ts2timestr(frame->pts, &decctx->time_base));
+ /* copy decoded frame to destination buffer:
+ * this is required since rawvideo expect non aligned data */
+ av_image_copy(dst_data, dst_linesize,
+ (const uint8_t **)(frame->data), frame->linesize,
+ decctx->pix_fmt, decctx->width, decctx->height);
+ /* write to rawvideo file */
+ fwrite(dst_data[0], 1, dst_bufsize, dst_file);
+ }
+ }
+
+ /* flush cached frames */
+ pkt.data = NULL;
+ pkt.size = 0;
+ do {
+ if (pkt.stream_index != stream_idx)
+ continue;
+
+ ret = avcodec_decode_video2(decctx, frame, &got_frame, &pkt);
+ if (ret < 0) {
+ fprintf(stderr, "Error decoding video frame\n");
+ goto end;
+ }
+
+ if (got_frame) {
+ printf("frame(cached) n:%d coded_n:%d pts:%s\n",
+ frame_count++, frame->coded_picture_number,
+ av_ts2timestr(frame->pts, &decctx->time_base));
+ /* copy decoded frame to destination buffer:
+ * this is required since rawvideo expect non aligned data */
+ av_image_copy(dst_data, dst_linesize,
+ (const uint8_t **)(frame->data), frame->linesize,
+ decctx->pix_fmt, decctx->width, decctx->height);
+ /* write to rawvideo file */
+ fwrite(dst_data[0], 1, dst_bufsize, dst_file);
+ }
+ } while (got_frame);
+
+ printf("Demuxing succeeded. Play the output file with the command:\n"
+ "ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
+ av_get_pix_fmt_name(decctx->pix_fmt), decctx->width, decctx->height,
+ dst_filename);
+
+end:
+ avcodec_close(decctx);
+ avformat_close_input(&fmtctx);
+ if (dst_file)
+ fclose(dst_file);
+ av_free(frame);
+ av_free(dst_data[0]);
+
+ return ret < 0;
+}
--
1.7.5.4
More information about the ffmpeg-devel
mailing list