I use FFmpeg to decode my flac file and write it to pcm file, then use GoldenWave to play it with pcm signed 16bit, little endian, mono and the total play time is ok.
I doubt i write the 2 channel file in one place, but i don't know how to get every signal channel and write it to pcm file.
any help? thank you.
while (av_read_frame(fmt_ctx, &pkt) >= 0) {
AVPacket orig_pkt = pkt;
do {
ret = decode_packet(&got_frame, 0);
if (ret < 0)
break;
pkt.data += ret;
pkt.size -= ret;
} while (pkt.size > 0);
av_free_packet(&orig_pkt);
}
pkt.data = NULL;
pkt.size = 0;
do {
decode_packet(&got_frame, 1);
LOG("flush cached frames");
} while (got_frame);
static int decode_packet(int *got_frame, int cached)
{
int ret = 0;
int decoded = pkt.size;
*got_frame = 0;
if (pkt.stream_index == audio_stream_idx) {
ret = avcodec_decode_audio4(audio_dec_ctx, frame, got_frame, &pkt);
if (ret < 0) {
LOG("Error decoding audio frame (%s)\n", av_err2str(ret));
return ret;
}
decoded = FFMIN(ret, pkt.size);
if (*got_frame) {
size_t unpadded_linesize = frame->nb_samples * av_get_bytes_per_sample(audio_dec_ctx->sample_fmt);
//decode packet nb_samples:4608, xx:2, unpadded_linesize: 9216
LOG("decode packet nb_samples:%d, xx:%d, unpadded_linesize: %d",
frame->nb_samples, av_get_bytes_per_sample(audio_dec_ctx->sample_fmt), unpadded_linesize);
fwrite(frame->extended_data[0], 1, unpadded_linesize, audio_dst_file);
//int nb_sample = frame->nb_samples;
//fwrite(frame->extended_data[0], 1, nb_sample, audio_dst_file);
//fwrite(frame->extended_data[0] + nb_sample, 1, nb_sample, audio_dst_file);
}
}
if (*got_frame && api_mode == API_MODE_NEW_API_REF_COUNT)
av_frame_unref(frame);
return decoded;
}
You didn't describe the problem you're having, but from what you're writing, I see two problems:
you're not checking the raw audio format of the frame, see frame->format (or audio_dec_ctx->sample_fmt). You're writing it as if it were AV_SAMPLE_FMT_S16, but you're not checking that it is
your unpadded_linesize is not multiplied by the number of channels (see e.g. frame->channels)
Related
I've been trying to seek in a mpegts video file. After exploring many codes, I've come up to the following stage of fetching frames and saving them.
However after using av_seek_frame also I'm getting following results:
Initial 7-8 frames are saved as grey frames.
Thereafter frames are fetched from beginning of the video only. i.e. 0 seconds onwards, while I'm trying to seek from say 12th seconds onwards.
Can you please explain how should I be calculating the timestamp and do the things correctly?
int main(int argc, const char *argv[])
{
int stream_id;
int64_t timestamp;
char ts_buf[60];
if (argc < 2) {
printf("You need to specify a media file.\n");
return -1;
}
logging("initializing all the containers, codecs and protocols.");
AVFormatContext *pFormatContext = avformat_alloc_context();
if (!pFormatContext) {
logging("ERROR could not allocate memory for Format Context");
return -1;
}
logging("opening the input file (%s) and loading format (container) header", argv[1]);
if (avformat_open_input(&pFormatContext, argv[1], NULL, NULL) != 0) {
logging("ERROR could not open the file");
return -1;
}
logging("format %s, duration %lld us, bit_rate %lld", pFormatContext->iformat->name, pFormatContext->duration, pFormatContext->bit_rate);
logging("finding stream info from format");
if (avformat_find_stream_info(pFormatContext, NULL) < 0) {
logging("ERROR could not get the stream info");
return -1;
}
AVCodec *pCodec = NULL;
AVCodecParameters *pCodecParameters = NULL;
int video_stream_index = -1;
// loop though all the streams and print its main information
for (int i = 0; i < pFormatContext->nb_streams; i++)
{
AVCodecParameters *pLocalCodecParameters = NULL;
pLocalCodecParameters = pFormatContext->streams[i]->codecpar;
logging("AVStream->time_base before open coded %d/%d", pFormatContext->streams[i]->time_base.num, pFormatContext->streams[i]->time_base.den);
logging("AVStream->r_frame_rate before open coded %d/%d", pFormatContext->streams[i]->r_frame_rate.num, pFormatContext->streams[i]->r_frame_rate.den);
logging("AVStream->start_time %" PRId64, pFormatContext->streams[i]->start_time);
logging("AVStream->duration %" PRId64, pFormatContext->streams[i]->duration);
logging("finding the proper decoder (CODEC)");
AVCodec *pLocalCodec = NULL;
pLocalCodec = avcodec_find_decoder(pLocalCodecParameters->codec_id);
if (pLocalCodec==NULL) {
logging("ERROR unsupported codec!");
// In this example if the codec is not found we just skip it
continue;
}
// when the stream is a video we store its index, codec parameters and codec
if (pLocalCodecParameters->codec_type == AVMEDIA_TYPE_VIDEO) {
if (video_stream_index == -1) {
video_stream_index = i;
pCodec = pLocalCodec;
pCodecParameters = pLocalCodecParameters;
}
logging("Video Codec: resolution %d x %d", pLocalCodecParameters->width, pLocalCodecParameters->height);
} else if (pLocalCodecParameters->codec_type == AVMEDIA_TYPE_AUDIO) {
logging("Audio Codec: %d channels, sample rate %d", pLocalCodecParameters->channels, pLocalCodecParameters->sample_rate);
}
// print its name, id and bitrate
logging("\tCodec %s ID %d bit_rate %lld", pLocalCodec->name, pLocalCodec->id, pLocalCodecParameters->bit_rate);
}
if (video_stream_index == -1) {
logging("File %s does not contain a video stream!", argv[1]);
return -1;
}
AVCodecContext *pCodecContext = avcodec_alloc_context3(pCodec);
if (!pCodecContext)
{
logging("failed to allocated memory for AVCodecContext");
return -1;
}
if (avcodec_parameters_to_context(pCodecContext, pCodecParameters) < 0)
{
logging("failed to copy codec params to codec context");
return -1;
}
if (avcodec_open2(pCodecContext, pCodec, NULL) < 0)
{
logging("failed to open codec through avcodec_open2");
return -1;
}
AVFrame *pFrame = av_frame_alloc();
if (!pFrame)
{
logging("failed to allocate memory for AVFrame");
return -1;
}
AVPacket *pPacket = av_packet_alloc();
if (!pPacket)
{
logging("failed to allocate memory for AVPacket");
return -1;
}
/*seek for 20 seconds*/
int64_t incr, pos = 5;
int64_t seek_target = (pos * AV_TIME_BASE), stream_index = 0, how_many_packets_to_process = 50, response = 0;
printf("seek_target before : %lu\n", seek_target);
seek_target = av_rescale_q(seek_target, AV_TIME_BASE_Q, pFormatContext->streams[stream_index]->time_base);
printf("seek_target after: %lu\n", seek_target);
do
{
response = decode_packet(pFormatContext, pPacket, pCodecContext, pFrame, seek_target);
if (response < 0)
break;
avcodec_flush_buffers(pCodecContext);
/*av_frame_unref(pFrame);
av_packet_unref(pPacket);*/
// stop it, otherwise we'll be saving hundreds of frames
if (--how_many_packets_to_process <= 0)
break;
}while(1);
logging("releasing all the resources");
avformat_close_input(&pFormatContext);
av_packet_free(&pPacket);
av_frame_free(&pFrame);
avcodec_free_context(&pCodecContext);
return 0;
}
int decode_packet(AVFormatContext *pFormatContext, AVPacket *pPacket, AVCodecContext *pCodecContext, AVFrame *pFrame, int64_t seek_target)
{
if(av_seek_frame(pFormatContext, 0, /*(startTime + frameTimestamp) / 10*/seek_target, AVSEEK_FLAG_BACKWARD) < 0)
{
printf("error while seeking\n"/*, pFormatContext->filename*/);
return -1;
}
avcodec_flush_buffers(pCodecContext);
while(1)
{
if(av_read_frame(pFormatContext, pPacket) < 0)
{
logging("av_read_frame failure");
break;
}
/* I'm not able to get to correct timestamp to discard prior frames upto desired seconds. This if hasn't worked out well as of now. */
if((av_q2d(pFormatContext->streams[0]->time_base) * pPacket->pts) < (seek_target * 1000))
{
printf("skipping the frame\npFormatContext->streams[0]->time_base: %d %d\tpckt.pts: %lu\tseek: %lu", pFormatContext->streams[0]->time_base.num, pFormatContext->streams[0]->time_base.den, pPacket->pts, seek_target);
av_packet_unref(pPacket);
continue;
}
// Send Packet for decoding
int response = avcodec_send_packet(pCodecContext, pPacket);
if (response < 0) {
logging("Error while sending a packet to the decoder: %s", av_err2str(response));
return response;
}
while (response >= 0)
{
response = avcodec_receive_frame(pCodecContext, pFrame);
if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) {
break;
} else if (response < 0) {
logging("Error while receiving a frame from the decoder: %s", av_err2str(response));
return response;
}
if (response >= 0) {
logging(
"Frame %d (type=%c, size=%d bytes, format=%d) pts %d key_frame %d [DTS %d]: %d",
pCodecContext->frame_number,
av_get_picture_type_char(pFrame->pict_type),
pFrame->pkt_size,
pFrame->format,
pFrame->pts,
pFrame->key_frame,
pFrame->coded_picture_number,
pPacket->dts
);
char frame_filename[1024];
snprintf(frame_filename, sizeof(frame_filename), "%s-%d.pgm", "im/frame", pCodecContext->frame_number);
if (pFrame->format != AV_PIX_FMT_YUV420P)
{
logging("Warning: the generated file may not be a grayscale image, but could e.g. be just the R component if the video format is RGB");
}
// save a grayscale frame into a .pgm file
save_gray_frame(pFrame->data[0], pFrame->linesize[0], pFrame->width, pFrame->height, frame_filename);
av_frame_unref(m_pAVFrame);
}
}
}
return 0;
}
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.
I am trying to read data from my codec. For reasons in my project I would like to do nonblocking, but every time I read the number of bytes available on my codec it says zero.
The algorithm is pretty simple: wait 1ms then check to see if there are 160+ samples available in the codec to read and then read the samples. But every time I do a read it says the sample count is zero.
Can someone help me understand why "rc = snd_pcm_avail(inputCodecHandle);" is always returning a zero?
Here is the thread with the code in it.
void CRadioStack::rcvThread() {
ChannelBuffer_t *buffer_p = NULL;
int8_t *inputBuf_p;
int rc;
int16_t *inputBuf16_p;
int samplesToRead;
const int rxFrameSize = 160;
snd_pcm_sframes_t delay;
snd_pcm_nonblock(inputCodecHandle, 1);
snd_pcm_prepare(inputCodecHandle);
while (true) {
TWTime::msleep(1);
// get the number of samples available
snd_pcm_delay(inputCodecHandle, &delay);
rc = snd_pcm_avail(inputCodecHandle);
if (rc < 0) {
myLog->warn("Error in getting sample count: %s", snd_strerror(rc));
snd_pcm_prepare(outputCodecHandle);
continue;
}
samplesToRead = rc;
// if number of samples > 160 then get 160 samples
if (samplesToRead <= rxFrameSize) {
continue;
}
// read the from the codec into the Channel Buffer.
rc = snd_pcm_readi(inputCodecHandle, inputBuf_p, rxFrameSize);
if (rc < 0) {
myLog->warn("Error reading Codec: %s", snd_strerror(rc));
continue;
} else if (rc != rxFrameSize) { // nothing to get
myLog->warn("Input samples on codec not 160");
}
pushToInputQueue(inputBuf_p);
}
}
And here is the code to open the codec.
bool CRadioStack::openInputCodec()
{
unsigned int val;
int dir;
const int NUM_OF_CHAN = 1;
codecRunning = false;
snd_pcm_uframes_t frames;
int rc;
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
inputCodecHandle = nullptr;
// Open pcm device for output
rc = snd_pcm_open(&handle, "hw:0,0", SND_PCM_STREAM_CAPTURE, 0);
if (rc < 0) {
myLog->error("Unable to open input codec: %s", snd_strerror(rc));
return false;
}
// allocate a hardware parameters object
snd_pcm_hw_params_alloca(¶ms);
// fill with default values
snd_pcm_hw_params_any(handle, params);
// now setup the hardware paramters
snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); // interleaved
snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE); // 16bin linear little-endian
snd_pcm_hw_params_set_channels(handle, params, NUM_OF_CHAN); // one channel
val = 0;
snd_pcm_hw_params_set_channels_near(handle, params, &val); // one channel
val = 8000;
dir = 0;
snd_pcm_hw_params_set_rate_near(handle, params, &val, &dir); // 8k sample rate.
frames = 160;
snd_pcm_hw_params_set_period_size_near(handle, params, &frames, &dir); // period size = 160 frames
// save the hardware parameters
rc = snd_pcm_hw_params(handle, params);
if (rc < 0) {
myLog->error("Unable to save hardware parameters to output codec.");
return false;
}
// ready to write to output codec.
// so save the handle so that it can be used elsewhere.
inputCodecHandle = handle;
return true;
}
Thanks!
The device was never started.
This would happen automatically with the first snd_pcm_read*() call, but can also be done explicitly with snd_pcm_start().
Im having an issue with playing audio.
Im new to the SDL World of things so im learning from a tutorial.
http://dranger.com/ffmpeg/tutorial03.html
As far as audio goes, i have exactly what he put down and didnt get the result he says I should get. In the end of the lesson he specifies that the audio should play normally. However all i get is excessively loud static noise. This leads me to believe that the packets arent being read correctly. However I have no idea how to debug or look for the issue.
Here is my main loop for parsing the packets:
while (av_read_frame(pFormatCtx, &packet) >= 0) {
if (packet.stream_index == videoStream) {
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
if (frameFinished){
AVPicture pict;
pict.data[0] = yPlane;
pict.data[1] = uPlane;
pict.data[2] = vPlane;
pict.linesize[0] = pCodecCtx->width;
pict.linesize[1] = uvPitch;
pict.linesize[2] = uvPitch;
sws_scale(sws_ctx,
pFrame->data, pFrame->linesize,
0, pCodecCtx->height,
pict.data, pict.linesize);
//SDL_UnlockTexture(bmp);
SDL_UpdateYUVTexture(bmp, 0,
yPlane, pCodecCtx->width,
uPlane, uvPitch,
vPlane, uvPitch);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, bmp, NULL, NULL);
SDL_RenderPresent(renderer);
av_free_packet(&packet);
}
}
else if (packet.stream_index == audioStream) {
packet_queue_put(&audioq, &packet);
}
else
av_free_packet(&packet);
SDL_PollEvent(&event);
switch (event.type) {
case SDL_QUIT:
quit = 1;
SDL_DestroyTexture(bmp);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(screen);
SDL_Quit();
exit(0);
break;
default:
break;
}
}
this is my initialization of the audio device :
aCodecCtxOrig = pFormatCtx->streams[audioStream]->codec;
aCodec = avcodec_find_decoder(aCodecCtxOrig->codec_id);
if (!aCodec) {
fprintf(stderr, "Unsupported codec!\n");
return -1;
}
// Copy context
aCodecCtx = avcodec_alloc_context3(aCodec);
if (avcodec_copy_context(aCodecCtx, aCodecCtxOrig) != 0) {
fprintf(stderr, "Couldn't copy codec context");
return -1; // Error copying codec context
}
wanted_spec.freq = aCodecCtx->sample_rate;
wanted_spec.format = AUDIO_U16SYS;
wanted_spec.channels = aCodecCtx->channels;
wanted_spec.silence = 0;
wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
wanted_spec.callback = audio_callback;
wanted_spec.userdata = aCodecCtx;
if (SDL_OpenAudio( &wanted_spec, &spec) < 0) {
fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
return -1;
}
avcodec_open2(aCodecCtx, aCodec, NULL);
// audio_st = pFormatCtx->streams[index]
packet_queue_init(&audioq);
SDL_PauseAudio(0);
The Call back (same as the tutorial):|
void audio_callback(void *userdata, Uint8 *stream, int len) {
AVCodecContext *aCodecCtx = (AVCodecContext *)userdata;
int len1, audio_size;
static uint8_t audio_buf[(MAX_AUDIO_FRAME_SIZE * 3) / 2];
static unsigned int audio_buf_size = 0;
static unsigned int audio_buf_index = 0;
while (len > 0) {
if (audio_buf_index >= audio_buf_size) {
/* We have already sent all our data; get more */
audio_size = audio_decode_frame(aCodecCtx, audio_buf, sizeof(audio_buf));
if (audio_size < 0) {
/* If error, output silence */
audio_buf_size = 1024; // arbitrary?
memset(audio_buf, 0, audio_buf_size);
}
else {
audio_buf_size = audio_size;
}
audio_buf_index = 0;
}
len1 = audio_buf_size - audio_buf_index;
if (len1 > len)
len1 = len;
memcpy(stream, (uint8_t *)audio_buf + audio_buf_index, len1);
len -= len1;
stream += len1;
audio_buf_index += len1;
}
}
I also had same problem when I learn tutorial 3. The comments by pprahul in https://github.com/mpenkov/ffmpeg-tutorial/issues/11 solve my problem when playing .MPG file with mp2 format audio. But the problem still occur when I play .MP4 file with AAC format audio.
A snapshot of that comment is to set the decode format manually, by
//after get the AVCodecContext *codecCtx(aCodecCtx in tutorial).
if (codecCtx->sample_fmt == AV_SAMPLE_FMT_S16P) {
codecCtx->request_sample_fmt = AV_SAMPLE_FMT_S16;
}
//...
It seems that the problem will not occur with early version of FFmpeg (1.01 and below).
I am pretty new to ffmpeg, and now I want to decode the frames and encode it back in reverse order. I have used this tutorial for decoding the video and this example to encode it back. I can decode the frames properly, but when I re-encode it, I get a dummy image rather than clear video. How can I get the actual reversed video?
static void video_encode_example(const char *filename, int codec_id)
{
AVCodec *codec;
AVCodecContext *c= NULL;
int i, ret, x, y, got_output;
FILE *f;
struct SwsContext *sws_ctx = NULL;
AVFrame *frame;
AVFrame *frameRGB = NULL;
AVPacket pkt;
uint8_t endcode[] = { 0, 0, 1, 0xb7 };
printf("Encode video file %s\n", filename);
/* find the mpeg1 video encoder */
codec = avcodec_find_encoder(codec_id);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
c = avcodec_alloc_context3(codec);
if (!c) {
fprintf(stderr, "Could not allocate video codec context\n");
exit(1);
}
/* put sample parameters */
c->bit_rate = 400000;
/* resolution must be a multiple of two */
c->width = 352;
c->height = 288;
/* frames per second */
c->time_base = (AVRational){1,25};
/* emit one intra frame every ten frames
* check frame pict_type before passing frame
* to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
* then gop_size is ignored and the output of encoder
* will always be I frame irrespective to gop_size
*/
c->gop_size = 10;
c->max_b_frames = 1;
c->pix_fmt = AV_PIX_FMT_YUV420P;
if (codec_id == AV_CODEC_ID_H264)
av_opt_set(c->priv_data, "preset", "slow", 0);
/* open it */
if (avcodec_open2(c, codec, NULL) < 0) {
fprintf(stderr, "Could not open codec\n");
exit(1);
}
f = fopen(filename, "wb");
if (!f) {
fprintf(stderr, "Could not open %s\n", filename);
exit(1);
}
frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, "Could not allocate video frame\n");
exit(1);
}
frame->format = c->pix_fmt;
frame->width = c->width;
frame->height = c->height;
/* the image can be allocated by any means and av_image_alloc() is
* just the most convenient way if av_malloc() is to be used */
ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height,
c->pix_fmt, 32);
if (ret < 0) {
fprintf(stderr, "Could not allocate raw picture buffer\n");
exit(1);
}
/* encode 1 second of video */
for (i = 0; i < 25; i++) {
av_init_packet(&pkt);
pkt.data = NULL; // packet data will be allocated by the encoder
pkt.size = 0;
fflush(stdout);
/* prepare a dummy image */
/* Y */
for (y = 0; y < c->height; y++) {
for (x = 0; x < c->width; x++) {
frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
}
}
/* Cb and Cr */
for (y = 0; y < c->height/2; y++) {
for (x = 0; x < c->width/2; x++) {
frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
}
}
frame->pts = i;
/* encode the image */
ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
if (ret < 0) {
fprintf(stderr, "Error encoding frame\n");
exit(1);
}
if (got_output) {
// Convert the image from its native format to RGB
sws_scale
(
sws_ctx,
(uint8_t const * const *)frame->data,
frame->linesize,
0,
c->height,
frameRGB->data,
frameRGB->linesize
);
printf("Write frame %3d (size=%5d)\n", i, pkt.size);
fwrite(pkt.data, 1, pkt.size, f);
av_free_packet(&pkt);
}
}
/* get the delayed frames */
for (got_output = 1; got_output; i++) {
fflush(stdout);
ret = avcodec_encode_video2(c, &pkt, NULL, &got_output);
if (ret < 0) {
fprintf(stderr, "Error encoding frame\n");
exit(1);
}
if (got_output) {
printf("Write frame %3d (size=%5d)\n", i, pkt.size);
fwrite(pkt.data, 1, pkt.size, f);
av_free_packet(&pkt);
}
}
/* add sequence end code to have a real mpeg file */
fwrite(endcode, 1, sizeof(endcode), f);
fclose(f);
avcodec_close(c);
av_free(c);
av_freep(&frame->data[0]);
av_frame_free(&frame);
printf("\n");
}
Assuming that your code sample have nothing to do with a video input nor a reverse encoding, but is only an "encode sample".
A video file is made of severa parts.
First, you have a container format (for example mkv, or avi). It contains various informations on which streams are in this file (subtiles? how many video? multiple audio stream? stuff like that).
Then you have the data streams.
A video stream inside this file is a list of packets, encoded by a specific codec (h264 or mpeg4 for example) from frames.
In your code sample, you have a list of frames, and your are encoding them with a codec, giving you a list of packets (I suppose it works, since it is a tutorial code).
But these packets are dumped into a file, without container. So if a video player wants to read them, I will have a hard time to guess what is the format.
It is stated as the first line in your link
Instead of dumping your packets in a file directly, you want to use FFmpeg to embed your packets in a container, and save your container.
You have an example of all this process here
Last thing: if you want to have your video in reverse order, you must encode frames in the reverse order, not only mux packets in reverse order.