[FFmpeg-user] Little help

JULIAN GARDNER joolzg at btinternet.com
Mon Apr 6 22:13:25 CEST 2015


Has anybody any code that will split an audio buffer so that the packet passed to the codec is of the right size.

The problem is in this code

                        frame           = here->frame;

                        /* convert samples from native format to destination codec format, using the resampler */
                            /* compute destination number of samples */
                        dst_nb_samples = av_rescale_rnd(swr_get_delay(inputSource->audio_swr_ctx, inputSource->audio_dec_ctx->sample_rate) + frame->nb_samples,
                                                        c->sample_rate, inputSource->audio_dec_ctx->sample_rate, AV_ROUND_UP);
                        //av_assert0(dst_nb_samples == frame->nb_samples);
//                        printf( "rescale_rnd %d to %d\r\n", frame->nb_samples, dst_nb_samples);

                        if( dst_nb_samples>ost->audio_frame_size) {
                            av_frame_free(&ost->audio_frame);
                            ost->audio_frame = alloc_audio_frame(c->sample_fmt, c->channel_layout, c->sample_rate, dst_nb_samples);
                            ost->audio_frame_size = dst_nb_samples;
                        }

                        /* when we pass a frame to the encoder, it may keep a reference to it
                         * internally;
                         * make sure we do not overwrite it here
                         */
                        ret = av_frame_make_writable(ost->audio_frame);
                        if (ret < 0)
                            exit(1);

                        /* convert to destination format */
                        ret = swr_convert(inputSource->audio_swr_ctx,
                                          ost->audio_frame->data, dst_nb_samples,
                                          (const uint8_t **)frame->data, frame->nb_samples);
                        if (ret < 0) {
                            fprintf(stderr, "Error while converting\n");
                            exit(1);
                        }
                        ost->audio_frame->pts       = av_rescale_q(ost->samples_count, (AVRational){1, c->sample_rate}, c->time_base);
                        ost->samples_count         += dst_nb_samples;
                        ost->next_pts               = av_rescale_q(ost->samples_count, (AVRational){1, c->sample_rate}, c->time_base);

// THIS CODE IS HERE TO PROVE THE ENCODER WORKS WITH RESAMPLED DATA
                if(1) {
                int size = ost->audio_frame->nb_samples;

                        ost->audio_frame->nb_samples = FFMIN( c->frame_size, dst_nb_samples);
                        ret = avcodec_encode_audio2(c, &pkt, ost->audio_frame, &got_packet);
                        ost->audio_frame->nb_samples = size;
                        if (ret < 0) {
                            fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret));
                            exit(1);
                        }
                }

                        if (got_packet) {
                            ret = write_frame(oc, &c->time_base, ost->st, &pkt);
                            if (ret < 0) {
                                fprintf(stderr, "Error while writing audio frame: %s\n",
                                        av_err2str(ret));
                                exit(1);
                            }
                        }

As you see i have to make sure the converted packet is no bigger that the allowed packet size in the encoder. My problem is the packet size before the convertor is 1024 but after conversion it is 1536, the codec (libfaac) on allows 1024 as the packet size, the above code works but i lose 1/3 of the data.

So how do i split an audio buffer when the format can be anything the user chooses

joolz


joolz


More information about the ffmpeg-user mailing list