[PATCH 1/2] api-flac-test: Fix stupid mistake
Was comparing 0 bytes in memcmp --- libavcodec/api-flac-test.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libavcodec/api-flac-test.c b/libavcodec/api-flac-test.c index 5ca8e8f..1540da9 100644 --- a/libavcodec/api-flac-test.c +++ b/libavcodec/api-flac-test.c @@ -117,7 +117,6 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx, AVFrame *in_frame, *out_frame; uint8_t *raw_in = NULL, *raw_out = NULL; int in_offset = 0, out_offset = 0; - int frame_data_size = 0; int result = 0; int got_output = 0; int i = 0; @@ -218,7 +217,7 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx, av_free_packet(&enc_pkt); } - if (memcmp(raw_in, raw_out, frame_data_size * NUMBER_OF_FRAMES) != 0) + if (memcmp(raw_in, raw_out, out_frame->linesize[0] * NUMBER_OF_FRAMES) != 0) { av_log(NULL, AV_LOG_ERROR, "Output differs\n"); return 1; -- 1.9.1
--- libavcodec/api-flac-test.c | 72 ++++++++++++++++------------------------------ 1 file changed, 24 insertions(+), 48 deletions(-) diff --git a/libavcodec/api-flac-test.c b/libavcodec/api-flac-test.c index 1540da9..5ff8f12 100644 --- a/libavcodec/api-flac-test.c +++ b/libavcodec/api-flac-test.c @@ -40,8 +40,7 @@ static int generate_raw_frame(uint16_t *frame_data, int i, int sample_rate, { int j, k; - for (j = 0; j < frame_size; j++) - { + for (j = 0; j < frame_size; j++) { frame_data[channels * j] = 10000 * ((j / 10 * i) % 2); for (k = 1; k < channels; k++) frame_data[channels * j + k] = frame_data[channels * j] * (k + 1); @@ -60,8 +59,7 @@ static int init_encoder(AVCodec *enc, AVCodecContext **enc_ctx, av_log(NULL, AV_LOG_INFO, "channel layout: %s, sample rate: %i\n", name_buff, sample_rate); ctx = avcodec_alloc_context3(enc); - if (!ctx) - { + if (!ctx) { av_log(NULL, AV_LOG_ERROR, "Can't allocate encoder context\n"); return AVERROR(ENOMEM); } @@ -71,8 +69,7 @@ static int init_encoder(AVCodec *enc, AVCodecContext **enc_ctx, ctx->channel_layout = ch_layout; result = avcodec_open2(ctx, enc, NULL); - if (result < 0) - { + if (result < 0) { av_log(ctx, AV_LOG_ERROR, "Can't open encoder\n"); return result; } @@ -88,8 +85,7 @@ static int init_decoder(AVCodec *dec, AVCodecContext **dec_ctx, int result; ctx = avcodec_alloc_context3(dec); - if (!ctx) - { + if (!ctx) { av_log(NULL, AV_LOG_ERROR , "Can't allocate decoder context\n"); return AVERROR(ENOMEM); } @@ -100,8 +96,7 @@ static int init_decoder(AVCodec *dec, AVCodecContext **dec_ctx, ctx->channel_layout = ch_layout; result = avcodec_open2(ctx, dec, NULL); - if (result < 0) - { + if (result < 0) { av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n"); return result; } @@ -122,8 +117,7 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx, int i = 0; in_frame = av_frame_alloc(); - if (!in_frame) - { + if (!in_frame) { av_log(NULL, AV_LOG_ERROR, "Can't allocate input frame\n"); return AVERROR(ENOMEM); } @@ -131,35 +125,30 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx, in_frame->nb_samples = enc_ctx->frame_size; in_frame->format = enc_ctx->sample_fmt; in_frame->channel_layout = enc_ctx->channel_layout; - if (av_frame_get_buffer(in_frame, 32) != 0) - { + if (av_frame_get_buffer(in_frame, 32) != 0) { av_log(NULL, AV_LOG_ERROR, "Can't allocate a buffer for input frame\n"); return AVERROR(ENOMEM); } out_frame = av_frame_alloc(); - if (!out_frame) - { + if (!out_frame) { av_log(NULL, AV_LOG_ERROR, "Can't allocate output frame\n"); return AVERROR(ENOMEM); } raw_in = av_malloc(in_frame->linesize[0] * NUMBER_OF_FRAMES); - if (!raw_in) - { + if (!raw_in) { av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for raw_in\n"); return AVERROR(ENOMEM); } raw_out = av_malloc(in_frame->linesize[0] * NUMBER_OF_FRAMES); - if (!raw_out) - { + if (!raw_out) { av_log(NULL, AV_LOG_ERROR, "Can't allocate memory for raw_out\n"); return AVERROR(ENOMEM); } - for (i = 0; i < NUMBER_OF_FRAMES; i++) - { + for (i = 0; i < NUMBER_OF_FRAMES; i++) { av_init_packet(&enc_pkt); enc_pkt.data = NULL; enc_pkt.size = 0; @@ -169,44 +158,36 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx, memcpy(raw_in + in_offset, in_frame->data[0], in_frame->linesize[0]); in_offset += in_frame->linesize[0]; result = avcodec_encode_audio2(enc_ctx, &enc_pkt, in_frame, &got_output); - if (result < 0) - { + if (result < 0) { av_log(NULL, AV_LOG_ERROR, "Error encoding audio frame\n"); return result; } /* if we get an encoded packet, feed it straight to the decoder */ - if (got_output) - { + if (got_output) { result = avcodec_decode_audio4(dec_ctx, out_frame, &got_output, &enc_pkt); - if (result < 0) - { + if (result < 0) { av_log(NULL, AV_LOG_ERROR, "Error decoding audio packet\n"); return result; } - if (got_output) - { - if (result != enc_pkt.size) - { + if (got_output) { + if (result != enc_pkt.size) { av_log(NULL, AV_LOG_INFO, "Decoder consumed only part of a packet, it is allowed to do so -- need to update this test\n"); return AVERROR_UNKNOWN; } - if (in_frame->nb_samples != out_frame->nb_samples) - { + if (in_frame->nb_samples != out_frame->nb_samples) { av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different number of samples\n"); return AVERROR_UNKNOWN; } - if (in_frame->channel_layout != out_frame->channel_layout) - { + if (in_frame->channel_layout != out_frame->channel_layout) { av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different channel layout\n"); return AVERROR_UNKNOWN; } - if (in_frame->format != out_frame->format) - { + if (in_frame->format != out_frame->format) { av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different sample format\n"); return AVERROR_UNKNOWN; } @@ -217,8 +198,7 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx, av_free_packet(&enc_pkt); } - if (memcmp(raw_in, raw_out, out_frame->linesize[0] * NUMBER_OF_FRAMES) != 0) - { + if (memcmp(raw_in, raw_out, out_frame->linesize[0] * NUMBER_OF_FRAMES) != 0) { av_log(NULL, AV_LOG_ERROR, "Output differs\n"); return 1; } @@ -257,23 +237,19 @@ int main(void) avcodec_register_all(); enc = avcodec_find_encoder(AV_CODEC_ID_FLAC); - if (!enc) - { + if (!enc) { av_log(NULL, AV_LOG_ERROR, "Can't find encoder\n"); return 1; } dec = avcodec_find_decoder(AV_CODEC_ID_FLAC); - if (!dec) - { + if (!dec) { av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n"); return 1; } - for (cl = 0; cl < FF_ARRAY_ELEMS(channel_layouts); cl++) - { - for (sr = 0; sr < FF_ARRAY_ELEMS(sample_rates); sr++) - { + for (cl = 0; cl < FF_ARRAY_ELEMS(channel_layouts); cl++) { + for (sr = 0; sr < FF_ARRAY_ELEMS(sample_rates); sr++) { if (init_encoder(enc, &enc_ctx, channel_layouts[cl], sample_rates[sr]) != 0) return 1; if (init_decoder(dec, &dec_ctx, channel_layouts[cl]) != 0) -- 1.9.1
On Wed, Apr 22, 2015 at 06:09:15PM +0300, Ludmila Glinskih wrote:
--- libavcodec/api-flac-test.c | 72 ++++++++++++++++------------------------------ 1 file changed, 24 insertions(+), 48 deletions(-)
applied thanks [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Concerning the gods, I have no means of knowing whether they exist or not or of what sort they may be, because of the obscurity of the subject, and the brevity of human life -- Protagoras
On Wed, 22 Apr 2015 18:09:14 +0300 Ludmila Glinskih <lglinskih@gmail.com> wrote:
Was comparing 0 bytes in memcmp --- libavcodec/api-flac-test.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/libavcodec/api-flac-test.c b/libavcodec/api-flac-test.c index 5ca8e8f..1540da9 100644 --- a/libavcodec/api-flac-test.c +++ b/libavcodec/api-flac-test.c @@ -117,7 +117,6 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx, AVFrame *in_frame, *out_frame; uint8_t *raw_in = NULL, *raw_out = NULL; int in_offset = 0, out_offset = 0; - int frame_data_size = 0; int result = 0; int got_output = 0; int i = 0; @@ -218,7 +217,7 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx, av_free_packet(&enc_pkt); }
- if (memcmp(raw_in, raw_out, frame_data_size * NUMBER_OF_FRAMES) != 0) + if (memcmp(raw_in, raw_out, out_frame->linesize[0] * NUMBER_OF_FRAMES) != 0) { av_log(NULL, AV_LOG_ERROR, "Output differs\n"); return 1;
Something that I just noticed, though it's not necessarily related to this particular patch: you should use nb_samples to copy the data in/out. linesize can be larger than the actual data due to padding. The padding is needed because SIMD instructions may write more data than actually needed, which would write past the memory allocation. So the memory allocation is simply made large enough to avoid problems. The API user doesn't need to care about these issues, because av_frame_get_buffer() already takes care of it. (The test still could check that nb_samples and the AVFrame format is consistent with linesize. Maybe.) Also, in this case, there's probably no observable issue with your code, because the frame sizes are already aligned, so that linesize equals the used size by coincidence. The doxygen for linesize says: * @note The linesize may be larger than the size of usable data -- there * may be extra padding present for performance reasons.
--- libavcodec/api-flac-test.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/libavcodec/api-flac-test.c b/libavcodec/api-flac-test.c index 402d4df..4cd0db7 100644 --- a/libavcodec/api-flac-test.c +++ b/libavcodec/api-flac-test.c @@ -112,10 +112,10 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx, AVFrame *in_frame, *out_frame; uint8_t *raw_in = NULL, *raw_out = NULL; int in_offset = 0, out_offset = 0; - int frame_data_size = 0; int result = 0; int got_output = 0; int i = 0; + int in_frame_bytes, out_frame_bytes; in_frame = av_frame_alloc(); if (!in_frame) { @@ -156,8 +156,13 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx, generate_raw_frame((uint16_t*)(in_frame->data[0]), i, enc_ctx->sample_rate, enc_ctx->channels, enc_ctx->frame_size); - memcpy(raw_in + in_offset, in_frame->data[0], in_frame->linesize[0]); - in_offset += in_frame->linesize[0]; + in_frame_bytes = in_frame->nb_samples * in_frame->channels * sizeof(uint16_t); + if (in_frame_bytes != in_frame->linesize[0]) { + av_log(NULL, AV_LOG_ERROR, "Incorrect value of input frame linesize\n"); + return 1; + } + memcpy(raw_in + in_offset, in_frame->data[0], in_frame_bytes); + in_offset += in_frame_bytes; result = avcodec_encode_audio2(enc_ctx, &enc_pkt, in_frame, &got_output); if (result < 0) { av_log(NULL, AV_LOG_ERROR, "Error encoding audio frame\n"); @@ -192,14 +197,19 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx, av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different sample format\n"); return AVERROR_UNKNOWN; } - memcpy(raw_out + out_offset, out_frame->data[0], out_frame->linesize[0]); - out_offset += out_frame->linesize[0]; + out_frame_bytes = out_frame->nb_samples * out_frame->channels * sizeof(uint16_t); + if (out_frame_bytes > out_frame->linesize[0]) { + av_log(NULL, AV_LOG_ERROR, "Incorrect value of output frame linesize\n"); + return 1; + } + memcpy(raw_out + out_offset, out_frame->data[0], out_frame_bytes); + out_offset += out_frame_bytes; } } av_free_packet(&enc_pkt); } - if (memcmp(raw_in, raw_out, frame_data_size * NUMBER_OF_FRAMES) != 0) { + if (memcmp(raw_in, raw_out, out_frame_bytes * NUMBER_OF_FRAMES) != 0) { av_log(NULL, AV_LOG_ERROR, "Output differs\n"); return 1; } -- 1.9.1
On Sat, Jun 27, 2015 at 02:01:15AM +0300, Ludmila Glinskih wrote:
--- libavcodec/api-flac-test.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
diff --git a/libavcodec/api-flac-test.c b/libavcodec/api-flac-test.c index 402d4df..4cd0db7 100644 --- a/libavcodec/api-flac-test.c +++ b/libavcodec/api-flac-test.c @@ -112,10 +112,10 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx, AVFrame *in_frame, *out_frame; uint8_t *raw_in = NULL, *raw_out = NULL; int in_offset = 0, out_offset = 0; - int frame_data_size = 0; int result = 0; int got_output = 0; int i = 0; + int in_frame_bytes, out_frame_bytes;
in_frame = av_frame_alloc(); if (!in_frame) { @@ -156,8 +156,13 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx,
generate_raw_frame((uint16_t*)(in_frame->data[0]), i, enc_ctx->sample_rate, enc_ctx->channels, enc_ctx->frame_size); - memcpy(raw_in + in_offset, in_frame->data[0], in_frame->linesize[0]); - in_offset += in_frame->linesize[0]; + in_frame_bytes = in_frame->nb_samples * in_frame->channels * sizeof(uint16_t);
+ if (in_frame_bytes != in_frame->linesize[0]) { + av_log(NULL, AV_LOG_ERROR, "Incorrect value of input frame linesize\n"); + return 1; + }
The linesize is not guranteed to match the "active area" i think you can just ignore the linesize completely or if you like check that it is >= in_frame_bytes
+ memcpy(raw_in + in_offset, in_frame->data[0], in_frame_bytes); + in_offset += in_frame_bytes; result = avcodec_encode_audio2(enc_ctx, &enc_pkt, in_frame, &got_output); if (result < 0) { av_log(NULL, AV_LOG_ERROR, "Error encoding audio frame\n"); @@ -192,14 +197,19 @@ static int run_test(AVCodec *enc, AVCodec *dec, AVCodecContext *enc_ctx, av_log(NULL, AV_LOG_ERROR, "Error frames before and after decoding has different sample format\n"); return AVERROR_UNKNOWN; } - memcpy(raw_out + out_offset, out_frame->data[0], out_frame->linesize[0]); - out_offset += out_frame->linesize[0];
+ out_frame_bytes = out_frame->nb_samples * out_frame->channels * sizeof(uint16_t);
AVFrame.channels should e accessed through av_frame_get_channels() libavutil/frame.h lists which AVFrame fields can be accessed directly from outide avutil [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB I have never wished to cater to the crowd; for what I know they do not approve, and what they approve I do not know. -- Epicurus
participants (3)
-
Ludmila Glinskih -
Michael Niedermayer -
wm4