This is an automated email from the git hooks/post-receive script. Git pushed a commit to branch master in repository ffmpeg.
commit bec2ce0f713bfc6d294e79a08a320a029762921c Author: Romain Beauxis <[email protected]> AuthorDate: Wed Sep 16 15:18:38 2026 -0500 Commit: James Almer <[email protected]> CommitDate: Sat Sep 19 13:40:52 2026 +0000 avcodec, avformat: switch to avpriv_mpegaudio_decode_header2() Port every caller to the new entry point, which lets libavcodec allocate the header, and prepare the old one for removal. The mp3 decoder keeps a named MPADecodeHeader2 in its context, so the macro spelling out the header fields is no longer needed. --- libavcodec/libmp3lame.c | 4 +- libavcodec/libshine.c | 4 +- libavcodec/mpegaudiodec_template.c | 160 ++++++++++++++++++------------------- libavcodec/mpegaudiodecheader.c | 6 ++ libavcodec/mpegaudiodecheader.h | 14 ++-- libavformat/mp3dec.c | 70 +++++++++------- libavformat/mp3enc.c | 39 ++++----- 7 files changed, 163 insertions(+), 134 deletions(-) diff --git a/libavcodec/libmp3lame.c b/libavcodec/libmp3lame.c index d6aa8cf62e..88a5ba34a8 100644 --- a/libavcodec/libmp3lame.c +++ b/libavcodec/libmp3lame.c @@ -200,7 +200,7 @@ static int mp3lame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr) { LAMEContext *s = avctx->priv_data; - MPADecodeHeader hdr; + MPADecodeHeader2 hdr; int len, ret, ch; int lame_result; uint32_t h; @@ -263,7 +263,7 @@ static int mp3lame_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, return 0; h = AV_RB32(s->buffer); - ret = avpriv_mpegaudio_decode_header(&hdr, h); + ret = ff_mpegaudio_decode_header(&hdr, h); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Invalid mp3 header at start of buffer\n"); return AVERROR_BUG; diff --git a/libavcodec/libshine.c b/libavcodec/libshine.c index 17368e5de8..e7edc5b03f 100644 --- a/libavcodec/libshine.c +++ b/libavcodec/libshine.c @@ -66,7 +66,7 @@ static int libshine_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr) { SHINEContext *s = avctx->priv_data; - MPADecodeHeader hdr; + MPADecodeHeader2 hdr; unsigned char *data; int written; int ret, len; @@ -92,7 +92,7 @@ static int libshine_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, if (s->buffer_index < 4 || !s->afq.frame_count) return 0; - if (avpriv_mpegaudio_decode_header(&hdr, AV_RB32(s->buffer))) { + if (ff_mpegaudio_decode_header(&hdr, AV_RB32(s->buffer))) { av_log(avctx, AV_LOG_ERROR, "free format output not supported\n"); return -1; } diff --git a/libavcodec/mpegaudiodec_template.c b/libavcodec/mpegaudiodec_template.c index 87c89fcd2e..2ac7be3b80 100644 --- a/libavcodec/mpegaudiodec_template.c +++ b/libavcodec/mpegaudiodec_template.c @@ -76,7 +76,7 @@ typedef struct GranuleDef { } GranuleDef; typedef struct MPADecodeContext { - MPA_DECODE_HEADER + MPADecodeHeader2 hdr; uint8_t last_buf[LAST_BUF_SIZE]; int last_buf_size; int extrasize; @@ -138,14 +138,14 @@ static void region_offset2size(GranuleDef *g) static void init_short_region(MPADecodeContext *s, GranuleDef *g) { if (g->block_type == 2) { - if (s->sample_rate_index != 8) + if (s->hdr.sample_rate_index != 8) g->region_size[0] = (36 / 2); else g->region_size[0] = (72 / 2); } else { - if (s->sample_rate_index <= 2) + if (s->hdr.sample_rate_index <= 2) g->region_size[0] = (36 / 2); - else if (s->sample_rate_index != 8) + else if (s->hdr.sample_rate_index != 8) g->region_size[0] = (54 / 2); else g->region_size[0] = (108 / 2); @@ -157,22 +157,22 @@ static void init_long_region(MPADecodeContext *s, GranuleDef *g, int ra1, int ra2) { int l; - g->region_size[0] = ff_band_index_long[s->sample_rate_index][ra1 + 1]; + g->region_size[0] = ff_band_index_long[s->hdr.sample_rate_index][ra1 + 1]; /* should not overflow */ l = FFMIN(ra1 + ra2 + 2, 22); - g->region_size[1] = ff_band_index_long[s->sample_rate_index][ l]; + g->region_size[1] = ff_band_index_long[s->hdr.sample_rate_index][ l]; } static void compute_band_indexes(MPADecodeContext *s, GranuleDef *g) { if (g->block_type == 2) { if (g->switch_point) { - if(s->sample_rate_index == 8) + if(s->hdr.sample_rate_index == 8) avpriv_request_sample(s->avctx, "switch point in 8khz"); /* if switched mode, we handle the 36 first samples as long blocks. For 8000Hz, we handle the 72 first exponents as long blocks */ - if (s->sample_rate_index <= 2) + if (s->hdr.sample_rate_index <= 2) g->long_end = 8; else g->long_end = 6; @@ -370,7 +370,7 @@ static void imdct12(INTFLOAT *out, SUINTFLOAT *in) static int handle_crc(MPADecodeContext *s, int sec_len) { - if (s->error_protection && (s->err_recognition & AV_EF_CRCCHECK)) { + if (s->hdr.error_protection && (s->err_recognition & AV_EF_CRCCHECK)) { const uint8_t *buf = s->gb.buffer - HEADER_SIZE; int sec_byte_len = sec_len >> 3; int sec_rem_bits = sec_len & 7; @@ -402,18 +402,18 @@ static int mp_decode_layer1(MPADecodeContext *s) uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT]; int ret; - ret = handle_crc(s, (s->nb_channels == 1) ? 8*16 : 8*32); + ret = handle_crc(s, (s->hdr.nb_channels == 1) ? 8*16 : 8*32); if (ret < 0) return ret; - if (s->mode == MPA_JSTEREO) - bound = (s->mode_ext + 1) * 4; + if (s->hdr.mode == MPA_JSTEREO) + bound = (s->hdr.mode_ext + 1) * 4; else bound = SBLIMIT; /* allocation bits */ for (i = 0; i < bound; i++) { - for (ch = 0; ch < s->nb_channels; ch++) { + for (ch = 0; ch < s->hdr.nb_channels; ch++) { allocation[ch][i] = get_bits(&s->gb, 4); } } @@ -422,7 +422,7 @@ static int mp_decode_layer1(MPADecodeContext *s) /* scale factors */ for (i = 0; i < bound; i++) { - for (ch = 0; ch < s->nb_channels; ch++) { + for (ch = 0; ch < s->hdr.nb_channels; ch++) { if (allocation[ch][i]) scale_factors[ch][i] = get_bits(&s->gb, 6); } @@ -437,7 +437,7 @@ static int mp_decode_layer1(MPADecodeContext *s) /* compute samples */ for (j = 0; j < 12; j++) { for (i = 0; i < bound; i++) { - for (ch = 0; ch < s->nb_channels; ch++) { + for (ch = 0; ch < s->hdr.nb_channels; ch++) { n = allocation[ch][i]; if (n) { mant = get_bits(&s->gb, n + 1); @@ -477,13 +477,13 @@ static int mp_decode_layer2(MPADecodeContext *s) int ret; /* select decoding table */ - table = ff_mpa_l2_select_table(s->bit_rate / 1000, s->nb_channels, - s->sample_rate, s->lsf); + table = ff_mpa_l2_select_table(s->hdr.bit_rate / 1000, s->hdr.nb_channels, + s->hdr.sample_rate, s->hdr.lsf); sblimit = ff_mpa_sblimit_table[table]; alloc_table = ff_mpa_alloc_tables[table]; - if (s->mode == MPA_JSTEREO) - bound = (s->mode_ext + 1) * 4; + if (s->hdr.mode == MPA_JSTEREO) + bound = (s->hdr.mode_ext + 1) * 4; else bound = sblimit; @@ -497,7 +497,7 @@ static int mp_decode_layer2(MPADecodeContext *s) j = 0; for (i = 0; i < bound; i++) { bit_alloc_bits = alloc_table[j]; - for (ch = 0; ch < s->nb_channels; ch++) + for (ch = 0; ch < s->hdr.nb_channels; ch++) bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits); j += 1 << bit_alloc_bits; } @@ -511,7 +511,7 @@ static int mp_decode_layer2(MPADecodeContext *s) /* scale codes */ for (i = 0; i < sblimit; i++) { - for (ch = 0; ch < s->nb_channels; ch++) { + for (ch = 0; ch < s->hdr.nb_channels; ch++) { if (bit_alloc[ch][i]) scale_code[ch][i] = get_bits(&s->gb, 2); } @@ -523,7 +523,7 @@ static int mp_decode_layer2(MPADecodeContext *s) /* scale factors */ for (i = 0; i < sblimit; i++) { - for (ch = 0; ch < s->nb_channels; ch++) { + for (ch = 0; ch < s->hdr.nb_channels; ch++) { if (bit_alloc[ch][i]) { sf = scale_factors[ch][i]; switch (scale_code[ch][i]) { @@ -559,7 +559,7 @@ static int mp_decode_layer2(MPADecodeContext *s) j = 0; for (i = 0; i < bound; i++) { bit_alloc_bits = alloc_table[j]; - for (ch = 0; ch < s->nb_channels; ch++) { + for (ch = 0; ch < s->hdr.nb_channels; ch++) { b = bit_alloc[ch][i]; if (b) { scale = scale_factors[ch][i][k]; @@ -646,7 +646,7 @@ static int mp_decode_layer2(MPADecodeContext *s) } /* fill remaining samples to zero */ for (i = sblimit; i < SBLIMIT; i++) { - for (ch = 0; ch < s->nb_channels; ch++) { + for (ch = 0; ch < s->hdr.nb_channels; ch++) { s->sb_samples[ch][k * 12 + l + 0][i] = 0; s->sb_samples[ch][k * 12 + l + 1][i] = 0; s->sb_samples[ch][k * 12 + l + 2][i] = 0; @@ -697,7 +697,7 @@ static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g, gain = g->global_gain - 210; shift = g->scalefac_scale + 1; - bstab = ff_band_size_long[s->sample_rate_index]; + bstab = ff_band_size_long[s->hdr.sample_rate_index]; pretab = ff_mpa_pretab[g->preflag]; for (i = 0; i < g->long_end; i++) { v0 = gain - ((g->scale_factors[i] + pretab[i]) << shift) + 400; @@ -707,7 +707,7 @@ static void exponents_from_scale_factors(MPADecodeContext *s, GranuleDef *g, } if (g->short_start < 13) { - bstab = ff_band_size_short[s->sample_rate_index]; + bstab = ff_band_size_short[s->hdr.sample_rate_index]; gains[0] = gain - (g->subblock_gain[0] << 3); gains[1] = gain - (g->subblock_gain[1] << 3); gains[2] = gain - (g->subblock_gain[2] << 3); @@ -916,7 +916,7 @@ static void reorder_block(MPADecodeContext *s, GranuleDef *g) return; if (g->switch_point) { - if (s->sample_rate_index != 8) + if (s->hdr.sample_rate_index != 8) ptr = g->sb_hybrid + 36; else ptr = g->sb_hybrid + 72; @@ -925,7 +925,7 @@ static void reorder_block(MPADecodeContext *s, GranuleDef *g) } for (i = g->short_start; i < 13; i++) { - len = ff_band_size_short[s->sample_rate_index][i]; + len = ff_band_size_short[s->hdr.sample_rate_index][i]; ptr1 = ptr; dst = tmp; for (j = len; j > 0; j--) { @@ -951,8 +951,8 @@ static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1) int non_zero_found_short[3]; /* intensity stereo */ - if (s->mode_ext & MODE_EXT_I_STEREO) { - if (!s->lsf) { + if (s->hdr.mode_ext & MODE_EXT_I_STEREO) { + if (!s->hdr.lsf) { is_tab = is_table; sf_max = 7; } else { @@ -971,7 +971,7 @@ static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1) /* for last band, use previous scale factor */ if (i != 11) k -= 3; - len = ff_band_size_short[s->sample_rate_index][i]; + len = ff_band_size_short[s->hdr.sample_rate_index][i]; for (l = 2; l >= 0; l--) { tab0 -= len; tab1 -= len; @@ -996,7 +996,7 @@ static void compute_stereo(MPADecodeContext *s, GranuleDef *g0, GranuleDef *g1) } } else { found1: - if (s->mode_ext & MODE_EXT_MS_STEREO) { + if (s->hdr.mode_ext & MODE_EXT_MS_STEREO) { /* lower part of the spectrum : do ms stereo if enabled */ for (j = 0; j < len; j++) { @@ -1015,7 +1015,7 @@ found1: non_zero_found_short[2]; for (i = g1->long_end - 1;i >= 0;i--) { - len = ff_band_size_long[s->sample_rate_index][i]; + len = ff_band_size_long[s->hdr.sample_rate_index][i]; tab0 -= len; tab1 -= len; /* test if non zero band. if so, stop doing i-stereo */ @@ -1040,7 +1040,7 @@ found1: } } else { found2: - if (s->mode_ext & MODE_EXT_MS_STEREO) { + if (s->hdr.mode_ext & MODE_EXT_MS_STEREO) { /* lower part of the spectrum : do ms stereo if enabled */ for (j = 0; j < len; j++) { @@ -1052,7 +1052,7 @@ found2: } } } - } else if (s->mode_ext & MODE_EXT_MS_STEREO) { + } else if (s->hdr.mode_ext & MODE_EXT_MS_STEREO) { /* ms stereo ONLY */ /* NOTE: the 1/sqrt(2) normalization factor is included in the global gain */ @@ -1219,20 +1219,20 @@ static int mp_decode_layer3(MPADecodeContext *s) int ret; /* read side info */ - if (s->lsf) { - ret = handle_crc(s, ((s->nb_channels == 1) ? 8*9 : 8*17)); + if (s->hdr.lsf) { + ret = handle_crc(s, ((s->hdr.nb_channels == 1) ? 8*9 : 8*17)); main_data_begin = get_bits(&s->gb, 8); - skip_bits(&s->gb, s->nb_channels); + skip_bits(&s->gb, s->hdr.nb_channels); nb_granules = 1; } else { - ret = handle_crc(s, ((s->nb_channels == 1) ? 8*17 : 8*32)); + ret = handle_crc(s, ((s->hdr.nb_channels == 1) ? 8*17 : 8*32)); main_data_begin = get_bits(&s->gb, 9); - if (s->nb_channels == 2) + if (s->hdr.nb_channels == 2) skip_bits(&s->gb, 3); else skip_bits(&s->gb, 5); nb_granules = 2; - for (ch = 0; ch < s->nb_channels; ch++) { + for (ch = 0; ch < s->hdr.nb_channels; ch++) { s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */ s->granules[ch][1].scfsi = get_bits(&s->gb, 4); } @@ -1241,7 +1241,7 @@ static int mp_decode_layer3(MPADecodeContext *s) return ret; for (gr = 0; gr < nb_granules; gr++) { - for (ch = 0; ch < s->nb_channels; ch++) { + for (ch = 0; ch < s->hdr.nb_channels; ch++) { ff_dlog(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch); g = &s->granules[ch][gr]; g->part2_3_length = get_bits(&s->gb, 12); @@ -1254,10 +1254,10 @@ static int mp_decode_layer3(MPADecodeContext *s) g->global_gain = get_bits(&s->gb, 8); /* if MS stereo only is selected, we precompute the 1/sqrt(2) renormalization factor */ - if ((s->mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) == + if ((s->hdr.mode_ext & (MODE_EXT_MS_STEREO | MODE_EXT_I_STEREO)) == MODE_EXT_MS_STEREO) g->global_gain -= 2; - if (s->lsf) + if (s->hdr.lsf) g->scalefac_compress = get_bits(&s->gb, 9); else g->scalefac_compress = get_bits(&s->gb, 4); @@ -1291,7 +1291,7 @@ static int mp_decode_layer3(MPADecodeContext *s) compute_band_indexes(s, g); g->preflag = 0; - if (!s->lsf) + if (!s->hdr.lsf) g->preflag = get_bits1(&s->gb); g->scalefac_scale = get_bits1(&s->gb); g->count1table_select = get_bits1(&s->gb); @@ -1315,7 +1315,7 @@ static int mp_decode_layer3(MPADecodeContext *s) init_get_bits(&s->gb, s->last_buf, (s->last_buf_size + s->extrasize) * 8); s->last_buf_size <<= 3; for (gr = 0; gr < nb_granules && (s->last_buf_size >> 3) < main_data_begin; gr++) { - for (ch = 0; ch < s->nb_channels; ch++) { + for (ch = 0; ch < s->hdr.nb_channels; ch++) { g = &s->granules[ch][gr]; s->last_buf_size += g->part2_3_length; memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid)); @@ -1337,11 +1337,11 @@ static int mp_decode_layer3(MPADecodeContext *s) } for (; gr < nb_granules; gr++) { - for (ch = 0; ch < s->nb_channels; ch++) { + for (ch = 0; ch < s->hdr.nb_channels; ch++) { g = &s->granules[ch][gr]; bits_pos = get_bits_count(&s->gb); - if (!s->lsf) { + if (!s->hdr.lsf) { uint8_t *sc; int slen, slen1, slen2; @@ -1402,7 +1402,7 @@ static int mp_decode_layer3(MPADecodeContext *s) tindex = 0; sf = g->scalefac_compress; - if ((s->mode_ext & MODE_EXT_I_STEREO) && ch == 1) { + if ((s->hdr.mode_ext & MODE_EXT_I_STEREO) && ch == 1) { /* intensity stereo case */ sf >>= 1; if (sf < 180) { @@ -1453,10 +1453,10 @@ static int mp_decode_layer3(MPADecodeContext *s) huffman_decode(s, g, exponents, bits_pos + g->part2_3_length); } /* ch */ - if (s->mode == MPA_JSTEREO) + if (s->hdr.mode == MPA_JSTEREO) compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]); - for (ch = 0; ch < s->nb_channels; ch++) { + for (ch = 0; ch < s->hdr.nb_channels; ch++) { g = &s->granules[ch][gr]; reorder_block(s, g); @@ -1476,10 +1476,10 @@ static int mp_decode_frame(MPADecodeContext *s, OUT_INT **samples, OUT_INT *samples_ptr; init_get_bits(&s->gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8); - if (s->error_protection) + if (s->hdr.error_protection) s->crc = get_bits(&s->gb, 16); - switch(s->layer) { + switch(s->hdr.layer) { case 1: s->avctx->frame_size = 384; nb_frames = mp_decode_layer1(s); @@ -1489,7 +1489,7 @@ static int mp_decode_frame(MPADecodeContext *s, OUT_INT **samples, nb_frames = mp_decode_layer2(s); break; case 3: - s->avctx->frame_size = s->lsf ? 576 : 1152; + s->avctx->frame_size = s->hdr.lsf ? 576 : 1152; av_fallthrough; default: nb_frames = mp_decode_layer3(s); @@ -1534,14 +1534,14 @@ static int mp_decode_frame(MPADecodeContext *s, OUT_INT **samples, } /* apply the synthesis filter */ - for (ch = 0; ch < s->nb_channels; ch++) { + for (ch = 0; ch < s->hdr.nb_channels; ch++) { int sample_stride; if (s->avctx->sample_fmt == OUT_FMT_P) { samples_ptr = samples[ch]; sample_stride = 1; } else { samples_ptr = samples[0] + ch; - sample_stride = s->nb_channels; + sample_stride = s->hdr.nb_channels; } for (i = 0; i < nb_frames; i++) { RENAME(ff_mpa_synth_filter)(&s->mpadsp, s->synth_buf[ch], @@ -1553,7 +1553,7 @@ static int mp_decode_frame(MPADecodeContext *s, OUT_INT **samples, } } - return nb_frames * 32 * sizeof(OUT_INT) * s->nb_channels; + return nb_frames * 32 * sizeof(OUT_INT) * s->hdr.nb_channels; } static int decode_frame(AVCodecContext *avctx, AVFrame *frame, @@ -1580,28 +1580,28 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame, av_log(avctx, AV_LOG_DEBUG, "discarding ID3 tag\n"); return buf_size + skipped; } - ret = avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header); + ret = ff_mpegaudio_decode_header(&s->hdr, header); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Header missing\n"); return AVERROR_INVALIDDATA; } else if (ret == 1) { /* free format: prepare to compute frame size */ - s->frame_size = -1; + s->hdr.frame_size = -1; return AVERROR_INVALIDDATA; } /* update codec info */ av_channel_layout_uninit(&avctx->ch_layout); - avctx->ch_layout = s->nb_channels == 1 ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO : + avctx->ch_layout = s->hdr.nb_channels == 1 ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO : (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; if (!avctx->bit_rate) - avctx->bit_rate = s->bit_rate; + avctx->bit_rate = s->hdr.bit_rate; - if (s->frame_size <= 0) { + if (s->hdr.frame_size <= 0) { av_log(avctx, AV_LOG_ERROR, "incomplete frame\n"); return AVERROR_INVALIDDATA; - } else if (s->frame_size < buf_size) { + } else if (s->hdr.frame_size < buf_size) { av_log(avctx, AV_LOG_DEBUG, "incorrect frame size - multiple frames in buffer?\n"); - buf_size= s->frame_size; + buf_size= s->hdr.frame_size; } s->frame = frame; @@ -1611,7 +1611,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame, s->frame->nb_samples = avctx->frame_size; *got_frame_ptr = 1; if (avctx->codec_id != AV_CODEC_ID_AHX) - avctx->sample_rate = s->sample_rate; + avctx->sample_rate = s->hdr.sample_rate; //FIXME maybe move the other codec info stuff from above here too } else { av_log(avctx, AV_LOG_ERROR, "Error while decoding MPEG audio frame.\n"); @@ -1624,7 +1624,7 @@ static int decode_frame(AVCodecContext *avctx, AVFrame *frame, if (buf_size == avpkt->size || ret != AVERROR_INVALIDDATA) return ret; } - s->frame_size = 0; + s->hdr.frame_size = 0; return buf_size + skipped; } @@ -1666,20 +1666,20 @@ static int decode_frame_adu(AVCodecContext *avctx, AVFrame *frame, // Get header and restore sync word header = AV_RB32(buf) | 0xffe00000; - ret = avpriv_mpegaudio_decode_header((MPADecodeHeader *)s, header); + ret = ff_mpegaudio_decode_header(&s->hdr, header); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Invalid frame header\n"); return ret; } /* update codec info */ - avctx->sample_rate = s->sample_rate; + avctx->sample_rate = s->hdr.sample_rate; av_channel_layout_uninit(&avctx->ch_layout); - avctx->ch_layout = s->nb_channels == 1 ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO : + avctx->ch_layout = s->hdr.nb_channels == 1 ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO : (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; if (!avctx->bit_rate) - avctx->bit_rate = s->bit_rate; + avctx->bit_rate = s->hdr.bit_rate; - s->frame_size = len; + s->hdr.frame_size = len; s->frame = frame; @@ -1852,37 +1852,37 @@ static int decode_frame_mp3on4(AVCodecContext *avctx, AVFrame *frame, } header = (AV_RB32(buf) & 0x000fffff) | s->syncword; // patch header - ret = avpriv_mpegaudio_decode_header((MPADecodeHeader *)m, header); + ret = ff_mpegaudio_decode_header(&m->hdr, header); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "Bad header, discard block\n"); return AVERROR_INVALIDDATA; } - if (ch + m->nb_channels > avctx->ch_layout.nb_channels || - s->coff[fr] + m->nb_channels > avctx->ch_layout.nb_channels) { + if (ch + m->hdr.nb_channels > avctx->ch_layout.nb_channels || + s->coff[fr] + m->hdr.nb_channels > avctx->ch_layout.nb_channels) { av_log(avctx, AV_LOG_ERROR, "frame channel count exceeds codec " "channel count\n"); return AVERROR_INVALIDDATA; } - ch += m->nb_channels; + ch += m->hdr.nb_channels; outptr[0] = out_samples[s->coff[fr]]; - if (m->nb_channels > 1) + if (m->hdr.nb_channels > 1) outptr[1] = out_samples[s->coff[fr] + 1]; if ((ret = mp_decode_frame(m, outptr, buf, fsize)) < 0) { av_log(avctx, AV_LOG_ERROR, "failed to decode channel %d\n", ch); memset(outptr[0], 0, MPA_FRAME_SIZE*sizeof(OUT_INT)); - if (m->nb_channels > 1) + if (m->hdr.nb_channels > 1) memset(outptr[1], 0, MPA_FRAME_SIZE*sizeof(OUT_INT)); - ret = m->nb_channels * MPA_FRAME_SIZE*sizeof(OUT_INT); + ret = m->hdr.nb_channels * MPA_FRAME_SIZE*sizeof(OUT_INT); } out_size += ret; buf += fsize; len -= fsize; - avctx->bit_rate += m->bit_rate; + avctx->bit_rate += m->hdr.bit_rate; } if (ch != avctx->ch_layout.nb_channels) { av_log(avctx, AV_LOG_ERROR, "failed to decode all channels\n"); @@ -1890,7 +1890,7 @@ static int decode_frame_mp3on4(AVCodecContext *avctx, AVFrame *frame, } /* update codec info */ - avctx->sample_rate = s->mp3decctx[0]->sample_rate; + avctx->sample_rate = s->mp3decctx[0]->hdr.sample_rate; frame->nb_samples = out_size / (avctx->ch_layout.nb_channels * sizeof(OUT_INT)); *got_frame_ptr = 1; diff --git a/libavcodec/mpegaudiodecheader.c b/libavcodec/mpegaudiodecheader.c index b8bf0ae530..babda0eb64 100644 --- a/libavcodec/mpegaudiodecheader.c +++ b/libavcodec/mpegaudiodecheader.c @@ -24,6 +24,8 @@ * MPEG Audio header decoder. */ +#include <string.h> + #include "libavutil/error.h" #include "libavutil/macros.h" #include "libavutil/mem.h" @@ -43,6 +45,8 @@ int ff_mpegaudio_decode_header(MPADecodeHeader2 *s, uint32_t header) if (ret < 0) return ret; + memset(s, 0, sizeof(*s)); + if (header & (1<<20)) { s->lsf = (header & (1<<19)) ? 0 : 1; mpeg25 = 0; @@ -143,6 +147,7 @@ int avpriv_mpegaudio_decode_header2(MPADecodeHeader2 **phdr, uint32_t header) return ret; } +#if FF_MPEGAUDIO_LEGACY_DECODE_HEADER int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header) { MPADecodeHeader2 hdr = { 0 }; @@ -166,6 +171,7 @@ int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header) return ret; } +#endif int ff_mpa_decode_header(uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate, enum AVCodecID *codec_id) { diff --git a/libavcodec/mpegaudiodecheader.h b/libavcodec/mpegaudiodecheader.h index f45dfbdc8f..5cb3cdfe6f 100644 --- a/libavcodec/mpegaudiodecheader.h +++ b/libavcodec/mpegaudiodecheader.h @@ -36,6 +36,9 @@ #define MP3_MASK 0xFFFE0CCF +#define FF_MPEGAUDIO_LEGACY_DECODE_HEADER (LIBAVCODEC_VERSION_MAJOR < 64) + +#if FF_MPEGAUDIO_LEGACY_DECODE_HEADER #define MPA_DECODE_HEADER \ int frame_size; \ int error_protection; \ @@ -52,6 +55,12 @@ typedef struct MPADecodeHeader { MPA_DECODE_HEADER } MPADecodeHeader; +/* header decoding. MUST check the header before because no + consistency check is done there. Return 1 if free format found and + that the frame size must be computed externally */ +int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header); +#endif + /** * Same as MPADecodeHeader, plus the header fields that carry no decoding * information. Its size is not part of the libavcodec ABI: it is only ever @@ -76,11 +85,6 @@ typedef struct MPADecodeHeader2 { int private_bit; } MPADecodeHeader2; -/* header decoding. MUST check the header before because no - consistency check is done there. Return 1 if free format found and - that the frame size must be computed externally */ -int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header); - /** * Decode an MPEG audio header into *phdr, which is allocated if it is NULL. * diff --git a/libavformat/mp3dec.c b/libavformat/mp3dec.c index c3c0352dc8..d326b9947f 100644 --- a/libavformat/mp3dec.c +++ b/libavformat/mp3dec.c @@ -25,6 +25,7 @@ #include "libavutil/intreadwrite.h" #include "libavutil/dict.h" #include "libavutil/mathematics.h" +#include "libavutil/mem.h" #include "avformat.h" #include "internal.h" #include "avio_internal.h" @@ -75,6 +76,7 @@ static int mp3_read_probe(const AVProbeData *p) int framesizes, max_framesizes; uint32_t header; const uint8_t *buf, *buf0, *buf2, *buf3, *end; + MPADecodeHeader2 *h = NULL; buf0 = p->buf; end = p->buf + p->buf_size - sizeof(uint32_t); @@ -88,28 +90,27 @@ static int mp3_read_probe(const AVProbeData *p) for (; buf < end; buf = buf2+1) { buf2 = buf; for (framesizes = frames = 0; buf2 < end; frames++) { - MPADecodeHeader h; int header_emu = 0; int available; header = AV_RB32(buf2); - ret = avpriv_mpegaudio_decode_header(&h, header); + ret = avpriv_mpegaudio_decode_header2(&h, header); if (ret != 0) break; - available = FFMIN(h.frame_size, end - buf2); + available = FFMIN(h->frame_size, end - buf2); for (buf3 = buf2 + 4; buf3 < buf2 + available; buf3++) { uint32_t next_sync = AV_RB32(buf3); header_emu += (next_sync & MP3_MASK) == (header & MP3_MASK); } if (header_emu > 2) break; - framesizes += h.frame_size; - if (available < h.frame_size) { + framesizes += h->frame_size; + if (available < h->frame_size) { frames++; break; } - buf2 += h.frame_size; + buf2 += h->frame_size; } max_frames = FFMAX(max_frames, frames); max_framesizes = FFMAX(max_framesizes, framesizes); @@ -119,6 +120,8 @@ static int mp3_read_probe(const AVProbeData *p) whole_used = 1; } } + av_freep(&h); + // keep this in sync with ac3 probe, both need to avoid // issues with MPEG-files! if (first_frames>=7) return AVPROBE_SCORE_EXTENSION + 2; @@ -159,7 +162,7 @@ static void read_xing_toc(AVFormatContext *s, int64_t filesize, int64_t duration } static void mp3_parse_info_tag(AVFormatContext *s, AVStream *st, - MPADecodeHeader *c, uint32_t spf) + MPADecodeHeader2 *c, uint32_t spf) { #define LAST_BITS(k, n) ((k) & ((1 << (n)) - 1)) #define MIDDLE_BITS(k, m, n) LAST_BITS((k) >> (m), ((n) - (m) + 1)) @@ -343,7 +346,7 @@ static const AVDictionaryEntry *find_itunes_smpb(const AVDictionary *metadata) * comment instead of in the LAME extension of the Xing tag. */ static void mp3_parse_itunes_smpb(AVFormatContext *s, AVStream *st, - const MPADecodeHeader *c, uint32_t spf) + const MPADecodeHeader2 *c, uint32_t spf) { FFStream *const sti = ffstream(st); MP3DecContext *mp3 = s->priv_data; @@ -383,7 +386,7 @@ static void mp3_parse_itunes_smpb(AVFormatContext *s, AVStream *st, static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base) { uint32_t v, spf; - MPADecodeHeader c; + MPADecodeHeader2 *c = NULL; int vbrtag_size = 0; MP3DecContext *mp3 = s->priv_data; int ret; @@ -392,32 +395,36 @@ static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base) v = avio_rb32(s->pb); - ret = avpriv_mpegaudio_decode_header(&c, v); + ret = avpriv_mpegaudio_decode_header2(&c, v); if (ret < 0) - return ret; + goto end; else if (ret == 0) - vbrtag_size = c.frame_size; - if (c.layer != 3) - return -1; + vbrtag_size = c->frame_size; + if (c->layer != 3) { + ret = -1; + goto end; + } - spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */ + spf = c->lsf ? 576 : 1152; /* Samples per frame, layer 3 */ mp3->frames = 0; mp3->header_filesize = 0; - mp3->frame_duration = av_rescale_q(spf, (AVRational){1, c.sample_rate}, st->time_base); + mp3->frame_duration = av_rescale_q(spf, (AVRational){1, c->sample_rate}, st->time_base); - mp3_parse_info_tag(s, st, &c, spf); + mp3_parse_info_tag(s, st, c, spf); mp3_parse_vbri_tag(s, st, base); - mp3_parse_itunes_smpb(s, st, &c, spf); + mp3_parse_itunes_smpb(s, st, c, spf); /* Packets keep the skipped samples, so shift the timeline instead. */ if (ffstream(st)->start_skip_samples) st->start_time = av_rescale_q(ffstream(st)->start_skip_samples, - (AVRational){1, c.sample_rate}, + (AVRational){1, c->sample_rate}, st->time_base); - if (!mp3->frames && !mp3->header_filesize) - return -1; + if (!mp3->frames && !mp3->header_filesize) { + ret = -1; + goto end; + } /* Skip the vbr tag frame */ avio_seek(s->pb, base + vbrtag_size, SEEK_SET); @@ -428,15 +435,19 @@ static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base) full_duration_samples = mp3->frames * (int64_t)spf; if (st->duration == AV_NOPTS_VALUE) st->duration = av_rescale_q(full_duration_samples - mp3->start_pad - mp3->end_pad, - (AVRational){1, c.sample_rate}, + (AVRational){1, c->sample_rate}, st->time_base); if (mp3->header_filesize && !mp3->is_cbr) - st->codecpar->bit_rate = av_rescale(mp3->header_filesize, 8 * c.sample_rate, + st->codecpar->bit_rate = av_rescale(mp3->header_filesize, 8 * c->sample_rate, full_duration_samples); } - return 0; + ret = 0; + +end: + av_freep(&c); + return ret; } static int mp3_read_header(AVFormatContext *s) @@ -549,7 +560,8 @@ static int check(AVIOContext *pb, int64_t pos, uint32_t *ret_header) int64_t ret = avio_seek(pb, pos, SEEK_SET); uint8_t header_buf[4]; unsigned header; - MPADecodeHeader sd; + MPADecodeHeader2 *sd = NULL; + int frame_size; if (ret < 0) return CHECK_SEEK_FAILED; @@ -561,12 +573,16 @@ static int check(AVIOContext *pb, int64_t pos, uint32_t *ret_header) header = AV_RB32(&header_buf[0]); if (ff_mpa_check_header(header) < 0) return CHECK_WRONG_HEADER; - if (avpriv_mpegaudio_decode_header(&sd, header) == 1) + if (avpriv_mpegaudio_decode_header2(&sd, header)) { + av_freep(&sd); return CHECK_WRONG_HEADER; + } + frame_size = sd->frame_size; + av_freep(&sd); if (ret_header) *ret_header = header; - return sd.frame_size; + return frame_size; } static int64_t mp3_sync(AVFormatContext *s, int64_t target_pos, int flags) diff --git a/libavformat/mp3enc.c b/libavformat/mp3enc.c index 6d42feb1ff..14e56c5738 100644 --- a/libavformat/mp3enc.c +++ b/libavformat/mp3enc.c @@ -126,6 +126,7 @@ typedef struct MP3Context { uint64_t bag[XING_NUM_BAGS]; int initial_bitrate; int has_variable_bitrate; + MPADecodeHeader2 *hdr; int delay; int64_t padding; @@ -150,7 +151,6 @@ static int mp3_write_xing(AVFormatContext *s) AVDictionaryEntry *enc = av_dict_get(s->streams[mp3->audio_stream_idx]->metadata, "encoder", NULL, 0); AVIOContext *dyn_ctx; int32_t header; - MPADecodeHeader mpah; int srate_idx, i, channels; int bitrate_idx; int best_bitrate_idx = -1; @@ -175,7 +175,7 @@ static int mp3_write_xing(AVFormatContext *s) } if (i == FF_ARRAY_ELEMS(ff_mpa_freq_tab)) { av_log(s, AV_LOG_WARNING, "Unsupported sample rate, not writing Xing header.\n"); - return -1; + return 0; } switch (par->ch_layout.nb_channels) { @@ -183,7 +183,7 @@ static int mp3_write_xing(AVFormatContext *s) case 2: channels = MPA_STEREO; break; default: av_log(s, AV_LOG_WARNING, "Unsupported number of channels, " "not writing Xing header.\n"); - return -1; + return 0; } /* dummy MPEG audio header */ @@ -206,15 +206,16 @@ static int mp3_write_xing(AVFormatContext *s) for (bitrate_idx = best_bitrate_idx; ; bitrate_idx++) { int32_t mask = bitrate_idx << (4 + 8); if (15 == bitrate_idx) - return -1; + return 0; header |= mask; - ret = avpriv_mpegaudio_decode_header(&mpah, header); - av_assert0(ret >= 0); - mp3->xing_offset = xing_offtbl[mpah.lsf == 1][mpah.nb_channels == 1] + 4; + ret = avpriv_mpegaudio_decode_header2(&mp3->hdr, header); + if (ret < 0) + return ret; + mp3->xing_offset = xing_offtbl[mp3->hdr->lsf == 1][mp3->hdr->nb_channels == 1] + 4; bytes_needed = mp3->xing_offset + XING_SIZE; - if (bytes_needed <= mpah.frame_size) + if (bytes_needed <= mp3->hdr->frame_size) break; header &= ~mask; @@ -230,7 +231,7 @@ static int mp3_write_xing(AVFormatContext *s) ffio_wfourcc(dyn_ctx, "Xing"); avio_wb32(dyn_ctx, 0x01 | 0x02 | 0x04 | 0x08); // frames / size / TOC / vbr scale - mp3->size = mpah.frame_size; + mp3->size = mp3->hdr->frame_size; mp3->want=1; mp3->seen=0; mp3->pos=0; @@ -275,7 +276,7 @@ static int mp3_write_xing(AVFormatContext *s) avio_wb16(dyn_ctx, 0); // music crc avio_wb16(dyn_ctx, 0); // tag crc - ffio_fill(dyn_ctx, 0, mpah.frame_size - bytes_needed); + ffio_fill(dyn_ctx, 0, mp3->hdr->frame_size - bytes_needed); mp3->xing_frame_size = avio_close_dyn_buf(dyn_ctx, &mp3->xing_frame); mp3->xing_frame_offset = avio_tell(s->pb); @@ -321,17 +322,18 @@ static int mp3_write_audio_packet(AVFormatContext *s, AVPacket *pkt) MP3Context *mp3 = s->priv_data; if (pkt->data && pkt->size >= 4) { - MPADecodeHeader mpah; int ret; uint32_t h; h = AV_RB32(pkt->data); - ret = avpriv_mpegaudio_decode_header(&mpah, h); + ret = avpriv_mpegaudio_decode_header2(&mp3->hdr, h); if (ret >= 0) { if (!mp3->initial_bitrate) - mp3->initial_bitrate = mpah.bit_rate; - if ((mpah.bit_rate == 0) || (mp3->initial_bitrate != mpah.bit_rate)) + mp3->initial_bitrate = mp3->hdr->bit_rate; + if ((mp3->hdr->bit_rate == 0) || (mp3->initial_bitrate != mp3->hdr->bit_rate)) mp3->has_variable_bitrate = 1; + } else if (ret == AVERROR(ENOMEM)) { + return ret; } else { av_log(s, AV_LOG_WARNING, "Audio packet of size %d (starting with %08"PRIX32"...) " "is invalid, writing it anyway.\n", pkt->size, h); @@ -339,7 +341,7 @@ static int mp3_write_audio_packet(AVFormatContext *s, AVPacket *pkt) #ifdef FILTER_VBR_HEADERS /* filter out XING and INFO headers. */ - int base = 4 + xing_offtbl[mpah.lsf == 1][mpah.nb_channels == 1]; + int base = 4 + xing_offtbl[mp3->hdr->lsf == 1][mp3->hdr->nb_channels == 1]; if (base + 4 <= pkt->size) { uint32_t v = AV_RB32(pkt->data + base); @@ -413,10 +415,10 @@ static int mp3_queue_flush(AVFormatContext *s) int ret = 0, write = 1; ret = mp3_finish_id3v2(s); + if (ret >= 0) + ret = mp3_write_xing(s); if (ret < 0) write = 0; - else - mp3_write_xing(s); while (mp3->queue.head) { ff_packet_list_get(&mp3->queue, pkt); @@ -678,7 +680,7 @@ static int mp3_write_header(struct AVFormatContext *s) if (!mp3->pics_to_write) { if (mp3->id3v2_version && (ret = mp3_finish_id3v2(s)) < 0) return ret; - mp3_write_xing(s); + return mp3_write_xing(s); } return 0; @@ -691,6 +693,7 @@ static void mp3_deinit(struct AVFormatContext *s) ff_packet_list_free(&mp3->queue); ffio_free_dyn_buf(&mp3->id3_pb); av_freep(&mp3->xing_frame); + av_freep(&mp3->hdr); } const FFOutputFormat ff_mp3_muxer = { -- To stop receiving notification emails like this one, please contact [email protected]. _______________________________________________ ffmpeg-cvslog mailing list -- [email protected] To unsubscribe send an email to [email protected]
