/*
* 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
*/
/* The different ways of decoding and managing data memory. You are not
* supposed to support all the modes in your application but pick the one most
* appropriate to your needs. Look for the use of api_mode in this example to
* see what are the differences of API usage between them */
enum {
};
{
*got_frame = 0;
/* decode video frame */
if (ret < 0) {
fprintf(stderr,
"Error decoding video frame (%s)\n",
av_err2str(ret));
}
if (*got_frame) {
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));
}
/* 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 the new API with reference counting, we own the data and need
* to de-reference it when we don't use it anymore */
return decoded;
}
{
if (ret < 0) {
fprintf(stderr, "Could not find %s stream in input file '%s'\n",
} else {
st = fmt_ctx->
streams[*stream_idx];
/* 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 0;
}
{
int i;
struct sample_fmt_entry {
} sample_fmt_entries[] = {
};
*fmt = NULL;
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=<old|new_norefcount|new_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. If unset, it's using\n"
"the classic old method.\n"
"\n", argv[0]);
exit(1);
}
if (argc == 5) {
const char *
mode = argv[1] + strlen(
"-refcount=");
else {
fprintf(stderr, "unknow mode '%s'\n", mode);
exit(1);
}
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;
}
/* When using the new API, you need to use the libavutil/frame.h API, while
* the classic frame management is available in libavcodec */
frame = avcodec_alloc_frame();
else
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",
}
avcodec_free_frame(&frame);
else
return ret < 0;
}