Decode data from an MP2 input file and generate a raw audio file to be played with ffplay.
/*
* Copyright (c) 2001 Fabrice Bellard
*
* 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 libavcodec audio decoding API usage example
* @example decode_audio.c
*
* Decode data from an MP2 input file and generate a raw audio file to
* be played with ffplay.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096
{
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;
}
{
/* send the packet with the compressed data to the decoder */
fprintf(stderr, "Error submitting the packet to the decoder\n");
exit(1);
}
/* read all the output frames (in general there may be any number of them */
return;
fprintf(stderr, "Error during decoding\n");
exit(1);
}
if (data_size < 0) {
/* This should not occur, checking just for paranoia */
fprintf(stderr, "Failed to calculate data size\n");
exit(1);
}
}
}
int main(
int argc,
char **argv)
{
const char *outfilename, *filename;
size_t data_size;
int n_channels = 0;
const char *fmt;
if (argc <= 2) {
fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
exit(0);
}
filename = argv[1];
outfilename = argv[2];
/* find the MPEG audio decoder */
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
if (!parser) {
fprintf(stderr, "Parser not found\n");
exit(1);
}
fprintf(stderr, "Could not allocate audio codec context\n");
exit(1);
}
/* open it */
fprintf(stderr, "Could not open codec\n");
exit(1);
}
f = fopen(filename,
"rb");
fprintf(stderr, "Could not open %s\n", filename);
exit(1);
}
outfile = fopen(outfilename,
"wb");
exit(1);
}
/* decode until eof */
while (data_size > 0) {
if (!decoded_frame) {
fprintf(stderr, "Could not allocate audio frame\n");
exit(1);
}
}
fprintf(stderr, "Error while parsing\n");
exit(1);
}
memmove(inbuf,
data, data_size);
}
}
/* flush the decoder */
/* print output pcm infomations, because there have no metadata of pcm */
printf(
"Warning: the sample format the decoder produced is planar "
"(%s). This example will output the first channel only.\n",
packed ? packed : "?");
}
n_channels =
c->ch_layout.nb_channels;
goto end;
printf(
"Play the output audio file with the command:\n"
"ffplay -f %s -ac %d -ar %d %s\n",
fmt, n_channels,
c->sample_rate,
outfilename);
end:
return 0;
}