[PATCH] E-AC-3 spectral extension (bump)
Hi, in the sake of restarting the review process, I'm restarting a thread left here: http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/2009-October/077490.html The one complain remaining on the patch at that time was the following (sorry for the shoddy editing):
+ copy_sizes[num_copy_sections++] = bin - s->spx_copy_start_freq; + + for (ch = 1; ch <= s->fbw_channels; ch++) { + if (!s->channel_uses_spx[ch]) + continue; + + /* Copy coeffs from normal bands to extension bands */ + bin = s->spx_start_freq; + for (i = 0; i < num_copy_sections; i++) { + memcpy(&s->transform_coeffs[ch][bin], + &s->transform_coeffs[ch][s->spx_copy_start_freq], + copy_sizes[i]*sizeof(float)); + bin += copy_sizes[i]; + } + + /* Calculate RMS energy for each SPX band. */ + bin = s->spx_start_freq; + for (bnd = 0; bnd < s->num_spx_bands; bnd++) { + int bandsize = s->spx_band_sizes[bnd]; + float accum = 0.0f; + for (i = 0; i < bandsize; i++) { + float coeff = s->transform_coeffs[ch][bin++]; + accum += coeff * coeff; + } + rms_energy[bnd] = sqrtf(accum / bandsize); + }
if i understand the code correctly, the same source coefficients can be copied to several destinations, thus it would be more efficient to not calculate this for all destination but rather for the source and copy as needed to destination bands
The RMS computations occur on strictly non-overlapping samples from band to band, and always on the SPX bands, never on the original source: s->spx_copy_start_freq+sum(copy_sizes[i]) <= s->spx_start_freq If one would output some info over the 2 samples available at: http://samples.mplayerhq.hu/A-codecs/AC3/eac3/ one would see for the 5.1 sample: Channel 1: Copying 72 elements to bin 109 from bin 25 RMS for band 0 on 109-121 elements RMS for band 1 on 121-133 elements RMS for band 2 on 133-145 elements RMS for band 3 on 145-157 elements RMS for band 4 on 157-181 elements (and so on for each channel and call to that function) For the stereo sample: Channel 1: Copying 24 elements to bin 133 from bin 25 RMS for band 0 on 133-145 elements RMS for band 1 on 145-157 elements (repeat for each channel and call) So: - no computation is remade - the number of samples for each iteration is for the available samples 12 and 24 - each band may start on an unaligned position (for SSE code at least), so scalarproduct & co functions can't be reused directly - ff_eac3_apply_spectral_extension is 0.5% of execution time at best (from a oprofile log) Attached is the patch rediff'ed against current SVN. I declare myself incompetent for more theoretical discussions, though. Best regards, Christophe
On Fri, Mar 19, 2010 at 01:03:16PM +0100, Christophe Gisquet wrote:
Hi,
in the sake of restarting the review process, I'm restarting a thread left here: http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/2009-October/077490.html
The one complain remaining on the patch at that time was the following (sorry for the shoddy editing):
+ copy_sizes[num_copy_sections++] = bin - s->spx_copy_start_freq; + + for (ch = 1; ch <= s->fbw_channels; ch++) { + if (!s->channel_uses_spx[ch]) + continue; + + /* Copy coeffs from normal bands to extension bands */ + bin = s->spx_start_freq; + for (i = 0; i < num_copy_sections; i++) { + memcpy(&s->transform_coeffs[ch][bin], + &s->transform_coeffs[ch][s->spx_copy_start_freq], + copy_sizes[i]*sizeof(float)); + bin += copy_sizes[i]; + } + + /* Calculate RMS energy for each SPX band. */ + bin = s->spx_start_freq; + for (bnd = 0; bnd < s->num_spx_bands; bnd++) { + int bandsize = s->spx_band_sizes[bnd]; + float accum = 0.0f; + for (i = 0; i < bandsize; i++) { + float coeff = s->transform_coeffs[ch][bin++]; + accum += coeff * coeff; + } + rms_energy[bnd] = sqrtf(accum / bandsize); + }
if i understand the code correctly, the same source coefficients can be copied to several destinations, thus it would be more efficient to not calculate this for all destination but rather for the source and copy as needed to destination bands
The RMS computations occur on strictly non-overlapping samples from band to band, and always on the SPX bands, never on the original source: s->spx_copy_start_freq+sum(copy_sizes[i]) <= s->spx_start_freq
If one would output some info over the 2 samples available at: http://samples.mplayerhq.hu/A-codecs/AC3/eac3/ one would see for the 5.1 sample: Channel 1: Copying 72 elements to bin 109 from bin 25 RMS for band 0 on 109-121 elements RMS for band 1 on 121-133 elements RMS for band 2 on 133-145 elements RMS for band 3 on 145-157 elements RMS for band 4 on 157-181 elements (and so on for each channel and call to that function) For the stereo sample: Channel 1: Copying 24 elements to bin 133 from bin 25 RMS for band 0 on 133-145 elements RMS for band 1 on 145-157 elements (repeat for each channel and call)
So: - no computation is remade
this is not neccessarily so. Actually ill bet its very very hard to proof the lack of redundant computations even for much simpler algorihms
- the number of samples for each iteration is for the available samples 12 and 24
- each band may start on an unaligned position (for SSE code at least), so scalarproduct & co functions can't be reused directly
you bring up an interresting point, do these sometimes or always start at an unaligned address?, if its always x*12*sizeof(float)+1 we can probably do something about it
- ff_eac3_apply_spectral_extension is 0.5% of execution time at best (from a oprofile log)
Attached is the patch rediff'ed against current SVN. I declare myself incompetent for more theoretical discussions, though.
iam incompetent as well, but you seem interrested. Maybe we can together resolve this, because one or both of us is not understanding the code completely and its quite complicated code though luckily much cleaner than our aac code. for example:
+ /* Copy coeffs from normal bands to extension bands */ + bin = s->spx_start_freq; + for (i = 0; i < num_copy_sections; i++) { + memcpy(&s->transform_coeffs[ch][bin], + &s->transform_coeffs[ch][s->spx_copy_start_freq], + copy_sizes[i]*sizeof(float)); + bin += copy_sizes[i]; + }
this clearly copies the same area several times if num_copy_sections is greater than 1. Can it be greater than 1? or to ask differently does this happen for the samples we have? if its guranteed to be ==1 the code can be simplified alot. if not, how representative are our samples? are these random recordings or from some reference/conformance suite? now if its copied 2 or more times i think the rms calculation is done several times over the same data it maybe just 0.5% for the samples we have but first it feels wrong if we do calculations redundantly and second it maybe more than 0.5% for other samples more comments below [...]
@@ -859,9 +949,9 @@ s->phase_flags_in_use = get_bits1(gbc);
/* coupling frequency range */ - /* TODO: modify coupling end freq if spectral extension is used */ cpl_start_subband = get_bits(gbc, 4); - cpl_end_subband = get_bits(gbc, 4) + 3; + cpl_end_subband = s->spx_in_use ? (s->spx_start_freq - 37) / 12 :
s->spx_start_freq/12 - 3 [...]
@@ -43,6 +66,7 @@ #define AC3_MAX_COEFS 256 #define AC3_BLOCK_SIZE 256 #define MAX_BLOCKS 6 +#define SPX_MAX_BANDS 17
typedef struct { AVCodecContext *avctx; ///< parent context @@ -89,6 +113,22 @@ int cpl_coords[AC3_MAX_CHANNELS][18]; ///< coupling coordinates (cplco) ///@}
+///@defgroup spx spectral extension +///@{ + int spx_in_use; ///< spectral extension in use (spxinu) + uint8_t channel_uses_spx[AC3_MAX_CHANNELS]; ///< channel uses spectral extension (chinspx) + int8_t spx_atten_code[AC3_MAX_CHANNELS]; ///< spx attenuation code (spxattencod)
+ int spx_start_freq; ///< spx start frequency bin + int spx_end_freq; ///< spx end frequency bin + int spx_copy_start_freq; ///< spx starting frequency bin for copying (copystartmant) + ///< the copy region ends at the start of the spx region.
start/end/copy_start are not ideal names, they should contain "src"/"dst" so its clear from where to where things are copied [...]
@@ -66,7 +62,97 @@ } EAC3GaqMode;
#define EAC3_SR_CODE_REDUCED 3 +#undef printf
ehm [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB The educated differ from the uneducated as much as the living from the dead. -- Aristotle
Michael Niedermayer wrote:
On Fri, Mar 19, 2010 at 01:03:16PM +0100, Christophe Gisquet wrote:
Hi,
in the sake of restarting the review process, I'm restarting a thread left here: http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/2009-October/077490.html
The one complain remaining on the patch at that time was the following (sorry for the shoddy editing):
+ copy_sizes[num_copy_sections++] = bin - s->spx_copy_start_freq; + + for (ch = 1; ch <= s->fbw_channels; ch++) { + if (!s->channel_uses_spx[ch]) + continue; + + /* Copy coeffs from normal bands to extension bands */ + bin = s->spx_start_freq; + for (i = 0; i < num_copy_sections; i++) { + memcpy(&s->transform_coeffs[ch][bin], + &s->transform_coeffs[ch][s->spx_copy_start_freq], + copy_sizes[i]*sizeof(float)); + bin += copy_sizes[i]; + } + + /* Calculate RMS energy for each SPX band. */ + bin = s->spx_start_freq; + for (bnd = 0; bnd < s->num_spx_bands; bnd++) { + int bandsize = s->spx_band_sizes[bnd]; + float accum = 0.0f; + for (i = 0; i < bandsize; i++) { + float coeff = s->transform_coeffs[ch][bin++]; + accum += coeff * coeff; + } + rms_energy[bnd] = sqrtf(accum / bandsize); + } if i understand the code correctly, the same source coefficients can be copied to several destinations, thus it would be more efficient to not calculate this for all destination but rather for the source and copy as needed to destination bands The RMS computations occur on strictly non-overlapping samples from band to band, and always on the SPX bands, never on the original source: s->spx_copy_start_freq+sum(copy_sizes[i]) <= s->spx_start_freq
If one would output some info over the 2 samples available at: http://samples.mplayerhq.hu/A-codecs/AC3/eac3/ one would see for the 5.1 sample: Channel 1: Copying 72 elements to bin 109 from bin 25 RMS for band 0 on 109-121 elements RMS for band 1 on 121-133 elements RMS for band 2 on 133-145 elements RMS for band 3 on 145-157 elements RMS for band 4 on 157-181 elements (and so on for each channel and call to that function) For the stereo sample: Channel 1: Copying 24 elements to bin 133 from bin 25 RMS for band 0 on 133-145 elements RMS for band 1 on 145-157 elements (repeat for each channel and call)
So: - no computation is remade
this is not neccessarily so. Actually ill bet its very very hard to proof the lack of redundant computations even for much simpler algorihms
- the number of samples for each iteration is for the available samples 12 and 24
- each band may start on an unaligned position (for SSE code at least), so scalarproduct & co functions can't be reused directly
you bring up an interresting point, do these sometimes or always start at an unaligned address?, if its always x*12*sizeof(float)+1 we can probably do something about it
Yes, it will always be that. The spec says "Transform coefficients #25 through #228 are grouped into 17 sub-bands of 12 coefficients each."
- ff_eac3_apply_spectral_extension is 0.5% of execution time at best (from a oprofile log)
Attached is the patch rediff'ed against current SVN. I declare myself incompetent for more theoretical discussions, though.
iam incompetent as well, but you seem interrested. Maybe we can together resolve this, because one or both of us is not understanding the code completely and its quite complicated code though luckily much cleaner than our aac code.
for example:
+ /* Copy coeffs from normal bands to extension bands */ + bin = s->spx_start_freq; + for (i = 0; i < num_copy_sections; i++) { + memcpy(&s->transform_coeffs[ch][bin], + &s->transform_coeffs[ch][s->spx_copy_start_freq], + copy_sizes[i]*sizeof(float)); + bin += copy_sizes[i]; + }
this clearly copies the same area several times if num_copy_sections is greater than 1. Can it be greater than 1? or to ask differently does this happen for the samples we have? if its guranteed to be ==1 the code can be simplified alot. if not, how representative are our samples? are these random recordings or from some reference/conformance suite?
now if its copied 2 or more times i think the rms calculation is done several times over the same data it maybe just 0.5% for the samples we have but first it feels wrong if we do calculations redundantly and second it maybe more than 0.5% for other samples
num_copy_sections can definitely be >1. That is the whole point of it. It might not be so with our current samples, but the spec is clear about how this is done. The copy region can be smaller than the spx region, and in that case you have to wrap back around to the beginning of the copy region to keep copying coefficients to the spx region, hence the wrap flags and notch filter. Also, each band has multiple sub-bands of 12 coeffs each. The copying is done in multiples of 12, but the wrapping can occur in the middle of a band. If we want to reduce calculations, we could accumulate sum of squared coeffs for each sub-band, then add them together for each band before dividing by bandsize and taking the square root.
[...]
@@ -43,6 +66,7 @@ #define AC3_MAX_COEFS 256 #define AC3_BLOCK_SIZE 256 #define MAX_BLOCKS 6 +#define SPX_MAX_BANDS 17
typedef struct { AVCodecContext *avctx; ///< parent context @@ -89,6 +113,22 @@ int cpl_coords[AC3_MAX_CHANNELS][18]; ///< coupling coordinates (cplco) ///@}
+///@defgroup spx spectral extension +///@{ + int spx_in_use; ///< spectral extension in use (spxinu) + uint8_t channel_uses_spx[AC3_MAX_CHANNELS]; ///< channel uses spectral extension (chinspx) + int8_t spx_atten_code[AC3_MAX_CHANNELS]; ///< spx attenuation code (spxattencod)
+ int spx_start_freq; ///< spx start frequency bin + int spx_end_freq; ///< spx end frequency bin + int spx_copy_start_freq; ///< spx starting frequency bin for copying (copystartmant) + ///< the copy region ends at the start of the spx region.
start/end/copy_start are not ideal names, they should contain "src"/"dst" so its clear from where to where things are copied
spx_src_start_freq, spx_dst_start_freq, spx_dst_end_freq ? -Justin
2010/3/20 Justin Ruggles <justin.ruggles at gmail.com>:
Also, each band has multiple sub-bands of 12 coeffs each. ?The copying is done in multiples of 12, but the wrapping can occur in the middle of a band. ?If we want to reduce calculations, we could accumulate sum of squared coeffs for each sub-band, then add them together for each band before dividing by bandsize and taking the square root.
So, the pseudo-code would be: for (k=0; k<max(copy_size[i]); k+=12) compute sum of square for 12 coeffs starting at s->spx_copy_start_freq+k store it in some kind of array Then actual RMS computation: for each SPX band: determine the subband(s) used, read the corresponding sum of square value(s), sum them and do the norm+sqrt The more complex part is now "determine subbands". Probably not very complex but I'll stay there for tonight.
Christophe Gisquet wrote:
2010/3/20 Justin Ruggles <justin.ruggles at gmail.com>:
Also, each band has multiple sub-bands of 12 coeffs each. The copying is done in multiples of 12, but the wrapping can occur in the middle of a band. If we want to reduce calculations, we could accumulate sum of squared coeffs for each sub-band, then add them together for each band before dividing by bandsize and taking the square root.
So, the pseudo-code would be: for (k=0; k<max(copy_size[i]); k+=12) compute sum of square for 12 coeffs starting at s->spx_copy_start_freq+k store it in some kind of array
Then actual RMS computation: for each SPX band: determine the subband(s) used, read the corresponding sum of square value(s), sum them and do the norm+sqrt
The more complex part is now "determine subbands". Probably not very complex but I'll stay there for tonight.
Determining source subbands for each band is done in the code above the RMS computation (except that there seems to be a bug I didn't see before where it uses 'copysize' before it is initialized). The algorithm is pretty simple though. You either start copying a band where the last band left off (if there is enough left in the copy region to copy the whole band) or you start over at the beginning of the copy region. It only gets a little complex if a band is larger than the full copy region (copysize < bandsize) since that is the only time you wrap in the middle of a band. -Justin
On Sat, Mar 20, 2010 at 09:13:36AM -0400, Justin Ruggles wrote:
Michael Niedermayer wrote:
On Fri, Mar 19, 2010 at 01:03:16PM +0100, Christophe Gisquet wrote:
Hi,
in the sake of restarting the review process, I'm restarting a thread left here: http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/2009-October/077490.html
The one complain remaining on the patch at that time was the following (sorry for the shoddy editing):
+ copy_sizes[num_copy_sections++] = bin - s->spx_copy_start_freq; + + for (ch = 1; ch <= s->fbw_channels; ch++) { + if (!s->channel_uses_spx[ch]) + continue; + + /* Copy coeffs from normal bands to extension bands */ + bin = s->spx_start_freq; + for (i = 0; i < num_copy_sections; i++) { + memcpy(&s->transform_coeffs[ch][bin], + &s->transform_coeffs[ch][s->spx_copy_start_freq], + copy_sizes[i]*sizeof(float)); + bin += copy_sizes[i]; + } + + /* Calculate RMS energy for each SPX band. */ + bin = s->spx_start_freq; + for (bnd = 0; bnd < s->num_spx_bands; bnd++) { + int bandsize = s->spx_band_sizes[bnd]; + float accum = 0.0f; + for (i = 0; i < bandsize; i++) { + float coeff = s->transform_coeffs[ch][bin++]; + accum += coeff * coeff; + } + rms_energy[bnd] = sqrtf(accum / bandsize); + } if i understand the code correctly, the same source coefficients can be copied to several destinations, thus it would be more efficient to not calculate this for all destination but rather for the source and copy as needed to destination bands The RMS computations occur on strictly non-overlapping samples from band to band, and always on the SPX bands, never on the original source: s->spx_copy_start_freq+sum(copy_sizes[i]) <= s->spx_start_freq
If one would output some info over the 2 samples available at: http://samples.mplayerhq.hu/A-codecs/AC3/eac3/ one would see for the 5.1 sample: Channel 1: Copying 72 elements to bin 109 from bin 25 RMS for band 0 on 109-121 elements RMS for band 1 on 121-133 elements RMS for band 2 on 133-145 elements RMS for band 3 on 145-157 elements RMS for band 4 on 157-181 elements (and so on for each channel and call to that function) For the stereo sample: Channel 1: Copying 24 elements to bin 133 from bin 25 RMS for band 0 on 133-145 elements RMS for band 1 on 145-157 elements (repeat for each channel and call)
So: - no computation is remade
this is not neccessarily so. Actually ill bet its very very hard to proof the lack of redundant computations even for much simpler algorihms
- the number of samples for each iteration is for the available samples 12 and 24
- each band may start on an unaligned position (for SSE code at least), so scalarproduct & co functions can't be reused directly
you bring up an interresting point, do these sometimes or always start at an unaligned address?, if its always x*12*sizeof(float)+1 we can probably do something about it
Yes, it will always be that. The spec says "Transform coefficients #25 through #228 are grouped into 17 sub-bands of 12 coefficients each."
would we overall have fewer unaligned accesses if we would place the array so that not element 0 but element 1 is aligned?
- ff_eac3_apply_spectral_extension is 0.5% of execution time at best (from a oprofile log)
Attached is the patch rediff'ed against current SVN. I declare myself incompetent for more theoretical discussions, though.
iam incompetent as well, but you seem interrested. Maybe we can together resolve this, because one or both of us is not understanding the code completely and its quite complicated code though luckily much cleaner than our aac code.
for example:
+ /* Copy coeffs from normal bands to extension bands */ + bin = s->spx_start_freq; + for (i = 0; i < num_copy_sections; i++) { + memcpy(&s->transform_coeffs[ch][bin], + &s->transform_coeffs[ch][s->spx_copy_start_freq], + copy_sizes[i]*sizeof(float)); + bin += copy_sizes[i]; + }
this clearly copies the same area several times if num_copy_sections is greater than 1. Can it be greater than 1? or to ask differently does this happen for the samples we have? if its guranteed to be ==1 the code can be simplified alot. if not, how representative are our samples? are these random recordings or from some reference/conformance suite?
now if its copied 2 or more times i think the rms calculation is done several times over the same data it maybe just 0.5% for the samples we have but first it feels wrong if we do calculations redundantly and second it maybe more than 0.5% for other samples
num_copy_sections can definitely be >1. That is the whole point of it.
if num_copy_sections is only rarely >1 than this optimization is maybe not worth it [...]
[...]
@@ -43,6 +66,7 @@ #define AC3_MAX_COEFS 256 #define AC3_BLOCK_SIZE 256 #define MAX_BLOCKS 6 +#define SPX_MAX_BANDS 17
typedef struct { AVCodecContext *avctx; ///< parent context @@ -89,6 +113,22 @@ int cpl_coords[AC3_MAX_CHANNELS][18]; ///< coupling coordinates (cplco) ///@}
+///@defgroup spx spectral extension +///@{ + int spx_in_use; ///< spectral extension in use (spxinu) + uint8_t channel_uses_spx[AC3_MAX_CHANNELS]; ///< channel uses spectral extension (chinspx) + int8_t spx_atten_code[AC3_MAX_CHANNELS]; ///< spx attenuation code (spxattencod)
+ int spx_start_freq; ///< spx start frequency bin + int spx_end_freq; ///< spx end frequency bin + int spx_copy_start_freq; ///< spx starting frequency bin for copying (copystartmant) + ///< the copy region ends at the start of the spx region.
start/end/copy_start are not ideal names, they should contain "src"/"dst" so its clear from where to where things are copied
spx_src_start_freq, spx_dst_start_freq, spx_dst_end_freq ?
sound good [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB There will always be a question for which you do not know the correct awnser.
2010/3/24 Michael Niedermayer <michaelni at gmx.at>:
would we overall have fewer unaligned accesses if we would place the array so that not element 0 but element 1 is aligned?
I've just now see that this might be troublesome later on, as int32_to_float_fmul_scalar and ff_imdct_half are used on those buffers and both expect 16-bytes aligned data (movaps used).
num_copy_sections can definitely be >1. ?That is the whole point of it.
if num_copy_sections is only rarely >1 than this optimization is maybe not worth it
At least the samples on mplayerhq and the French TNT-HD broadcasts are in this situation. Last but not least, I've used START/STOP_TIMER and oprofile to cycle counts and % of execution time. It appears that the CSI samples have CRC whose computation eats most of the runtime. Here are those figures as food for thought: | (1) | (2) | (3) | (4) | (5) | (6) | rambo | 1.0 | 64 | 3870 | 14000 | 11.2 | 0.0 | 11.2 CSI | 5.1 | 256 | 5020 | 83000 | 0.9 | 81.4 | 5.1 CSI | 2.0 | 128 | 1920 | 12700 | 0.3 | 81.1 | 1.6 (1) Channel configuration (2) Bitrate (Kb/s) (3) dezicycles for the RMS computation loop (4) dezicycles for ff_eac3_apply_spectral_extension (5) oprofile % for ff_eac3_apply_spectral_extension (6) oprofile % for av_crc (7) oprofile pseudo % for ff_eac3_apply_spectral_extension if no av_crc
spx_src_start_freq, spx_dst_start_freq, spx_dst_end_freq ?
sound good
Locally changed, though I'm wondering if Justin expects me to be the one submitting the patch :) Best regards, Christophe
On Thu, Mar 25, 2010 at 03:52:31PM +0100, Christophe Gisquet wrote:
2010/3/24 Michael Niedermayer <michaelni at gmx.at>:
would we overall have fewer unaligned accesses if we would place the array so that not element 0 but element 1 is aligned?
I've just now see that this might be troublesome later on, as int32_to_float_fmul_scalar and ff_imdct_half are used on those buffers and both expect 16-bytes aligned data (movaps used).
num_copy_sections can definitely be >1. ?That is the whole point of it.
if num_copy_sections is only rarely >1 than this optimization is maybe not worth it
At least the samples on mplayerhq and the French TNT-HD broadcasts are in this situation.
Last but not least, I've used START/STOP_TIMER and oprofile to cycle counts and % of execution time. It appears that the CSI samples have CRC whose computation eats most of the runtime. Here are those figures as food for thought:
| (1) | (2) | (3) | (4) | (5) | (6) | rambo | 1.0 | 64 | 3870 | 14000 | 11.2 | 0.0 | 11.2 CSI | 5.1 | 256 | 5020 | 83000 | 0.9 | 81.4 | 5.1 CSI | 2.0 | 128 | 1920 | 12700 | 0.3 | 81.1 | 1.6
i dont belive that crc calculation takes 80% of the time, if it really does this is a bug please make sure this percetage does not inlcude once run table init on a very short file. [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Observe your enemies, for they first find out your faults. -- Antisthenes
2010/3/25 Michael Niedermayer <michaelni at gmx.at>:
i dont belive that crc calculation takes 80% of the time, if it really does this is a bug please make sure this percetage does not inlcude once run table init on a very short file.
It's not really relevant to the issue: I was merely computing a value not swayed by CRC computations that were (buggy or not) taking much time. Furthermore, it has indeed to do with initialization (probably determining the input type): -er 0 and mostly -f ac3 make av_crc disappear.
On Thu, Mar 25, 2010 at 03:52:31PM +0100, Christophe Gisquet wrote:
2010/3/24 Michael Niedermayer <michaelni at gmx.at>:
would we overall have fewer unaligned accesses if we would place the array so that not element 0 but element 1 is aligned?
I've just now see that this might be troublesome later on, as int32_to_float_fmul_scalar and ff_imdct_half are used on those buffers and both expect 16-bytes aligned data (movaps used).
num_copy_sections can definitely be >1. ?That is the whole point of it.
if num_copy_sections is only rarely >1 than this optimization is maybe not worth it
At least the samples on mplayerhq and the French TNT-HD broadcasts are in this situation.
Last but not least, I've used START/STOP_TIMER and oprofile to cycle counts and % of execution time. It appears that the CSI samples have CRC whose computation eats most of the runtime. Here are those figures as food for thought:
| (1) | (2) | (3) | (4) | (5) | (6) | rambo | 1.0 | 64 | 3870 | 14000 | 11.2 | 0.0 | 11.2 CSI | 5.1 | 256 | 5020 | 83000 | 0.9 | 81.4 | 5.1 CSI | 2.0 | 128 | 1920 | 12700 | 0.3 | 81.1 | 1.6
(1) Channel configuration (2) Bitrate (Kb/s) (3) dezicycles for the RMS computation loop (4) dezicycles for ff_eac3_apply_spectral_extension (5) oprofile % for ff_eac3_apply_spectral_extension (6) oprofile % for av_crc (7) oprofile pseudo % for ff_eac3_apply_spectral_extension if no av_crc
iam not 100% happy but this seems to be blocking things so lets drop this optimization idea, it appears minor enough [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Observe your enemies, for they first find out your faults. -- Antisthenes
Hi, 2010/3/25 Michael Niedermayer <michaelni at gmx.at>:
iam not 100% happy but this seems to be blocking things so lets drop this optimization idea, it appears minor enough
So here's another patch, ran through patcheck. Best regards, Christophe
On Fri, Mar 26, 2010 at 10:19:22AM +0100, Christophe Gisquet wrote:
Hi,
2010/3/25 Michael Niedermayer <michaelni at gmx.at>:
iam not 100% happy but this seems to be blocking things so lets drop this optimization idea, it appears minor enough
So here's another patch, ran through patcheck.
Best regards, Christophe
Changelog | 1 libavcodec/ac3dec.c | 118 ++++++++++++++++++++++++++++++++++++++++++---- libavcodec/ac3dec.h | 47 ++++++++++++++++++ libavcodec/ac3dec_data.c | 6 ++ libavcodec/ac3dec_data.h | 1 libavcodec/eac3dec.c | 106 +++++++++++++++++++++++++++++++++++++---- libavcodec/eac3dec_data.c | 39 +++++++++++++++ libavcodec/eac3dec_data.h | 1 8 files changed, 298 insertions(+), 21 deletions(-) 44607388d178664e2a17918100fb7e7923997c1f eac3_spx_10.diff Index: Changelog =================================================================== --- Changelog (revision 22692) +++ Changelog (working copy) @@ -66,6 +66,7 @@ - VorbisComment writing for FLAC, Ogg FLAC and Ogg Speex files - RTP depacketization of Theora - HTTP Digest authentication +- spectral extension support in the E-AC-3 decoder
Index: libavcodec/ac3dec.c =================================================================== --- libavcodec/ac3dec.c (revision 22692) +++ libavcodec/ac3dec.c (working copy) @@ -815,15 +815,105 @@
/* spectral extension strategy */ if (s->eac3 && (!blk || get_bits1(gbc))) { - if (get_bits1(gbc)) { - av_log_missing_feature(s->avctx, "Spectral extension", 1); - return -1; + s->spx_in_use = get_bits1(gbc); + if (s->spx_in_use) { + int copy_start, start_subband, end_subband, start_freq, end_freq; + + /* determine which channels use spx */ + if (s->channel_mode == AC3_CHMODE_MONO) { + s->channel_uses_spx[1] = 1; + } else { + for (ch = 1; ch <= fbw_channels; ch++) + s->channel_uses_spx[ch] = get_bits1(gbc); + } + + /* get the frequency bins of the spx copy region and the spx start + and end subbands */ + copy_start = get_bits(gbc, 2); + start_subband = get_bits(gbc, 3) + 2; + if (start_subband > 7) + start_subband += start_subband - 7; + end_subband = get_bits(gbc, 3) + 5; + if (end_subband > 7) + end_subband += end_subband - 7; + copy_start = copy_start * 12 + 25; + start_freq = start_subband * 12 + 25; + end_freq = end_subband * 12 + 25; + + /* check validity of spx ranges */ + if (start_subband >= end_subband) { + av_log(s->avctx, AV_LOG_ERROR, "invalid spectral extension " + "range (%d >= %d)\n", start_subband, end_subband); + return -1; + } + if (copy_start >= start_freq) { + av_log(s->avctx, AV_LOG_ERROR, "invalid spectral extension " + "copy start bin (%d >= %d)\n", copy_start, start_freq); + return -1; + } +
+ s->spx_dst_start_freq = copy_start; + s->spx_src_start_freq = start_freq; + s->spx_dst_end_freq = end_freq;
vertical align also the src/dst could beadded to the local 3 vars here [...]
+///@defgroup spx spectral extension +///@{ + int spx_in_use; ///< spectral extension in use (spxinu) + uint8_t channel_uses_spx[AC3_MAX_CHANNELS]; ///< channel uses spectral extension (chinspx) + int8_t spx_atten_code[AC3_MAX_CHANNELS]; ///< spx attenuation code (spxattencod) + int spx_src_start_freq; ///< spx start frequency bin + int spx_dst_end_freq; ///< spx end frequency bin
align messed up too i guess you should grep forthe changed var and make sure it still looks ok
+ int spx_dst_start_freq; ///< spx starting frequency bin for copying (copystartmant) + ///< the copy region ends at the start of the spx region.
too [....]
@@ -492,13 +577,11 @@ }
/* spectral extension attenuation data */ - if (parse_spx_atten_data) { - av_log_missing_feature(s->avctx, "Spectral extension attenuation", 1); - for (ch = 1; ch <= s->fbw_channels; ch++) { - if (get_bits1(gbc)) { // channel has spx attenuation - skip_bits(gbc, 5); // skip spx attenuation code - } - } + for (ch = 1; ch <= s->fbw_channels; ch++) {
+ if (parse_spx_atten_data && get_bits1(gbc)) + s->spx_atten_code[ch] = get_bits(gbc, 5); + else
should have {} didnt patcheck detect this? [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB No human being will ever know the Truth, for even if they happen to say it by chance, they would not even known they had done so. -- Xenophanes
Hi, 2010/3/26 Michael Niedermayer <michaelni at gmx.at>:
+ ? ? ? ? ? ?s->spx_dst_start_freq = copy_start; + ? ? ? ? ? ?s->spx_src_start_freq ? ? ?= start_freq; + ? ? ? ? ? ?s->spx_dst_end_freq ? ? ? ?= end_freq;
vertical align also the src/dst could beadded to the local 3 vars here [...]
+ ? ?int8_t spx_atten_code[AC3_MAX_CHANNELS]; ? ?///< spx attenuation code ? ? ? ? ? ? ? ? ? (spxattencod) + ? ?int spx_src_start_freq; ? ? ? ? ? ? ? ? ? ? ? ? ///< spx start frequency bin + ? ?int spx_dst_end_freq; ? ? ? ? ? ? ? ? ? ? ? ? ? ///< spx end frequency bin + ? ?int spx_dst_start_freq; ? ? ? ? ? ? ? ? ? ?///< spx starting frequency bin for copying (copystartmant) + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?///< the copy region ends at the start of the spx region.
too
Vars renamed, alignment fixed.
+ ? ? ? ?if (parse_spx_atten_data && get_bits1(gbc)) + ? ? ? ? ? ?s->spx_atten_code[ch] = get_bits(gbc, 5); + ? ? ? ?else
should have {} didnt patcheck detect this?
It did, and I do remember brushing it off, assuming bitstream spec wouldn't change. Changed, whatever my reason was. I also didn't add {} to places where it would only introduce a cosmetical change. Best regards, Christophe
On Fri, Mar 26, 2010 at 05:44:08PM +0100, Christophe Gisquet wrote:
Hi,
2010/3/26 Michael Niedermayer <michaelni at gmx.at>:
+ ? ? ? ? ? ?s->spx_dst_start_freq = copy_start; + ? ? ? ? ? ?s->spx_src_start_freq ? ? ?= start_freq; + ? ? ? ? ? ?s->spx_dst_end_freq ? ? ? ?= end_freq;
vertical align also the src/dst could beadded to the local 3 vars here [...]
+ ? ?int8_t spx_atten_code[AC3_MAX_CHANNELS]; ? ?///< spx attenuation code ? ? ? ? ? ? ? ? ? (spxattencod) + ? ?int spx_src_start_freq; ? ? ? ? ? ? ? ? ? ? ? ? ///< spx start frequency bin + ? ?int spx_dst_end_freq; ? ? ? ? ? ? ? ? ? ? ? ? ? ///< spx end frequency bin + ? ?int spx_dst_start_freq; ? ? ? ? ? ? ? ? ? ?///< spx starting frequency bin for copying (copystartmant) + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?///< the copy region ends at the start of the spx region.
too
Vars renamed, alignment fixed.
+ ? ? ? ?if (parse_spx_atten_data && get_bits1(gbc)) + ? ? ? ? ? ?s->spx_atten_code[ch] = get_bits(gbc, 5); + ? ? ? ?else
should have {} didnt patcheck detect this?
It did, and I do remember brushing it off, assuming bitstream spec wouldn't change. Changed, whatever my reason was.
I also didn't add {} to places where it would only introduce a cosmetical change.
Best regards, Christophe
Changelog | 1 libavcodec/ac3dec.c | 119 ++++++++++++++++++++++++++++++++++++++++++---- libavcodec/ac3dec.h | 47 ++++++++++++++++++ libavcodec/ac3dec_data.c | 6 ++ libavcodec/ac3dec_data.h | 1 libavcodec/eac3dec.c | 105 ++++++++++++++++++++++++++++++++++++---- libavcodec/eac3dec_data.c | 39 +++++++++++++++ libavcodec/eac3dec_data.h | 1 8 files changed, 299 insertions(+), 20 deletions(-) 989de678ad9f88b3ed4d4f9c9bc40d46600db9c2 eac3_spx_11.diff
looks ok if tested (and also tested with tools/trasher files) [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB If a bugfix only changes things apparently unrelated to the bug with no further explanation, that is a good sign that the bugfix is wrong.
2010/3/29 Michael Niedermayer <michaelni at gmx.at>:
looks ok if tested
No audible artifacts in my samples. The patch has also been applied in VLC since 1.0.2 (which seems a quite wider test bed than I have), and there doesn't seem to be complains on their bugtracker.
(and also tested with tools/trasher files)
Not quite used to it, ran it with something like this on my samples without a segfault: for count in `seq 1 50`; do for maxburst in `seq 1 5`; do for seed in `seq 0 30`; do cp $1 test tools/trasher test $count $maxburst $seed ./ffmpeg_g -f ac3 -er 0 -i test -benchmark -vn -sn \ -f null /dev/null 1>/dev/null 2&>1 done done echo "Count=$count" done Also, roundup report related to this: https://roundup.ffmpeg.org/issue1256
On Tue, Mar 30, 2010 at 02:18:29PM +0200, Christophe Gisquet wrote:
2010/3/29 Michael Niedermayer <michaelni at gmx.at>:
looks ok if tested
No audible artifacts in my samples.
The patch has also been applied in VLC since 1.0.2 (which seems a quite wider test bed than I have), and there doesn't seem to be complains on their bugtracker.
(and also tested with tools/trasher files)
Not quite used to it, ran it with something like this on my samples without a segfault:
should be ok then [..] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB It is dangerous to be right in matters on which the established authorities are wrong. -- Voltaire
Michael Niedermayer <michaelni <at> gmx.at> writes:
Changelog | 1 libavcodec/ac3dec.c | 119 ++++++++++++++++++++++++++++++++++++++++++---- libavcodec/ac3dec.h | 47 ++++++++++++++++++ libavcodec/ac3dec_data.c | 6 ++ libavcodec/ac3dec_data.h | 1 libavcodec/eac3dec.c | 105 ++++++++++++++++++++++++++++++++++++---- libavcodec/eac3dec_data.c | 39 +++++++++++++++ libavcodec/eac3dec_data.h | 1 8 files changed, 299 insertions(+), 20 deletions(-) 989de678ad9f88b3ed4d4f9c9bc40d46600db9c2 eac3_spx_11.diff
looks ok if tested (and also tested with tools/trasher files)
Patch applied with a minor version bump. Thank you for resubmitting and testing, Carl Eugen
Carl Eugen Hoyos wrote:
Michael Niedermayer <michaelni <at> gmx.at> writes:
Changelog | 1 libavcodec/ac3dec.c | 119 ++++++++++++++++++++++++++++++++++++++++++---- libavcodec/ac3dec.h | 47 ++++++++++++++++++ libavcodec/ac3dec_data.c | 6 ++ libavcodec/ac3dec_data.h | 1 libavcodec/eac3dec.c | 105 ++++++++++++++++++++++++++++++++++++---- libavcodec/eac3dec_data.c | 39 +++++++++++++++ libavcodec/eac3dec_data.h | 1 8 files changed, 299 insertions(+), 20 deletions(-) 989de678ad9f88b3ed4d4f9c9bc40d46600db9c2 eac3_spx_11.diff looks ok if tested (and also tested with tools/trasher files)
Patch applied with a minor version bump.
Thank you for resubmitting and testing, Carl Eugen
Yes, thank you very much. And thanks for applying it Carl Eugen. -Justin
Hi, 2010/3/19 Michael Niedermayer <michaelni at gmx.at>:
So: - no computation is remade
this is not neccessarily so. Actually ill bet its very very hard to proof the lack of redundant computations even for much simpler algorihms
Indeed, I overlooked num_copy, which is 1 for all my samples.
- each band may start on an unaligned position (for SSE code at least), so scalarproduct & co functions can't be reused directly
you bring up an interresting point, do these sometimes or always start at an unaligned address?, if its always x*12*sizeof(float)+1 we can probably do something about it
I don't know what the specs would say, but the code seems to say so: (spx_)start_freq = start_subband * 12 + 25 so indeed, it is (x*12+1)*sizeof(float) (the +1 is probably due do the DC) Not considering the computation factorization, this leads to 2 possibilities: - 3 SSE operations with movups - 3 SSE operations with movaps, a sufficiently padded buffer to avoid reads out-of-bound, a mask to erase the result for the value not belonging to the band, and a final classical x87 computation
iam incompetent as well, but you seem interrested. Maybe we can together resolve this,
At least Justin chimed in and the 3 of us can get somewhere.
because one or both of us is not understanding the code completely
I was clearly misunderstanding the copy. I'm not discussing the other remarks for the patch, and will continue the redundant computation removal optimization with Justin's post. Best regards, Christophe
participants (4)
-
cehoyos@ag.or.at -
christophe.gisquet@gmail.com -
justin.ruggles@gmail.com -
michaelni@gmx.at