Benjamin Gretsch schrieb:
> Hi everybody,
>
> I am still new to libavcodec and to a/v processing.
>
> I have to process an audio stream from a movie source and encode it as AC3.
> So far I have only the decoding and encoding (call it transcoding) without
> processing, but with a problem: The output file has a lot of noise, that
> wasn't there before.
>
> This is how I do it in pseudo code:
> make avformat contexts for input and output
> make codec contexts for decoder and encoder
> setup and open encoder
> write headers to the output file
>
> in a loop: read packets, and if they belong to the correct stream, decode
> the audio, collect the samples, split them into channels*frame_size parts
> and encode them as frames. Then send the encoded data as packet to the
> output format context.
>
> write trailer
> close the output file and free everything
>
> Did I forget something?
>
> Best Regards
> Benjamin
>
I have now dumped the samples into files and I see that the samples that my
encoding class receives differs from the samples that I send to avcodec.
Something has to be wrong with my buffering code, but I don't see the
mistake. The first difference occurs in the first sample of the second
output frame.
Ok, it's not avcodec related anymore, but does anybody see the problem here?
bool LAVCAudioEncoder::encodeSamples(int16_t *samples, int nb_samples)
{
//debug
FILE *f = fopen("/media/Prometheus/tvrec/src.tmp", "ab");
fwrite(samples, 2, nb_samples, f);
fclose(f);
qDebug() << "Encode" << nb_samples <<"samples";
//debug end
//if there are too frew samples to encode a whole frame,
//put them into the buffer and return
if (sampleBufferUsed + nb_samples < channels*cctx->frame_size)
{
memcpy(&sampleBuffer[sampleBufferUsed], samples, 2*nb_samples);
sampleBufferUsed += nb_samples;
qDebug() << "Put" << nb_samples << "into buffer and return";
return true;
}
//if there are already samples in buffer, fill the buffer with
//more samples and encode the frame
if (sampleBufferUsed > 0)
{
int n = channels*cctx->frame_size - sampleBufferUsed;
qDebug() << "append" << n <<"samples to the buffer + encode
frame";
if (n > 0)
{
memcpy(&sampleBuffer[sampleBufferUsed], samples, 2*n);
}
sampleBufferUsed = 0;
if (!encodeFrame(sampleBuffer))
return false;
nb_samples -= n;
samples += 2*n;
}
//while we have enough samples left for a whole frame, encode them
while (nb_samples >= channels*cctx->frame_size)
{
qDebug() << "Encode frame directly";
if (!encodeFrame(samples))
return false;
samples += 2*channels*cctx->frame_size;
nb_samples -= channels*cctx->frame_size;
}
//put the remaining samples into the buffer
if (nb_samples > 0)
{
qDebug() << "Put" << nb_samples <<"remaining samples into
buffer";
memcpy(sampleBuffer, samples, 2*nb_samples);
sampleBufferUsed = nb_samples;
}
return true;
}
The debug output starts with
encoder has a frame size of 1536 that is 3072 samples/frame
Encode 2304 samples
Put 2304 into buffer and return
Encode 2304 samples
append 768 samples to the buffer + encode frame
Put 1536 remaining samples into buffer
Encode 2304 samples
append 1536 samples to the buffer + encode frame
Put 768 remaining samples into buffer
...
Thanks in advance
Benjamin
_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user