Decoding and resampling audio with FFmpeg for output with libao - c

I'm trying to write a program to read and play an audio file using FFmpeg and libao. I've been following the procedure outlined in the FFmpeg documentation for decoding audio using the new avcodec_send_packet and avcodec_receive_frame functions, but the examples I've been able to find are few and far between (the ones in the FFmpeg documentation either don't use libavformat or use the deprecated avcodec_decode_audio4). I've based a lot of my program off of the transcode_aac.c example (up to init_resampler) in the FFmpeg documentation, but that also uses the deprecated decoding function.
I believe I have the decoding part of the program working, but I need to resample the audio in order to convert it into an interleaved format to send to libao, for which I'm attempting to use libswresample. Whenever the program is run in its current state, it outputs (many times) "Error resampling: Output changed". The test file I've been using is just a YouTube rip that I had on hand. ffprobe reports the only stream as:
Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
This is my first program with FFmpeg (and I'm still relatively new to C), so any advice on how to improve/fix other parts of the program would be welcome.
#include<stdio.h>
#include<libavcodec/avcodec.h>
#include<libavformat/avformat.h>
#include<libavutil/avutil.h>
#include<libswresample/swresample.h>
#include<ao/ao.h>
#define OUTPUT_CHANNELS 2
#define OUTPUT_RATE 44100
#define BUFFER_SIZE 192000
#define OUTPUT_BITS 16
#define OUTPUT_FMT AV_SAMPLE_FMT_S16
static char *errtext (int err) {
static char errbuff[256];
av_strerror(err,errbuff,sizeof(errbuff));
return errbuff;
}
static int open_audio_file (const char *filename, AVFormatContext **context, AVCodecContext **codec_context) {
AVCodecContext *avctx;
AVCodec *codec;
int ret;
int stream_id;
int i;
// Open input file
if ((ret = avformat_open_input(context,filename,NULL,NULL)) < 0) {
fprintf(stderr,"Error opening input file '%s': %s\n",filename,errtext(ret));
*context = NULL;
return ret;
}
// Get stream info
if ((ret = avformat_find_stream_info(*context,NULL)) < 0) {
fprintf(stderr,"Unable to find stream info: %s\n",errtext(ret));
avformat_close_input(context);
return ret;
}
// Find the best stream
if ((stream_id = av_find_best_stream(*context,AVMEDIA_TYPE_AUDIO,-1,-1,&codec,0)) < 0) {
fprintf(stderr,"Unable to find valid audio stream: %s\n",errtext(stream_id));
avformat_close_input(context);
return stream_id;
}
// Allocate a decoding context
if (!(avctx = avcodec_alloc_context3(codec))) {
fprintf(stderr,"Unable to allocate decoder context\n");
avformat_close_input(context);
return AVERROR(ENOMEM);
}
// Initialize stream parameters
if ((ret = avcodec_parameters_to_context(avctx,(*context)->streams[stream_id]->codecpar)) < 0) {
fprintf(stderr,"Unable to get stream parameters: %s\n",errtext(ret));
avformat_close_input(context);
avcodec_free_context(&avctx);
return ret;
}
// Open the decoder
if ((ret = avcodec_open2(avctx,codec,NULL)) < 0) {
fprintf(stderr,"Could not open codec: %s\n",errtext(ret));
avformat_close_input(context);
avcodec_free_context(&avctx);
return ret;
}
*codec_context = avctx;
return 0;
}
static void init_packet (AVPacket *packet) {
av_init_packet(packet);
packet->data = NULL;
packet->size = 0;
}
static int init_resampler (AVCodecContext *codec_context, SwrContext **resample_context) {
int ret;
// Set resampler options
*resample_context = swr_alloc_set_opts(NULL,
av_get_default_channel_layout(OUTPUT_CHANNELS),
OUTPUT_FMT,
codec_context->sample_rate,
av_get_default_channel_layout(codec_context->channels),
codec_context->sample_fmt,
codec_context->sample_rate,
0,NULL);
if (!(*resample_context)) {
fprintf(stderr,"Unable to allocate resampler context\n");
return AVERROR(ENOMEM);
}
// Open the resampler
if ((ret = swr_init(*resample_context)) < 0) {
fprintf(stderr,"Unable to open resampler context: %s\n",errtext(ret));
swr_free(resample_context);
return ret;
}
return 0;
}
static int init_frame (AVFrame **frame) {
if (!(*frame = av_frame_alloc())) {
fprintf(stderr,"Could not allocate frame\n");
return AVERROR(ENOMEM);
}
return 0;
}
int main (int argc, char *argv[]) {
AVFormatContext *context = 0;
AVCodecContext *codec_context;
SwrContext *resample_context = NULL;
AVPacket packet;
AVFrame *frame = 0;
AVFrame *resampled = 0;
int16_t *buffer;
int ret, packet_ret, finished;
ao_device *device;
ao_sample_format format;
int default_driver;
if (argc != 2) {
fprintf(stderr,"Usage: %s <filename>\n",argv[0]);
return 1;
}
av_register_all();
printf("Opening file...\n");
if (open_audio_file(argv[1],&context,&codec_context) < 0)
return 1;
printf("Initializing resampler...\n");
if (init_resampler(codec_context,&resample_context) < 0) {
avformat_close_input(&context);
avcodec_free_context(&codec_context);
return 1;
}
// Setup libao
printf("Starting audio device...\n");
ao_initialize();
default_driver = ao_default_driver_id();
format.bits = OUTPUT_BITS;
format.channels = OUTPUT_CHANNELS;
format.rate = codec_context->sample_rate;
format.byte_format = AO_FMT_NATIVE;
format.matrix = 0;
if ((device = ao_open_live(default_driver,&format,NULL)) == NULL) {
fprintf(stderr,"Error opening audio device\n");
avformat_close_input(&context);
avcodec_free_context(&codec_context);
swr_free(&resample_context);
return 1;
}
// Mainloop
printf("Beginning mainloop...\n");
init_packet(&packet);
// Read packets until done
while (1) {
packet_ret = av_read_frame(context,&packet);
// Send a packet
if ((ret = avcodec_send_packet(codec_context,&packet)) < 0)
fprintf(stderr,"Error sending packet to decoder: %s\n",errtext(ret));
av_packet_unref(&packet);
while (1) {
if (!frame)
frame = av_frame_alloc();
ret = avcodec_receive_frame(codec_context,frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) // Need more input
break;
else if (ret < 0) {
fprintf(stderr,"Error receiving frame: %s\n",errtext(ret));
break;
}
// We have a valid frame, need to resample it
if (!resampled)
resampled = av_frame_alloc();
resampled->channel_layout = av_get_default_channel_layout(OUTPUT_CHANNELS);
resampled->sample_rate = codec_context->sample_rate;
resampled->format = OUTPUT_FMT;
if ((ret = swr_convert_frame(resample_context,resampled,frame)) < 0) {
fprintf(stderr,"Error resampling: %s\n",errtext(ret));
} else {
ao_play(device,(char*)resampled->extended_data[0],resampled->linesize[0]);
}
av_frame_unref(resampled);
av_frame_unref(frame);
}
if (packet_ret == AVERROR_EOF)
break;
}
printf("Closing file and freeing contexts...\n");
avformat_close_input(&context);
avcodec_free_context(&codec_context);
swr_free(&resample_context);
printf("Closing audio device...\n");
ao_close(device);
ao_shutdown();
return 0;
}
UPDATE: I've got it playing sound now, but it sounds like samples are missing (and MP3 files warn that "Could not update timestamps for skipped samples"). The issue was that the resampled frame needed to have certain attributes set before being passed to swr_convert_frame. I've also added av_packet_unref and av_frame_unref, but I'm still unsure as to where to best locate them.

ao_play(device,(char*)resampled->extended_data[0],resampled->linesize[0]);
You have problem in this line. Resampled audio frame has incorrect linesize parameters. swr_convert_frame aligns data and extended_data fields with silence. This silence is included into linesize parameter so you pass incorrect frame size into ao_play function.
ao_play(device, (char*)resampled->extended_data[0], av_sample_get_buffer_size(resampled->linesize, resampled->channels, resampled->nb_samples, resampled->format, 0));
Function av_sample_get_buffer_size() returns true sample size, without align. When I faced similar problem, this was the solution.

Related

FFMPEG: AV out of sync when writing a part of a video to a new file

I'm developing a data preprocessing program for a computer vision project using FFMPEG and Face detection API. In this program, I need to extract the shots that contain human faces from a given input video file and output them into a new file. But when I played the output video file generated by that program, the video and audio track was out of sync. I think a possible reason is that the timestamp of video frame or audio frame is set incorrectly, but I can't fix it by myself as I'm not very familiar with FFMPEG library, Please help me solving this out-of-sync issue.
To simplify the code shown below, I have removed all face detection code and use an empty function called faceDetect to represent it instead.
// ffmpegAPI.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
extern "C" {
#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/channel_layout.h>
#include <libavutil/common.h>
#include <libavutil/imgutils.h>
#include <libavutil/mathematics.h>
#include <libavutil/samplefmt.h>
#include <libavutil/pixdesc.h>
#include <libswscale/swscale.h>
}
bool faceDetect(AVFrame *frame)
{
/*...*/
return true;
}
int main(int argc, char **argv)
{
int64_t videoPts = 0, audioPts = 0;
int samples_count = 0;
AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
AVOutputFormat *ofmt = NULL;
AVPacket pkt;
AVFrame *frame = NULL;
int videoindex = -1; int audioindex = -1;
double videoTime = DBL_MAX;
const char *in_filename, *out_filename;
int ret, i;
in_filename = "C:\\input.flv";//Input file name
out_filename = "C:\\output.avi";//Output file name
av_register_all();
//Open input file
if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
fprintf(stderr, "Could not open input file '%s'", in_filename);
goto end;
}
//Find input streams
if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
fprintf(stderr, "Failed to retrieve input stream information");
goto end;
}
//Retrive AV stream information
for (i = 0; i < ifmt_ctx->nb_streams; i++)
{
AVStream *stream;
AVCodecContext *codec_ctx;
stream = ifmt_ctx->streams[i];//Get current stream
codec_ctx = stream->codec;//Get current stream codec
if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
{
videoindex = i;//video stream index
}
else if (codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO)
{
audioindex = i;//audio stream index
}
if (videoindex == -1)//no video stream is found
{
printf("can't find video stream\n");
goto end;
}
}
av_dump_format(ifmt_ctx, 0, in_filename, 0);
//Configure output
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, out_filename);
if (!ofmt_ctx) {
fprintf(stderr, "Could not create output context\n");
ret = AVERROR_UNKNOWN;
goto end;
}
ofmt = ofmt_ctx->oformat;
//Configure output streams
for (i = 0; i < ifmt_ctx->nb_streams; i++) {//Traversal input streams
AVStream *in_stream = ifmt_ctx->streams[i];//Get current stream
AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);//Create a corresponding output stream
if (!out_stream) {
fprintf(stderr, "Failed allocating output stream\n");
ret = AVERROR_UNKNOWN;
goto end;
}
//Copy codec from current input stream to corresponding output stream
ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
if (ret < 0) {
fprintf(stderr, "Failed to copy context from input to output stream codec context\n");
goto end;
}
if (i == videoindex)//Video stream
{
if (out_stream->codec->codec_id == AV_CODEC_ID_H264)
{
out_stream->codec->me_range = 16;
out_stream->codec->max_qdiff = 4;
out_stream->codec->qmin = 10;
out_stream->codec->qmax = 51;
out_stream->codec->qcompress = 1;
}
}
AVCodecContext *codec_ctx = out_stream->codec;
if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
|| codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
//Find codec encoder
AVCodec *encoder = avcodec_find_encoder(codec_ctx->codec_id);
if (!encoder) {
av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n");
ret = AVERROR_INVALIDDATA;
goto end;
}
//Open encoder
ret = avcodec_open2(codec_ctx, encoder, NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
goto end;
}
out_stream->codec->codec_tag = 0;
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
//Open the decoder for input stream
codec_ctx = in_stream->codec;
if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
|| codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
ret = avcodec_open2(codec_ctx,
avcodec_find_decoder(codec_ctx->codec_id), NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Failed to open decoder for stream #%u\n", i);
}
}
}
av_dump_format(ofmt_ctx, 0, out_filename, 1);
//Open output file for writing
if (!(ofmt->flags & AVFMT_NOFILE)) {
ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
if (ret < 0) {
fprintf(stderr, "Could not open output file '%s'", out_filename);
goto end;
}
}
//Write video header
ret = avformat_write_header(ofmt_ctx, NULL);
if (ret < 0) {
fprintf(stderr, "Error occurred when opening output file\n");
goto end;
}
//Write frames in a loop
while (1) {
AVStream *in_stream, *out_stream;
//Read one frame from the input file
ret = av_read_frame(ifmt_ctx, &pkt);
if (ret < 0)
break;
in_stream = ifmt_ctx->streams[pkt.stream_index];//Get current input stream
out_stream = ofmt_ctx->streams[pkt.stream_index];//Get current output stream
if (pkt.stream_index == videoindex)//video frame
{
int got_frame;
frame = av_frame_alloc();
if (!frame) {
ret = AVERROR(ENOMEM);
break;
}
//Readjust packet timestamp for decoding
av_packet_rescale_ts(&pkt,
in_stream->time_base,
in_stream->codec->time_base);
//Decode video frame
int len = avcodec_decode_video2(in_stream->codec, frame, &got_frame, &pkt);
if (len < 0)
{
av_frame_free(&frame);
av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
break;
}
if (got_frame)//Got a decoded video frame
{
int64_t pts = av_frame_get_best_effort_timestamp(frame);
//determine if the frame image contains human face
bool result = faceDetect(frame);
if (result) //face contained
{
videoTime = pts* av_q2d(out_stream->time_base);
frame->pts = videoPts++;//Set pts of video frame
AVPacket enc_pkt;
av_log(NULL, AV_LOG_INFO, "Encoding video frame\n");
//Create packet for encoding
enc_pkt.data = NULL;
enc_pkt.size = 0;
av_init_packet(&enc_pkt);
//Encoding frame
ret = avcodec_encode_video2(out_stream->codec, &enc_pkt,
frame, &got_frame);
av_frame_free(&frame);
if (!(got_frame))
ret = 0;
/* Configure encoding properties */
enc_pkt.stream_index = videoindex;
av_packet_rescale_ts(&enc_pkt,
out_stream->codec->time_base,
out_stream->time_base);
av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
/* Write encoded frame */
ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
if (ret < 0)
break;
}
else //no face contained
{
//Set the videoTime as maximum double value,
//making the corresponding audio frame not been processed
if (videoTime < DBL_MAX)
videoTime = DBL_MAX;
}
}
else
{
av_frame_free(&frame);
}
}
else//Audio frame
{
//Get current frame time
double audioTime = pkt.pts * av_q2d(in_stream->time_base);
if (audioTime >= videoTime)
{//The current frame should be written into output file
int got_frame;
frame = av_frame_alloc();
if (!frame) {
ret = AVERROR(ENOMEM);
break;
}
//Readjust packet timestamp for decoding
av_packet_rescale_ts(&pkt,
in_stream->time_base,
in_stream->codec->time_base);
//Decode audio frame
int len = avcodec_decode_audio4(in_stream->codec, frame, &got_frame, &pkt);
if (len < 0)
{
av_frame_free(&frame);
av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
break;
}
if (got_frame)//Got a decoded audio frame
{
//Set pts of audio frame
frame->pts = audioPts;
audioPts += frame->nb_samples;
AVPacket enc_pkt;
av_log(NULL, AV_LOG_INFO, "Encoding audio frame");
//Create packet for encoding
enc_pkt.data = NULL;
enc_pkt.size = 0;
av_init_packet(&enc_pkt);
//Encode audio frame
ret = avcodec_encode_audio2(out_stream->codec, &enc_pkt,
frame, &got_frame);
av_frame_free(&frame);
if (!(got_frame))
ret = 0;
/* Configure encoding properties */
enc_pkt.stream_index = audioindex;
av_packet_rescale_ts(&enc_pkt,
out_stream->codec->time_base,
out_stream->time_base);
av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
/* Write encoded frame */
ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
if (ret < 0)
break;
}
else //Shouldn't be written
{
av_frame_free(&frame);
}
}
}
av_packet_unref(&pkt);
}
//Write video trailer
av_write_trailer(ofmt_ctx);
end://Clean up
av_log(NULL, AV_LOG_INFO, "Clean up\n");
av_frame_free(&frame);
for (i = 0; i < ifmt_ctx->nb_streams; i++) {
avcodec_close(ifmt_ctx->streams[i]->codec);
if (ofmt_ctx && ofmt_ctx->nb_streams > i && ofmt_ctx->streams[i] && ofmt_ctx->streams[i]->codec)
avcodec_close(ofmt_ctx->streams[i]->codec);
}
avformat_close_input(&ifmt_ctx);
/* Close output file */
if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
avio_closep(&ofmt_ctx->pb);
avformat_free_context(ofmt_ctx);
if (ret < 0 && ret != AVERROR_EOF) {
char buf[256];
av_strerror(ret, buf, sizeof(buf));
av_log(NULL, AV_LOG_ERROR, "Error occurred:%s\n", buf);
system("Pause");
return 1;
}
//Program end
printf("The End.\n");
system("Pause");
return 0;
}
In the original code, I have set the pts this way.
frame->pts = videoPts++;//Set pts of video frame
This means that I assume the pts increment between each video frame is 1. But it's actually not the case when I inspected the video stream of the input file under Debug mode, finding that its pts increment is 2. After some observation, I found that the pkt_duration property of the decoded frame may stands for the pts increment in the input video stream because it has the same value with the pts increment. So I changed the above code to
frame->pts = videoPts += frame->pkt_duration;
When I tested the modified code, the video stream and audio stream was not out of sync anymore. So far I cannot ensure that the pkt_duration and pts increment is actually the same thing, but in my case it seems like that. Anyway, using pkt_duration has solved my out-of-sync problem.

Forcing global headers in LibAV

I'm trying to write a C program using LibAV that takes input video from a webcam and saves it as an H264 MP4 file. I'm modifying a working program that saves .ppm frames from the webcam. I'm unable to convert the AVPackets so that they may be written, though--specifically, avformat_write_header() is failing, with the messages
[mp4 # 0050c000] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 # 0050c000] Could not find tag for codec none in stream #0, codec not currently supported in container
The call is apparently returning error -22, but I can find no place where that error code is actually explained. How can I force avformat_write_header() to add in global headers when it's trying to write the MP4? Code below; some of it is adapted from this question, but I'm trying to adapt it from an input video file to a webcam.
int _tmain(int argc, _TCHAR* argv[])
{
AVInputFormat *inputFormat = NULL;
AVDictionary *inputDictionary= NULL;
AVFormatContext *inputFormatCtx = NULL;
AVFormatContext *outputFormatCtx = NULL;
AVCodecContext *inputCodecCtxOrig = NULL;
AVCodecContext *inputCodecCtx = NULL;
AVCodecContext *outputCodecCtx;
AVCodec *inputCodec = NULL;
AVCodec *outputCodec = NULL;
AVStream *stream = NULL;
AVIOContext *avioContext = NULL;
avcodec_register_all();
av_register_all();
avdevice_register_all();
av_dict_set(&inputDictionary, "Logitech HD Pro Webcam C920", "video", 0);
avformat_alloc_output_context2(&outputFormatCtx, NULL, NULL, "output.mp4");
avio_open(&avioContext, "output.mp4", AVIO_FLAG_WRITE);
outputFormatCtx->pb = avioContext;
stream = avformat_new_stream(outputFormatCtx, outputCodec);
inputFormat = av_find_input_format("dshow");
int r = avformat_open_input(&inputFormatCtx, "video=Logitech HD Pro Webcam C920", inputFormat, &inputDictionary);
if (r != 0) {
fprintf(stderr, "avformat_open_input() failed with error %d!\n", r);
return -1; }
r = avformat_find_stream_info(inputFormatCtx, NULL);
if (r != 0) {
fprintf(stderr, "avformat_find_stream_info() failed!\n");
return -1; }
av_dump_format(inputFormatCtx, 0, "video=Logitech HD Pro Webcam C920", 0);
unsigned int i;
int videoStream;
videoStream = -1;
for (i = 0; i < inputFormatCtx->nb_streams; i++) {
if (inputFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO {
videoStream = i;
break; }
}
if (videoStream == -1)
{ return -1; }
inputCodecCtxOrig = inputFormatCtx->streams[videoStream]->codec;
inputCodec = avcodec_find_decoder(inputCodecCtxOrig->codec_id);
if (inputCodec == NULL) {
fprintf(stderr, "avcodec_find_decoder() failed!\n");
return -1; }
else { printf("Supported codec!\n"); }
inputCodecCtx = avcodec_alloc_context3(inputCodec);
if (inputCodecCtx == NULL) {
fprintf(stderr, "avcodec_alloc_context3() failed!\n");
return -1; }
if (avcodec_copy_context(inputCodecCtx, inputCodecCtxOrig) != 0) {
fprintf(stderr, "avcodec_copy_context() failed!\n");
return -1; }
if (avcodec_open2(inputCodecCtx,inputCodec,&inputDictionary) < 0) {
fprintf(stderr, "avcodec_open2() failed!\n");
return -1; }
outputFormatCtx->oformat = av_guess_format(NULL, "output.mp4", NULL);
outputFormatCtx->oformat->flags |= AVFMT_GLOBALHEADER;
outputCodecCtx = avcodec_alloc_context3(outputCodec);
avcodec_copy_context(outputCodecCtx, inputCodecCtx);
outputCodec = inputCodec;
avcodec_open2(outputCodecCtx, outputCodec, NULL);
AVPacket packet;
printf("foo\n");
int errnum = avformat_write_header(outputFormatCtx, &inputDictionary);
printf("bar %d\n", errnum);
while(av_read_frame(inputFormatCtx, &packet)>=0) {
av_interleaved_write_frame(outputFormatCtx, &packet);
av_free_packet(&packet);
}
avcodec_close(inputCodecCtx);
avcodec_close(inputCodecCtxOrig);
avformat_close_input(&inputFormatCtx);
return 0;
}
How can I force avformat_write_header() to add in global headers
Global headers are written by the encoder, later the muxer reads them from the codec's extra_data field. So you should set this flag in the codec's context, before you call avcodec_open2().
outputCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
[mp4 # 0050c000] Could not find tag for codec none in stream #0, codec
not currently supported in container
You can try to setup the encoder explicitly (i.e. manually), or copy the codeccontext from original input codeccontext.
outputCodec = av_codec_find_encoder(AV_CODEC_ID_H264);
if(!outputCodec) return -1; //no encoder found
outputCodecCtx = avcodec_alloc_context3(outputCodec);
avcodec_copy_context(outputCodecCtx, inputCodecCtxOrig); //copy from orig
//You have to make sure each field is populated correctly
//with debugger or assertations
assert(outputCodecCtx->codec_id == AV_CODEC_ID_H264); //etc
outputCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
if(avcodec_open2(outputCodecCtx, outputCodec, NULL) <0) return -1;

C - Transcoding to UDP using FFmpeg?

I'm trying to use the FFmpeg libraries to take an existing video file and stream it over a UDP connection. Specifically, I've been looking at the muxing.c and demuxing.c example files in the source code doc/example directory of FFmpeg. The demuxing file presents code which allows an input video to be converted into the video and audio streams. The muxing file presents code which creates fake data and can already be output to a UDP connection as I would like. I've begun work combining the two. Below can be found my code which is basically a copy of the muxing file with some parts replaced/appended with parts of the demuxing file. Unfortunately I'm running into plenty of complications attempting my goal through this approach. Is there an existing source code example which does the transcoding I'm looking for? Or at least a tutorial on how one might create this? If not, at least a few pointers might be helpful in directing my work in combing the two files to achieve my goal. Specifically, I'm getting the error:
[NULL # 0x23b4040] Unable to find a suitable output format for 'udp://localhost:7777'
Could not deduce output format from file extension: using MPEG.
Output #0, mpeg, to 'udp://localhost:7777':
Even though the muxing file could accept UDP formats. Any suggestions? Thank you much!
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <libavutil/mathematics.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
/* 5 seconds stream duration */
#define STREAM_DURATION 200.0
#define STREAM_FRAME_RATE 25 /* 25 images/s */
#define STREAM_NB_FRAMES ((int)(STREAM_DURATION * STREAM_FRAME_RATE))
#define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
//FROM DE
static AVFormatContext *fmt_ctx = NULL;
static AVCodecContext *video_dec_ctx = NULL, *audio_dec_ctx;
static AVStream *video_stream = NULL, *audio_stream = NULL;
static const char *src_filename = NULL;
static const char *video_dst_filename = NULL;
static const char *audio_dst_filename = NULL;
static FILE *video_dst_file = NULL;
static FILE *audio_dst_file = NULL;
static uint8_t *video_dst_data[4] = {NULL};
static int video_dst_linesize[4];
static int video_dst_bufsize;
static uint8_t **audio_dst_data = NULL;
static int audio_dst_linesize;
static int audio_dst_bufsize;
static int video_stream_idx = -1, audio_stream_idx = -1;
static AVFrame *frame = NULL;
static AVPacket pkt;
static int video_frame_count = 0;
static int audio_frame_count = 0;
//END DE
static int sws_flags = SWS_BICUBIC;
/**************************************************************/
/* audio output */
static float t, tincr, tincr2;
static int16_t *samples;
static int audio_input_frame_size;
/* Add an output stream. */
static AVStream *add_stream(AVFormatContext *oc, AVCodec **codec,
enum AVCodecID codec_id)
{
AVCodecContext *c;
AVStream *st;
/* find the encoder */
*codec = avcodec_find_encoder(codec_id);
if (!(*codec)) {
fprintf(stderr, "Could not find encoder for '%s'\n",
avcodec_get_name(codec_id));
exit(1);
}
st = avformat_new_stream(oc, *codec);
if (!st) {
fprintf(stderr, "Could not allocate stream\n");
exit(1);
}
st->id = oc->nb_streams-1;
c = st->codec;
switch ((*codec)->type) {
case AVMEDIA_TYPE_AUDIO:
st->id = 1;
c->sample_fmt = AV_SAMPLE_FMT_S16;
c->bit_rate = 64000;
c->sample_rate = 44100;
c->channels = 2;
break;
case AVMEDIA_TYPE_VIDEO:
c->codec_id = codec_id;
c->bit_rate = 400000;
/* Resolution must be a multiple of two. */
c->width = 352;
c->height = 288;
/* timebase: This is the fundamental unit of time (in seconds) in terms
* of which frame timestamps are represented. For fixed-fps content,
* timebase should be 1/framerate and timestamp increments should be
* identical to 1. */
c->time_base.den = STREAM_FRAME_RATE;
c->time_base.num = 1;
c->gop_size = 12; /* emit one intra frame every twelve frames at most */
c->pix_fmt = STREAM_PIX_FMT;
if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
/* just for testing, we also add B frames */
c->max_b_frames = 2;
}
if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
/* Needed to avoid using macroblocks in which some coeffs overflow.
* This does not happen with normal video, it just happens here as
* the motion of the chroma plane does not match the luma plane. */
c->mb_decision = 2;
}
break;
default:
break;
}
/* Some formats want stream headers to be separate. */
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
return st;
}
/**************************************************************/
/* audio output */
static float t, tincr, tincr2;
static int16_t *samples;
static int audio_input_frame_size;
static void open_audio(AVFormatContext *oc, AVCodec *codec, AVStream *st)
{
AVCodecContext *c;
int ret;
c = st->codec;
/* open it */
ret = avcodec_open2(c, codec, NULL);
if (ret < 0) {
fprintf(stderr, "Could not open audio codec: %s\n", av_err2str(ret));
exit(1);
}
/* init signal generator */
t = 0;
tincr = 2 * M_PI * 110.0 / c->sample_rate;
/* increment frequency by 110 Hz per second */
tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
if (c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)
audio_input_frame_size = 10000;
else
audio_input_frame_size = c->frame_size;
samples = av_malloc(audio_input_frame_size *
av_get_bytes_per_sample(c->sample_fmt) *
c->channels);
if (!samples) {
fprintf(stderr, "Could not allocate audio samples buffer\n");
exit(1);
}
}
/* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
* 'nb_channels' channels. */
static void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
{
int j, i, v;
int16_t *q;
q = samples;
for (j = 0; j < frame_size; j++) {
v = (int)(sin(t) * 10000);
for (i = 0; i < nb_channels; i++)
*q++ = v;
t += tincr;
tincr += tincr2;
}
}
static void write_audio_frame(AVFormatContext *oc, AVStream *st)
{
AVCodecContext *c;
AVPacket pkt = { 0 }; // data and size must be 0;
AVFrame *frame = avcodec_alloc_frame();
int got_packet, ret;
av_init_packet(&pkt);
c = st->codec;
get_audio_frame(samples, audio_input_frame_size, c->channels);
frame->nb_samples = audio_input_frame_size;
avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
(uint8_t *)samples,
audio_input_frame_size *
av_get_bytes_per_sample(c->sample_fmt) *
c->channels, 1);
ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);
if (ret < 0) {
fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret));
exit(1);
}
if (!got_packet)
return;
pkt.stream_index = st->index;
/* Write the compressed frame to the media file. */
ret = av_interleaved_write_frame(oc, &pkt);
if (ret != 0) {
fprintf(stderr, "Error while writing audio frame: %s\n",
av_err2str(ret));
exit(1);
}
avcodec_free_frame(&frame);
}
static void close_audio(AVFormatContext *oc, AVStream *st)
{
avcodec_close(st->codec);
av_free(samples);
}
/**************************************************************/
/* video output */
static AVFrame *frame;
static AVPicture src_picture, dst_picture;
static int frame_count;
static void open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st)
{
int ret;
AVCodecContext *c = st->codec;
/* open the codec */
ret = avcodec_open2(c, codec, NULL);
if (ret < 0) {
fprintf(stderr, "Could not open video codec: %s\n", av_err2str(ret));
exit(1);
}
/* allocate and init a re-usable frame */
frame = avcodec_alloc_frame();
if (!frame) {
fprintf(stderr, "Could not allocate video frame\n");
exit(1);
}
/* Allocate the encoded raw picture. */
ret = avpicture_alloc(&dst_picture, c->pix_fmt, c->width, c->height);
if (ret < 0) {
fprintf(stderr, "Could not allocate picture: %s\n", av_err2str(ret));
exit(1);
}
/* If the output format is not YUV420P, then a temporary YUV420P
* picture is needed too. It is then converted to the required
* output format. */
if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
ret = avpicture_alloc(&src_picture, AV_PIX_FMT_YUV420P, c->width, c->height);
if (ret < 0) {
fprintf(stderr, "Could not allocate temporary picture: %s\n",
av_err2str(ret));
exit(1);
}
}
/* copy data and linesize picture pointers to frame */
*((AVPicture *)frame) = dst_picture;
}
/* Prepare a dummy image. */
static void fill_yuv_image(AVPicture *pict, int frame_index,
int width, int height)
{
int x, y, i;
i = frame_index;
/* Y */
for (y = 0; y < height; y++)
for (x = 0; x < width; x++)
pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3;
/* Cb and Cr */
for (y = 0; y < height / 2; y++) {
for (x = 0; x < width / 2; x++) {
pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2;
pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5;
}
}
}
static void write_video_frame(AVFormatContext *oc, AVStream *st)
{
int ret;
static struct SwsContext *sws_ctx;
AVCodecContext *c = st->codec;
if (frame_count >= STREAM_NB_FRAMES) {
/* No more frames to compress. The codec has a latency of a few
* frames if using B-frames, so we get the last frames by
* passing the same picture again. */
} else {
if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
/* as we only generate a YUV420P picture, we must convert it
* to the codec pixel format if needed */
if (!sws_ctx) {
sws_ctx = sws_getContext(c->width, c->height, AV_PIX_FMT_YUV420P,
c->width, c->height, c->pix_fmt,
sws_flags, NULL, NULL, NULL);
if (!sws_ctx) {
fprintf(stderr,
"Could not initialize the conversion context\n");
exit(1);
}
}
fill_yuv_image(&src_picture, frame_count, c->width, c->height);
sws_scale(sws_ctx,
(const uint8_t * const *)src_picture.data, src_picture.linesize,
0, c->height, dst_picture.data, dst_picture.linesize);
} else {
fill_yuv_image(&dst_picture, frame_count, c->width, c->height);
}
}
if (oc->oformat->flags & AVFMT_RAWPICTURE) {
/* Raw video case - directly store the picture in the packet */
AVPacket pkt;
av_init_packet(&pkt);
pkt.flags |= AV_PKT_FLAG_KEY;
pkt.stream_index = st->index;
pkt.data = dst_picture.data[0];
pkt.size = sizeof(AVPicture);
ret = av_interleaved_write_frame(oc, &pkt);
} else {
AVPacket pkt = { 0 };
int got_packet;
av_init_packet(&pkt);
/* encode the image */
ret = avcodec_encode_video2(c, &pkt, frame, &got_packet);
if (ret < 0) {
fprintf(stderr, "Error encoding video frame: %s\n", av_err2str(ret));
exit(1);
}
/* If size is zero, it means the image was buffered. */
if (!ret && got_packet && pkt.size) {
pkt.stream_index = st->index;
/* Write the compressed frame to the media file. */
ret = av_interleaved_write_frame(oc, &pkt);
} else {
ret = 0;
}
}
if (ret != 0) {
fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret));
exit(1);
}
frame_count++;
}
static void close_video(AVFormatContext *oc, AVStream *st)
{
avcodec_close(st->codec);
av_free(src_picture.data[0]);
av_free(dst_picture.data[0]);
av_free(frame);
}
static int open_codec_context(int *stream_idx,
AVFormatContext *fmt_ctx, enum AVMediaType type)
{
int ret;
AVStream *st;
AVCodecContext *dec_ctx = NULL;
AVCodec *dec = NULL;
ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
if (ret < 0) {
fprintf(stderr, "Could not find %s stream in input file '%s'\n",
av_get_media_type_string(type), src_filename);
return ret;
} else {
*stream_idx = ret;
st = fmt_ctx->streams[*stream_idx];
/* find decoder for the stream */
dec_ctx = st->codec;
dec = avcodec_find_decoder(dec_ctx->codec_id);
if (!dec) {
fprintf(stderr, "Failed to find %s codec\n",
av_get_media_type_string(type));
return ret;
}
if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
fprintf(stderr, "Failed to open %s codec\n",
av_get_media_type_string(type));
return ret;
}
}
return 0;
}
/**************************************************************/
/* media file output */
int main(int argc, char **argv)
{
const char *filename;
AVOutputFormat *fmt;
AVFormatContext *oc;
AVStream *audio_st, *video_st;
AVCodec *audio_codec, *video_codec;
double audio_pts, video_pts;
int ret = 0, got_frame;;
/* Initialize libavcodec, and register all codecs and formats. */
av_register_all();
if (argc != 3) {
printf("usage: %s input_file output_file\n"
"\n", argv[0]);
return 1;
}
src_filename = argv[1];
filename = argv[2];
/* allocate the output media context */
avformat_alloc_output_context2(&oc, NULL, NULL, filename);
if (!oc) {
printf("Could not deduce output format from file extension: using MPEG.\n");
avformat_alloc_output_context2(&oc, NULL, "mpeg", filename);
}
if (!oc) {
return 1;
}
fmt = oc->oformat;
/* Add the audio and video streams using the default format codecs
* and initialize the codecs. */
video_stream = NULL;
audio_stream = NULL;
//FROM DE
/* open input file, and allocate format context */
if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) {
fprintf(stderr, "Could not open source file %s\n", src_filename);
exit(1);
}
/* retrieve stream information */
if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
fprintf(stderr, "Could not find stream information\n");
exit(1);
}
if (open_codec_context(&video_stream_idx, fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) {
video_stream = fmt_ctx->streams[video_stream_idx];
video_dec_ctx = video_stream->codec;
/* allocate image where the decoded image will be put */
ret = av_image_alloc(video_dst_data, video_dst_linesize,
video_dec_ctx->width, video_dec_ctx->height,
video_dec_ctx->pix_fmt, 1);
if (ret < 0) {
fprintf(stderr, "Could not allocate raw video buffer\n");
goto end;
}
video_dst_bufsize = ret;
}
if (open_codec_context(&audio_stream_idx, fmt_ctx, AVMEDIA_TYPE_AUDIO) >= 0) {
int nb_planes;
audio_stream = fmt_ctx->streams[audio_stream_idx];
audio_dec_ctx = audio_stream->codec;
nb_planes = av_sample_fmt_is_planar(audio_dec_ctx->sample_fmt) ?
audio_dec_ctx->channels : 1;
audio_dst_data = av_mallocz(sizeof(uint8_t *) * nb_planes);
if (!audio_dst_data) {
fprintf(stderr, "Could not allocate audio data buffers\n");
ret = AVERROR(ENOMEM);
goto end;
}
}
//END DE
/* Now that all the parameters are set, we can open the audio and
* video codecs and allocate the necessary encode buffers. */
if (video_stream)
open_video(oc, video_codec, video_stream);
if (audio_stream)
open_audio(oc, audio_codec, audio_stream);
av_dump_format(oc, 0, filename, 1);
/* open the output file, if needed */
if (!(fmt->flags & AVFMT_NOFILE)) {
ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
if (ret < 0) {
fprintf(stderr, "Could not open '%s': %s\n", filename,
av_err2str(ret));
return 1;
}
}
/* Write the stream header, if any. */
ret = avformat_write_header(oc, NULL);
if (ret < 0) {
fprintf(stderr, "Error occurred when opening output file: %s\n",
av_err2str(ret));
return 1;
}
if (frame)
frame->pts = 0;
for (;;) {
/* Compute current audio and video time. */
if (audio_stream)
audio_pts = (double)audio_stream->pts.val * audio_stream->time_base.num / audio_stream->time_base.den;
else
audio_pts = 0.0;
if (video_stream)
video_pts = (double)video_stream->pts.val * video_stream->time_base.num /
video_stream->time_base.den;
else
video_pts = 0.0;
if ((!audio_stream || audio_pts >= STREAM_DURATION) &&
(!video_stream || video_pts >= STREAM_DURATION))
break;
/* write interleaved audio and video frames */
if (!video_stream || (video_stream && audio_st && audio_pts < video_pts)) {
write_audio_frame(oc, audio_stream);
} else {
write_video_frame(oc, video_stream);
frame->pts += av_rescale_q(1, video_stream->codec->time_base, video_stream->time_base);
}
}
/* Write the trailer, if any. The trailer must be written before you
* close the CodecContexts open when you wrote the header; otherwise
* av_write_trailer() may try to use memory that was freed on
* av_codec_close(). */
av_write_trailer(oc);
/* Close each codec. */
if (video_st)
close_video(oc, video_st);
if (audio_st)
close_audio(oc, audio_st);
if (!(fmt->flags & AVFMT_NOFILE))
/* Close the output file. */
avio_close(oc->pb);
/* free the stream */
avformat_free_context(oc);
end:
if (video_dec_ctx)
avcodec_close(video_dec_ctx);
if (audio_dec_ctx)
avcodec_close(audio_dec_ctx);
avformat_close_input(&fmt_ctx);
if (video_dst_file)
fclose(video_dst_file);
if (audio_dst_file)
fclose(audio_dst_file);
av_free(frame);
av_free(video_dst_data[0]);
av_free(audio_dst_data);
return 0;
}
The trouble you are facing is because udp is not a multimedia format. It is a network protocol. Ffmpeg converts to multimedia formats and can dump output to various locations such as disk (the most common case) or in this case on the network. So you must decide what format you want your content to be transmuxed to before you send it out over udp. From the command line this would be done using the -f format flag. Streamable formats are transport stream, webm in containers and elementary streams. You may want to use rtp for streaming over udp else on the reciever side you will face problems of missing packets and out of order arrival.
Given your current state, I suggest first you achieve what you want to do via the command line itself. This will give you an understanding of what are the various things you must do. Then you can go to the program.

ffmpeg transcode

I want to do a audio trancode using ffmpeg library. Now i have out file but I can listen only noise .
The steps of my program are:
1) Open input file and decode in raw format using avcodec_decode_audio4
2) encode and save the raw format .
I don't Know where I wrong. This is my code.
/*
* File: newmain.c
* Author: antonello
*
* Created on 23 gennaio 2013, 11.24
*/
#include <stdio.h>
#include <stdlib.h>
#include <libavutil/samplefmt.h>
#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>
#include <libavcodec/old_codec_ids.h>
static AVCodecContext *get_encoder(int sampleRate, int channels, int audioBitrate)
{
AVCodecContext *audioCodec;
AVCodec *codec;
//Set up audio encoder
codec = avcodec_find_encoder(CODEC_ID_AAC);
if (codec == NULL)
{
printf("avcodec_find_encoder: ERROR\n");
return NULL;
}
audioCodec = avcodec_alloc_context();
audioCodec->bit_rate = audioBitrate;
audioCodec->sample_fmt = AV_SAMPLE_FMT_S16P;
audioCodec->sample_rate = sampleRate;
audioCodec->channels = channels;
audioCodec->profile = FF_PROFILE_AAC_MAIN;
audioCodec->channel_layout=AV_CH_LAYOUT_MONO;
//audioCodec->time_base = (AVRational){1, sampleRate};
audioCodec->time_base.num = 1;
audioCodec->time_base.den = sampleRate;
audioCodec->codec_type = AVMEDIA_TYPE_AUDIO;
if (avcodec_open(audioCodec, codec) < 0)
{
printf("avcodec_open: ERROR\n");
return NULL;
}
return audioCodec;
}
int main(int argc, char** argv) {
AVFormatContext *aFormatCtx_decoder = NULL;
AVFormatContext *aFormatCtx_encoder = NULL;
int i, audioStream;
AVPacket packet_decoder;
AVPacket packet_encoder;
int got_frame=0;
int complete_decode=0;
int len;
AVFrame *decoded_frame = NULL;
AVCodecContext *aCodec_decoderCtx = NULL;
AVCodec *aCodec_decoder = NULL;
FILE *outfile;
//reding input file
avcodec_register_all();
//register all codecs
av_register_all();
//open file
if(avformat_open_input(&aFormatCtx_decoder, "sample.aac", NULL, NULL)!=0){
fprintf(stderr, "Could not open source file \n");
return -1; // Couldn't open file
}
// Retrieve stream information
if(avformat_find_stream_info(aFormatCtx_decoder, NULL)<0){
fprintf(stderr, "Couldn't find stream information \n");
return -1; // Couldn't find stream information
}
// Dump information about file onto standard error
//av_dump_format(aFormatCtx_decode, 0, argv[1], 0);
// Find the first audio stream
audioStream=-1;
for(i=0; i<aFormatCtx_decoder->nb_streams; i++) {
if(aFormatCtx_decoder->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO &&
audioStream < 0) {
audioStream=i;
}
}
if(audioStream==-1){
fprintf(stderr, "File haven't sudio stream \n");
return -1;
}
//get audio codec contex
aCodec_decoderCtx=aFormatCtx_decoder->streams[audioStream]->codec;
//get audio codec
aCodec_decoder = avcodec_find_decoder(aCodec_decoderCtx->codec_id);
aCodec_decoder->sample_fmts=AV_SAMPLE_FMT_S16P;
if(!aCodec_decoder) {
fprintf(stderr, "Unsupported codec!\n");
return -1;//Unsupported codec!
}
//open codec
// Open codec
if(avcodec_open2(aCodec_decoderCtx, aCodec_decoder, NULL)<0)
return -1; // Could not open codec
// allocate audio frame
decoded_frame = avcodec_alloc_frame();
if (!decoded_frame) {
fprintf(stderr, "Could not allocate audio frame\n");
return -1;//Could not allocate audio frame
}
aCodec_decoderCtx->bit_rate=12000;
aFormatCtx_encoder=get_encoder(8000,1,12000);
av_init_packet(&packet_encoder);
printf("param %d %d %d",aCodec_decoderCtx->sample_fmt,aCodec_decoderCtx->channels,aCodec_decoderCtx->bit_rate);
outfile = fopen("out.aac", "wb");
if (!outfile) {
printf(stderr, "Could not open outfile \n");
return -1;//Could not open outfile
}
while(av_read_frame(aFormatCtx_decoder, &packet_decoder)>=0) {
// decode frame
len = avcodec_decode_audio4(aCodec_decoderCtx, decoded_frame, &got_frame, &packet_decoder);
if (len < 0) {
fprintf(stderr, "Error while decoding\n");
return -1;
}
if (got_frame){
avcodec_encode_audio2(aFormatCtx_encoder,&packet_encoder,decoded_frame,&complete_decode);
if(complete_decode){
// printf("complete decode frame");
fwrite(packet_encoder.data, 1, packet_encoder.size, outfile);
av_free_packet(&packet_encoder);
}
}
}
fclose(outfile);
return (EXIT_SUCCESS);
}
use the following code for sample format conversion.
you can get example in ffmpeg/doc/examples/resampling_audio.c
SwrContext *swr = swr_alloc();
av_opt_set_int(node_handle->swr, "in_channel_layout", decoded_frame->channel_layout, 0);
av_opt_set_int(node_handle->swr, "out_channel_layout", encoder_ctx->channel_layout, 0);
av_opt_set_int(node_handle->swr, "in_sample_rate", decoded_frame->sample_rate, 0);
av_opt_set_int(node_handle->swr, "out_sample_rate", encoder_ctx->sample_rate, 0);
av_opt_set_sample_fmt(swr, "in_sample_fmt", decoded_frame->format, 0);
av_opt_set_sample_fmt(swr, "out_sample_fmt", encoder_ctx->sample_fmt, 0);
swr_init(swr);
uint8_t* swr_out_data;
int linesize;
av_samples_alloc(&swr_out_data,
linesize,
encoder_ctx->nb_channels,
decoded_frame->nb_samples,
encoder_ctx->sample_fmt,
0
);
swr_convert(swr,&swr_out_data, decoded_frame->nb_samples, decoded_frame->data, decoded_frame->nb_samples);
You can not set arbitrary value of of the variable sample_fmts:
aCodec_decoder->sample_fmts=AV_SAMPLE_FMT_S16P; // It's wrong
Decoding will always be performed ​​with the parameters set by the codec.
You have to create SwrContext and perform format conversion to the target (SwrContext converts sample format, sample rate and channels layout)
modify to this, works fine
aCodec_decoder->sample_fmts=audioCodec->sample_fmt;

FFmpeg: avcodec_encode_video() and JPEG images

I'm trying to encode a series of .jpg files into a video using the ffmpeg library, and I can't seem to get the frames to encode. (I have to use the ffmpeg library, and using ffmpeg from a command line is not an option in my case.)
Except for the part where I'm trying to open JPG files as AVFrames, my code is more or less the same thing as found in api-example.c from the ffmpeg library. When I populate the frames as the example does, everything works as expected. In the code below, I fail to encode any frames. Obviously the trouble is related to how I'm opening the JPG files, but I can't figure out what.
I'm opening the image like this:
AVFrame* open_image(const char* imageFileName, int width, int height, long * bufSize)
{
AVFormatContext *pFormatCtx;
if(av_open_input_file(&pFormatCtx, imageFileName, NULL, 0, NULL)!=0)
{
printf("Can't open image file '%s'\n", imageFileName);
return NULL;
}
AVCodecContext *pCodecCtx;
pCodecCtx = pFormatCtx->streams[0]->codec;
pCodecCtx->width = width;
pCodecCtx->height = height;
pCodecCtx->pix_fmt = PIX_FMT_YUV420P;
// Find the decoder for the video stream
AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if (!pCodec)
{
printf("Codec not found\n");
return NULL;
}
// Open codec
if(avcodec_open(pCodecCtx, pCodec)<0)
{
printf("Could not open codec\n");
return NULL;
}
AVFrame *pFrame = avcodec_alloc_frame();
if (!pFrame)
{
LOGV(TAG, "Can't allocate memory for AVFrame\n");
return NULL;
}
int frameFinished;
int numBytes;
// Determine required buffer size and allocate buffer
numBytes = avpicture_get_size(PIX_FMT_YUVJ420P, pCodecCtx->width, pCodecCtx->height);
// ***
*bufSize = numBytes;
// ***
uint8_t *buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
avpicture_fill((AVPicture *) pFrame, buffer, PIX_FMT_YUVJ420P, pCodecCtx->width, pCodecCtx->height);
// Read frame
AVPacket packet;
int framesNumber = 0;
while (av_read_frame(pFormatCtx, &packet) >= 0)
{
if(packet.stream_index != 0)
continue;
int ret = avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
if (ret > 0)
{
sprintf(buf, "Frame is decoded, size %d", ret);
LOGV(TAG, buf);
pFrame->quality = 4;
return pFrame;
}
else {
// printf("Error [%d] while decoding frame: %s\n", ret, strerror(AVERROR(ret)));
sprintf(buf, "Error %d decoding frame: %s", ret, strerror(AVERROR(ret)));
LOGV(TAG, buf);
}
}
}
...and attempting to encode them like this:
DIR * dir = opendir(path);
int i = 0;
if (dir != NULL) {
for(struct dirent *ent = readdir(dir); ent != NULL; ent = readdir(dir)) {
fflush(stdout);
printf("%s/%s", path, ent->d_name);
LOGV(TAG, filename);
// If not a jpg file, pass it over
const char * ext = strrchr(filename, '.');
if((!ext) || (strcmp(ext, ".jpg"))) {
continue;
}
/*** NOTE: Is this where I've gone wrong? Bufsize is set in open_image based on av_picture_size() */
long bufSize = 0L;
AVFrame * frame = open_image(filename, width, height, &bufSize);
if(frame) {
// This is what it needs to do, and it does not work.
// Causes:
// Wrong format?
// Wrong buffer size?
uint8_t * picBuf = (uint8_t *)malloc(bufSize);
out_size = avcodec_encode_video(c, picBuf, bufSize, frame);
printf("encoding frame %3d (size=%5d)\n", i++, out_size);
/** On the first image, out_size is 0. On the next, it's -1, and fails. */
if(out_size < 0) {
printf("Error encoding frame");
return -6;
}
fwrite(picBuf, 1, bufSize, f);
free(picBuf);
av_free(frame);
}
else {
printf("Couldn't open image");
return -5;
}
}
closedir(dir);
}
else {
printf("Couldn't open directory %s\n", path);
return -4;
}
Could someone point me in the right direction?
What exactly is the error you are getting? Examine the Encoding context after opening it to see it's supported pixel formats, you might have to use sws_scale to convert to a format the encoder supports.

Resources