/*
* 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
* Demuxing and decoding example.
*
* Show how to use the libavformat and libavcodec API to demux and
* decode audio and video data.
* @example demuxing_decoding.c
*/
/* Enable or disable frame reference counting. You are not supposed to support
* both paths in your application but pick the one most appropriate to your
* needs. Look for the use of refcount in this example to see what are the
* differences of API usage between them. */
{
int ret = 0;
*got_frame = 0;
/* decode video frame */
if (ret < 0) {
fprintf(stderr,
"Error decoding video frame (%s)\n",
av_err2str(ret));
return ret;
}
if (*got_frame) {
/* To handle this change, one could call av_image_alloc again and
* decode the following frames into another rawvideo file. */
fprintf(stderr, "Error: Width, height and pixel format have to be "
"constant in a rawvideo file, but the width, height or "
"pixel format of the input video changed:\n"
"old: width = %d, height = %d, format = %s\n"
"new: width = %d, height = %d, format = %s\n",
return -1;
}
printf("video_frame%s n:%d coded_n:%d pts:%s\n",
cached ? "(cached)" : "",
/* copy decoded frame to destination buffer:
* this is required since rawvideo expects non aligned data */
/* write to rawvideo file */
}
/* decode audio frame */
if (ret < 0) {
fprintf(stderr,
"Error decoding audio frame (%s)\n",
av_err2str(ret));
return ret;
}
/* Some audio decoders decode only part of the packet, and have to be
* called again with the remainder of the packet data.
* Sample: fate-suite/lossless-audio/luckynight-partial.shn
* Also, some decoders might over-read the packet. */
if (*got_frame) {
printf("audio_frame%s n:%d nb_samples:%d pts:%s\n",
cached ? "(cached)" : "",
/* Write the raw audio data samples of the first plane. This works
* fine for packed formats (e.g. AV_SAMPLE_FMT_S16). However,
* most audio decoders output planar audio, which uses a separate
* plane of audio samples for each channel (e.g. AV_SAMPLE_FMT_S16P).
* In other words, this code will write only the first audio channel
* in these cases.
* You should use libswresample or libavfilter to convert the frame
* to packed data. */
}
}
/* If we use frame reference counting, we own the data and need
* to de-reference it when we don't use it anymore */
return decoded;
}
{
int ret, stream_index;
if (ret < 0) {
fprintf(stderr, "Could not find %s stream in input file '%s'\n",
return ret;
} else {
stream_index = ret;
st = fmt_ctx->
streams[stream_index];
/* find decoder for the stream */
if (!dec) {
fprintf(stderr, "Failed to find %s codec\n",
}
/* Init the decoders, with or without reference counting */
fprintf(stderr, "Failed to open %s codec\n",
return ret;
}
*stream_idx = stream_index;
}
return 0;
}
{
int i;
struct sample_fmt_entry {
} sample_fmt_entries[] = {
};
struct sample_fmt_entry *entry = &sample_fmt_entries[i];
if (sample_fmt == entry->sample_fmt) {
*fmt =
AV_NE(entry->fmt_be, entry->fmt_le);
return 0;
}
}
fprintf(stderr,
"sample format %s is not supported as output format\n",
return -1;
}
int main (
int argc,
char **argv)
{
int ret = 0, got_frame;
if (argc != 4 && argc != 5) {
fprintf(stderr, "usage: %s [-refcount] input_file video_output_file audio_output_file\n"
"API example program to show how to read frames from an input file.\n"
"This program reads frames from a file, decodes them, and writes decoded\n"
"video frames to a rawvideo file named video_output_file, and decoded\n"
"audio frames to a rawaudio file named audio_output_file.\n\n"
"If the -refcount option is specified, the program use the\n"
"reference counting frame system which allows keeping a copy of\n"
"the data for longer than one decode call.\n"
"\n", argv[0]);
exit(1);
}
if (argc == 5 && !strcmp(argv[1], "-refcount")) {
argv++;
}
/* register all formats and codecs */
/* open input file, and allocate format context */
fprintf(stderr,
"Could not open source file %s\n",
src_filename);
exit(1);
}
/* retrieve stream information */
fprintf(stderr, "Could not find stream information\n");
exit(1);
}
video_dec_ctx = video_stream->
codec;
ret = 1;
}
/* allocate image where the decoded image will be put */
if (ret < 0) {
fprintf(stderr, "Could not allocate raw video buffer\n");
}
}
ret = 1;
}
}
/* dump input information to stderr */
fprintf(stderr, "Could not find audio or video stream in the input, aborting\n");
ret = 1;
}
if (!frame) {
fprintf(stderr, "Could not allocate frame\n");
}
/* initialize packet, set data to NULL, let the demuxer fill it */
if (video_stream)
/* read frames from the file */
do {
if (ret < 0)
break;
}
/* flush cached frames */
do {
} while (got_frame);
printf("Demuxing succeeded.\n");
if (video_stream) {
printf("Play the output video file with the command:\n"
"ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
}
printf("Warning: the sample format the decoder produced is planar "
"(%s). This example will output the first channel only.\n",
packed ? packed : "?");
n_channels = 1;
}
printf("Play the output audio file with the command:\n"
"ffplay -f %s -ac %d -ar %d %s\n",
}
return ret < 0;
}