History

Merge remote-tracking branch 'qatar/master'

* qatar/master: dca: Switch dca_sample_rates to avpriv_ prefix; it is used across libs ARM: use =const syntax instead of explicit literal pools ARM: use standard syntax for all LDRD/STRD instructions fft: port FFT/IMDCT 3dnow functions to yasm, and disable on x86-64. dct-test: allow to compile without HAVE_INLINE_ASM. x86/dsputilenc: bury inline asm under HAVE_INLINE_ASM. dca: Move tables used outside of dcadec.c to a separate file. dca: Rename dca.c ---> dcadec.c x86: h264dsp: Remove unused variable ff_pb_3_1 apetag: change a forgotten return to return 0 Conflicts: libavcodec/Makefile libavcodec/dca.c libavcodec/x86/fft_3dn.c libavcodec/x86/fft_3dn2.c libavcodec/x86/fft_mmx.asm

Michael Niedermayer committed 14 years ago

Commit ec7ecb82 parents 4c8fc6a 19cf71622 files · +2648 −2626Browse files

Unified · Split

22 files
  1. libavcodec/Makefile +4 −3
  2. libavcodec/arm/h264cmc_neon.S +2 −2
  3. libavcodec/arm/h264dsp_neon.S +6 −6
  4. libavcodec/arm/mpegvideo_armv5te_s.S +3 −3
  5. libavcodec/arm/simple_idct_arm.S +16 −40
  6. libavcodec/arm/simple_idct_armv5te.S +14 −20
  7. libavcodec/arm/simple_idct_armv6.S +12 −21
  8. libavcodec/arm/simple_idct_neon.S +4 −4
  9. libavcodec/dca.c +5 −2479
  10. libavcodec/dca.h +4 −0
  11. libavcodec/dca_parser.c +1 −2
  12. libavcodec/dcadata.h +0 −6
  13. libavcodec/dcadec.c +2507 −0
  14. libavcodec/dcaenc.c +3 −2
  15. libavcodec/dct-test.c +1 −1
  16. libavcodec/x86/dsputilenc_mmx.c +47 −33
  17. libavcodec/x86/fdct_mmx.c +4 −0
  18. libavcodec/x86/fft.c +2 −0
  19. libavcodec/x86/h264dsp_mmx.c +0 −2
  20. libavcodec/x86/motion_est_mmx.c +6 −0
  21. libavcodec/x86/mpegvideo_mmx.c +6 −0
  22. libavformat/spdifenc.c +1 −2
libavcodec/Makefile+4−3
@@ -134,8 +134,8 @@ OBJS-$(CONFIG_CLJR_ENCODER) += cljr.o · 133 unmodified lines
134134 OBJS-$(CONFIG_COOK_DECODER) += cook.o
135135 OBJS-$(CONFIG_CSCD_DECODER) += cscd.o
136136 OBJS-$(CONFIG_CYUV_DECODER) += cyuv.o
137−OBJS-$(CONFIG_DCA_DECODER) += dca.o synth_filter.o dcadsp.o \
138− dca_parser.o
137+OBJS-$(CONFIG_DCA_DECODER) += dcadec.o dca.o dcadsp.o \
138+ dca_parser.o synth_filter.o
139139 OBJS-$(CONFIG_DCA_ENCODER) += dcaenc.o
140140 OBJS-$(CONFIG_DIRAC_DECODER) += diracdec.o dirac.o diracdsp.o \
141141 dirac_arith.o mpeg12data.o dwt.o
@@ -660,6 +660,7 @@ OBJS-$(CONFIG_OGG_MUXER) += xiph.o flac.o flacdata.o \ · 518 unmodified lines
660660 vorbis_data.o
661661 OBJS-$(CONFIG_RTP_MUXER) += mpeg4audio.o mpegvideo.o xiph.o
662662 OBJS-$(CONFIG_SPDIF_DEMUXER) += aacadtsdec.o mpeg4audio.o
663+OBJS-$(CONFIG_SPDIF_MUXER) += dca.o
663664 OBJS-$(CONFIG_WEBM_MUXER) += mpeg4audio.o mpegaudiodata.o \
664665 xiph.o flac.o flacdata.o \
665666 vorbis_data.o
@@ -716,7 +717,7 @@ OBJS-$(CONFIG_AC3_PARSER) += ac3_parser.o ac3tab.o \ · 50 unmodified lines
716717 OBJS-$(CONFIG_ADX_PARSER) += adx_parser.o adx.o
717718 OBJS-$(CONFIG_CAVSVIDEO_PARSER) += cavs_parser.o
718719 OBJS-$(CONFIG_COOK_PARSER) += cook_parser.o
719−OBJS-$(CONFIG_DCA_PARSER) += dca_parser.o
720+OBJS-$(CONFIG_DCA_PARSER) += dca_parser.o dca.o
720721 OBJS-$(CONFIG_DIRAC_PARSER) += dirac_parser.o
721722 OBJS-$(CONFIG_DNXHD_PARSER) += dnxhd_parser.o
722723 OBJS-$(CONFIG_DVBSUB_PARSER) += dvbsub_parser.o
libavcodec/arm/h264cmc_neon.S+2−2
@@ -24,7 +24,7 @@ · 23 unmodified lines
2424 .macro h264_chroma_mc8 type, codec=h264
2525 function ff_\type\()_\codec\()_chroma_mc8_neon, export=1
2626 push {r4-r7, lr}
27− ldrd r4, [sp, #20]
27+ ldrd r4, r5, [sp, #20]
2828 .ifc \type,avg
2929 mov lr, r0
3030 .endif
@@ -182,7 +182,7 @@ endfunc · 151 unmodified lines
182182 .macro h264_chroma_mc4 type, codec=h264
183183 function ff_\type\()_\codec\()_chroma_mc4_neon, export=1
184184 push {r4-r7, lr}
185− ldrd r4, [sp, #20]
185+ ldrd r4, r5, [sp, #20]
186186 .ifc \type,avg
187187 mov lr, r0
188188 .endif
libavcodec/arm/h264dsp_neon.S+6−6
@@ -886,7 +886,7 @@ T mov sp, r0 · 885 unmodified lines
886886 mov r12, #8
887887 vpush {d8-d15}
888888 bl put_h264_qpel8_h_lowpass_neon
889− ldrd r0, [r11], #8
889+ ldrd r0, r1, [r11], #8
890890 mov r3, r2
891891 add r12, sp, #64
892892 sub r1, r1, r2, lsl #1
@@ -913,7 +913,7 @@ T mov sp, r0 · 20 unmodified lines
913913 vpush {d8-d15}
914914 bl put_h264_qpel8_h_lowpass_neon
915915 mov r4, r0
916− ldrd r0, [r11], #8
916+ ldrd r0, r1, [r11], #8
917917 sub r1, r1, r2, lsl #1
918918 sub r1, r1, #2
919919 mov r3, r2
@@ -958,7 +958,7 @@ T mov sp, r0 · 38 unmodified lines
958958 vpush {d8-d15}
959959 bl put_h264_qpel8_v_lowpass_neon
960960 mov r4, r0
961− ldrd r0, [r11], #8
961+ ldrd r0, r1, [r11], #8
962962 sub r1, r1, r3, lsl #1
963963 sub r1, r1, #2
964964 sub r2, r4, #64
@@ -1071,7 +1071,7 @@ T mov sp, r0 · 106 unmodified lines
10711071 mov r3, #16
10721072 vpush {d8-d15}
10731073 bl put_h264_qpel16_h_lowpass_neon
1074− ldrd r0, [r11], #8
1074+ ldrd r0, r1, [r11], #8
10751075 mov r3, r2
10761076 add r12, sp, #64
10771077 sub r1, r1, r2, lsl #1
@@ -1096,7 +1096,7 @@ T mov sp, r0 · 18 unmodified lines
10961096 vpush {d8-d15}
10971097 bl put_h264_qpel16_h_lowpass_neon_packed
10981098 mov r4, r0
1099− ldrd r0, [r11], #8
1099+ ldrd r0, r1, [r11], #8
11001100 sub r1, r1, r2, lsl #1
11011101 sub r1, r1, #2
11021102 mov r3, r2
@@ -1139,7 +1139,7 @@ T mov sp, r0 · 36 unmodified lines
11391139 vpush {d8-d15}
11401140 bl put_h264_qpel16_v_lowpass_neon_packed
11411141 mov r4, r0
1142− ldrd r0, [r11], #8
1142+ ldrd r0, r1, [r11], #8
11431143 sub r1, r1, r3, lsl #1
11441144 sub r1, r1, #2
11451145 mov r2, r3
libavcodec/arm/mpegvideo_armv5te_s.S+3−3
@@ -61,9 +61,9 @@ function ff_dct_unquantize_h263_armv5te, export=1 · 60 unmodified lines
6161 mov ip, #0
6262 subs r3, r3, #2
6363 ble 2f
64− ldrd r4, [r0, #0]
64+ ldrd r4, r5, [r0, #0]
6565 1:
66− ldrd r6, [r0, #8]
66+ ldrd r6, r7, [r0, #8]
6767
6868 dequant_t r9, r4, r1, r2, r9
6969 dequant_t lr, r5, r1, r2, lr
@@ -87,7 +87,7 @@ function ff_dct_unquantize_h263_armv5te, export=1 · 17 unmodified lines
8787
8888 subs r3, r3, #8
8989 it gt
90− ldrdgt r4, [r0, #0] /* load data early to avoid load/use pipeline stall */
90+ ldrdgt r4, r5, [r0, #0] /* load data early to avoid load/use pipeline stall */
9191 bgt 1b
9292
9393 adds r3, r3, #2
libavcodec/arm/simple_idct_arm.S+16−40
@@ -25,8 +25,7 @@ · 24 unmodified lines
2525
2626 #include "libavutil/arm/asm.S"
2727
28−/* useful constants for the algorithm, they are save in __constant_ptr__ at */
29−/* the end of the source code.*/
28+/* useful constants for the algorithm */
3029 #define W1 22725
3130 #define W2 21407
3231 #define W3 19266
@@ -36,16 +35,6 @@ · 3 unmodified lines
3635 #define W7 4520
3736 #define MASK_MSHW 0xFFFF0000
3837
39−/* offsets of the constants in the vector */
40−#define offW1 0
41−#define offW2 4
42−#define offW3 8
43−#define offW4 12
44−#define offW5 16
45−#define offW6 20
46−#define offW7 24
47−#define offMASK_MSHW 28
48−
4938 #define ROW_SHIFT 11
5039 #define ROW_SHIFT2MSHW (16-11)
5140 #define COL_SHIFT 20
@@ -63,7 +52,6 @@ function ff_simple_idct_arm, export=1 · 11 unmodified lines
6352 stmfd sp!, {r4-r11, r14} @ R14 is also called LR
6453 @@ at this point, R0=block, other registers are free.
6554 add r14, r0, #112 @ R14=&block[8*7], better start from the last row, and decrease the value until row=0, i.e. R12=block.
66− adr r12, __constant_ptr__ @ R12=__constant_ptr__, the vector containing the constants, probably not necessary to reserve a register for it
6755 @@ add 2 temporary variables in the stack: R0 and R14
6856 sub sp, sp, #8 @ allow 2 local variables
6957 str r0, [sp, #0] @ save block in sp[0]
@@ -109,13 +97,13 @@ __b_evaluation: · 39 unmodified lines
10997 @@ MAC16(b1, -W7, row[3]);
11098 @@ MAC16(b2, -W1, row[3]);
11199 @@ MAC16(b3, -W5, row[3]);
112− ldr r8, [r12, #offW1] @ R8=W1
100+ ldr r8, =W1 @ R8=W1
113101 mov r2, r2, asr #16 @ R2=ROWr16[3]
114102 mul r0, r8, r7 @ R0=W1*ROWr16[1]=b0 (ROWr16[1] must be the second arg, to have the possibility to save 1 cycle)
115− ldr r9, [r12, #offW3] @ R9=W3
116− ldr r10, [r12, #offW5] @ R10=W5
103+ ldr r9, =W3 @ R9=W3
104+ ldr r10, =W5 @ R10=W5
117105 mul r1, r9, r7 @ R1=W3*ROWr16[1]=b1 (ROWr16[1] must be the second arg, to have the possibility to save 1 cycle)
118− ldr r11, [r12, #offW7] @ R11=W7
106+ ldr r11, =W7 @ R11=W7
119107 mul r5, r10, r7 @ R5=W5*ROWr16[1]=b2 (ROWr16[1] must be the second arg, to have the possibility to save 1 cycle)
120108 mul r7, r11, r7 @ R7=W7*ROWr16[1]=b3 (ROWr16[1] must be the second arg, to have the possibility to save 1 cycle)
121109 teq r2, #0 @ if null avoid muls
@@ -177,14 +165,14 @@ __a_evaluation: · 55 unmodified lines
177165 @@ a2 = a0 - W6 * row[2];
178166 @@ a3 = a0 - W2 * row[2];
179167 @@ a0 = a0 + W2 * row[2];
180− ldr r9, [r12, #offW4] @ R9=W4
168+ ldr r9, =W4 @ R9=W4
181169 mul r6, r9, r6 @ R6=W4*ROWr16[0]
182− ldr r10, [r12, #offW6] @ R10=W6
170+ ldr r10, =W6 @ R10=W6
183171 ldrsh r4, [r14, #4] @ R4=ROWr16[2] (a3 not defined yet)
184172 add r6, r6, #ROW_SHIFTED_1 @ R6=W4*ROWr16[0] + 1<<(ROW_SHIFT-1) (a0)
185173
186174 mul r11, r10, r4 @ R11=W6*ROWr16[2]
187− ldr r8, [r12, #offW2] @ R8=W2
175+ ldr r8, =W2 @ R8=W2
188176 sub r3, r6, r11 @ R3=a0-W6*ROWr16[2] (a2)
189177 @@ temp = ((uint32_t*)row)[2] | ((uint32_t*)row)[3];
190178 @@ if (temp != 0) {}
@@ -248,7 +236,7 @@ __end_a_evaluation: · 57 unmodified lines
248236 add r9, r2, r1 @ R9=a1+b1
249237 @@ put 2 16 bits half-words in a 32bits word
250238 @@ ROWr32[0]=ROWr16[0] | (ROWr16[1]<<16) (only Little Endian compliant then!!!)
251− ldr r10, [r12, #offMASK_MSHW] @ R10=0xFFFF0000
239+ ldr r10, =MASK_MSHW @ R10=0xFFFF0000
252240 and r9, r10, r9, lsl #ROW_SHIFT2MSHW @ R9=0xFFFF0000 & ((a1+b1)<<5)
253241 mvn r11, r10 @ R11= NOT R10= 0x0000FFFF
254242 and r8, r11, r8, asr #ROW_SHIFT @ R8=0x0000FFFF & ((a0+b0)>>11)
@@ -319,13 +307,13 @@ __b_evaluation2: · 64 unmodified lines
319307 @@ MAC16(b1, -W7, col[8x3]);
320308 @@ MAC16(b2, -W1, col[8x3]);
321309 @@ MAC16(b3, -W5, col[8x3]);
322− ldr r8, [r12, #offW1] @ R8=W1
310+ ldr r8, =W1 @ R8=W1
323311 ldrsh r7, [r14, #16]
324312 mul r0, r8, r7 @ R0=W1*ROWr16[1]=b0 (ROWr16[1] must be the second arg, to have the possibility to save 1 cycle)
325− ldr r9, [r12, #offW3] @ R9=W3
326− ldr r10, [r12, #offW5] @ R10=W5
313+ ldr r9, =W3 @ R9=W3
314+ ldr r10, =W5 @ R10=W5
327315 mul r1, r9, r7 @ R1=W3*ROWr16[1]=b1 (ROWr16[1] must be the second arg, to have the possibility to save 1 cycle)
328− ldr r11, [r12, #offW7] @ R11=W7
316+ ldr r11, =W7 @ R11=W7
329317 mul r5, r10, r7 @ R5=W5*ROWr16[1]=b2 (ROWr16[1] must be the second arg, to have the possibility to save 1 cycle)
330318 ldrsh r2, [r14, #48]
331319 mul r7, r11, r7 @ R7=W7*ROWr16[1]=b3 (ROWr16[1] must be the second arg, to have the possibility to save 1 cycle)
@@ -381,13 +369,13 @@ __a_evaluation2: · 49 unmodified lines
381369 @@ a3 = a0 - W2 * row[2];
382370 @@ a0 = a0 + W2 * row[2];
383371 ldrsh r6, [r14, #0]
384− ldr r9, [r12, #offW4] @ R9=W4
372+ ldr r9, =W4 @ R9=W4
385373 mul r6, r9, r6 @ R6=W4*ROWr16[0]
386− ldr r10, [r12, #offW6] @ R10=W6
374+ ldr r10, =W6 @ R10=W6
387375 ldrsh r4, [r14, #32] @ R4=ROWr16[2] (a3 not defined yet)
388376 add r6, r6, #COL_SHIFTED_1 @ R6=W4*ROWr16[0] + 1<<(COL_SHIFT-1) (a0)
389377 mul r11, r10, r4 @ R11=W6*ROWr16[2]
390− ldr r8, [r12, #offW2] @ R8=W2
378+ ldr r8, =W2 @ R8=W2
391379 add r2, r6, r11 @ R2=a0+W6*ROWr16[2] (a1)
392380 sub r3, r6, r11 @ R3=a0-W6*ROWr16[2] (a2)
393381 mul r11, r8, r4 @ R11=W2*ROWr16[2]
@@ -489,15 +477,3 @@ __end_bef_a_evaluation: · 95 unmodified lines
489477 sub r4, r6, r11 @ R4=a0-W2*ROWr16[2] (a3)
490478 add r6, r6, r11 @ R6=a0+W2*ROWr16[2] (a0)
491479 bal __end_a_evaluation
492−
493−
494− .align
495−__constant_ptr__: @@ see #defines at the beginning of the source code for values.
496− .word W1
497− .word W2
498− .word W3
499− .word W4
500− .word W5
501− .word W6
502− .word W7
503− .word MASK_MSHW
libavcodec/arm/simple_idct_armv5te.S+14−20
@@ -37,17 +37,11 @@ · 36 unmodified lines
3737 #define W26 (W2 | (W6 << 16))
3838 #define W57 (W5 | (W7 << 16))
3939
40− .text
41− .align
42−w13: .long W13
43−w26: .long W26
44−w57: .long W57
45−
4640 function idct_row_armv5te
4741 str lr, [sp, #-4]!
4842
49− ldrd v1, [a1, #8]
50− ldrd a3, [a1] /* a3 = row[1:0], a4 = row[3:2] */
43+ ldrd v1, v2, [a1, #8]
44+ ldrd a3, a4, [a1] /* a3 = row[1:0], a4 = row[3:2] */
5145 orrs v1, v1, v2
5246 itt eq
5347 cmpeq v1, a4
@@ -58,7 +52,7 @@ function idct_row_armv5te · 4 unmodified lines
5852 mov ip, #16384
5953 sub ip, ip, #1 /* ip = W4 */
6054 smlabb v1, ip, a3, v1 /* v1 = W4*row[0]+(1<<(RS-1)) */
61− ldr ip, w26 /* ip = W2 | (W6 << 16) */
55+ ldr ip, =W26 /* ip = W2 | (W6 << 16) */
6256 smultb a2, ip, a4
6357 smulbb lr, ip, a4
6458 add v2, v1, a2
@@ -66,8 +60,8 @@ function idct_row_armv5te · 1 unmodified line
6660 sub v4, v1, lr
6761 add v1, v1, lr
6862
69− ldr ip, w13 /* ip = W1 | (W3 << 16) */
70− ldr lr, w57 /* lr = W5 | (W7 << 16) */
63+ ldr ip, =W13 /* ip = W1 | (W3 << 16) */
64+ ldr lr, =W57 /* lr = W5 | (W7 << 16) */
7165 smulbt v5, ip, a3
7266 smultt v6, lr, a4
7367 smlatt v5, ip, a4, v5
@@ -78,7 +72,7 @@ function idct_row_armv5te · 4 unmodified lines
7872 smultt fp, lr, a3
7973 sub v7, v7, a2
8074 smulbt a2, lr, a4
81− ldrd a3, [a1, #8] /* a3=row[5:4] a4=row[7:6] */
75+ ldrd a3, a4, [a1, #8] /* a3=row[5:4] a4=row[7:6] */
8276 sub fp, fp, a2
8377
8478 orrs a2, a3, a4
@@ -94,7 +88,7 @@ function idct_row_armv5te · 9 unmodified lines
9488 smlatt v7, ip, a4, v7
9589 sub fp, fp, a2
9690
97− ldr ip, w26 /* ip = W2 | (W6 << 16) */
91+ ldr ip, =W26 /* ip = W2 | (W6 << 16) */
9892 mov a2, #16384
9993 sub a2, a2, #1 /* a2 = W4 */
10094 smulbb a2, a2, a3 /* a2 = W4*row[4] */
@@ -121,7 +115,7 @@ function idct_row_armv5te · 20 unmodified lines
121115 add a2, v4, fp
122116 mov a2, a2, lsr #11
123117 add a4, a4, a2, lsl #16
124− strd a3, [a1]
118+ strd a3, a4, [a1]
125119
126120 sub a2, v4, fp
127121 mov a3, a2, lsr #11
@@ -135,7 +129,7 @@ function idct_row_armv5te · 7 unmodified lines
135129 sub a2, v1, v5
136130 mov a2, a2, lsr #11
137131 add a4, a4, a2, lsl #16
138− strd a3, [a1, #8]
132+ strd a3, a4, [a1, #8]
139133
140134 ldr pc, [sp], #4
141135
@@ -144,8 +138,8 @@ row_dc_only: · 2 unmodified lines
144138 bic a3, a3, #0xe000
145139 mov a3, a3, lsl #3
146140 mov a4, a3
147− strd a3, [a1]
148− strd a3, [a1, #8]
141+ strd a3, a4, [a1]
142+ strd a3, a4, [a1, #8]
149143
150144 ldr pc, [sp], #4
151145 endfunc
@@ -178,7 +172,7 @@ endfunc · 26 unmodified lines
178172 sub v4, v2, a3
179173 sub v6, v2, a3
180174 add fp, v2, a3
181− ldr ip, w26
175+ ldr ip, =W26
182176 ldr a4, [a1, #(16*2)]
183177 add v2, v2, a3
184178
@@ -211,9 +205,9 @@ endfunc · 26 unmodified lines
211205
212206 stmfd sp!, {v1, v2, v3, v4, v5, v6, v7, fp}
213207
214− ldr ip, w13
208+ ldr ip, =W13
215209 ldr a4, [a1, #(16*1)]
216− ldr lr, w57
210+ ldr lr, =W57
217211 smulbb v1, ip, a4
218212 smultb v3, ip, a4
219213 smulbb v5, lr, a4
libavcodec/arm/simple_idct_armv6.S+12−21
@@ -40,15 +40,6 @@ · 39 unmodified lines
4040 #define W46 (W4 | (W6 << 16))
4141 #define W57 (W5 | (W7 << 16))
4242
43− .text
44− .align
45−w13: .long W13
46−w26: .long W26
47−w42: .long W42
48−w42n: .long W42n
49−w46: .long W46
50−w57: .long W57
51−
5243 /*
5344 Compute partial IDCT of single row.
5445 shift = left-shift amount
@@ -60,12 +51,12 @@ w57: .long W57 · 5 unmodified lines
6051 Output in registers r4--r11
6152 */
6253 .macro idct_row shift
63− ldr lr, w46 /* lr = W4 | (W6 << 16) */
54+ ldr lr, =W46 /* lr = W4 | (W6 << 16) */
6455 mov r1, #(1<<(\shift-1))
6556 smlad r4, r2, ip, r1
6657 smlsd r7, r2, ip, r1
67− ldr ip, w13 /* ip = W1 | (W3 << 16) */
68− ldr r10,w57 /* r10 = W5 | (W7 << 16) */
58+ ldr ip, =W13 /* ip = W1 | (W3 << 16) */
59+ ldr r10,=W57 /* r10 = W5 | (W7 << 16) */
6960 smlad r5, r2, lr, r1
7061 smlsd r6, r2, lr, r1
7162
@@ -78,11 +69,11 @@ w57: .long W57 · 6 unmodified lines
7869 smlad r8, lr, r10,r8 /* B0 += W5*row[5] + W7*row[7] */
7970 smusdx r10,r3, r1 /* r10 = B2 = W5*row[1] - W1*row[3] */
8071
81− ldr r3, w42n /* r3 = -W4 | (-W2 << 16) */
72+ ldr r3, =W42n /* r3 = -W4 | (-W2 << 16) */
8273 smlad r10,lr, r2, r10 /* B2 += W7*row[5] + W3*row[7] */
8374 ldr r2, [r0, #4] /* r2 = row[6,4] */
8475 smlsdx r11,lr, ip, r11 /* B3 += W3*row[5] - W1*row[7] */
85− ldr ip, w46 /* ip = W4 | (W6 << 16) */
76+ ldr ip, =W46 /* ip = W4 | (W6 << 16) */
8677 smlad r9, lr, r1, r9 /* B1 -= W1*row[5] + W5*row[7] */
8778
8879 smlad r5, r2, r3, r5 /* A1 += -W4*row[4] - W2*row[6] */
@@ -101,12 +92,12 @@ w57: .long W57 · 12 unmodified lines
10192 Output in registers r4--r11
10293 */
10394 .macro idct_row4 shift
104− ldr lr, w46 /* lr = W4 | (W6 << 16) */
105− ldr r10,w57 /* r10 = W5 | (W7 << 16) */
95+ ldr lr, =W46 /* lr = W4 | (W6 << 16) */
96+ ldr r10,=W57 /* r10 = W5 | (W7 << 16) */
10697 mov r1, #(1<<(\shift-1))
10798 smlad r4, r2, ip, r1
10899 smlsd r7, r2, ip, r1
109− ldr ip, w13 /* ip = W1 | (W3 << 16) */
100+ ldr ip, =W13 /* ip = W1 | (W3 << 16) */
110101 smlad r5, r2, lr, r1
111102 smlsd r6, r2, lr, r1
112103 smusdx r11,r3, r10 /* r11 = B3 = W7*row[1] - W5*row[3] */
@@ -205,7 +196,7 @@ function idct_row_armv6 · 92 unmodified lines
205196 cmpeq lr, r2, lsr #16
206197 beq 1f
207198 push {r1}
208− ldr ip, w42 /* ip = W4 | (W2 << 16) */
199+ ldr ip, =W42 /* ip = W4 | (W2 << 16) */
209200 cmp lr, #0
210201 beq 2f
211202
@@ -249,7 +240,7 @@ function idct_col_armv6 · 37 unmodified lines
249240 push {r1, lr}
250241
251242 ldr r2, [r0] /* r2 = row[2,0] */
252− ldr ip, w42 /* ip = W4 | (W2 << 16) */
243+ ldr ip, =W42 /* ip = W4 | (W2 << 16) */
253244 ldr r3, [r0, #8] /* r3 = row[3,1] */
254245 idct_row COL_SHIFT
255246 pop {r1}
@@ -277,7 +268,7 @@ function idct_col_put_armv6 · 21 unmodified lines
277268 push {r1, r2, lr}
278269
279270 ldr r2, [r0] /* r2 = row[2,0] */
280− ldr ip, w42 /* ip = W4 | (W2 << 16) */
271+ ldr ip, =W42 /* ip = W4 | (W2 << 16) */
281272 ldr r3, [r0, #8] /* r3 = row[3,1] */
282273 idct_row COL_SHIFT
283274 pop {r1, r2}
@@ -307,7 +298,7 @@ function idct_col_add_armv6 · 23 unmodified lines
307298 push {r1, r2, lr}
308299
309300 ldr r2, [r0] /* r2 = row[2,0] */
310− ldr ip, w42 /* ip = W4 | (W2 << 16) */
301+ ldr ip, =W42 /* ip = W4 | (W2 << 16) */
311302 ldr r3, [r0, #8] /* r3 = row[3,1] */
312303 idct_row COL_SHIFT
313304 pop {r1, r2}
libavcodec/arm/simple_idct_neon.S+4−4
@@ -159,8 +159,8 @@ function idct_col4_neon · 158 unmodified lines
159159 vmull.s16 q15, d30, w4 /* q15 = W4*(col[0]+(1<<COL_SHIFT-1)/W4)*/
160160 vld1.64 {d8}, [r2,:64], ip /* d5 = col[3] */
161161
162− ldrd r4, [r2]
163− ldrd r6, [r2, #16]
162+ ldrd r4, r5, [r2]
163+ ldrd r6, r7, [r2, #16]
164164 orrs r4, r4, r5
165165
166166 idct_col4_top
@@ -176,7 +176,7 @@ function idct_col4_neon · 9 unmodified lines
176176 vadd.i32 q14, q14, q7
177177
178178 1: orrs r6, r6, r7
179− ldrd r4, [r2, #16]
179+ ldrd r4, r5, [r2, #16]
180180 it eq
181181 addeq r2, r2, #16
182182 beq 2f
@@ -188,7 +188,7 @@ function idct_col4_neon · 5 unmodified lines
188188 vmlal.s16 q6, d5, w3 /* q6 += W3 * col[5] */
189189
190190 2: orrs r4, r4, r5
191− ldrd r4, [r2, #16]
191+ ldrd r4, r5, [r2, #16]
192192 it eq
193193 addeq r2, r2, #16
194194 beq 3f
libavcodec/dca.c+5−2479
@@ -1,5 +1,5 @@
11 /*
2− * DCA compatible decoder
2+ * DCA compatible decoder data
33 * Copyright (C) 2004 Gildas Bazin
44 * Copyright (C) 2004 Benjamin Zores
55 * Copyright (C) 2006 Benjamin Larsson
@@ -22,2486 +22,12 @@ · 16 unmodified lines
2222 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2323 */
2424
25−#include <math.h>
26−#include <stddef.h>
27−#include <stdio.h>
25+#include <stdint.h>
2826
29−#include "libavutil/common.h"
30−#include "libavutil/float_dsp.h"
31−#include "libavutil/intmath.h"
32−#include "libavutil/intreadwrite.h"
33−#include "libavutil/mathematics.h"
34−#include "libavutil/audioconvert.h"
35−#include "avcodec.h"
36−#include "dsputil.h"
37−#include "fft.h"
38−#include "get_bits.h"
39−#include "put_bits.h"
40−#include "dcadata.h"
41−#include "dcahuff.h"
4227 #include "dca.h"
43−#include "dca_parser.h"
44−#include "synth_filter.h"
45−#include "dcadsp.h"
46−#include "fmtconvert.h"
4728
48−#if ARCH_ARM
49−# include "arm/dca.h"
50−#endif
51−
52−//#define TRACE
53−
54−#define DCA_PRIM_CHANNELS_MAX (7)
55−#define DCA_SUBBANDS (64)
56−#define DCA_ABITS_MAX (32) /* Should be 28 */
57−#define DCA_SUBSUBFRAMES_MAX (4)
58−#define DCA_SUBFRAMES_MAX (16)
59−#define DCA_BLOCKS_MAX (16)
60−#define DCA_LFE_MAX (3)
61−#define DCA_CHSETS_MAX (4)
62−#define DCA_CHSET_CHANS_MAX (8)
63−
64−enum DCAMode {
65− DCA_MONO = 0,
66− DCA_CHANNEL,
67− DCA_STEREO,
68− DCA_STEREO_SUMDIFF,
69− DCA_STEREO_TOTAL,
70− DCA_3F,
71− DCA_2F1R,
72− DCA_3F1R,
73− DCA_2F2R,
74− DCA_3F2R,
75− DCA_4F2R
76−};
77−
78−/* these are unconfirmed but should be mostly correct */
79−enum DCAExSSSpeakerMask {
80− DCA_EXSS_FRONT_CENTER = 0x0001,
81− DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
82− DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
83− DCA_EXSS_LFE = 0x0008,
84− DCA_EXSS_REAR_CENTER = 0x0010,
85− DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
86− DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
87− DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
88− DCA_EXSS_OVERHEAD = 0x0100,
89− DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
90− DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
91− DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
92− DCA_EXSS_LFE2 = 0x1000,
93− DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
94− DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
95− DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
96−};
97−
98−enum DCAXxchSpeakerMask {
99− DCA_XXCH_FRONT_CENTER = 0x0000001,
100− DCA_XXCH_FRONT_LEFT = 0x0000002,
101− DCA_XXCH_FRONT_RIGHT = 0x0000004,
102− DCA_XXCH_SIDE_REAR_LEFT = 0x0000008,
103− DCA_XXCH_SIDE_REAR_RIGHT = 0x0000010,
104− DCA_XXCH_LFE1 = 0x0000020,
105− DCA_XXCH_REAR_CENTER = 0x0000040,
106− DCA_XXCH_SURROUND_REAR_LEFT = 0x0000080,
107− DCA_XXCH_SURROUND_REAR_RIGHT = 0x0000100,
108− DCA_XXCH_SIDE_SURROUND_LEFT = 0x0000200,
109− DCA_XXCH_SIDE_SURROUND_RIGHT = 0x0000400,
110− DCA_XXCH_FRONT_CENTER_LEFT = 0x0000800,
111− DCA_XXCH_FRONT_CENTER_RIGHT = 0x0001000,
112− DCA_XXCH_FRONT_HIGH_LEFT = 0x0002000,
113− DCA_XXCH_FRONT_HIGH_CENTER = 0x0004000,
114− DCA_XXCH_FRONT_HIGH_RIGHT = 0x0008000,
115− DCA_XXCH_LFE2 = 0x0010000,
116− DCA_XXCH_SIDE_FRONT_LEFT = 0x0020000,
117− DCA_XXCH_SIDE_FRONT_RIGHT = 0x0040000,
118− DCA_XXCH_OVERHEAD = 0x0080000,
119− DCA_XXCH_SIDE_HIGH_LEFT = 0x0100000,
120− DCA_XXCH_SIDE_HIGH_RIGHT = 0x0200000,
121− DCA_XXCH_REAR_HIGH_CENTER = 0x0400000,
122− DCA_XXCH_REAR_HIGH_LEFT = 0x0800000,
123− DCA_XXCH_REAR_HIGH_RIGHT = 0x1000000,
124− DCA_XXCH_REAR_LOW_CENTER = 0x2000000,
125− DCA_XXCH_REAR_LOW_LEFT = 0x4000000,
126− DCA_XXCH_REAR_LOW_RIGHT = 0x8000000,
127−};
128−
129−static const uint32_t map_xxch_to_native[28] = {
130− AV_CH_FRONT_CENTER,
131− AV_CH_FRONT_LEFT,
132− AV_CH_FRONT_RIGHT,
133− AV_CH_SIDE_LEFT,
134− AV_CH_SIDE_RIGHT,
135− AV_CH_LOW_FREQUENCY,
136− AV_CH_BACK_CENTER,
137− AV_CH_BACK_LEFT,
138− AV_CH_BACK_RIGHT,
139− AV_CH_SIDE_LEFT, /* side surround left -- dup sur side L */
140− AV_CH_SIDE_RIGHT, /* side surround right -- dup sur side R */
141− AV_CH_FRONT_LEFT_OF_CENTER,
142− AV_CH_FRONT_RIGHT_OF_CENTER,
143− AV_CH_TOP_FRONT_LEFT,
144− AV_CH_TOP_FRONT_CENTER,
145− AV_CH_TOP_FRONT_RIGHT,
146− AV_CH_LOW_FREQUENCY, /* lfe2 -- duplicate lfe1 position */
147− AV_CH_FRONT_LEFT_OF_CENTER, /* side front left -- dup front cntr L */
148− AV_CH_FRONT_RIGHT_OF_CENTER,/* side front right -- dup front cntr R */
149− AV_CH_TOP_CENTER, /* overhead */
150− AV_CH_TOP_FRONT_LEFT, /* side high left -- dup */
151− AV_CH_TOP_FRONT_RIGHT, /* side high right -- dup */
152− AV_CH_TOP_BACK_CENTER,
153− AV_CH_TOP_BACK_LEFT,
154− AV_CH_TOP_BACK_RIGHT,
155− AV_CH_BACK_CENTER, /* rear low center -- dup */
156− AV_CH_BACK_LEFT, /* rear low left -- dup */
157− AV_CH_BACK_RIGHT /* read low right -- dup */
158−};
159−
160−enum DCAExtensionMask {
161− DCA_EXT_CORE = 0x001, ///< core in core substream
162− DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
163− DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
164− DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
165− DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
166− DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
167− DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
168− DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
169− DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
170− DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
171−};
172−
173−/* -1 are reserved or unknown */
174−static const int dca_ext_audio_descr_mask[] = {
175− DCA_EXT_XCH,
176− -1,
177− DCA_EXT_X96,
178− DCA_EXT_XCH | DCA_EXT_X96,
179− -1,
180− -1,
181− DCA_EXT_XXCH,
182− -1,
183−};
184−
185−/* extensions that reside in core substream */
186−#define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
187−
188−/* Tables for mapping dts channel configurations to libavcodec multichannel api.
189− * Some compromises have been made for special configurations. Most configurations
190− * are never used so complete accuracy is not needed.
191− *
192− * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
193− * S -> side, when both rear and back are configured move one of them to the side channel
194− * OV -> center back
195− * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
196− */
197−static const uint64_t dca_core_channel_layout[] = {
198− AV_CH_FRONT_CENTER, ///< 1, A
199− AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
200− AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
201− AV_CH_LAYOUT_STEREO, ///< 2, (L + R) + (L - R) (sum-difference)
202− AV_CH_LAYOUT_STEREO, ///< 2, LT + RT (left and right total)
203− AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER, ///< 3, C + L + R
204− AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER, ///< 3, L + R + S
205− AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 4, C + L + R + S
206− AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 4, L + R + SL + SR
207−
208− AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
209− AV_CH_SIDE_RIGHT, ///< 5, C + L + R + SL + SR
210−
211− AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
212− AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
213−
214− AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
215− AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 6, C + L + R + LR + RR + OV
216−
217− AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
218− AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER |
219− AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 6, CF + CR + LF + RF + LR + RR
220−
221− AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
222− AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
223− AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
224−
225− AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
226− AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
227− AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
228−
229− AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
230− AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
231− AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT, ///< 8, CL + C + CR + L + R + SL + S + SR
232−};
233−
234−static const int8_t dca_lfe_index[] = {
235− 1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
236−};
237−
238−static const int8_t dca_channel_reorder_lfe[][9] = {
239− { 0, -1, -1, -1, -1, -1, -1, -1, -1},
240− { 0, 1, -1, -1, -1, -1, -1, -1, -1},
241− { 0, 1, -1, -1, -1, -1, -1, -1, -1},
242− { 0, 1, -1, -1, -1, -1, -1, -1, -1},
243− { 0, 1, -1, -1, -1, -1, -1, -1, -1},
244− { 2, 0, 1, -1, -1, -1, -1, -1, -1},
245− { 0, 1, 3, -1, -1, -1, -1, -1, -1},
246− { 2, 0, 1, 4, -1, -1, -1, -1, -1},
247− { 0, 1, 3, 4, -1, -1, -1, -1, -1},
248− { 2, 0, 1, 4, 5, -1, -1, -1, -1},
249− { 3, 4, 0, 1, 5, 6, -1, -1, -1},
250− { 2, 0, 1, 4, 5, 6, -1, -1, -1},
251− { 0, 6, 4, 5, 2, 3, -1, -1, -1},
252− { 4, 2, 5, 0, 1, 6, 7, -1, -1},
253− { 5, 6, 0, 1, 7, 3, 8, 4, -1},
254− { 4, 2, 5, 0, 1, 6, 8, 7, -1},
255−};
256−
257−static const int8_t dca_channel_reorder_lfe_xch[][9] = {
258− { 0, 2, -1, -1, -1, -1, -1, -1, -1},
259− { 0, 1, 3, -1, -1, -1, -1, -1, -1},
260− { 0, 1, 3, -1, -1, -1, -1, -1, -1},
261− { 0, 1, 3, -1, -1, -1, -1, -1, -1},
262− { 0, 1, 3, -1, -1, -1, -1, -1, -1},
263− { 2, 0, 1, 4, -1, -1, -1, -1, -1},
264− { 0, 1, 3, 4, -1, -1, -1, -1, -1},
265− { 2, 0, 1, 4, 5, -1, -1, -1, -1},
266− { 0, 1, 4, 5, 3, -1, -1, -1, -1},
267− { 2, 0, 1, 5, 6, 4, -1, -1, -1},
268− { 3, 4, 0, 1, 6, 7, 5, -1, -1},
269− { 2, 0, 1, 4, 5, 6, 7, -1, -1},
270− { 0, 6, 4, 5, 2, 3, 7, -1, -1},
271− { 4, 2, 5, 0, 1, 7, 8, 6, -1},
272− { 5, 6, 0, 1, 8, 3, 9, 4, 7},
273− { 4, 2, 5, 0, 1, 6, 9, 8, 7},
274−};
275−
276−static const int8_t dca_channel_reorder_nolfe[][9] = {
277− { 0, -1, -1, -1, -1, -1, -1, -1, -1},
278− { 0, 1, -1, -1, -1, -1, -1, -1, -1},
279− { 0, 1, -1, -1, -1, -1, -1, -1, -1},
280− { 0, 1, -1, -1, -1, -1, -1, -1, -1},
281− { 0, 1, -1, -1, -1, -1, -1, -1, -1},
282− { 2, 0, 1, -1, -1, -1, -1, -1, -1},
283− { 0, 1, 2, -1, -1, -1, -1, -1, -1},
284− { 2, 0, 1, 3, -1, -1, -1, -1, -1},
285− { 0, 1, 2, 3, -1, -1, -1, -1, -1},
286− { 2, 0, 1, 3, 4, -1, -1, -1, -1},
287− { 2, 3, 0, 1, 4, 5, -1, -1, -1},
288− { 2, 0, 1, 3, 4, 5, -1, -1, -1},
289− { 0, 5, 3, 4, 1, 2, -1, -1, -1},
290− { 3, 2, 4, 0, 1, 5, 6, -1, -1},
291− { 4, 5, 0, 1, 6, 2, 7, 3, -1},
292− { 3, 2, 4, 0, 1, 5, 7, 6, -1},
293−};
294−
295−static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
296− { 0, 1, -1, -1, -1, -1, -1, -1, -1},
297− { 0, 1, 2, -1, -1, -1, -1, -1, -1},
298− { 0, 1, 2, -1, -1, -1, -1, -1, -1},
299− { 0, 1, 2, -1, -1, -1, -1, -1, -1},
300− { 0, 1, 2, -1, -1, -1, -1, -1, -1},
301− { 2, 0, 1, 3, -1, -1, -1, -1, -1},
302− { 0, 1, 2, 3, -1, -1, -1, -1, -1},
303− { 2, 0, 1, 3, 4, -1, -1, -1, -1},
304− { 0, 1, 3, 4, 2, -1, -1, -1, -1},
305− { 2, 0, 1, 4, 5, 3, -1, -1, -1},
306− { 2, 3, 0, 1, 5, 6, 4, -1, -1},
307− { 2, 0, 1, 3, 4, 5, 6, -1, -1},
308− { 0, 5, 3, 4, 1, 2, 6, -1, -1},
309− { 3, 2, 4, 0, 1, 6, 7, 5, -1},
310− { 4, 5, 0, 1, 7, 2, 8, 3, 6},
311− { 3, 2, 4, 0, 1, 5, 8, 7, 6},
312−};
313−
314−#define DCA_DOLBY 101 /* FIXME */
315−
316−#define DCA_CHANNEL_BITS 6
317−#define DCA_CHANNEL_MASK 0x3F
318−
319−#define DCA_LFE 0x80
320−
321−#define HEADER_SIZE 14
322−
323−#define DCA_MAX_FRAME_SIZE 16384
324−#define DCA_MAX_EXSS_HEADER_SIZE 4096
325−
326−#define DCA_BUFFER_PADDING_SIZE 1024
327−
328−/** Bit allocation */
329−typedef struct {
330− int offset; ///< code values offset
331− int maxbits[8]; ///< max bits in VLC
332− int wrap; ///< wrap for get_vlc2()
333− VLC vlc[8]; ///< actual codes
334−} BitAlloc;
335−
336−static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
337−static BitAlloc dca_tmode; ///< transition mode VLCs
338−static BitAlloc dca_scalefactor; ///< scalefactor VLCs
339−static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
340−
341−static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
342− int idx)
343−{
344− return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
345− ba->offset;
346−}
347−
348−typedef struct {
349− AVCodecContext *avctx;
350− AVFrame frame;
351− /* Frame header */
352− int frame_type; ///< type of the current frame
353− int samples_deficit; ///< deficit sample count
354− int crc_present; ///< crc is present in the bitstream
355− int sample_blocks; ///< number of PCM sample blocks
356− int frame_size; ///< primary frame byte size
357− int amode; ///< audio channels arrangement
358− int sample_rate; ///< audio sampling rate
359− int bit_rate; ///< transmission bit rate
360− int bit_rate_index; ///< transmission bit rate index
361−
362− int downmix; ///< embedded downmix enabled
363− int dynrange; ///< embedded dynamic range flag
364− int timestamp; ///< embedded time stamp flag
365− int aux_data; ///< auxiliary data flag
366− int hdcd; ///< source material is mastered in HDCD
367− int ext_descr; ///< extension audio descriptor flag
368− int ext_coding; ///< extended coding flag
369− int aspf; ///< audio sync word insertion flag
370− int lfe; ///< low frequency effects flag
371− int predictor_history; ///< predictor history flag
372− int header_crc; ///< header crc check bytes
373− int multirate_inter; ///< multirate interpolator switch
374− int version; ///< encoder software revision
375− int copy_history; ///< copy history
376− int source_pcm_res; ///< source pcm resolution
377− int front_sum; ///< front sum/difference flag
378− int surround_sum; ///< surround sum/difference flag
379− int dialog_norm; ///< dialog normalisation parameter
380−
381− /* Primary audio coding header */
382− int subframes; ///< number of subframes
383− int total_channels; ///< number of channels including extensions
384− int prim_channels; ///< number of primary audio channels
385− int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
386− int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
387− int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
388− int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
389− int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
390− int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
391− int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
392− float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
393−
394− /* Primary audio coding side information */
395− int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
396− int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
397− int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
398− int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
399− int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
400− int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
401− int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient)
402− int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
403− int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
404− int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients
405− int dynrange_coef; ///< dynamic range coefficient
406−
407− int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
408−
409− float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
410− int lfe_scale_factor;
411−
412− /* Subband samples history (for ADPCM) */
413− DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
414− DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
415− DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
416− int hist_index[DCA_PRIM_CHANNELS_MAX];
417− DECLARE_ALIGNED(32, float, raXin)[32];
418−
419− int output; ///< type of output
420− float scale_bias; ///< output scale
421−
422− DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
423− DECLARE_ALIGNED(32, float, samples)[(DCA_PRIM_CHANNELS_MAX + 1) * 256];
424− const float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
425−
426− uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
427− int dca_buffer_size; ///< how much data is in the dca_buffer
428−
429− const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
430− GetBitContext gb;
431− /* Current position in DCA frame */
432− int current_subframe;
433− int current_subsubframe;
434−
435− int core_ext_mask; ///< present extensions in the core substream
436−
437− /* XCh extension information */
438− int xch_present; ///< XCh extension present and valid
439− int xch_base_channel; ///< index of first (only) channel containing XCH data
440−
441− /* XXCH extension information */
442− int xxch_chset;
443− int xxch_nbits_spk_mask;
444− uint32_t xxch_core_spkmask;
445− uint32_t xxch_spk_masks[4]; /* speaker masks, last element is core mask */
446− int xxch_chset_nch[4];
447− float xxch_dmix_sf[DCA_CHSETS_MAX];
448−
449− uint32_t xxch_downmix; /* downmix enabled per channel set */
450− uint32_t xxch_dmix_embedded; /* lower layer has mix pre-embedded, per chset */
451− float xxch_dmix_coeff[DCA_PRIM_CHANNELS_MAX][32]; /* worst case sizing */
452−
453− int8_t xxch_order_tab[32];
454− int8_t lfe_index;
455−
456− /* ExSS header parser */
457− int static_fields; ///< static fields present
458− int mix_metadata; ///< mixing metadata present
459− int num_mix_configs; ///< number of mix out configurations
460− int mix_config_num_ch[4]; ///< number of channels in each mix out configuration
461−
462− int profile;
463−
464− int debug_flag; ///< used for suppressing repeated error messages output
465− AVFloatDSPContext fdsp;
466− FFTContext imdct;
467− SynthFilterContext synth;
468− DCADSPContext dcadsp;
469− FmtConvertContext fmt_conv;
470−} DCAContext;
471−
472−static const uint16_t dca_vlc_offs[] = {
473− 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
474− 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
475− 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
476− 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
477− 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
478− 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
479−};
480−
481−static av_cold void dca_init_vlcs(void)
482−{
483− static int vlcs_initialized = 0;
484− int i, j, c = 14;
485− static VLC_TYPE dca_table[23622][2];
486−
487− if (vlcs_initialized)
488− return;
489−
490− dca_bitalloc_index.offset = 1;
491− dca_bitalloc_index.wrap = 2;
492− for (i = 0; i < 5; i++) {
493− dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
494− dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
495− init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
496− bitalloc_12_bits[i], 1, 1,
497− bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
498− }
499− dca_scalefactor.offset = -64;
500− dca_scalefactor.wrap = 2;
501− for (i = 0; i < 5; i++) {
502− dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
503− dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
504− init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
505− scales_bits[i], 1, 1,
506− scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
507− }
508− dca_tmode.offset = 0;
509− dca_tmode.wrap = 1;
510− for (i = 0; i < 4; i++) {
511− dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
512− dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
513− init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
514− tmode_bits[i], 1, 1,
515− tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
516− }
517−
518− for (i = 0; i < 10; i++)
519− for (j = 0; j < 7; j++) {
520− if (!bitalloc_codes[i][j])
521− break;
522− dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
523− dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
524− dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
525− dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
526−
527− init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
528− bitalloc_sizes[i],
529− bitalloc_bits[i][j], 1, 1,
530− bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
531− c++;
532− }
533− vlcs_initialized = 1;
534−}
535−
536−static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
537−{
538− while (len--)
539− *dst++ = get_bits(gb, bits);
540−}
541−
542−static inline int dca_xxch2index(DCAContext *s, int xxch_ch)
543−{
544− int i, base, mask;
545−
546− /* locate channel set containing the channel */
547− for (i = -1, base = 0, mask = (s->xxch_core_spkmask & ~DCA_XXCH_LFE1);
548− i <= s->xxch_chset && !(mask & xxch_ch); mask = s->xxch_spk_masks[++i])
549− base += av_popcount(mask);
550−
551− return base + av_popcount(mask & (xxch_ch - 1));
552−}
553−
554−static int dca_parse_audio_coding_header(DCAContext *s, int base_channel,
555− int xxch)
556−{
557− int i, j;
558− static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
559− static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
560− static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
561− int hdr_pos = 0, hdr_size = 0;
562− float sign, mag, scale_factor;
563− int this_chans, acc_mask;
564− int embedded_downmix;
565− int nchans, mask[8];
566− int coeff, ichan;
567−
568− /* xxch has arbitrary sized audio coding headers */
569− if (xxch) {
570− hdr_pos = get_bits_count(&s->gb);
571− hdr_size = get_bits(&s->gb, 7) + 1;
572− }
573−
574− nchans = get_bits(&s->gb, 3) + 1;
575− s->total_channels = nchans + base_channel;
576− s->prim_channels = s->total_channels;
577−
578− /* obtain speaker layout mask & downmix coefficients for XXCH */
579− if (xxch) {
580− acc_mask = s->xxch_core_spkmask;
581−
582− this_chans = get_bits(&s->gb, s->xxch_nbits_spk_mask - 6) << 6;
583− s->xxch_spk_masks[s->xxch_chset] = this_chans;
584− s->xxch_chset_nch[s->xxch_chset] = nchans;
585−
586− for (i = 0; i <= s->xxch_chset; i++)
587− acc_mask |= s->xxch_spk_masks[i];
588−
589− /* check for downmixing information */
590− if (get_bits1(&s->gb)) {
591− embedded_downmix = get_bits1(&s->gb);
592− scale_factor =
593− 1.0f / dca_downmix_scale_factors[(get_bits(&s->gb, 6) - 1) << 2];
594−
595− s->xxch_dmix_sf[s->xxch_chset] = scale_factor;
596−
597− for (i = base_channel; i < s->prim_channels; i++) {
598− s->xxch_downmix |= (1 << i);
599− mask[i] = get_bits(&s->gb, s->xxch_nbits_spk_mask);
600− }
601−
602− for (j = base_channel; j < s->prim_channels; j++) {
603− memset(s->xxch_dmix_coeff[j], 0, sizeof(s->xxch_dmix_coeff[0]));
604− s->xxch_dmix_embedded |= (embedded_downmix << j);
605− for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
606− if (mask[j] & (1 << i)) {
607− if ((1 << i) == DCA_XXCH_LFE1) {
608− av_log(s->avctx, AV_LOG_WARNING,
609− "DCA-XXCH: dmix to LFE1 not supported.\n");
610− continue;
611− }
612−
613− coeff = get_bits(&s->gb, 7);
614− sign = (coeff & 64) ? 1.0 : -1.0;
615− mag = dca_downmix_scale_factors[((coeff & 63) - 1) << 2];
616− ichan = dca_xxch2index(s, 1 << i);
617− s->xxch_dmix_coeff[j][ichan] = sign * mag;
618− }
619− }
620− }
621− }
622− }
623−
624− if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
625− s->prim_channels = DCA_PRIM_CHANNELS_MAX;
626−
627−
628− for (i = base_channel; i < s->prim_channels; i++) {
629− s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
630− if (s->subband_activity[i] > DCA_SUBBANDS)
631− s->subband_activity[i] = DCA_SUBBANDS;
632− }
633− for (i = base_channel; i < s->prim_channels; i++) {
634− s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
635− if (s->vq_start_subband[i] > DCA_SUBBANDS)
636− s->vq_start_subband[i] = DCA_SUBBANDS;
637− }
638− get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
639− get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
640− get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
641− get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
642−
643− /* Get codebooks quantization indexes */
644− if (!base_channel)
645− memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
646− for (j = 1; j < 11; j++)
647− for (i = base_channel; i < s->prim_channels; i++)
648− s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
649−
650− /* Get scale factor adjustment */
651− for (j = 0; j < 11; j++)
652− for (i = base_channel; i < s->prim_channels; i++)
653− s->scalefactor_adj[i][j] = 1;
654−
655− for (j = 1; j < 11; j++)
656− for (i = base_channel; i < s->prim_channels; i++)
657− if (s->quant_index_huffman[i][j] < thr[j])
658− s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
659−
660− if (!xxch) {
661− if (s->crc_present) {
662− /* Audio header CRC check */
663− get_bits(&s->gb, 16);
664− }
665− } else {
666− /* Skip to the end of the header, also ignore CRC if present */
667− i = get_bits_count(&s->gb);
668− if (hdr_pos + 8 * hdr_size > i)
669− skip_bits_long(&s->gb, hdr_pos + 8 * hdr_size - i);
670− }
671−
672− s->current_subframe = 0;
673− s->current_subsubframe = 0;
674−
675−#ifdef TRACE
676− av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
677− av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
678− for (i = base_channel; i < s->prim_channels; i++) {
679− av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n",
680− s->subband_activity[i]);
681− av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n",
682− s->vq_start_subband[i]);
683− av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n",
684− s->joint_intensity[i]);
685− av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n",
686− s->transient_huffman[i]);
687− av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n",
688− s->scalefactor_huffman[i]);
689− av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n",
690− s->bitalloc_huffman[i]);
691− av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
692− for (j = 0; j < 11; j++)
693− av_log(s->avctx, AV_LOG_DEBUG, " %i", s->quant_index_huffman[i][j]);
694− av_log(s->avctx, AV_LOG_DEBUG, "\n");
695− av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
696− for (j = 0; j < 11; j++)
697− av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
698− av_log(s->avctx, AV_LOG_DEBUG, "\n");
699− }
700−#endif
701−
702− return 0;
703−}
704−
705−static int dca_parse_frame_header(DCAContext *s)
706−{
707− init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
708−
709− /* Sync code */
710− skip_bits_long(&s->gb, 32);
711−
712− /* Frame header */
713− s->frame_type = get_bits(&s->gb, 1);
714− s->samples_deficit = get_bits(&s->gb, 5) + 1;
715− s->crc_present = get_bits(&s->gb, 1);
716− s->sample_blocks = get_bits(&s->gb, 7) + 1;
717− s->frame_size = get_bits(&s->gb, 14) + 1;
718− if (s->frame_size < 95)
719− return AVERROR_INVALIDDATA;
720− s->amode = get_bits(&s->gb, 6);
721− s->sample_rate = dca_sample_rates[get_bits(&s->gb, 4)];
722− if (!s->sample_rate)
723− return AVERROR_INVALIDDATA;
724− s->bit_rate_index = get_bits(&s->gb, 5);
725− s->bit_rate = dca_bit_rates[s->bit_rate_index];
726− if (!s->bit_rate)
727− return AVERROR_INVALIDDATA;
728−
729− s->downmix = get_bits(&s->gb, 1); /* note: this is FixedBit == 0 */
730− s->dynrange = get_bits(&s->gb, 1);
731− s->timestamp = get_bits(&s->gb, 1);
732− s->aux_data = get_bits(&s->gb, 1);
733− s->hdcd = get_bits(&s->gb, 1);
734− s->ext_descr = get_bits(&s->gb, 3);
735− s->ext_coding = get_bits(&s->gb, 1);
736− s->aspf = get_bits(&s->gb, 1);
737− s->lfe = get_bits(&s->gb, 2);
738− s->predictor_history = get_bits(&s->gb, 1);
739−
740− /* TODO: check CRC */
741− if (s->crc_present)
742− s->header_crc = get_bits(&s->gb, 16);
743−
744− s->multirate_inter = get_bits(&s->gb, 1);
745− s->version = get_bits(&s->gb, 4);
746− s->copy_history = get_bits(&s->gb, 2);
747− s->source_pcm_res = get_bits(&s->gb, 3);
748− s->front_sum = get_bits(&s->gb, 1);
749− s->surround_sum = get_bits(&s->gb, 1);
750− s->dialog_norm = get_bits(&s->gb, 4);
751−
752− /* FIXME: channels mixing levels */
753− s->output = s->amode;
754− if (s->lfe)
755− s->output |= DCA_LFE;
756−
757−#ifdef TRACE
758− av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
759− av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
760− av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
761− av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
762− s->sample_blocks, s->sample_blocks * 32);
763− av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
764− av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
765− s->amode, dca_channels[s->amode]);
766− av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
767− s->sample_rate);
768− av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
769− s->bit_rate);
770− av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
771− av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
772− av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
773− av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
774− av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
775− av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
776− av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
777− av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
778− av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
779− av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
780− s->predictor_history);
781− av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
782− av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
783− s->multirate_inter);
784− av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
785− av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
786− av_log(s->avctx, AV_LOG_DEBUG,
787− "source pcm resolution: %i (%i bits/sample)\n",
788− s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
789− av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
790− av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
791− av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
792− av_log(s->avctx, AV_LOG_DEBUG, "\n");
793−#endif
794−
795− /* Primary audio coding header */
796− s->subframes = get_bits(&s->gb, 4) + 1;
797−
798− return dca_parse_audio_coding_header(s, 0, 0);
799−}
800−
801−
802−static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
803−{
804− if (level < 5) {
805− /* huffman encoded */
806− value += get_bitalloc(gb, &dca_scalefactor, level);
807− value = av_clip(value, 0, (1 << log2range) - 1);
808− } else if (level < 8) {
809− if (level + 1 > log2range) {
810− skip_bits(gb, level + 1 - log2range);
811− value = get_bits(gb, log2range);
812− } else {
813− value = get_bits(gb, level + 1);
814− }
815− }
816− return value;
817−}
818−
819−static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
820−{
821− /* Primary audio coding side information */
822− int j, k;
823−
824− if (get_bits_left(&s->gb) < 0)
825− return AVERROR_INVALIDDATA;
826−
827− if (!base_channel) {
828− s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
829− s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
830− }
831−
832− for (j = base_channel; j < s->prim_channels; j++) {
833− for (k = 0; k < s->subband_activity[j]; k++)
834− s->prediction_mode[j][k] = get_bits(&s->gb, 1);
835− }
836−
837− /* Get prediction codebook */
838− for (j = base_channel; j < s->prim_channels; j++) {
839− for (k = 0; k < s->subband_activity[j]; k++) {
840− if (s->prediction_mode[j][k] > 0) {
841− /* (Prediction coefficient VQ address) */
842− s->prediction_vq[j][k] = get_bits(&s->gb, 12);
843− }
844− }
845− }
846−
847− /* Bit allocation index */
848− for (j = base_channel; j < s->prim_channels; j++) {
849− for (k = 0; k < s->vq_start_subband[j]; k++) {
850− if (s->bitalloc_huffman[j] == 6)
851− s->bitalloc[j][k] = get_bits(&s->gb, 5);
852− else if (s->bitalloc_huffman[j] == 5)
853− s->bitalloc[j][k] = get_bits(&s->gb, 4);
854− else if (s->bitalloc_huffman[j] == 7) {
855− av_log(s->avctx, AV_LOG_ERROR,
856− "Invalid bit allocation index\n");
857− return AVERROR_INVALIDDATA;
858− } else {
859− s->bitalloc[j][k] =
860− get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
861− }
862−
863− if (s->bitalloc[j][k] > 26) {
864− // av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index [%i][%i] too big (%i)\n",
865− // j, k, s->bitalloc[j][k]);
866− return AVERROR_INVALIDDATA;
867− }
868− }
869− }
870−
871− /* Transition mode */
872− for (j = base_channel; j < s->prim_channels; j++) {
873− for (k = 0; k < s->subband_activity[j]; k++) {
874− s->transition_mode[j][k] = 0;
875− if (s->subsubframes[s->current_subframe] > 1 &&
876− k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
877− s->transition_mode[j][k] =
878− get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
879− }
880− }
881− }
882−
883− if (get_bits_left(&s->gb) < 0)
884− return AVERROR_INVALIDDATA;
885−
886− for (j = base_channel; j < s->prim_channels; j++) {
887− const uint32_t *scale_table;
888− int scale_sum, log_size;
889−
890− memset(s->scale_factor[j], 0,
891− s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
892−
893− if (s->scalefactor_huffman[j] == 6) {
894− scale_table = scale_factor_quant7;
895− log_size = 7;
896− } else {
897− scale_table = scale_factor_quant6;
898− log_size = 6;
899− }
900−
901− /* When huffman coded, only the difference is encoded */
902− scale_sum = 0;
903−
904− for (k = 0; k < s->subband_activity[j]; k++) {
905− if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
906− scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
907− s->scale_factor[j][k][0] = scale_table[scale_sum];
908− }
909−
910− if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
911− /* Get second scale factor */
912− scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
913− s->scale_factor[j][k][1] = scale_table[scale_sum];
914− }
915− }
916− }
917−
918− /* Joint subband scale factor codebook select */
919− for (j = base_channel; j < s->prim_channels; j++) {
920− /* Transmitted only if joint subband coding enabled */
921− if (s->joint_intensity[j] > 0)
922− s->joint_huff[j] = get_bits(&s->gb, 3);
923− }
924−
925− if (get_bits_left(&s->gb) < 0)
926− return AVERROR_INVALIDDATA;
927−
928− /* Scale factors for joint subband coding */
929− for (j = base_channel; j < s->prim_channels; j++) {
930− int source_channel;
931−
932− /* Transmitted only if joint subband coding enabled */
933− if (s->joint_intensity[j] > 0) {
934− int scale = 0;
935− source_channel = s->joint_intensity[j] - 1;
936−
937− /* When huffman coded, only the difference is encoded
938− * (is this valid as well for joint scales ???) */
939−
940− for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
941− scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
942− s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
943− }
944−
945− if (!(s->debug_flag & 0x02)) {
946− av_log(s->avctx, AV_LOG_DEBUG,
947− "Joint stereo coding not supported\n");
948− s->debug_flag |= 0x02;
949− }
950− }
951− }
952−
953− /* Stereo downmix coefficients */
954− if (!base_channel && s->prim_channels > 2) {
955− if (s->downmix) {
956− for (j = base_channel; j < s->prim_channels; j++) {
957− s->downmix_coef[j][0] = get_bits(&s->gb, 7);
958− s->downmix_coef[j][1] = get_bits(&s->gb, 7);
959− }
960− } else {
961− int am = s->amode & DCA_CHANNEL_MASK;
962− if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
963− av_log(s->avctx, AV_LOG_ERROR,
964− "Invalid channel mode %d\n", am);
965− return AVERROR_INVALIDDATA;
966− }
967− for (j = base_channel; j < FFMIN(s->prim_channels, FF_ARRAY_ELEMS(dca_default_coeffs[am])); j++) {
968− s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
969− s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
970− }
971− }
972− }
973−
974− /* Dynamic range coefficient */
975− if (!base_channel && s->dynrange)
976− s->dynrange_coef = get_bits(&s->gb, 8);
977−
978− /* Side information CRC check word */
979− if (s->crc_present) {
980− get_bits(&s->gb, 16);
981− }
982−
983− /*
984− * Primary audio data arrays
985− */
986−
987− /* VQ encoded high frequency subbands */
988− for (j = base_channel; j < s->prim_channels; j++)
989− for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
990− /* 1 vector -> 32 samples */
991− s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
992−
993− /* Low frequency effect data */
994− if (!base_channel && s->lfe) {
995− int quant7;
996− /* LFE samples */
997− int lfe_samples = 2 * s->lfe * (4 + block_index);
998− int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
999− float lfe_scale;
1000−
1001− for (j = lfe_samples; j < lfe_end_sample; j++) {
1002− /* Signed 8 bits int */
1003− s->lfe_data[j] = get_sbits(&s->gb, 8);
1004− }
1005−
1006− /* Scale factor index */
1007− quant7 = get_bits(&s->gb, 8);
1008− if (quant7 > 127) {
1009− av_log_ask_for_sample(s->avctx, "LFEScaleIndex larger than 127\n");
1010− return AVERROR_INVALIDDATA;
1011− }
1012− s->lfe_scale_factor = scale_factor_quant7[quant7];
1013−
1014− /* Quantization step size * scale factor */
1015− lfe_scale = 0.035 * s->lfe_scale_factor;
1016−
1017− for (j = lfe_samples; j < lfe_end_sample; j++)
1018− s->lfe_data[j] *= lfe_scale;
1019− }
1020−
1021−#ifdef TRACE
1022− av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
1023− s->subsubframes[s->current_subframe]);
1024− av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
1025− s->partial_samples[s->current_subframe]);
1026−
1027− for (j = base_channel; j < s->prim_channels; j++) {
1028− av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
1029− for (k = 0; k < s->subband_activity[j]; k++)
1030− av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
1031− av_log(s->avctx, AV_LOG_DEBUG, "\n");
1032− }
1033− for (j = base_channel; j < s->prim_channels; j++) {
1034− for (k = 0; k < s->subband_activity[j]; k++)
1035− av_log(s->avctx, AV_LOG_DEBUG,
1036− "prediction coefs: %f, %f, %f, %f\n",
1037− (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
1038− (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
1039− (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
1040− (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
1041− }
1042− for (j = base_channel; j < s->prim_channels; j++) {
1043− av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
1044− for (k = 0; k < s->vq_start_subband[j]; k++)
1045− av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
1046− av_log(s->avctx, AV_LOG_DEBUG, "\n");
1047− }
1048− for (j = base_channel; j < s->prim_channels; j++) {
1049− av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
1050− for (k = 0; k < s->subband_activity[j]; k++)
1051− av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
1052− av_log(s->avctx, AV_LOG_DEBUG, "\n");
1053− }
1054− for (j = base_channel; j < s->prim_channels; j++) {
1055− av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
1056− for (k = 0; k < s->subband_activity[j]; k++) {
1057− if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
1058− av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
1059− if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
1060− av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
1061− }
1062− av_log(s->avctx, AV_LOG_DEBUG, "\n");
1063− }
1064− for (j = base_channel; j < s->prim_channels; j++) {
1065− if (s->joint_intensity[j] > 0) {
1066− int source_channel = s->joint_intensity[j] - 1;
1067− av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
1068− for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
1069− av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
1070− av_log(s->avctx, AV_LOG_DEBUG, "\n");
1071− }
1072− }
1073− if (!base_channel && s->prim_channels > 2 && s->downmix) {
1074− av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
1075− for (j = 0; j < s->prim_channels; j++) {
1076− av_log(s->avctx, AV_LOG_DEBUG, "Channel 0, %d = %f\n", j,
1077− dca_downmix_coeffs[s->downmix_coef[j][0]]);
1078− av_log(s->avctx, AV_LOG_DEBUG, "Channel 1, %d = %f\n", j,
1079− dca_downmix_coeffs[s->downmix_coef[j][1]]);
1080− }
1081− av_log(s->avctx, AV_LOG_DEBUG, "\n");
1082− }
1083− for (j = base_channel; j < s->prim_channels; j++)
1084− for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
1085− av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
1086− if (!base_channel && s->lfe) {
1087− int lfe_samples = 2 * s->lfe * (4 + block_index);
1088− int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
1089−
1090− av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
1091− for (j = lfe_samples; j < lfe_end_sample; j++)
1092− av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
1093− av_log(s->avctx, AV_LOG_DEBUG, "\n");
1094− }
1095−#endif
1096−
1097− return 0;
1098−}
1099−
1100−static void qmf_32_subbands(DCAContext *s, int chans,
1101− float samples_in[32][8], float *samples_out,
1102− float scale)
1103−{
1104− const float *prCoeff;
1105− int i;
1106−
1107− int sb_act = s->subband_activity[chans];
1108− int subindex;
1109−
1110− scale *= sqrt(1 / 8.0);
1111−
1112− /* Select filter */
1113− if (!s->multirate_inter) /* Non-perfect reconstruction */
1114− prCoeff = fir_32bands_nonperfect;
1115− else /* Perfect reconstruction */
1116− prCoeff = fir_32bands_perfect;
1117−
1118− for (i = sb_act; i < 32; i++)
1119− s->raXin[i] = 0.0;
1120−
1121− /* Reconstructed channel sample index */
1122− for (subindex = 0; subindex < 8; subindex++) {
1123− /* Load in one sample from each subband and clear inactive subbands */
1124− for (i = 0; i < sb_act; i++) {
1125− unsigned sign = (i - 1) & 2;
1126− uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ sign << 30;
1127− AV_WN32A(&s->raXin[i], v);
1128− }
1129−
1130− s->synth.synth_filter_float(&s->imdct,
1131− s->subband_fir_hist[chans],
1132− &s->hist_index[chans],
1133− s->subband_fir_noidea[chans], prCoeff,
1134− samples_out, s->raXin, scale);
1135− samples_out += 32;
1136− }
1137−}
1138−
1139−static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
1140− int num_deci_sample, float *samples_in,
1141− float *samples_out, float scale)
1142−{
1143− /* samples_in: An array holding decimated samples.
1144− * Samples in current subframe starts from samples_in[0],
1145− * while samples_in[-1], samples_in[-2], ..., stores samples
1146− * from last subframe as history.
1147− *
1148− * samples_out: An array holding interpolated samples
1149− */
1150−
1151− int decifactor;
1152− const float *prCoeff;
1153− int deciindex;
1154−
1155− /* Select decimation filter */
1156− if (decimation_select == 1) {
1157− decifactor = 64;
1158− prCoeff = lfe_fir_128;
1159− } else {
1160− decifactor = 32;
1161− prCoeff = lfe_fir_64;
1162− }
1163− /* Interpolation */
1164− for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
1165− s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor, scale);
1166− samples_in++;
1167− samples_out += 2 * decifactor;
1168− }
1169−}
1170−
1171−/* downmixing routines */
1172−#define MIX_REAR1(samples, si1, rs, coef) \
1173− samples[i] += samples[si1] * coef[rs][0]; \
1174− samples[i+256] += samples[si1] * coef[rs][1];
1175−
1176−#define MIX_REAR2(samples, si1, si2, rs, coef) \
1177− samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs + 1][0]; \
1178− samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs + 1][1];
1179−
1180−#define MIX_FRONT3(samples, coef) \
1181− t = samples[i + c]; \
1182− u = samples[i + l]; \
1183− v = samples[i + r]; \
1184− samples[i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
1185− samples[i+256] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
1186−
1187−#define DOWNMIX_TO_STEREO(op1, op2) \
1188− for (i = 0; i < 256; i++) { \
1189− op1 \
1190− op2 \
1191− }
1192−
1193−static void dca_downmix(float *samples, int srcfmt,
1194− int downmix_coef[DCA_PRIM_CHANNELS_MAX][2],
1195− const int8_t *channel_mapping)
1196−{
1197− int c, l, r, sl, sr, s;
1198− int i;
1199− float t, u, v;
1200− float coef[DCA_PRIM_CHANNELS_MAX][2];
1201−
1202− for (i = 0; i < DCA_PRIM_CHANNELS_MAX; i++) {
1203− coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
1204− coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
1205− }
1206−
1207− switch (srcfmt) {
1208− case DCA_MONO:
1209− case DCA_CHANNEL:
1210− case DCA_STEREO_TOTAL:
1211− case DCA_STEREO_SUMDIFF:
1212− case DCA_4F2R:
1213− av_log(NULL, 0, "Not implemented!\n");
1214− break;
1215− case DCA_STEREO:
1216− break;
1217− case DCA_3F:
1218− c = channel_mapping[0] * 256;
1219− l = channel_mapping[1] * 256;
1220− r = channel_mapping[2] * 256;
1221− DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
1222− break;
1223− case DCA_2F1R:
1224− s = channel_mapping[2] * 256;
1225− DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + s, 2, coef), );
1226− break;
1227− case DCA_3F1R:
1228− c = channel_mapping[0] * 256;
1229− l = channel_mapping[1] * 256;
1230− r = channel_mapping[2] * 256;
1231− s = channel_mapping[3] * 256;
1232− DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1233− MIX_REAR1(samples, i + s, 3, coef));
1234− break;
1235− case DCA_2F2R:
1236− sl = channel_mapping[2] * 256;
1237− sr = channel_mapping[3] * 256;
1238− DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + sl, i + sr, 2, coef), );
1239− break;
1240− case DCA_3F2R:
1241− c = channel_mapping[0] * 256;
1242− l = channel_mapping[1] * 256;
1243− r = channel_mapping[2] * 256;
1244− sl = channel_mapping[3] * 256;
1245− sr = channel_mapping[4] * 256;
1246− DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1247− MIX_REAR2(samples, i + sl, i + sr, 3, coef));
1248− break;
1249− }
1250−}
1251−
1252−
1253−#ifndef decode_blockcodes
1254−/* Very compact version of the block code decoder that does not use table
1255− * look-up but is slightly slower */
1256−static int decode_blockcode(int code, int levels, int *values)
1257−{
1258− int i;
1259− int offset = (levels - 1) >> 1;
1260−
1261− for (i = 0; i < 4; i++) {
1262− int div = FASTDIV(code, levels);
1263− values[i] = code - offset - div * levels;
1264− code = div;
1265− }
1266−
1267− return code;
1268−}
1269−
1270−static int decode_blockcodes(int code1, int code2, int levels, int *values)
1271−{
1272− return decode_blockcode(code1, levels, values) |
1273− decode_blockcode(code2, levels, values + 4);
1274−}
1275−#endif
1276−
1277−static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
1278−static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
1279−
1280−#ifndef int8x8_fmul_int32
1281−static inline void int8x8_fmul_int32(float *dst, const int8_t *src, int scale)
1282−{
1283− float fscale = scale / 16.0;
1284− int i;
1285− for (i = 0; i < 8; i++)
1286− dst[i] = src[i] * fscale;
1287−}
1288−#endif
1289−
1290−static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
1291−{
1292− int k, l;
1293− int subsubframe = s->current_subsubframe;
1294−
1295− const float *quant_step_table;
1296−
1297− /* FIXME */
1298− float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1299− LOCAL_ALIGNED_16(int, block, [8]);
1300−
1301− /*
1302− * Audio data
1303− */
1304−
1305− /* Select quantization step size table */
1306− if (s->bit_rate_index == 0x1f)
1307− quant_step_table = lossless_quant_d;
1308− else
1309− quant_step_table = lossy_quant_d;
1310−
1311− for (k = base_channel; k < s->prim_channels; k++) {
1312− if (get_bits_left(&s->gb) < 0)
1313− return AVERROR_INVALIDDATA;
1314−
1315− for (l = 0; l < s->vq_start_subband[k]; l++) {
1316− int m;
1317−
1318− /* Select the mid-tread linear quantizer */
1319− int abits = s->bitalloc[k][l];
1320−
1321− float quant_step_size = quant_step_table[abits];
1322−
1323− /*
1324− * Determine quantization index code book and its type
1325− */
1326−
1327− /* Select quantization index code book */
1328− int sel = s->quant_index_huffman[k][abits];
1329−
1330− /*
1331− * Extract bits from the bit stream
1332− */
1333− if (!abits) {
1334− memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
1335− } else {
1336− /* Deal with transients */
1337− int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1338− float rscale = quant_step_size * s->scale_factor[k][l][sfi] *
1339− s->scalefactor_adj[k][sel];
1340−
1341− if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
1342− if (abits <= 7) {
1343− /* Block code */
1344− int block_code1, block_code2, size, levels, err;
1345−
1346− size = abits_sizes[abits - 1];
1347− levels = abits_levels[abits - 1];
1348−
1349− block_code1 = get_bits(&s->gb, size);
1350− block_code2 = get_bits(&s->gb, size);
1351− err = decode_blockcodes(block_code1, block_code2,
1352− levels, block);
1353− if (err) {
1354− av_log(s->avctx, AV_LOG_ERROR,
1355− "ERROR: block code look-up failed\n");
1356− return AVERROR_INVALIDDATA;
1357− }
1358− } else {
1359− /* no coding */
1360− for (m = 0; m < 8; m++)
1361− block[m] = get_sbits(&s->gb, abits - 3);
1362− }
1363− } else {
1364− /* Huffman coded */
1365− for (m = 0; m < 8; m++)
1366− block[m] = get_bitalloc(&s->gb,
1367− &dca_smpl_bitalloc[abits], sel);
1368− }
1369−
1370− s->fmt_conv.int32_to_float_fmul_scalar(subband_samples[k][l],
1371− block, rscale, 8);
1372− }
1373−
1374− /*
1375− * Inverse ADPCM if in prediction mode
1376− */
1377− if (s->prediction_mode[k][l]) {
1378− int n;
1379− for (m = 0; m < 8; m++) {
1380− for (n = 1; n <= 4; n++)
1381− if (m >= n)
1382− subband_samples[k][l][m] +=
1383− (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1384− subband_samples[k][l][m - n] / 8192);
1385− else if (s->predictor_history)
1386− subband_samples[k][l][m] +=
1387− (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1388− s->subband_samples_hist[k][l][m - n + 4] / 8192);
1389− }
1390− }
1391− }
1392−
1393− /*
1394− * Decode VQ encoded high frequencies
1395− */
1396− for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
1397− /* 1 vector -> 32 samples but we only need the 8 samples
1398− * for this subsubframe. */
1399− int hfvq = s->high_freq_vq[k][l];
1400−
1401− if (!s->debug_flag & 0x01) {
1402− av_log(s->avctx, AV_LOG_DEBUG,
1403− "Stream with high frequencies VQ coding\n");
1404− s->debug_flag |= 0x01;
1405− }
1406−
1407− int8x8_fmul_int32(subband_samples[k][l],
1408− &high_freq_vq[hfvq][subsubframe * 8],
1409− s->scale_factor[k][l][0]);
1410− }
1411− }
1412−
1413− /* Check for DSYNC after subsubframe */
1414− if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1415− if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1416−#ifdef TRACE
1417− av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1418−#endif
1419− } else {
1420− av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1421− }
1422− }
1423−
1424− /* Backup predictor history for adpcm */
1425− for (k = base_channel; k < s->prim_channels; k++)
1426− for (l = 0; l < s->vq_start_subband[k]; l++)
1427− memcpy(s->subband_samples_hist[k][l],
1428− &subband_samples[k][l][4],
1429− 4 * sizeof(subband_samples[0][0][0]));
1430−
1431− return 0;
1432−}
1433−
1434−static int dca_filter_channels(DCAContext *s, int block_index)
1435−{
1436− float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1437− int k;
1438−
1439− /* 32 subbands QMF */
1440− for (k = 0; k < s->prim_channels; k++) {
1441−/* static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
1442− 0, 8388608.0, 8388608.0 };*/
1443− qmf_32_subbands(s, k, subband_samples[k],
1444− &s->samples[256 * s->channel_order_tab[k]],
1445− M_SQRT1_2 * s->scale_bias /* pcm_to_double[s->source_pcm_res] */);
1446− }
1447−
1448− /* Down mixing */
1449− if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
1450− dca_downmix(s->samples, s->amode, s->downmix_coef, s->channel_order_tab);
1451− }
1452−
1453− /* Generate LFE samples for this subsubframe FIXME!!! */
1454− if (s->output & DCA_LFE) {
1455− lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1456− s->lfe_data + 2 * s->lfe * (block_index + 4),
1457− &s->samples[256 * s->lfe_index],
1458− (1.0 / 256.0) * s->scale_bias);
1459− /* Outputs 20bits pcm samples */
1460− }
1461−
1462− return 0;
1463−}
1464−
1465−
1466−static int dca_subframe_footer(DCAContext *s, int base_channel)
1467−{
1468− int aux_data_count = 0, i;
1469−
1470− /*
1471− * Unpack optional information
1472− */
1473−
1474− /* presumably optional information only appears in the core? */
1475− if (!base_channel) {
1476− if (s->timestamp)
1477− skip_bits_long(&s->gb, 32);
1478−
1479− if (s->aux_data)
1480− aux_data_count = get_bits(&s->gb, 6);
1481−
1482− for (i = 0; i < aux_data_count; i++)
1483− get_bits(&s->gb, 8);
1484−
1485− if (s->crc_present && (s->downmix || s->dynrange))
1486− get_bits(&s->gb, 16);
1487− }
1488−
1489− return 0;
1490−}
1491−
1492−/**
1493− * Decode a dca frame block
1494− *
1495− * @param s pointer to the DCAContext
1496− */
1497−
1498−static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1499−{
1500− int ret;
1501−
1502− /* Sanity check */
1503− if (s->current_subframe >= s->subframes) {
1504− av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1505− s->current_subframe, s->subframes);
1506− return AVERROR_INVALIDDATA;
1507− }
1508−
1509− if (!s->current_subsubframe) {
1510−#ifdef TRACE
1511− av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1512−#endif
1513− /* Read subframe header */
1514− if ((ret = dca_subframe_header(s, base_channel, block_index)))
1515− return ret;
1516− }
1517−
1518− /* Read subsubframe */
1519−#ifdef TRACE
1520− av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1521−#endif
1522− if ((ret = dca_subsubframe(s, base_channel, block_index)))
1523− return ret;
1524−
1525− /* Update state */
1526− s->current_subsubframe++;
1527− if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1528− s->current_subsubframe = 0;
1529− s->current_subframe++;
1530− }
1531− if (s->current_subframe >= s->subframes) {
1532−#ifdef TRACE
1533− av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1534−#endif
1535− /* Read subframe footer */
1536− if ((ret = dca_subframe_footer(s, base_channel)))
1537− return ret;
1538− }
1539−
1540− return 0;
1541−}
1542−
1543−/**
1544− * Return the number of channels in an ExSS speaker mask (HD)
1545− */
1546−static int dca_exss_mask2count(int mask)
29+const uint32_t avpriv_dca_sample_rates[16] =
154730 {
1548− /* count bits that mean speaker pairs twice */
1549− return av_popcount(mask) +
1550− av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
1551− DCA_EXSS_FRONT_LEFT_RIGHT |
1552− DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
1553− DCA_EXSS_WIDE_LEFT_RIGHT |
1554− DCA_EXSS_SIDE_LEFT_RIGHT |
1555− DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
1556− DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
1557− DCA_EXSS_REAR_LEFT_RIGHT |
1558− DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
1559−}
1560−
1561−/**
1562− * Skip mixing coefficients of a single mix out configuration (HD)
1563− */
1564−static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1565−{
1566− int i;
1567−
1568− for (i = 0; i < channels; i++) {
1569− int mix_map_mask = get_bits(gb, out_ch);
1570− int num_coeffs = av_popcount(mix_map_mask);
1571− skip_bits_long(gb, num_coeffs * 6);
1572− }
1573−}
1574−
1575−/**
1576− * Parse extension substream asset header (HD)
1577− */
1578−static int dca_exss_parse_asset_header(DCAContext *s)
1579−{
1580− int header_pos = get_bits_count(&s->gb);
1581− int header_size;
1582− int channels = 0;
1583− int embedded_stereo = 0;
1584− int embedded_6ch = 0;
1585− int drc_code_present;
1586− int av_uninit(extensions_mask);
1587− int i, j;
1588−
1589− if (get_bits_left(&s->gb) < 16)
1590− return -1;
1591−
1592− /* We will parse just enough to get to the extensions bitmask with which
1593− * we can set the profile value. */
1594−
1595− header_size = get_bits(&s->gb, 9) + 1;
1596− skip_bits(&s->gb, 3); // asset index
1597−
1598− if (s->static_fields) {
1599− if (get_bits1(&s->gb))
1600− skip_bits(&s->gb, 4); // asset type descriptor
1601− if (get_bits1(&s->gb))
1602− skip_bits_long(&s->gb, 24); // language descriptor
1603−
1604− if (get_bits1(&s->gb)) {
1605− /* How can one fit 1024 bytes of text here if the maximum value
1606− * for the asset header size field above was 512 bytes? */
1607− int text_length = get_bits(&s->gb, 10) + 1;
1608− if (get_bits_left(&s->gb) < text_length * 8)
1609− return -1;
1610− skip_bits_long(&s->gb, text_length * 8); // info text
1611− }
1612−
1613− skip_bits(&s->gb, 5); // bit resolution - 1
1614− skip_bits(&s->gb, 4); // max sample rate code
1615− channels = get_bits(&s->gb, 8) + 1;
1616−
1617− if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1618− int spkr_remap_sets;
1619− int spkr_mask_size = 16;
1620− int num_spkrs[7];
1621−
1622− if (channels > 2)
1623− embedded_stereo = get_bits1(&s->gb);
1624− if (channels > 6)
1625− embedded_6ch = get_bits1(&s->gb);
1626−
1627− if (get_bits1(&s->gb)) {
1628− spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1629− skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1630− }
1631−
1632− spkr_remap_sets = get_bits(&s->gb, 3);
1633−
1634− for (i = 0; i < spkr_remap_sets; i++) {
1635− /* std layout mask for each remap set */
1636− num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1637− }
1638−
1639− for (i = 0; i < spkr_remap_sets; i++) {
1640− int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1641− if (get_bits_left(&s->gb) < 0)
1642− return -1;
1643−
1644− for (j = 0; j < num_spkrs[i]; j++) {
1645− int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1646− int num_dec_ch = av_popcount(remap_dec_ch_mask);
1647− skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1648− }
1649− }
1650−
1651− } else {
1652− skip_bits(&s->gb, 3); // representation type
1653− }
1654− }
1655−
1656− drc_code_present = get_bits1(&s->gb);
1657− if (drc_code_present)
1658− get_bits(&s->gb, 8); // drc code
1659−
1660− if (get_bits1(&s->gb))
1661− skip_bits(&s->gb, 5); // dialog normalization code
1662−
1663− if (drc_code_present && embedded_stereo)
1664− get_bits(&s->gb, 8); // drc stereo code
1665−
1666− if (s->mix_metadata && get_bits1(&s->gb)) {
1667− skip_bits(&s->gb, 1); // external mix
1668− skip_bits(&s->gb, 6); // post mix gain code
1669−
1670− if (get_bits(&s->gb, 2) != 3) // mixer drc code
1671− skip_bits(&s->gb, 3); // drc limit
1672− else
1673− skip_bits(&s->gb, 8); // custom drc code
1674−
1675− if (get_bits1(&s->gb)) // channel specific scaling
1676− for (i = 0; i < s->num_mix_configs; i++)
1677− skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1678− else
1679− skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1680−
1681− for (i = 0; i < s->num_mix_configs; i++) {
1682− if (get_bits_left(&s->gb) < 0)
1683− return -1;
1684− dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1685− if (embedded_6ch)
1686− dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
1687− if (embedded_stereo)
1688− dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
1689− }
1690− }
1691−
1692− switch (get_bits(&s->gb, 2)) {
1693− case 0: extensions_mask = get_bits(&s->gb, 12); break;
1694− case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
1695− case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
1696− case 3: extensions_mask = 0; /* aux coding */ break;
1697− }
1698−
1699− /* not parsed further, we were only interested in the extensions mask */
1700−
1701− if (get_bits_left(&s->gb) < 0)
1702− return -1;
1703−
1704− if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1705− av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1706− return -1;
1707− }
1708− skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1709−
1710− if (extensions_mask & DCA_EXT_EXSS_XLL)
1711− s->profile = FF_PROFILE_DTS_HD_MA;
1712− else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1713− DCA_EXT_EXSS_XXCH))
1714− s->profile = FF_PROFILE_DTS_HD_HRA;
1715−
1716− if (!(extensions_mask & DCA_EXT_CORE))
1717− av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1718− if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1719− av_log(s->avctx, AV_LOG_WARNING,
1720− "DTS extensions detection mismatch (%d, %d)\n",
1721− extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1722−
1723− return 0;
1724−}
1725−
1726−static int dca_xbr_parse_frame(DCAContext *s)
1727−{
1728− int scale_table_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS][2];
1729− int active_bands[DCA_CHSETS_MAX][DCA_CHSET_CHANS_MAX];
1730− int abits_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS];
1731− int anctemp[DCA_CHSET_CHANS_MAX];
1732− int chset_fsize[DCA_CHSETS_MAX];
1733− int n_xbr_ch[DCA_CHSETS_MAX];
1734− int hdr_size, num_chsets, xbr_tmode, hdr_pos;
1735− int i, j, k, l, chset, chan_base;
1736−
1737− av_log(s->avctx, AV_LOG_DEBUG, "DTS-XBR: decoding XBR extension\n");
1738−
1739− /* get bit position of sync header */
1740− hdr_pos = get_bits_count(&s->gb) - 32;
1741−
1742− hdr_size = get_bits(&s->gb, 6) + 1;
1743− num_chsets = get_bits(&s->gb, 2) + 1;
1744−
1745− for(i = 0; i < num_chsets; i++)
1746− chset_fsize[i] = get_bits(&s->gb, 14) + 1;
1747−
1748− xbr_tmode = get_bits1(&s->gb);
1749−
1750− for(i = 0; i < num_chsets; i++) {
1751− n_xbr_ch[i] = get_bits(&s->gb, 3) + 1;
1752− k = get_bits(&s->gb, 2) + 5;
1753− for(j = 0; j < n_xbr_ch[i]; j++)
1754− active_bands[i][j] = get_bits(&s->gb, k) + 1;
1755− }
1756−
1757− /* skip to the end of the header */
1758− i = get_bits_count(&s->gb);
1759− if(hdr_pos + hdr_size * 8 > i)
1760− skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1761−
1762− /* loop over the channel data sets */
1763− /* only decode as many channels as we've decoded base data for */
1764− for(chset = 0, chan_base = 0;
1765− chset < num_chsets && chan_base + n_xbr_ch[chset] <= s->prim_channels;
1766− chan_base += n_xbr_ch[chset++]) {
1767− int start_posn = get_bits_count(&s->gb);
1768− int subsubframe = 0;
1769− int subframe = 0;
1770−
1771− /* loop over subframes */
1772− for (k = 0; k < (s->sample_blocks / 8); k++) {
1773− /* parse header if we're on first subsubframe of a block */
1774− if(subsubframe == 0) {
1775− /* Parse subframe header */
1776− for(i = 0; i < n_xbr_ch[chset]; i++) {
1777− anctemp[i] = get_bits(&s->gb, 2) + 2;
1778− }
1779−
1780− for(i = 0; i < n_xbr_ch[chset]; i++) {
1781− get_array(&s->gb, abits_high[i], active_bands[chset][i], anctemp[i]);
1782− }
1783−
1784− for(i = 0; i < n_xbr_ch[chset]; i++) {
1785− anctemp[i] = get_bits(&s->gb, 3);
1786− if(anctemp[i] < 1) {
1787− av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: SYNC ERROR\n");
1788− return AVERROR_INVALIDDATA;
1789− }
1790− }
1791−
1792− /* generate scale factors */
1793− for(i = 0; i < n_xbr_ch[chset]; i++) {
1794− const uint32_t *scale_table;
1795− int nbits;
1796−
1797− if (s->scalefactor_huffman[chan_base+i] == 6) {
1798− scale_table = scale_factor_quant7;
1799− } else {
1800− scale_table = scale_factor_quant6;
1801− }
1802−
1803− nbits = anctemp[i];
1804−
1805− for(j = 0; j < active_bands[chset][i]; j++) {
1806− if(abits_high[i][j] > 0) {
1807− scale_table_high[i][j][0] =
1808− scale_table[get_bits(&s->gb, nbits)];
1809−
1810− if(xbr_tmode && s->transition_mode[i][j]) {
1811− scale_table_high[i][j][1] =
1812− scale_table[get_bits(&s->gb, nbits)];
1813− }
1814− }
1815− }
1816− }
1817− }
1818−
1819− /* decode audio array for this block */
1820− for(i = 0; i < n_xbr_ch[chset]; i++) {
1821− for(j = 0; j < active_bands[chset][i]; j++) {
1822− const int xbr_abits = abits_high[i][j];
1823− const float quant_step_size = lossless_quant_d[xbr_abits];
1824− const int sfi = xbr_tmode && s->transition_mode[i][j] && subsubframe >= s->transition_mode[i][j];
1825− const float rscale = quant_step_size * scale_table_high[i][j][sfi];
1826− float *subband_samples = s->subband_samples[k][chan_base+i][j];
1827− int block[8];
1828−
1829− if(xbr_abits <= 0)
1830− continue;
1831−
1832− if(xbr_abits > 7) {
1833− get_array(&s->gb, block, 8, xbr_abits - 3);
1834− } else {
1835− int block_code1, block_code2, size, levels, err;
1836−
1837− size = abits_sizes[xbr_abits - 1];
1838− levels = abits_levels[xbr_abits - 1];
1839−
1840− block_code1 = get_bits(&s->gb, size);
1841− block_code2 = get_bits(&s->gb, size);
1842− err = decode_blockcodes(block_code1, block_code2,
1843− levels, block);
1844− if (err) {
1845− av_log(s->avctx, AV_LOG_ERROR,
1846− "ERROR: DTS-XBR: block code look-up failed\n");
1847− return AVERROR_INVALIDDATA;
1848− }
1849− }
1850−
1851− /* scale & sum into subband */
1852− for(l = 0; l < 8; l++)
1853− subband_samples[l] += (float)block[l] * rscale;
1854− }
1855− }
1856−
1857− /* check DSYNC marker */
1858− if(s->aspf || subsubframe == s->subsubframes[subframe] - 1) {
1859− if(get_bits(&s->gb, 16) != 0xffff) {
1860− av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: Didn't get subframe DSYNC\n");
1861− return AVERROR_INVALIDDATA;
1862− }
1863− }
1864−
1865− /* advance sub-sub-frame index */
1866− if(++subsubframe >= s->subsubframes[subframe]) {
1867− subsubframe = 0;
1868− subframe++;
1869− }
1870− }
1871−
1872− /* skip to next channel set */
1873− i = get_bits_count(&s->gb);
1874− if(start_posn + chset_fsize[chset] * 8 != i) {
1875− j = start_posn + chset_fsize[chset] * 8 - i;
1876− if(j < 0 || j >= 8)
1877− av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: end of channel set,"
1878− " skipping further than expected (%d bits)\n", j);
1879− skip_bits_long(&s->gb, j);
1880− }
1881− }
1882−
1883− return 0;
1884−}
1885−
1886−/* parse initial header for XXCH and dump details */
1887−static int dca_xxch_decode_frame(DCAContext *s)
1888−{
1889− int hdr_size, chhdr_crc, spkmsk_bits, num_chsets, core_spk, hdr_pos;
1890− int i, chset, base_channel, chstart, fsize[8];
1891−
1892− /* assume header word has already been parsed */
1893− hdr_pos = get_bits_count(&s->gb) - 32;
1894− hdr_size = get_bits(&s->gb, 6) + 1;
1895− chhdr_crc = get_bits1(&s->gb);
1896− spkmsk_bits = get_bits(&s->gb, 5) + 1;
1897− num_chsets = get_bits(&s->gb, 2) + 1;
1898−
1899− for (i = 0; i < num_chsets; i++)
1900− fsize[i] = get_bits(&s->gb, 14) + 1;
1901−
1902− core_spk = get_bits(&s->gb, spkmsk_bits);
1903− s->xxch_core_spkmask = core_spk;
1904− s->xxch_nbits_spk_mask = spkmsk_bits;
1905− s->xxch_downmix = 0;
1906− s->xxch_dmix_embedded = 0;
1907−
1908− /* skip to the end of the header */
1909− i = get_bits_count(&s->gb);
1910− if (hdr_pos + hdr_size * 8 > i)
1911− skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1912−
1913− for (chset = 0; chset < num_chsets; chset++) {
1914− chstart = get_bits_count(&s->gb);
1915− base_channel = s->prim_channels;
1916− s->xxch_chset = chset;
1917−
1918− /* XXCH and Core headers differ, see 6.4.2 "XXCH Channel Set Header" vs.
1919− 5.3.2 "Primary Audio Coding Header", DTS Spec 1.3.1 */
1920− dca_parse_audio_coding_header(s, base_channel, 1);
1921−
1922− /* decode channel data */
1923− for (i = 0; i < (s->sample_blocks / 8); i++) {
1924− if (dca_decode_block(s, base_channel, i)) {
1925− av_log(s->avctx, AV_LOG_ERROR,
1926− "Error decoding DTS-XXCH extension\n");
1927− continue;
1928− }
1929− }
1930−
1931− /* skip to end of this section */
1932− i = get_bits_count(&s->gb);
1933− if (chstart + fsize[chset] * 8 > i)
1934− skip_bits_long(&s->gb, chstart + fsize[chset] * 8 - i);
1935− }
1936− s->xxch_chset = num_chsets;
1937−
1938− return 0;
1939−}
1940−
1941−/**
1942− * Parse extension substream header (HD)
1943− */
1944−static void dca_exss_parse_header(DCAContext *s)
1945−{
1946− int asset_size[8];
1947− int ss_index;
1948− int blownup;
1949− int num_audiop = 1;
1950− int num_assets = 1;
1951− int active_ss_mask[8];
1952− int i, j;
1953− int start_posn;
1954− int hdrsize;
1955− uint32_t mkr;
1956−
1957− if (get_bits_left(&s->gb) < 52)
1958− return;
1959−
1960− start_posn = get_bits_count(&s->gb) - 32;
1961−
1962− skip_bits(&s->gb, 8); // user data
1963− ss_index = get_bits(&s->gb, 2);
1964−
1965− blownup = get_bits1(&s->gb);
1966− hdrsize = get_bits(&s->gb, 8 + 4 * blownup) + 1; // header_size
1967− skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
1968−
1969− s->static_fields = get_bits1(&s->gb);
1970− if (s->static_fields) {
1971− skip_bits(&s->gb, 2); // reference clock code
1972− skip_bits(&s->gb, 3); // frame duration code
1973−
1974− if (get_bits1(&s->gb))
1975− skip_bits_long(&s->gb, 36); // timestamp
1976−
1977− /* a single stream can contain multiple audio assets that can be
1978− * combined to form multiple audio presentations */
1979−
1980− num_audiop = get_bits(&s->gb, 3) + 1;
1981− if (num_audiop > 1) {
1982− av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio presentations.");
1983− /* ignore such streams for now */
1984− return;
1985− }
1986−
1987− num_assets = get_bits(&s->gb, 3) + 1;
1988− if (num_assets > 1) {
1989− av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio assets.");
1990− /* ignore such streams for now */
1991− return;
1992− }
1993−
1994− for (i = 0; i < num_audiop; i++)
1995− active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
1996−
1997− for (i = 0; i < num_audiop; i++)
1998− for (j = 0; j <= ss_index; j++)
1999− if (active_ss_mask[i] & (1 << j))
2000− skip_bits(&s->gb, 8); // active asset mask
2001−
2002− s->mix_metadata = get_bits1(&s->gb);
2003− if (s->mix_metadata) {
2004− int mix_out_mask_size;
2005−
2006− skip_bits(&s->gb, 2); // adjustment level
2007− mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
2008− s->num_mix_configs = get_bits(&s->gb, 2) + 1;
2009−
2010− for (i = 0; i < s->num_mix_configs; i++) {
2011− int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
2012− s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
2013− }
2014− }
2015− }
2016−
2017− for (i = 0; i < num_assets; i++)
2018− asset_size[i] = get_bits_long(&s->gb, 16 + 4 * blownup);
2019−
2020− for (i = 0; i < num_assets; i++) {
2021− if (dca_exss_parse_asset_header(s))
2022− return;
2023− }
2024−
2025− /* not parsed further, we were only interested in the extensions mask
2026− * from the asset header */
2027−
2028− if (num_assets > 0) {
2029− j = get_bits_count(&s->gb);
2030− if (start_posn + hdrsize * 8 > j)
2031− skip_bits_long(&s->gb, start_posn + hdrsize * 8 - j);
2032−
2033− for (i = 0; i < num_assets; i++) {
2034− start_posn = get_bits_count(&s->gb);
2035− mkr = get_bits_long(&s->gb, 32);
2036−
2037− /* parse extensions that we know about */
2038− if (mkr == 0x655e315e) {
2039− dca_xbr_parse_frame(s);
2040− } else if (mkr == 0x47004a03) {
2041− dca_xxch_decode_frame(s);
2042− s->core_ext_mask |= DCA_EXT_XXCH; /* xxx use for chan reordering */
2043− } else {
2044− av_log(s->avctx, AV_LOG_DEBUG,
2045− "DTS-ExSS: unknown marker = 0x%08x\n", mkr);
2046− }
2047−
2048− /* skip to end of block */
2049− j = get_bits_count(&s->gb);
2050− if (start_posn + asset_size[i] * 8 > j)
2051− skip_bits_long(&s->gb, start_posn + asset_size[i] * 8 - j);
2052− }
2053− }
2054−}
2055−
2056−/**
2057− * Main frame decoding function
2058− * FIXME add arguments
2059− */
2060−static int dca_decode_frame(AVCodecContext *avctx, void *data,
2061− int *got_frame_ptr, AVPacket *avpkt)
2062−{
2063− const uint8_t *buf = avpkt->data;
2064− int buf_size = avpkt->size;
2065− int channel_mask;
2066− int channel_layout;
2067− int lfe_samples;
2068− int num_core_channels = 0;
2069− int i, ret;
2070− float *samples_flt;
2071− float *src_chan;
2072− float *dst_chan;
2073− int16_t *samples_s16;
2074− DCAContext *s = avctx->priv_data;
2075− int core_ss_end;
2076− int channels;
2077− float scale;
2078− int achan;
2079− int chset;
2080− int mask;
2081− int lavc;
2082− int posn;
2083− int j, k;
2084− int ch;
2085− int endch;
2086−
2087− s->xch_present = 0;
2088−
2089− s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
2090− DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
2091− if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
2092− av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
2093− return AVERROR_INVALIDDATA;
2094− }
2095−
2096− init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
2097− if ((ret = dca_parse_frame_header(s)) < 0) {
2098− //seems like the frame is corrupt, try with the next one
2099− return ret;
2100− }
2101− //set AVCodec values with parsed data
2102− avctx->sample_rate = s->sample_rate;
2103− avctx->bit_rate = s->bit_rate;
2104−
2105− s->profile = FF_PROFILE_DTS;
2106−
2107− for (i = 0; i < (s->sample_blocks / 8); i++) {
2108− if ((ret = dca_decode_block(s, 0, i))) {
2109− av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
2110− return ret;
2111− }
2112− }
2113−
2114− /* record number of core channels incase less than max channels are requested */
2115− num_core_channels = s->prim_channels;
2116−
2117− if (s->ext_coding)
2118− s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
2119− else
2120− s->core_ext_mask = 0;
2121−
2122− core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
2123−
2124− /* only scan for extensions if ext_descr was unknown or indicated a
2125− * supported XCh extension */
2126− if (s->core_ext_mask < 0 || s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH)) {
2127−
2128− /* if ext_descr was unknown, clear s->core_ext_mask so that the
2129− * extensions scan can fill it up */
2130− s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
2131−
2132− /* extensions start at 32-bit boundaries into bitstream */
2133− skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2134−
2135− while (core_ss_end - get_bits_count(&s->gb) >= 32) {
2136− uint32_t bits = get_bits_long(&s->gb, 32);
2137−
2138− switch (bits) {
2139− case 0x5a5a5a5a: {
2140− int ext_amode, xch_fsize;
2141−
2142− s->xch_base_channel = s->prim_channels;
2143−
2144− /* validate sync word using XCHFSIZE field */
2145− xch_fsize = show_bits(&s->gb, 10);
2146− if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
2147− (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
2148− continue;
2149−
2150− /* skip length-to-end-of-frame field for the moment */
2151− skip_bits(&s->gb, 10);
2152−
2153− s->core_ext_mask |= DCA_EXT_XCH;
2154−
2155− /* extension amode(number of channels in extension) should be 1 */
2156− /* AFAIK XCh is not used for more channels */
2157− if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
2158− av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
2159− " supported!\n", ext_amode);
2160− continue;
2161− }
2162−
2163− /* much like core primary audio coding header */
2164− dca_parse_audio_coding_header(s, s->xch_base_channel, 0);
2165−
2166− for (i = 0; i < (s->sample_blocks / 8); i++)
2167− if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
2168− av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
2169− continue;
2170− }
2171−
2172− s->xch_present = 1;
2173− break;
2174− }
2175− case 0x47004a03:
2176− /* XXCh: extended channels */
2177− /* usually found either in core or HD part in DTS-HD HRA streams,
2178− * but not in DTS-ES which contains XCh extensions instead */
2179− s->core_ext_mask |= DCA_EXT_XXCH;
2180− dca_xxch_decode_frame(s);
2181− break;
2182−
2183− case 0x1d95f262: {
2184− int fsize96 = show_bits(&s->gb, 12) + 1;
2185− if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
2186− continue;
2187−
2188− av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
2189− get_bits_count(&s->gb));
2190− skip_bits(&s->gb, 12);
2191− av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
2192− av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
2193−
2194− s->core_ext_mask |= DCA_EXT_X96;
2195− break;
2196− }
2197− }
2198−
2199− skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2200− }
2201− } else {
2202− /* no supported extensions, skip the rest of the core substream */
2203− skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
2204− }
2205−
2206− if (s->core_ext_mask & DCA_EXT_X96)
2207− s->profile = FF_PROFILE_DTS_96_24;
2208− else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
2209− s->profile = FF_PROFILE_DTS_ES;
2210−
2211− /* check for ExSS (HD part) */
2212− if (s->dca_buffer_size - s->frame_size > 32 &&
2213− get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
2214− dca_exss_parse_header(s);
2215−
2216− avctx->profile = s->profile;
2217−
2218− channels = s->prim_channels + !!s->lfe;
2219−
2220− /* If we have XXCH then the channel layout is managed differently */
2221− /* note that XLL will also have another way to do things */
2222− if (!(s->core_ext_mask & DCA_EXT_XXCH)
2223− || (s->core_ext_mask & DCA_EXT_XXCH && avctx->request_channels > 0
2224− && avctx->request_channels
2225− < num_core_channels + !!s->lfe + s->xxch_chset_nch[0]))
2226− { /* xxx should also do MA extensions */
2227− if (s->amode < 16) {
2228− avctx->channel_layout = dca_core_channel_layout[s->amode];
2229−
2230− if (s->xch_present && (!avctx->request_channels ||
2231− avctx->request_channels
2232− > num_core_channels + !!s->lfe)) {
2233− avctx->channel_layout |= AV_CH_BACK_CENTER;
2234− if (s->lfe) {
2235− avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
2236− s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
2237− } else {
2238− s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
2239− }
2240− } else {
2241− channels = num_core_channels + !!s->lfe;
2242− s->xch_present = 0; /* disable further xch processing */
2243− if (s->lfe) {
2244− avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
2245− s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
2246− } else
2247− s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
2248− }
2249−
2250− if (channels > !!s->lfe &&
2251− s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
2252− return AVERROR_INVALIDDATA;
2253−
2254− if (avctx->request_channels == 2 && s->prim_channels > 2) {
2255− channels = 2;
2256− s->output = DCA_STEREO;
2257− avctx->channel_layout = AV_CH_LAYOUT_STEREO;
2258− }
2259− else if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
2260− static const int8_t dca_channel_order_native[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
2261− s->channel_order_tab = dca_channel_order_native;
2262− }
2263− s->lfe_index = dca_lfe_index[s->amode];
2264− } else {
2265− av_log(avctx, AV_LOG_ERROR,
2266− "Non standard configuration %d !\n", s->amode);
2267− return AVERROR_INVALIDDATA;
2268− }
2269−
2270− s->xxch_downmix = 0;
2271− } else {
2272− /* we only get here if an XXCH channel set can be added to the mix */
2273− channel_mask = s->xxch_core_spkmask;
2274−
2275− if (avctx->request_channels > 0
2276− && avctx->request_channels < s->prim_channels) {
2277− channels = num_core_channels + !!s->lfe;
2278− for (i = 0; i < s->xxch_chset && channels + s->xxch_chset_nch[i]
2279− <= avctx->request_channels; i++) {
2280− channels += s->xxch_chset_nch[i];
2281− channel_mask |= s->xxch_spk_masks[i];
2282− }
2283− } else {
2284− channels = s->prim_channels + !!s->lfe;
2285− for (i = 0; i < s->xxch_chset; i++) {
2286− channel_mask |= s->xxch_spk_masks[i];
2287− }
2288− }
2289−
2290− /* Given the DTS spec'ed channel mask, generate an avcodec version */
2291− channel_layout = 0;
2292− for (i = 0; i < s->xxch_nbits_spk_mask; ++i) {
2293− if (channel_mask & (1 << i)) {
2294− channel_layout |= map_xxch_to_native[i];
2295− }
2296− }
2297−
2298− /* make sure that we have managed to get equivelant dts/avcodec channel
2299− * masks in some sense -- unfortunately some channels could overlap */
2300− if (av_popcount(channel_mask) != av_popcount(channel_layout)) {
2301− av_log(avctx, AV_LOG_DEBUG,
2302− "DTS-XXCH: Inconsistant avcodec/dts channel layouts\n");
2303− return AVERROR_INVALIDDATA;
2304− }
2305−
2306− avctx->channel_layout = channel_layout;
2307−
2308− if (!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE)) {
2309− /* Estimate DTS --> avcodec ordering table */
2310− for (chset = -1, j = 0; chset < s->xxch_chset; ++chset) {
2311− mask = chset >= 0 ? s->xxch_spk_masks[chset]
2312− : s->xxch_core_spkmask;
2313− for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
2314− if (mask & ~(DCA_XXCH_LFE1 | DCA_XXCH_LFE2) & (1 << i)) {
2315− lavc = map_xxch_to_native[i];
2316− posn = av_popcount(channel_layout & (lavc - 1));
2317− s->xxch_order_tab[j++] = posn;
2318− }
2319− }
2320− }
2321−
2322− s->lfe_index = av_popcount(channel_layout & (AV_CH_LOW_FREQUENCY-1));
2323− } else { /* native ordering */
2324− for (i = 0; i < channels; i++)
2325− s->xxch_order_tab[i] = i;
2326−
2327− s->lfe_index = channels - 1;
2328− }
2329−
2330− s->channel_order_tab = s->xxch_order_tab;
2331− }
2332−
2333− if (avctx->channels != channels) {
2334− if (avctx->channels)
2335− av_log(avctx, AV_LOG_INFO, "Number of channels changed in DCA decoder (%d -> %d)\n", avctx->channels, channels);
2336− avctx->channels = channels;
2337− }
2338−
2339− /* get output buffer */
2340− s->frame.nb_samples = 256 * (s->sample_blocks / 8);
2341− if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
2342− av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
2343− return ret;
2344− }
2345− samples_flt = (float *) s->frame.data[0];
2346− samples_s16 = (int16_t *) s->frame.data[0];
2347−
2348− /* filter to get final output */
2349− for (i = 0; i < (s->sample_blocks / 8); i++) {
2350− dca_filter_channels(s, i);
2351−
2352− /* If this was marked as a DTS-ES stream we need to subtract back- */
2353− /* channel from SL & SR to remove matrixed back-channel signal */
2354− if ((s->source_pcm_res & 1) && s->xch_present) {
2355− float *back_chan = s->samples + s->channel_order_tab[s->xch_base_channel] * 256;
2356− float *lt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 2] * 256;
2357− float *rt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 1] * 256;
2358− s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
2359− s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
2360− }
2361−
2362− /* If stream contains XXCH, we might need to undo an embedded downmix */
2363− if (s->xxch_dmix_embedded) {
2364− /* Loop over channel sets in turn */
2365− ch = num_core_channels;
2366− for (chset = 0; chset < s->xxch_chset; chset++) {
2367− endch = ch + s->xxch_chset_nch[chset];
2368− mask = s->xxch_dmix_embedded;
2369−
2370− /* undo downmix */
2371− for (j = ch; j < endch; j++) {
2372− if (mask & (1 << j)) { /* this channel has been mixed-out */
2373− src_chan = s->samples + s->channel_order_tab[j] * 256;
2374− for (k = 0; k < endch; k++) {
2375− achan = s->channel_order_tab[k];
2376− scale = s->xxch_dmix_coeff[j][k];
2377− if (scale != 0.0) {
2378− dst_chan = s->samples + achan * 256;
2379− s->fdsp.vector_fmac_scalar(dst_chan, src_chan,
2380− -scale, 256);
2381− }
2382− }
2383− }
2384− }
2385−
2386− /* if a downmix has been embedded then undo the pre-scaling */
2387− if ((mask & (1 << ch)) && s->xxch_dmix_sf[chset] != 1.0f) {
2388− scale = s->xxch_dmix_sf[chset];
2389−
2390− for (j = 0; j < ch; j++) {
2391− src_chan = s->samples + s->channel_order_tab[j] * 256;
2392− for (k = 0; k < 256; k++)
2393− src_chan[k] *= scale;
2394− }
2395−
2396− /* LFE channel is always part of core, scale if it exists */
2397− if (s->lfe) {
2398− src_chan = s->samples + s->lfe_index * 256;
2399− for (k = 0; k < 256; k++)
2400− src_chan[k] *= scale;
2401− }
2402− }
2403−
2404− ch = endch;
2405− }
2406−
2407− }
2408−
2409− if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
2410− s->fmt_conv.float_interleave(samples_flt, s->samples_chanptr, 256,
2411− channels);
2412− samples_flt += 256 * channels;
2413− } else {
2414− s->fmt_conv.float_to_int16_interleave(samples_s16,
2415− s->samples_chanptr, 256,
2416− channels);
2417− samples_s16 += 256 * channels;
2418− }
2419− }
2420−
2421− /* update lfe history */
2422− lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
2423− for (i = 0; i < 2 * s->lfe * 4; i++)
2424− s->lfe_data[i] = s->lfe_data[i + lfe_samples];
2425−
2426− *got_frame_ptr = 1;
2427− *(AVFrame *) data = s->frame;
2428−
2429− return buf_size;
2430−}
2431−
2432−
2433−
2434−/**
2435− * DCA initialization
2436− *
2437− * @param avctx pointer to the AVCodecContext
2438− */
2439−
2440−static av_cold int dca_decode_init(AVCodecContext *avctx)
2441−{
2442− DCAContext *s = avctx->priv_data;
2443− int i;
2444−
2445− s->avctx = avctx;
2446− dca_init_vlcs();
2447−
2448− avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
2449− ff_mdct_init(&s->imdct, 6, 1, 1.0);
2450− ff_synth_filter_init(&s->synth);
2451− ff_dcadsp_init(&s->dcadsp);
2452− ff_fmt_convert_init(&s->fmt_conv, avctx);
2453−
2454− for (i = 0; i < DCA_PRIM_CHANNELS_MAX + 1; i++)
2455− s->samples_chanptr[i] = s->samples + i * 256;
2456−
2457− if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
2458− avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
2459− s->scale_bias = 1.0 / 32768.0;
2460− } else {
2461− avctx->sample_fmt = AV_SAMPLE_FMT_S16;
2462− s->scale_bias = 1.0;
2463− }
2464−
2465− /* allow downmixing to stereo */
2466− if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
2467− avctx->request_channels == 2) {
2468− avctx->channels = avctx->request_channels;
2469− }
2470−
2471− avcodec_get_frame_defaults(&s->frame);
2472− avctx->coded_frame = &s->frame;
2473−
2474− return 0;
2475−}
2476−
2477−static av_cold int dca_decode_end(AVCodecContext *avctx)
2478−{
2479− DCAContext *s = avctx->priv_data;
2480− ff_mdct_end(&s->imdct);
2481− return 0;
2482−}
2483−
2484−static const AVProfile profiles[] = {
2485− { FF_PROFILE_DTS, "DTS" },
2486− { FF_PROFILE_DTS_ES, "DTS-ES" },
2487− { FF_PROFILE_DTS_96_24, "DTS 96/24" },
2488− { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
2489− { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
2490− { FF_PROFILE_UNKNOWN },
2491−};
2492−
2493−AVCodec ff_dca_decoder = {
2494− .name = "dca",
2495− .type = AVMEDIA_TYPE_AUDIO,
2496− .id = CODEC_ID_DTS,
2497− .priv_data_size = sizeof(DCAContext),
2498− .init = dca_decode_init,
2499− .decode = dca_decode_frame,
2500− .close = dca_decode_end,
2501− .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2502− .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2503− .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
2504− AV_SAMPLE_FMT_S16,
2505− AV_SAMPLE_FMT_NONE },
2506− .profiles = NULL_IF_CONFIG_SMALL(profiles),
31+ 0, 8000, 16000, 32000, 0, 0, 11025, 22050, 44100, 0, 0,
32+ 12000, 24000, 48000, 96000, 192000
250733 };
libavcodec/dca.h+4−0
@@ -25,6 +25,8 @@ · 24 unmodified lines
2525 #ifndef AVCODEC_DCA_H
2626 #define AVCODEC_DCA_H
2727
28+#include <stdint.h>
29+
2830 /** DCA syncwords, also used for bitstream type detection */
2931 #define DCA_MARKER_RAW_BE 0x7FFE8001
3032 #define DCA_MARKER_RAW_LE 0xFE7F0180
@@ -34,4 +36,6 @@ · 3 unmodified lines
3436 /** DCA-HD specific block starts with this marker. */
3537 #define DCA_HD_MARKER 0x64582025
3638
39+extern const uint32_t avpriv_dca_sample_rates[16];
40+
3741 #endif /* AVCODEC_DCA_H */
libavcodec/dca_parser.c+1−2
@@ -24,7 +24,6 @@ · 23 unmodified lines
2424
2525 #include "parser.h"
2626 #include "dca.h"
27−#include "dcadata.h"
2827 #include "dca_parser.h"
2928 #include "get_bits.h"
3029 #include "put_bits.h"
@@ -160,7 +159,7 @@ static int dca_parse_params(const uint8_t *buf, int buf_size, int *duration, · 129 unmodified lines
160159
161160 skip_bits(&gb, 20);
162161 sr_code = get_bits(&gb, 4);
163− *sample_rate = dca_sample_rates[sr_code];
162+ *sample_rate = avpriv_dca_sample_rates[sr_code];
164163 if (*sample_rate == 0)
165164 return AVERROR_INVALIDDATA;
166165
libavcodec/dcadata.h+0−6
@@ -28,12 +28,6 @@ · 27 unmodified lines
2828
2929 /* Generic tables */
3030
31−static const uint32_t dca_sample_rates[16] =
32−{
33− 0, 8000, 16000, 32000, 0, 0, 11025, 22050, 44100, 0, 0,
34− 12000, 24000, 48000, 96000, 192000
35−};
36−
3731 static const uint32_t dca_bit_rates[32] =
3832 {
3933 32000, 56000, 64000, 96000, 112000, 128000,
libavcodec/dcadec.c+2507−0
@@ -0,0 +1,2507 @@
1+/*
2+ * DCA compatible decoder
3+ * Copyright (C) 2004 Gildas Bazin
4+ * Copyright (C) 2004 Benjamin Zores
5+ * Copyright (C) 2006 Benjamin Larsson
6+ * Copyright (C) 2007 Konstantin Shishkov
7+ *
8+ * This file is part of FFmpeg.
9+ *
10+ * FFmpeg is free software; you can redistribute it and/or
11+ * modify it under the terms of the GNU Lesser General Public
12+ * License as published by the Free Software Foundation; either
13+ * version 2.1 of the License, or (at your option) any later version.
14+ *
15+ * FFmpeg is distributed in the hope that it will be useful,
16+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+ * Lesser General Public License for more details.
19+ *
20+ * You should have received a copy of the GNU Lesser General Public
21+ * License along with FFmpeg; if not, write to the Free Software
22+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23+ */
24+
25+#include <math.h>
26+#include <stddef.h>
27+#include <stdio.h>
28+
29+#include "libavutil/common.h"
30+#include "libavutil/float_dsp.h"
31+#include "libavutil/intmath.h"
32+#include "libavutil/intreadwrite.h"
33+#include "libavutil/mathematics.h"
34+#include "libavutil/audioconvert.h"
35+#include "avcodec.h"
36+#include "dsputil.h"
37+#include "fft.h"
38+#include "get_bits.h"
39+#include "put_bits.h"
40+#include "dcadata.h"
41+#include "dcahuff.h"
42+#include "dca.h"
43+#include "dca_parser.h"
44+#include "synth_filter.h"
45+#include "dcadsp.h"
46+#include "fmtconvert.h"
47+
48+#if ARCH_ARM
49+# include "arm/dca.h"
50+#endif
51+
52+//#define TRACE
53+
54+#define DCA_PRIM_CHANNELS_MAX (7)
55+#define DCA_SUBBANDS (64)
56+#define DCA_ABITS_MAX (32) /* Should be 28 */
57+#define DCA_SUBSUBFRAMES_MAX (4)
58+#define DCA_SUBFRAMES_MAX (16)
59+#define DCA_BLOCKS_MAX (16)
60+#define DCA_LFE_MAX (3)
61+#define DCA_CHSETS_MAX (4)
62+#define DCA_CHSET_CHANS_MAX (8)
63+
64+enum DCAMode {
65+ DCA_MONO = 0,
66+ DCA_CHANNEL,
67+ DCA_STEREO,
68+ DCA_STEREO_SUMDIFF,
69+ DCA_STEREO_TOTAL,
70+ DCA_3F,
71+ DCA_2F1R,
72+ DCA_3F1R,
73+ DCA_2F2R,
74+ DCA_3F2R,
75+ DCA_4F2R
76+};
77+
78+/* these are unconfirmed but should be mostly correct */
79+enum DCAExSSSpeakerMask {
80+ DCA_EXSS_FRONT_CENTER = 0x0001,
81+ DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
82+ DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
83+ DCA_EXSS_LFE = 0x0008,
84+ DCA_EXSS_REAR_CENTER = 0x0010,
85+ DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
86+ DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
87+ DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
88+ DCA_EXSS_OVERHEAD = 0x0100,
89+ DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
90+ DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
91+ DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
92+ DCA_EXSS_LFE2 = 0x1000,
93+ DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
94+ DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
95+ DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
96+};
97+
98+enum DCAXxchSpeakerMask {
99+ DCA_XXCH_FRONT_CENTER = 0x0000001,
100+ DCA_XXCH_FRONT_LEFT = 0x0000002,
101+ DCA_XXCH_FRONT_RIGHT = 0x0000004,
102+ DCA_XXCH_SIDE_REAR_LEFT = 0x0000008,
103+ DCA_XXCH_SIDE_REAR_RIGHT = 0x0000010,
104+ DCA_XXCH_LFE1 = 0x0000020,
105+ DCA_XXCH_REAR_CENTER = 0x0000040,
106+ DCA_XXCH_SURROUND_REAR_LEFT = 0x0000080,
107+ DCA_XXCH_SURROUND_REAR_RIGHT = 0x0000100,
108+ DCA_XXCH_SIDE_SURROUND_LEFT = 0x0000200,
109+ DCA_XXCH_SIDE_SURROUND_RIGHT = 0x0000400,
110+ DCA_XXCH_FRONT_CENTER_LEFT = 0x0000800,
111+ DCA_XXCH_FRONT_CENTER_RIGHT = 0x0001000,
112+ DCA_XXCH_FRONT_HIGH_LEFT = 0x0002000,
113+ DCA_XXCH_FRONT_HIGH_CENTER = 0x0004000,
114+ DCA_XXCH_FRONT_HIGH_RIGHT = 0x0008000,
115+ DCA_XXCH_LFE2 = 0x0010000,
116+ DCA_XXCH_SIDE_FRONT_LEFT = 0x0020000,
117+ DCA_XXCH_SIDE_FRONT_RIGHT = 0x0040000,
118+ DCA_XXCH_OVERHEAD = 0x0080000,
119+ DCA_XXCH_SIDE_HIGH_LEFT = 0x0100000,
120+ DCA_XXCH_SIDE_HIGH_RIGHT = 0x0200000,
121+ DCA_XXCH_REAR_HIGH_CENTER = 0x0400000,
122+ DCA_XXCH_REAR_HIGH_LEFT = 0x0800000,
123+ DCA_XXCH_REAR_HIGH_RIGHT = 0x1000000,
124+ DCA_XXCH_REAR_LOW_CENTER = 0x2000000,
125+ DCA_XXCH_REAR_LOW_LEFT = 0x4000000,
126+ DCA_XXCH_REAR_LOW_RIGHT = 0x8000000,
127+};
128+
129+static const uint32_t map_xxch_to_native[28] = {
130+ AV_CH_FRONT_CENTER,
131+ AV_CH_FRONT_LEFT,
132+ AV_CH_FRONT_RIGHT,
133+ AV_CH_SIDE_LEFT,
134+ AV_CH_SIDE_RIGHT,
135+ AV_CH_LOW_FREQUENCY,
136+ AV_CH_BACK_CENTER,
137+ AV_CH_BACK_LEFT,
138+ AV_CH_BACK_RIGHT,
139+ AV_CH_SIDE_LEFT, /* side surround left -- dup sur side L */
140+ AV_CH_SIDE_RIGHT, /* side surround right -- dup sur side R */
141+ AV_CH_FRONT_LEFT_OF_CENTER,
142+ AV_CH_FRONT_RIGHT_OF_CENTER,
143+ AV_CH_TOP_FRONT_LEFT,
144+ AV_CH_TOP_FRONT_CENTER,
145+ AV_CH_TOP_FRONT_RIGHT,
146+ AV_CH_LOW_FREQUENCY, /* lfe2 -- duplicate lfe1 position */
147+ AV_CH_FRONT_LEFT_OF_CENTER, /* side front left -- dup front cntr L */
148+ AV_CH_FRONT_RIGHT_OF_CENTER,/* side front right -- dup front cntr R */
149+ AV_CH_TOP_CENTER, /* overhead */
150+ AV_CH_TOP_FRONT_LEFT, /* side high left -- dup */
151+ AV_CH_TOP_FRONT_RIGHT, /* side high right -- dup */
152+ AV_CH_TOP_BACK_CENTER,
153+ AV_CH_TOP_BACK_LEFT,
154+ AV_CH_TOP_BACK_RIGHT,
155+ AV_CH_BACK_CENTER, /* rear low center -- dup */
156+ AV_CH_BACK_LEFT, /* rear low left -- dup */
157+ AV_CH_BACK_RIGHT /* read low right -- dup */
158+};
159+
160+enum DCAExtensionMask {
161+ DCA_EXT_CORE = 0x001, ///< core in core substream
162+ DCA_EXT_XXCH = 0x002, ///< XXCh channels extension in core substream
163+ DCA_EXT_X96 = 0x004, ///< 96/24 extension in core substream
164+ DCA_EXT_XCH = 0x008, ///< XCh channel extension in core substream
165+ DCA_EXT_EXSS_CORE = 0x010, ///< core in ExSS (extension substream)
166+ DCA_EXT_EXSS_XBR = 0x020, ///< extended bitrate extension in ExSS
167+ DCA_EXT_EXSS_XXCH = 0x040, ///< XXCh channels extension in ExSS
168+ DCA_EXT_EXSS_X96 = 0x080, ///< 96/24 extension in ExSS
169+ DCA_EXT_EXSS_LBR = 0x100, ///< low bitrate component in ExSS
170+ DCA_EXT_EXSS_XLL = 0x200, ///< lossless extension in ExSS
171+};
172+
173+/* -1 are reserved or unknown */
174+static const int dca_ext_audio_descr_mask[] = {
175+ DCA_EXT_XCH,
176+ -1,
177+ DCA_EXT_X96,
178+ DCA_EXT_XCH | DCA_EXT_X96,
179+ -1,
180+ -1,
181+ DCA_EXT_XXCH,
182+ -1,
183+};
184+
185+/* extensions that reside in core substream */
186+#define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
187+
188+/* Tables for mapping dts channel configurations to libavcodec multichannel api.
189+ * Some compromises have been made for special configurations. Most configurations
190+ * are never used so complete accuracy is not needed.
191+ *
192+ * L = left, R = right, C = center, S = surround, F = front, R = rear, T = total, OV = overhead.
193+ * S -> side, when both rear and back are configured move one of them to the side channel
194+ * OV -> center back
195+ * All 2 channel configurations -> AV_CH_LAYOUT_STEREO
196+ */
197+static const uint64_t dca_core_channel_layout[] = {
198+ AV_CH_FRONT_CENTER, ///< 1, A
199+ AV_CH_LAYOUT_STEREO, ///< 2, A + B (dual mono)
200+ AV_CH_LAYOUT_STEREO, ///< 2, L + R (stereo)
201+ AV_CH_LAYOUT_STEREO, ///< 2, (L + R) + (L - R) (sum-difference)
202+ AV_CH_LAYOUT_STEREO, ///< 2, LT + RT (left and right total)
203+ AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER, ///< 3, C + L + R
204+ AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER, ///< 3, L + R + S
205+ AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 4, C + L + R + S
206+ AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 4, L + R + SL + SR
207+
208+ AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER | AV_CH_SIDE_LEFT |
209+ AV_CH_SIDE_RIGHT, ///< 5, C + L + R + SL + SR
210+
211+ AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
212+ AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER, ///< 6, CL + CR + L + R + SL + SR
213+
214+ AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT |
215+ AV_CH_FRONT_CENTER | AV_CH_BACK_CENTER, ///< 6, C + L + R + LR + RR + OV
216+
217+ AV_CH_FRONT_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
218+ AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_BACK_CENTER |
219+ AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 6, CF + CR + LF + RF + LR + RR
220+
221+ AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
222+ AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
223+ AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT, ///< 7, CL + C + CR + L + R + SL + SR
224+
225+ AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER |
226+ AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT |
227+ AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT, ///< 8, CL + CR + L + R + SL1 + SL2 + SR1 + SR2
228+
229+ AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_CENTER |
230+ AV_CH_FRONT_RIGHT_OF_CENTER | AV_CH_LAYOUT_STEREO |
231+ AV_CH_SIDE_LEFT | AV_CH_BACK_CENTER | AV_CH_SIDE_RIGHT, ///< 8, CL + C + CR + L + R + SL + S + SR
232+};
233+
234+static const int8_t dca_lfe_index[] = {
235+ 1, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3, 1, 3, 2, 3
236+};
237+
238+static const int8_t dca_channel_reorder_lfe[][9] = {
239+ { 0, -1, -1, -1, -1, -1, -1, -1, -1},
240+ { 0, 1, -1, -1, -1, -1, -1, -1, -1},
241+ { 0, 1, -1, -1, -1, -1, -1, -1, -1},
242+ { 0, 1, -1, -1, -1, -1, -1, -1, -1},
243+ { 0, 1, -1, -1, -1, -1, -1, -1, -1},
244+ { 2, 0, 1, -1, -1, -1, -1, -1, -1},
245+ { 0, 1, 3, -1, -1, -1, -1, -1, -1},
246+ { 2, 0, 1, 4, -1, -1, -1, -1, -1},
247+ { 0, 1, 3, 4, -1, -1, -1, -1, -1},
248+ { 2, 0, 1, 4, 5, -1, -1, -1, -1},
249+ { 3, 4, 0, 1, 5, 6, -1, -1, -1},
250+ { 2, 0, 1, 4, 5, 6, -1, -1, -1},
251+ { 0, 6, 4, 5, 2, 3, -1, -1, -1},
252+ { 4, 2, 5, 0, 1, 6, 7, -1, -1},
253+ { 5, 6, 0, 1, 7, 3, 8, 4, -1},
254+ { 4, 2, 5, 0, 1, 6, 8, 7, -1},
255+};
256+
257+static const int8_t dca_channel_reorder_lfe_xch[][9] = {
258+ { 0, 2, -1, -1, -1, -1, -1, -1, -1},
259+ { 0, 1, 3, -1, -1, -1, -1, -1, -1},
260+ { 0, 1, 3, -1, -1, -1, -1, -1, -1},
261+ { 0, 1, 3, -1, -1, -1, -1, -1, -1},
262+ { 0, 1, 3, -1, -1, -1, -1, -1, -1},
263+ { 2, 0, 1, 4, -1, -1, -1, -1, -1},
264+ { 0, 1, 3, 4, -1, -1, -1, -1, -1},
265+ { 2, 0, 1, 4, 5, -1, -1, -1, -1},
266+ { 0, 1, 4, 5, 3, -1, -1, -1, -1},
267+ { 2, 0, 1, 5, 6, 4, -1, -1, -1},
268+ { 3, 4, 0, 1, 6, 7, 5, -1, -1},
269+ { 2, 0, 1, 4, 5, 6, 7, -1, -1},
270+ { 0, 6, 4, 5, 2, 3, 7, -1, -1},
271+ { 4, 2, 5, 0, 1, 7, 8, 6, -1},
272+ { 5, 6, 0, 1, 8, 3, 9, 4, 7},
273+ { 4, 2, 5, 0, 1, 6, 9, 8, 7},
274+};
275+
276+static const int8_t dca_channel_reorder_nolfe[][9] = {
277+ { 0, -1, -1, -1, -1, -1, -1, -1, -1},
278+ { 0, 1, -1, -1, -1, -1, -1, -1, -1},
279+ { 0, 1, -1, -1, -1, -1, -1, -1, -1},
280+ { 0, 1, -1, -1, -1, -1, -1, -1, -1},
281+ { 0, 1, -1, -1, -1, -1, -1, -1, -1},
282+ { 2, 0, 1, -1, -1, -1, -1, -1, -1},
283+ { 0, 1, 2, -1, -1, -1, -1, -1, -1},
284+ { 2, 0, 1, 3, -1, -1, -1, -1, -1},
285+ { 0, 1, 2, 3, -1, -1, -1, -1, -1},
286+ { 2, 0, 1, 3, 4, -1, -1, -1, -1},
287+ { 2, 3, 0, 1, 4, 5, -1, -1, -1},
288+ { 2, 0, 1, 3, 4, 5, -1, -1, -1},
289+ { 0, 5, 3, 4, 1, 2, -1, -1, -1},
290+ { 3, 2, 4, 0, 1, 5, 6, -1, -1},
291+ { 4, 5, 0, 1, 6, 2, 7, 3, -1},
292+ { 3, 2, 4, 0, 1, 5, 7, 6, -1},
293+};
294+
295+static const int8_t dca_channel_reorder_nolfe_xch[][9] = {
296+ { 0, 1, -1, -1, -1, -1, -1, -1, -1},
297+ { 0, 1, 2, -1, -1, -1, -1, -1, -1},
298+ { 0, 1, 2, -1, -1, -1, -1, -1, -1},
299+ { 0, 1, 2, -1, -1, -1, -1, -1, -1},
300+ { 0, 1, 2, -1, -1, -1, -1, -1, -1},
301+ { 2, 0, 1, 3, -1, -1, -1, -1, -1},
302+ { 0, 1, 2, 3, -1, -1, -1, -1, -1},
303+ { 2, 0, 1, 3, 4, -1, -1, -1, -1},
304+ { 0, 1, 3, 4, 2, -1, -1, -1, -1},
305+ { 2, 0, 1, 4, 5, 3, -1, -1, -1},
306+ { 2, 3, 0, 1, 5, 6, 4, -1, -1},
307+ { 2, 0, 1, 3, 4, 5, 6, -1, -1},
308+ { 0, 5, 3, 4, 1, 2, 6, -1, -1},
309+ { 3, 2, 4, 0, 1, 6, 7, 5, -1},
310+ { 4, 5, 0, 1, 7, 2, 8, 3, 6},
311+ { 3, 2, 4, 0, 1, 5, 8, 7, 6},
312+};
313+
314+#define DCA_DOLBY 101 /* FIXME */
315+
316+#define DCA_CHANNEL_BITS 6
317+#define DCA_CHANNEL_MASK 0x3F
318+
319+#define DCA_LFE 0x80
320+
321+#define HEADER_SIZE 14
322+
323+#define DCA_MAX_FRAME_SIZE 16384
324+#define DCA_MAX_EXSS_HEADER_SIZE 4096
325+
326+#define DCA_BUFFER_PADDING_SIZE 1024
327+
328+/** Bit allocation */
329+typedef struct {
330+ int offset; ///< code values offset
331+ int maxbits[8]; ///< max bits in VLC
332+ int wrap; ///< wrap for get_vlc2()
333+ VLC vlc[8]; ///< actual codes
334+} BitAlloc;
335+
336+static BitAlloc dca_bitalloc_index; ///< indexes for samples VLC select
337+static BitAlloc dca_tmode; ///< transition mode VLCs
338+static BitAlloc dca_scalefactor; ///< scalefactor VLCs
339+static BitAlloc dca_smpl_bitalloc[11]; ///< samples VLCs
340+
341+static av_always_inline int get_bitalloc(GetBitContext *gb, BitAlloc *ba,
342+ int idx)
343+{
344+ return get_vlc2(gb, ba->vlc[idx].table, ba->vlc[idx].bits, ba->wrap) +
345+ ba->offset;
346+}
347+
348+typedef struct {
349+ AVCodecContext *avctx;
350+ AVFrame frame;
351+ /* Frame header */
352+ int frame_type; ///< type of the current frame
353+ int samples_deficit; ///< deficit sample count
354+ int crc_present; ///< crc is present in the bitstream
355+ int sample_blocks; ///< number of PCM sample blocks
356+ int frame_size; ///< primary frame byte size
357+ int amode; ///< audio channels arrangement
358+ int sample_rate; ///< audio sampling rate
359+ int bit_rate; ///< transmission bit rate
360+ int bit_rate_index; ///< transmission bit rate index
361+
362+ int downmix; ///< embedded downmix enabled
363+ int dynrange; ///< embedded dynamic range flag
364+ int timestamp; ///< embedded time stamp flag
365+ int aux_data; ///< auxiliary data flag
366+ int hdcd; ///< source material is mastered in HDCD
367+ int ext_descr; ///< extension audio descriptor flag
368+ int ext_coding; ///< extended coding flag
369+ int aspf; ///< audio sync word insertion flag
370+ int lfe; ///< low frequency effects flag
371+ int predictor_history; ///< predictor history flag
372+ int header_crc; ///< header crc check bytes
373+ int multirate_inter; ///< multirate interpolator switch
374+ int version; ///< encoder software revision
375+ int copy_history; ///< copy history
376+ int source_pcm_res; ///< source pcm resolution
377+ int front_sum; ///< front sum/difference flag
378+ int surround_sum; ///< surround sum/difference flag
379+ int dialog_norm; ///< dialog normalisation parameter
380+
381+ /* Primary audio coding header */
382+ int subframes; ///< number of subframes
383+ int total_channels; ///< number of channels including extensions
384+ int prim_channels; ///< number of primary audio channels
385+ int subband_activity[DCA_PRIM_CHANNELS_MAX]; ///< subband activity count
386+ int vq_start_subband[DCA_PRIM_CHANNELS_MAX]; ///< high frequency vq start subband
387+ int joint_intensity[DCA_PRIM_CHANNELS_MAX]; ///< joint intensity coding index
388+ int transient_huffman[DCA_PRIM_CHANNELS_MAX]; ///< transient mode code book
389+ int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
390+ int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX]; ///< bit allocation quantizer select
391+ int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
392+ float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< scale factor adjustment
393+
394+ /* Primary audio coding side information */
395+ int subsubframes[DCA_SUBFRAMES_MAX]; ///< number of subsubframes
396+ int partial_samples[DCA_SUBFRAMES_MAX]; ///< partial subsubframe samples count
397+ int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction mode (ADPCM used or not)
398+ int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< prediction VQ coefs
399+ int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< bit allocation index
400+ int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< transition mode (transients)
401+ int scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2]; ///< scale factors (2 if transient)
402+ int joint_huff[DCA_PRIM_CHANNELS_MAX]; ///< joint subband scale factors codebook
403+ int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
404+ int downmix_coef[DCA_PRIM_CHANNELS_MAX][2]; ///< stereo downmix coefficients
405+ int dynrange_coef; ///< dynamic range coefficient
406+
407+ int high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< VQ encoded high frequency subbands
408+
409+ float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)]; ///< Low frequency effect data
410+ int lfe_scale_factor;
411+
412+ /* Subband samples history (for ADPCM) */
413+ DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
414+ DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
415+ DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
416+ int hist_index[DCA_PRIM_CHANNELS_MAX];
417+ DECLARE_ALIGNED(32, float, raXin)[32];
418+
419+ int output; ///< type of output
420+ float scale_bias; ///< output scale
421+
422+ DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
423+ DECLARE_ALIGNED(32, float, samples)[(DCA_PRIM_CHANNELS_MAX + 1) * 256];
424+ const float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
425+
426+ uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
427+ int dca_buffer_size; ///< how much data is in the dca_buffer
428+
429+ const int8_t *channel_order_tab; ///< channel reordering table, lfe and non lfe
430+ GetBitContext gb;
431+ /* Current position in DCA frame */
432+ int current_subframe;
433+ int current_subsubframe;
434+
435+ int core_ext_mask; ///< present extensions in the core substream
436+
437+ /* XCh extension information */
438+ int xch_present; ///< XCh extension present and valid
439+ int xch_base_channel; ///< index of first (only) channel containing XCH data
440+
441+ /* XXCH extension information */
442+ int xxch_chset;
443+ int xxch_nbits_spk_mask;
444+ uint32_t xxch_core_spkmask;
445+ uint32_t xxch_spk_masks[4]; /* speaker masks, last element is core mask */
446+ int xxch_chset_nch[4];
447+ float xxch_dmix_sf[DCA_CHSETS_MAX];
448+
449+ uint32_t xxch_downmix; /* downmix enabled per channel set */
450+ uint32_t xxch_dmix_embedded; /* lower layer has mix pre-embedded, per chset */
451+ float xxch_dmix_coeff[DCA_PRIM_CHANNELS_MAX][32]; /* worst case sizing */
452+
453+ int8_t xxch_order_tab[32];
454+ int8_t lfe_index;
455+
456+ /* ExSS header parser */
457+ int static_fields; ///< static fields present
458+ int mix_metadata; ///< mixing metadata present
459+ int num_mix_configs; ///< number of mix out configurations
460+ int mix_config_num_ch[4]; ///< number of channels in each mix out configuration
461+
462+ int profile;
463+
464+ int debug_flag; ///< used for suppressing repeated error messages output
465+ AVFloatDSPContext fdsp;
466+ FFTContext imdct;
467+ SynthFilterContext synth;
468+ DCADSPContext dcadsp;
469+ FmtConvertContext fmt_conv;
470+} DCAContext;
471+
472+static const uint16_t dca_vlc_offs[] = {
473+ 0, 512, 640, 768, 1282, 1794, 2436, 3080, 3770, 4454, 5364,
474+ 5372, 5380, 5388, 5392, 5396, 5412, 5420, 5428, 5460, 5492, 5508,
475+ 5572, 5604, 5668, 5796, 5860, 5892, 6412, 6668, 6796, 7308, 7564,
476+ 7820, 8076, 8620, 9132, 9388, 9910, 10166, 10680, 11196, 11726, 12240,
477+ 12752, 13298, 13810, 14326, 14840, 15500, 16022, 16540, 17158, 17678, 18264,
478+ 18796, 19352, 19926, 20468, 21472, 22398, 23014, 23622,
479+};
480+
481+static av_cold void dca_init_vlcs(void)
482+{
483+ static int vlcs_initialized = 0;
484+ int i, j, c = 14;
485+ static VLC_TYPE dca_table[23622][2];
486+
487+ if (vlcs_initialized)
488+ return;
489+
490+ dca_bitalloc_index.offset = 1;
491+ dca_bitalloc_index.wrap = 2;
492+ for (i = 0; i < 5; i++) {
493+ dca_bitalloc_index.vlc[i].table = &dca_table[dca_vlc_offs[i]];
494+ dca_bitalloc_index.vlc[i].table_allocated = dca_vlc_offs[i + 1] - dca_vlc_offs[i];
495+ init_vlc(&dca_bitalloc_index.vlc[i], bitalloc_12_vlc_bits[i], 12,
496+ bitalloc_12_bits[i], 1, 1,
497+ bitalloc_12_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
498+ }
499+ dca_scalefactor.offset = -64;
500+ dca_scalefactor.wrap = 2;
501+ for (i = 0; i < 5; i++) {
502+ dca_scalefactor.vlc[i].table = &dca_table[dca_vlc_offs[i + 5]];
503+ dca_scalefactor.vlc[i].table_allocated = dca_vlc_offs[i + 6] - dca_vlc_offs[i + 5];
504+ init_vlc(&dca_scalefactor.vlc[i], SCALES_VLC_BITS, 129,
505+ scales_bits[i], 1, 1,
506+ scales_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
507+ }
508+ dca_tmode.offset = 0;
509+ dca_tmode.wrap = 1;
510+ for (i = 0; i < 4; i++) {
511+ dca_tmode.vlc[i].table = &dca_table[dca_vlc_offs[i + 10]];
512+ dca_tmode.vlc[i].table_allocated = dca_vlc_offs[i + 11] - dca_vlc_offs[i + 10];
513+ init_vlc(&dca_tmode.vlc[i], tmode_vlc_bits[i], 4,
514+ tmode_bits[i], 1, 1,
515+ tmode_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
516+ }
517+
518+ for (i = 0; i < 10; i++)
519+ for (j = 0; j < 7; j++) {
520+ if (!bitalloc_codes[i][j])
521+ break;
522+ dca_smpl_bitalloc[i + 1].offset = bitalloc_offsets[i];
523+ dca_smpl_bitalloc[i + 1].wrap = 1 + (j > 4);
524+ dca_smpl_bitalloc[i + 1].vlc[j].table = &dca_table[dca_vlc_offs[c]];
525+ dca_smpl_bitalloc[i + 1].vlc[j].table_allocated = dca_vlc_offs[c + 1] - dca_vlc_offs[c];
526+
527+ init_vlc(&dca_smpl_bitalloc[i + 1].vlc[j], bitalloc_maxbits[i][j],
528+ bitalloc_sizes[i],
529+ bitalloc_bits[i][j], 1, 1,
530+ bitalloc_codes[i][j], 2, 2, INIT_VLC_USE_NEW_STATIC);
531+ c++;
532+ }
533+ vlcs_initialized = 1;
534+}
535+
536+static inline void get_array(GetBitContext *gb, int *dst, int len, int bits)
537+{
538+ while (len--)
539+ *dst++ = get_bits(gb, bits);
540+}
541+
542+static inline int dca_xxch2index(DCAContext *s, int xxch_ch)
543+{
544+ int i, base, mask;
545+
546+ /* locate channel set containing the channel */
547+ for (i = -1, base = 0, mask = (s->xxch_core_spkmask & ~DCA_XXCH_LFE1);
548+ i <= s->xxch_chset && !(mask & xxch_ch); mask = s->xxch_spk_masks[++i])
549+ base += av_popcount(mask);
550+
551+ return base + av_popcount(mask & (xxch_ch - 1));
552+}
553+
554+static int dca_parse_audio_coding_header(DCAContext *s, int base_channel,
555+ int xxch)
556+{
557+ int i, j;
558+ static const float adj_table[4] = { 1.0, 1.1250, 1.2500, 1.4375 };
559+ static const int bitlen[11] = { 0, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3 };
560+ static const int thr[11] = { 0, 1, 3, 3, 3, 3, 7, 7, 7, 7, 7 };
561+ int hdr_pos = 0, hdr_size = 0;
562+ float sign, mag, scale_factor;
563+ int this_chans, acc_mask;
564+ int embedded_downmix;
565+ int nchans, mask[8];
566+ int coeff, ichan;
567+
568+ /* xxch has arbitrary sized audio coding headers */
569+ if (xxch) {
570+ hdr_pos = get_bits_count(&s->gb);
571+ hdr_size = get_bits(&s->gb, 7) + 1;
572+ }
573+
574+ nchans = get_bits(&s->gb, 3) + 1;
575+ s->total_channels = nchans + base_channel;
576+ s->prim_channels = s->total_channels;
577+
578+ /* obtain speaker layout mask & downmix coefficients for XXCH */
579+ if (xxch) {
580+ acc_mask = s->xxch_core_spkmask;
581+
582+ this_chans = get_bits(&s->gb, s->xxch_nbits_spk_mask - 6) << 6;
583+ s->xxch_spk_masks[s->xxch_chset] = this_chans;
584+ s->xxch_chset_nch[s->xxch_chset] = nchans;
585+
586+ for (i = 0; i <= s->xxch_chset; i++)
587+ acc_mask |= s->xxch_spk_masks[i];
588+
589+ /* check for downmixing information */
590+ if (get_bits1(&s->gb)) {
591+ embedded_downmix = get_bits1(&s->gb);
592+ scale_factor =
593+ 1.0f / dca_downmix_scale_factors[(get_bits(&s->gb, 6) - 1) << 2];
594+
595+ s->xxch_dmix_sf[s->xxch_chset] = scale_factor;
596+
597+ for (i = base_channel; i < s->prim_channels; i++) {
598+ s->xxch_downmix |= (1 << i);
599+ mask[i] = get_bits(&s->gb, s->xxch_nbits_spk_mask);
600+ }
601+
602+ for (j = base_channel; j < s->prim_channels; j++) {
603+ memset(s->xxch_dmix_coeff[j], 0, sizeof(s->xxch_dmix_coeff[0]));
604+ s->xxch_dmix_embedded |= (embedded_downmix << j);
605+ for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
606+ if (mask[j] & (1 << i)) {
607+ if ((1 << i) == DCA_XXCH_LFE1) {
608+ av_log(s->avctx, AV_LOG_WARNING,
609+ "DCA-XXCH: dmix to LFE1 not supported.\n");
610+ continue;
611+ }
612+
613+ coeff = get_bits(&s->gb, 7);
614+ sign = (coeff & 64) ? 1.0 : -1.0;
615+ mag = dca_downmix_scale_factors[((coeff & 63) - 1) << 2];
616+ ichan = dca_xxch2index(s, 1 << i);
617+ s->xxch_dmix_coeff[j][ichan] = sign * mag;
618+ }
619+ }
620+ }
621+ }
622+ }
623+
624+ if (s->prim_channels > DCA_PRIM_CHANNELS_MAX)
625+ s->prim_channels = DCA_PRIM_CHANNELS_MAX;
626+
627+
628+ for (i = base_channel; i < s->prim_channels; i++) {
629+ s->subband_activity[i] = get_bits(&s->gb, 5) + 2;
630+ if (s->subband_activity[i] > DCA_SUBBANDS)
631+ s->subband_activity[i] = DCA_SUBBANDS;
632+ }
633+ for (i = base_channel; i < s->prim_channels; i++) {
634+ s->vq_start_subband[i] = get_bits(&s->gb, 5) + 1;
635+ if (s->vq_start_subband[i] > DCA_SUBBANDS)
636+ s->vq_start_subband[i] = DCA_SUBBANDS;
637+ }
638+ get_array(&s->gb, s->joint_intensity + base_channel, s->prim_channels - base_channel, 3);
639+ get_array(&s->gb, s->transient_huffman + base_channel, s->prim_channels - base_channel, 2);
640+ get_array(&s->gb, s->scalefactor_huffman + base_channel, s->prim_channels - base_channel, 3);
641+ get_array(&s->gb, s->bitalloc_huffman + base_channel, s->prim_channels - base_channel, 3);
642+
643+ /* Get codebooks quantization indexes */
644+ if (!base_channel)
645+ memset(s->quant_index_huffman, 0, sizeof(s->quant_index_huffman));
646+ for (j = 1; j < 11; j++)
647+ for (i = base_channel; i < s->prim_channels; i++)
648+ s->quant_index_huffman[i][j] = get_bits(&s->gb, bitlen[j]);
649+
650+ /* Get scale factor adjustment */
651+ for (j = 0; j < 11; j++)
652+ for (i = base_channel; i < s->prim_channels; i++)
653+ s->scalefactor_adj[i][j] = 1;
654+
655+ for (j = 1; j < 11; j++)
656+ for (i = base_channel; i < s->prim_channels; i++)
657+ if (s->quant_index_huffman[i][j] < thr[j])
658+ s->scalefactor_adj[i][j] = adj_table[get_bits(&s->gb, 2)];
659+
660+ if (!xxch) {
661+ if (s->crc_present) {
662+ /* Audio header CRC check */
663+ get_bits(&s->gb, 16);
664+ }
665+ } else {
666+ /* Skip to the end of the header, also ignore CRC if present */
667+ i = get_bits_count(&s->gb);
668+ if (hdr_pos + 8 * hdr_size > i)
669+ skip_bits_long(&s->gb, hdr_pos + 8 * hdr_size - i);
670+ }
671+
672+ s->current_subframe = 0;
673+ s->current_subsubframe = 0;
674+
675+#ifdef TRACE
676+ av_log(s->avctx, AV_LOG_DEBUG, "subframes: %i\n", s->subframes);
677+ av_log(s->avctx, AV_LOG_DEBUG, "prim channels: %i\n", s->prim_channels);
678+ for (i = base_channel; i < s->prim_channels; i++) {
679+ av_log(s->avctx, AV_LOG_DEBUG, "subband activity: %i\n",
680+ s->subband_activity[i]);
681+ av_log(s->avctx, AV_LOG_DEBUG, "vq start subband: %i\n",
682+ s->vq_start_subband[i]);
683+ av_log(s->avctx, AV_LOG_DEBUG, "joint intensity: %i\n",
684+ s->joint_intensity[i]);
685+ av_log(s->avctx, AV_LOG_DEBUG, "transient mode codebook: %i\n",
686+ s->transient_huffman[i]);
687+ av_log(s->avctx, AV_LOG_DEBUG, "scale factor codebook: %i\n",
688+ s->scalefactor_huffman[i]);
689+ av_log(s->avctx, AV_LOG_DEBUG, "bit allocation quantizer: %i\n",
690+ s->bitalloc_huffman[i]);
691+ av_log(s->avctx, AV_LOG_DEBUG, "quant index huff:");
692+ for (j = 0; j < 11; j++)
693+ av_log(s->avctx, AV_LOG_DEBUG, " %i", s->quant_index_huffman[i][j]);
694+ av_log(s->avctx, AV_LOG_DEBUG, "\n");
695+ av_log(s->avctx, AV_LOG_DEBUG, "scalefac adj:");
696+ for (j = 0; j < 11; j++)
697+ av_log(s->avctx, AV_LOG_DEBUG, " %1.3f", s->scalefactor_adj[i][j]);
698+ av_log(s->avctx, AV_LOG_DEBUG, "\n");
699+ }
700+#endif
701+
702+ return 0;
703+}
704+
705+static int dca_parse_frame_header(DCAContext *s)
706+{
707+ init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
708+
709+ /* Sync code */
710+ skip_bits_long(&s->gb, 32);
711+
712+ /* Frame header */
713+ s->frame_type = get_bits(&s->gb, 1);
714+ s->samples_deficit = get_bits(&s->gb, 5) + 1;
715+ s->crc_present = get_bits(&s->gb, 1);
716+ s->sample_blocks = get_bits(&s->gb, 7) + 1;
717+ s->frame_size = get_bits(&s->gb, 14) + 1;
718+ if (s->frame_size < 95)
719+ return AVERROR_INVALIDDATA;
720+ s->amode = get_bits(&s->gb, 6);
721+ s->sample_rate = avpriv_dca_sample_rates[get_bits(&s->gb, 4)];
722+ if (!s->sample_rate)
723+ return AVERROR_INVALIDDATA;
724+ s->bit_rate_index = get_bits(&s->gb, 5);
725+ s->bit_rate = dca_bit_rates[s->bit_rate_index];
726+ if (!s->bit_rate)
727+ return AVERROR_INVALIDDATA;
728+
729+ s->downmix = get_bits(&s->gb, 1); /* note: this is FixedBit == 0 */
730+ s->dynrange = get_bits(&s->gb, 1);
731+ s->timestamp = get_bits(&s->gb, 1);
732+ s->aux_data = get_bits(&s->gb, 1);
733+ s->hdcd = get_bits(&s->gb, 1);
734+ s->ext_descr = get_bits(&s->gb, 3);
735+ s->ext_coding = get_bits(&s->gb, 1);
736+ s->aspf = get_bits(&s->gb, 1);
737+ s->lfe = get_bits(&s->gb, 2);
738+ s->predictor_history = get_bits(&s->gb, 1);
739+
740+ /* TODO: check CRC */
741+ if (s->crc_present)
742+ s->header_crc = get_bits(&s->gb, 16);
743+
744+ s->multirate_inter = get_bits(&s->gb, 1);
745+ s->version = get_bits(&s->gb, 4);
746+ s->copy_history = get_bits(&s->gb, 2);
747+ s->source_pcm_res = get_bits(&s->gb, 3);
748+ s->front_sum = get_bits(&s->gb, 1);
749+ s->surround_sum = get_bits(&s->gb, 1);
750+ s->dialog_norm = get_bits(&s->gb, 4);
751+
752+ /* FIXME: channels mixing levels */
753+ s->output = s->amode;
754+ if (s->lfe)
755+ s->output |= DCA_LFE;
756+
757+#ifdef TRACE
758+ av_log(s->avctx, AV_LOG_DEBUG, "frame type: %i\n", s->frame_type);
759+ av_log(s->avctx, AV_LOG_DEBUG, "samples deficit: %i\n", s->samples_deficit);
760+ av_log(s->avctx, AV_LOG_DEBUG, "crc present: %i\n", s->crc_present);
761+ av_log(s->avctx, AV_LOG_DEBUG, "sample blocks: %i (%i samples)\n",
762+ s->sample_blocks, s->sample_blocks * 32);
763+ av_log(s->avctx, AV_LOG_DEBUG, "frame size: %i bytes\n", s->frame_size);
764+ av_log(s->avctx, AV_LOG_DEBUG, "amode: %i (%i channels)\n",
765+ s->amode, dca_channels[s->amode]);
766+ av_log(s->avctx, AV_LOG_DEBUG, "sample rate: %i Hz\n",
767+ s->sample_rate);
768+ av_log(s->avctx, AV_LOG_DEBUG, "bit rate: %i bits/s\n",
769+ s->bit_rate);
770+ av_log(s->avctx, AV_LOG_DEBUG, "downmix: %i\n", s->downmix);
771+ av_log(s->avctx, AV_LOG_DEBUG, "dynrange: %i\n", s->dynrange);
772+ av_log(s->avctx, AV_LOG_DEBUG, "timestamp: %i\n", s->timestamp);
773+ av_log(s->avctx, AV_LOG_DEBUG, "aux_data: %i\n", s->aux_data);
774+ av_log(s->avctx, AV_LOG_DEBUG, "hdcd: %i\n", s->hdcd);
775+ av_log(s->avctx, AV_LOG_DEBUG, "ext descr: %i\n", s->ext_descr);
776+ av_log(s->avctx, AV_LOG_DEBUG, "ext coding: %i\n", s->ext_coding);
777+ av_log(s->avctx, AV_LOG_DEBUG, "aspf: %i\n", s->aspf);
778+ av_log(s->avctx, AV_LOG_DEBUG, "lfe: %i\n", s->lfe);
779+ av_log(s->avctx, AV_LOG_DEBUG, "predictor history: %i\n",
780+ s->predictor_history);
781+ av_log(s->avctx, AV_LOG_DEBUG, "header crc: %i\n", s->header_crc);
782+ av_log(s->avctx, AV_LOG_DEBUG, "multirate inter: %i\n",
783+ s->multirate_inter);
784+ av_log(s->avctx, AV_LOG_DEBUG, "version number: %i\n", s->version);
785+ av_log(s->avctx, AV_LOG_DEBUG, "copy history: %i\n", s->copy_history);
786+ av_log(s->avctx, AV_LOG_DEBUG,
787+ "source pcm resolution: %i (%i bits/sample)\n",
788+ s->source_pcm_res, dca_bits_per_sample[s->source_pcm_res]);
789+ av_log(s->avctx, AV_LOG_DEBUG, "front sum: %i\n", s->front_sum);
790+ av_log(s->avctx, AV_LOG_DEBUG, "surround sum: %i\n", s->surround_sum);
791+ av_log(s->avctx, AV_LOG_DEBUG, "dialog norm: %i\n", s->dialog_norm);
792+ av_log(s->avctx, AV_LOG_DEBUG, "\n");
793+#endif
794+
795+ /* Primary audio coding header */
796+ s->subframes = get_bits(&s->gb, 4) + 1;
797+
798+ return dca_parse_audio_coding_header(s, 0, 0);
799+}
800+
801+
802+static inline int get_scale(GetBitContext *gb, int level, int value, int log2range)
803+{
804+ if (level < 5) {
805+ /* huffman encoded */
806+ value += get_bitalloc(gb, &dca_scalefactor, level);
807+ value = av_clip(value, 0, (1 << log2range) - 1);
808+ } else if (level < 8) {
809+ if (level + 1 > log2range) {
810+ skip_bits(gb, level + 1 - log2range);
811+ value = get_bits(gb, log2range);
812+ } else {
813+ value = get_bits(gb, level + 1);
814+ }
815+ }
816+ return value;
817+}
818+
819+static int dca_subframe_header(DCAContext *s, int base_channel, int block_index)
820+{
821+ /* Primary audio coding side information */
822+ int j, k;
823+
824+ if (get_bits_left(&s->gb) < 0)
825+ return AVERROR_INVALIDDATA;
826+
827+ if (!base_channel) {
828+ s->subsubframes[s->current_subframe] = get_bits(&s->gb, 2) + 1;
829+ s->partial_samples[s->current_subframe] = get_bits(&s->gb, 3);
830+ }
831+
832+ for (j = base_channel; j < s->prim_channels; j++) {
833+ for (k = 0; k < s->subband_activity[j]; k++)
834+ s->prediction_mode[j][k] = get_bits(&s->gb, 1);
835+ }
836+
837+ /* Get prediction codebook */
838+ for (j = base_channel; j < s->prim_channels; j++) {
839+ for (k = 0; k < s->subband_activity[j]; k++) {
840+ if (s->prediction_mode[j][k] > 0) {
841+ /* (Prediction coefficient VQ address) */
842+ s->prediction_vq[j][k] = get_bits(&s->gb, 12);
843+ }
844+ }
845+ }
846+
847+ /* Bit allocation index */
848+ for (j = base_channel; j < s->prim_channels; j++) {
849+ for (k = 0; k < s->vq_start_subband[j]; k++) {
850+ if (s->bitalloc_huffman[j] == 6)
851+ s->bitalloc[j][k] = get_bits(&s->gb, 5);
852+ else if (s->bitalloc_huffman[j] == 5)
853+ s->bitalloc[j][k] = get_bits(&s->gb, 4);
854+ else if (s->bitalloc_huffman[j] == 7) {
855+ av_log(s->avctx, AV_LOG_ERROR,
856+ "Invalid bit allocation index\n");
857+ return AVERROR_INVALIDDATA;
858+ } else {
859+ s->bitalloc[j][k] =
860+ get_bitalloc(&s->gb, &dca_bitalloc_index, s->bitalloc_huffman[j]);
861+ }
862+
863+ if (s->bitalloc[j][k] > 26) {
864+ // av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index [%i][%i] too big (%i)\n",
865+ // j, k, s->bitalloc[j][k]);
866+ return AVERROR_INVALIDDATA;
867+ }
868+ }
869+ }
870+
871+ /* Transition mode */
872+ for (j = base_channel; j < s->prim_channels; j++) {
873+ for (k = 0; k < s->subband_activity[j]; k++) {
874+ s->transition_mode[j][k] = 0;
875+ if (s->subsubframes[s->current_subframe] > 1 &&
876+ k < s->vq_start_subband[j] && s->bitalloc[j][k] > 0) {
877+ s->transition_mode[j][k] =
878+ get_bitalloc(&s->gb, &dca_tmode, s->transient_huffman[j]);
879+ }
880+ }
881+ }
882+
883+ if (get_bits_left(&s->gb) < 0)
884+ return AVERROR_INVALIDDATA;
885+
886+ for (j = base_channel; j < s->prim_channels; j++) {
887+ const uint32_t *scale_table;
888+ int scale_sum, log_size;
889+
890+ memset(s->scale_factor[j], 0,
891+ s->subband_activity[j] * sizeof(s->scale_factor[0][0][0]) * 2);
892+
893+ if (s->scalefactor_huffman[j] == 6) {
894+ scale_table = scale_factor_quant7;
895+ log_size = 7;
896+ } else {
897+ scale_table = scale_factor_quant6;
898+ log_size = 6;
899+ }
900+
901+ /* When huffman coded, only the difference is encoded */
902+ scale_sum = 0;
903+
904+ for (k = 0; k < s->subband_activity[j]; k++) {
905+ if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0) {
906+ scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
907+ s->scale_factor[j][k][0] = scale_table[scale_sum];
908+ }
909+
910+ if (k < s->vq_start_subband[j] && s->transition_mode[j][k]) {
911+ /* Get second scale factor */
912+ scale_sum = get_scale(&s->gb, s->scalefactor_huffman[j], scale_sum, log_size);
913+ s->scale_factor[j][k][1] = scale_table[scale_sum];
914+ }
915+ }
916+ }
917+
918+ /* Joint subband scale factor codebook select */
919+ for (j = base_channel; j < s->prim_channels; j++) {
920+ /* Transmitted only if joint subband coding enabled */
921+ if (s->joint_intensity[j] > 0)
922+ s->joint_huff[j] = get_bits(&s->gb, 3);
923+ }
924+
925+ if (get_bits_left(&s->gb) < 0)
926+ return AVERROR_INVALIDDATA;
927+
928+ /* Scale factors for joint subband coding */
929+ for (j = base_channel; j < s->prim_channels; j++) {
930+ int source_channel;
931+
932+ /* Transmitted only if joint subband coding enabled */
933+ if (s->joint_intensity[j] > 0) {
934+ int scale = 0;
935+ source_channel = s->joint_intensity[j] - 1;
936+
937+ /* When huffman coded, only the difference is encoded
938+ * (is this valid as well for joint scales ???) */
939+
940+ for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++) {
941+ scale = get_scale(&s->gb, s->joint_huff[j], 64 /* bias */, 7);
942+ s->joint_scale_factor[j][k] = scale; /*joint_scale_table[scale]; */
943+ }
944+
945+ if (!(s->debug_flag & 0x02)) {
946+ av_log(s->avctx, AV_LOG_DEBUG,
947+ "Joint stereo coding not supported\n");
948+ s->debug_flag |= 0x02;
949+ }
950+ }
951+ }
952+
953+ /* Stereo downmix coefficients */
954+ if (!base_channel && s->prim_channels > 2) {
955+ if (s->downmix) {
956+ for (j = base_channel; j < s->prim_channels; j++) {
957+ s->downmix_coef[j][0] = get_bits(&s->gb, 7);
958+ s->downmix_coef[j][1] = get_bits(&s->gb, 7);
959+ }
960+ } else {
961+ int am = s->amode & DCA_CHANNEL_MASK;
962+ if (am >= FF_ARRAY_ELEMS(dca_default_coeffs)) {
963+ av_log(s->avctx, AV_LOG_ERROR,
964+ "Invalid channel mode %d\n", am);
965+ return AVERROR_INVALIDDATA;
966+ }
967+ for (j = base_channel; j < FFMIN(s->prim_channels, FF_ARRAY_ELEMS(dca_default_coeffs[am])); j++) {
968+ s->downmix_coef[j][0] = dca_default_coeffs[am][j][0];
969+ s->downmix_coef[j][1] = dca_default_coeffs[am][j][1];
970+ }
971+ }
972+ }
973+
974+ /* Dynamic range coefficient */
975+ if (!base_channel && s->dynrange)
976+ s->dynrange_coef = get_bits(&s->gb, 8);
977+
978+ /* Side information CRC check word */
979+ if (s->crc_present) {
980+ get_bits(&s->gb, 16);
981+ }
982+
983+ /*
984+ * Primary audio data arrays
985+ */
986+
987+ /* VQ encoded high frequency subbands */
988+ for (j = base_channel; j < s->prim_channels; j++)
989+ for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
990+ /* 1 vector -> 32 samples */
991+ s->high_freq_vq[j][k] = get_bits(&s->gb, 10);
992+
993+ /* Low frequency effect data */
994+ if (!base_channel && s->lfe) {
995+ int quant7;
996+ /* LFE samples */
997+ int lfe_samples = 2 * s->lfe * (4 + block_index);
998+ int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
999+ float lfe_scale;
1000+
1001+ for (j = lfe_samples; j < lfe_end_sample; j++) {
1002+ /* Signed 8 bits int */
1003+ s->lfe_data[j] = get_sbits(&s->gb, 8);
1004+ }
1005+
1006+ /* Scale factor index */
1007+ quant7 = get_bits(&s->gb, 8);
1008+ if (quant7 > 127) {
1009+ av_log_ask_for_sample(s->avctx, "LFEScaleIndex larger than 127\n");
1010+ return AVERROR_INVALIDDATA;
1011+ }
1012+ s->lfe_scale_factor = scale_factor_quant7[quant7];
1013+
1014+ /* Quantization step size * scale factor */
1015+ lfe_scale = 0.035 * s->lfe_scale_factor;
1016+
1017+ for (j = lfe_samples; j < lfe_end_sample; j++)
1018+ s->lfe_data[j] *= lfe_scale;
1019+ }
1020+
1021+#ifdef TRACE
1022+ av_log(s->avctx, AV_LOG_DEBUG, "subsubframes: %i\n",
1023+ s->subsubframes[s->current_subframe]);
1024+ av_log(s->avctx, AV_LOG_DEBUG, "partial samples: %i\n",
1025+ s->partial_samples[s->current_subframe]);
1026+
1027+ for (j = base_channel; j < s->prim_channels; j++) {
1028+ av_log(s->avctx, AV_LOG_DEBUG, "prediction mode:");
1029+ for (k = 0; k < s->subband_activity[j]; k++)
1030+ av_log(s->avctx, AV_LOG_DEBUG, " %i", s->prediction_mode[j][k]);
1031+ av_log(s->avctx, AV_LOG_DEBUG, "\n");
1032+ }
1033+ for (j = base_channel; j < s->prim_channels; j++) {
1034+ for (k = 0; k < s->subband_activity[j]; k++)
1035+ av_log(s->avctx, AV_LOG_DEBUG,
1036+ "prediction coefs: %f, %f, %f, %f\n",
1037+ (float) adpcm_vb[s->prediction_vq[j][k]][0] / 8192,
1038+ (float) adpcm_vb[s->prediction_vq[j][k]][1] / 8192,
1039+ (float) adpcm_vb[s->prediction_vq[j][k]][2] / 8192,
1040+ (float) adpcm_vb[s->prediction_vq[j][k]][3] / 8192);
1041+ }
1042+ for (j = base_channel; j < s->prim_channels; j++) {
1043+ av_log(s->avctx, AV_LOG_DEBUG, "bitalloc index: ");
1044+ for (k = 0; k < s->vq_start_subband[j]; k++)
1045+ av_log(s->avctx, AV_LOG_DEBUG, "%2.2i ", s->bitalloc[j][k]);
1046+ av_log(s->avctx, AV_LOG_DEBUG, "\n");
1047+ }
1048+ for (j = base_channel; j < s->prim_channels; j++) {
1049+ av_log(s->avctx, AV_LOG_DEBUG, "Transition mode:");
1050+ for (k = 0; k < s->subband_activity[j]; k++)
1051+ av_log(s->avctx, AV_LOG_DEBUG, " %i", s->transition_mode[j][k]);
1052+ av_log(s->avctx, AV_LOG_DEBUG, "\n");
1053+ }
1054+ for (j = base_channel; j < s->prim_channels; j++) {
1055+ av_log(s->avctx, AV_LOG_DEBUG, "Scale factor:");
1056+ for (k = 0; k < s->subband_activity[j]; k++) {
1057+ if (k >= s->vq_start_subband[j] || s->bitalloc[j][k] > 0)
1058+ av_log(s->avctx, AV_LOG_DEBUG, " %i", s->scale_factor[j][k][0]);
1059+ if (k < s->vq_start_subband[j] && s->transition_mode[j][k])
1060+ av_log(s->avctx, AV_LOG_DEBUG, " %i(t)", s->scale_factor[j][k][1]);
1061+ }
1062+ av_log(s->avctx, AV_LOG_DEBUG, "\n");
1063+ }
1064+ for (j = base_channel; j < s->prim_channels; j++) {
1065+ if (s->joint_intensity[j] > 0) {
1066+ int source_channel = s->joint_intensity[j] - 1;
1067+ av_log(s->avctx, AV_LOG_DEBUG, "Joint scale factor index:\n");
1068+ for (k = s->subband_activity[j]; k < s->subband_activity[source_channel]; k++)
1069+ av_log(s->avctx, AV_LOG_DEBUG, " %i", s->joint_scale_factor[j][k]);
1070+ av_log(s->avctx, AV_LOG_DEBUG, "\n");
1071+ }
1072+ }
1073+ if (!base_channel && s->prim_channels > 2 && s->downmix) {
1074+ av_log(s->avctx, AV_LOG_DEBUG, "Downmix coeffs:\n");
1075+ for (j = 0; j < s->prim_channels; j++) {
1076+ av_log(s->avctx, AV_LOG_DEBUG, "Channel 0, %d = %f\n", j,
1077+ dca_downmix_coeffs[s->downmix_coef[j][0]]);
1078+ av_log(s->avctx, AV_LOG_DEBUG, "Channel 1, %d = %f\n", j,
1079+ dca_downmix_coeffs[s->downmix_coef[j][1]]);
1080+ }
1081+ av_log(s->avctx, AV_LOG_DEBUG, "\n");
1082+ }
1083+ for (j = base_channel; j < s->prim_channels; j++)
1084+ for (k = s->vq_start_subband[j]; k < s->subband_activity[j]; k++)
1085+ av_log(s->avctx, AV_LOG_DEBUG, "VQ index: %i\n", s->high_freq_vq[j][k]);
1086+ if (!base_channel && s->lfe) {
1087+ int lfe_samples = 2 * s->lfe * (4 + block_index);
1088+ int lfe_end_sample = 2 * s->lfe * (4 + block_index + s->subsubframes[s->current_subframe]);
1089+
1090+ av_log(s->avctx, AV_LOG_DEBUG, "LFE samples:\n");
1091+ for (j = lfe_samples; j < lfe_end_sample; j++)
1092+ av_log(s->avctx, AV_LOG_DEBUG, " %f", s->lfe_data[j]);
1093+ av_log(s->avctx, AV_LOG_DEBUG, "\n");
1094+ }
1095+#endif
1096+
1097+ return 0;
1098+}
1099+
1100+static void qmf_32_subbands(DCAContext *s, int chans,
1101+ float samples_in[32][8], float *samples_out,
1102+ float scale)
1103+{
1104+ const float *prCoeff;
1105+ int i;
1106+
1107+ int sb_act = s->subband_activity[chans];
1108+ int subindex;
1109+
1110+ scale *= sqrt(1 / 8.0);
1111+
1112+ /* Select filter */
1113+ if (!s->multirate_inter) /* Non-perfect reconstruction */
1114+ prCoeff = fir_32bands_nonperfect;
1115+ else /* Perfect reconstruction */
1116+ prCoeff = fir_32bands_perfect;
1117+
1118+ for (i = sb_act; i < 32; i++)
1119+ s->raXin[i] = 0.0;
1120+
1121+ /* Reconstructed channel sample index */
1122+ for (subindex = 0; subindex < 8; subindex++) {
1123+ /* Load in one sample from each subband and clear inactive subbands */
1124+ for (i = 0; i < sb_act; i++) {
1125+ unsigned sign = (i - 1) & 2;
1126+ uint32_t v = AV_RN32A(&samples_in[i][subindex]) ^ sign << 30;
1127+ AV_WN32A(&s->raXin[i], v);
1128+ }
1129+
1130+ s->synth.synth_filter_float(&s->imdct,
1131+ s->subband_fir_hist[chans],
1132+ &s->hist_index[chans],
1133+ s->subband_fir_noidea[chans], prCoeff,
1134+ samples_out, s->raXin, scale);
1135+ samples_out += 32;
1136+ }
1137+}
1138+
1139+static void lfe_interpolation_fir(DCAContext *s, int decimation_select,
1140+ int num_deci_sample, float *samples_in,
1141+ float *samples_out, float scale)
1142+{
1143+ /* samples_in: An array holding decimated samples.
1144+ * Samples in current subframe starts from samples_in[0],
1145+ * while samples_in[-1], samples_in[-2], ..., stores samples
1146+ * from last subframe as history.
1147+ *
1148+ * samples_out: An array holding interpolated samples
1149+ */
1150+
1151+ int decifactor;
1152+ const float *prCoeff;
1153+ int deciindex;
1154+
1155+ /* Select decimation filter */
1156+ if (decimation_select == 1) {
1157+ decifactor = 64;
1158+ prCoeff = lfe_fir_128;
1159+ } else {
1160+ decifactor = 32;
1161+ prCoeff = lfe_fir_64;
1162+ }
1163+ /* Interpolation */
1164+ for (deciindex = 0; deciindex < num_deci_sample; deciindex++) {
1165+ s->dcadsp.lfe_fir(samples_out, samples_in, prCoeff, decifactor, scale);
1166+ samples_in++;
1167+ samples_out += 2 * decifactor;
1168+ }
1169+}
1170+
1171+/* downmixing routines */
1172+#define MIX_REAR1(samples, si1, rs, coef) \
1173+ samples[i] += samples[si1] * coef[rs][0]; \
1174+ samples[i+256] += samples[si1] * coef[rs][1];
1175+
1176+#define MIX_REAR2(samples, si1, si2, rs, coef) \
1177+ samples[i] += samples[si1] * coef[rs][0] + samples[si2] * coef[rs + 1][0]; \
1178+ samples[i+256] += samples[si1] * coef[rs][1] + samples[si2] * coef[rs + 1][1];
1179+
1180+#define MIX_FRONT3(samples, coef) \
1181+ t = samples[i + c]; \
1182+ u = samples[i + l]; \
1183+ v = samples[i + r]; \
1184+ samples[i] = t * coef[0][0] + u * coef[1][0] + v * coef[2][0]; \
1185+ samples[i+256] = t * coef[0][1] + u * coef[1][1] + v * coef[2][1];
1186+
1187+#define DOWNMIX_TO_STEREO(op1, op2) \
1188+ for (i = 0; i < 256; i++) { \
1189+ op1 \
1190+ op2 \
1191+ }
1192+
1193+static void dca_downmix(float *samples, int srcfmt,
1194+ int downmix_coef[DCA_PRIM_CHANNELS_MAX][2],
1195+ const int8_t *channel_mapping)
1196+{
1197+ int c, l, r, sl, sr, s;
1198+ int i;
1199+ float t, u, v;
1200+ float coef[DCA_PRIM_CHANNELS_MAX][2];
1201+
1202+ for (i = 0; i < DCA_PRIM_CHANNELS_MAX; i++) {
1203+ coef[i][0] = dca_downmix_coeffs[downmix_coef[i][0]];
1204+ coef[i][1] = dca_downmix_coeffs[downmix_coef[i][1]];
1205+ }
1206+
1207+ switch (srcfmt) {
1208+ case DCA_MONO:
1209+ case DCA_CHANNEL:
1210+ case DCA_STEREO_TOTAL:
1211+ case DCA_STEREO_SUMDIFF:
1212+ case DCA_4F2R:
1213+ av_log(NULL, 0, "Not implemented!\n");
1214+ break;
1215+ case DCA_STEREO:
1216+ break;
1217+ case DCA_3F:
1218+ c = channel_mapping[0] * 256;
1219+ l = channel_mapping[1] * 256;
1220+ r = channel_mapping[2] * 256;
1221+ DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef), );
1222+ break;
1223+ case DCA_2F1R:
1224+ s = channel_mapping[2] * 256;
1225+ DOWNMIX_TO_STEREO(MIX_REAR1(samples, i + s, 2, coef), );
1226+ break;
1227+ case DCA_3F1R:
1228+ c = channel_mapping[0] * 256;
1229+ l = channel_mapping[1] * 256;
1230+ r = channel_mapping[2] * 256;
1231+ s = channel_mapping[3] * 256;
1232+ DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1233+ MIX_REAR1(samples, i + s, 3, coef));
1234+ break;
1235+ case DCA_2F2R:
1236+ sl = channel_mapping[2] * 256;
1237+ sr = channel_mapping[3] * 256;
1238+ DOWNMIX_TO_STEREO(MIX_REAR2(samples, i + sl, i + sr, 2, coef), );
1239+ break;
1240+ case DCA_3F2R:
1241+ c = channel_mapping[0] * 256;
1242+ l = channel_mapping[1] * 256;
1243+ r = channel_mapping[2] * 256;
1244+ sl = channel_mapping[3] * 256;
1245+ sr = channel_mapping[4] * 256;
1246+ DOWNMIX_TO_STEREO(MIX_FRONT3(samples, coef),
1247+ MIX_REAR2(samples, i + sl, i + sr, 3, coef));
1248+ break;
1249+ }
1250+}
1251+
1252+
1253+#ifndef decode_blockcodes
1254+/* Very compact version of the block code decoder that does not use table
1255+ * look-up but is slightly slower */
1256+static int decode_blockcode(int code, int levels, int *values)
1257+{
1258+ int i;
1259+ int offset = (levels - 1) >> 1;
1260+
1261+ for (i = 0; i < 4; i++) {
1262+ int div = FASTDIV(code, levels);
1263+ values[i] = code - offset - div * levels;
1264+ code = div;
1265+ }
1266+
1267+ return code;
1268+}
1269+
1270+static int decode_blockcodes(int code1, int code2, int levels, int *values)
1271+{
1272+ return decode_blockcode(code1, levels, values) |
1273+ decode_blockcode(code2, levels, values + 4);
1274+}
1275+#endif
1276+
1277+static const uint8_t abits_sizes[7] = { 7, 10, 12, 13, 15, 17, 19 };
1278+static const uint8_t abits_levels[7] = { 3, 5, 7, 9, 13, 17, 25 };
1279+
1280+#ifndef int8x8_fmul_int32
1281+static inline void int8x8_fmul_int32(float *dst, const int8_t *src, int scale)
1282+{
1283+ float fscale = scale / 16.0;
1284+ int i;
1285+ for (i = 0; i < 8; i++)
1286+ dst[i] = src[i] * fscale;
1287+}
1288+#endif
1289+
1290+static int dca_subsubframe(DCAContext *s, int base_channel, int block_index)
1291+{
1292+ int k, l;
1293+ int subsubframe = s->current_subsubframe;
1294+
1295+ const float *quant_step_table;
1296+
1297+ /* FIXME */
1298+ float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1299+ LOCAL_ALIGNED_16(int, block, [8]);
1300+
1301+ /*
1302+ * Audio data
1303+ */
1304+
1305+ /* Select quantization step size table */
1306+ if (s->bit_rate_index == 0x1f)
1307+ quant_step_table = lossless_quant_d;
1308+ else
1309+ quant_step_table = lossy_quant_d;
1310+
1311+ for (k = base_channel; k < s->prim_channels; k++) {
1312+ if (get_bits_left(&s->gb) < 0)
1313+ return AVERROR_INVALIDDATA;
1314+
1315+ for (l = 0; l < s->vq_start_subband[k]; l++) {
1316+ int m;
1317+
1318+ /* Select the mid-tread linear quantizer */
1319+ int abits = s->bitalloc[k][l];
1320+
1321+ float quant_step_size = quant_step_table[abits];
1322+
1323+ /*
1324+ * Determine quantization index code book and its type
1325+ */
1326+
1327+ /* Select quantization index code book */
1328+ int sel = s->quant_index_huffman[k][abits];
1329+
1330+ /*
1331+ * Extract bits from the bit stream
1332+ */
1333+ if (!abits) {
1334+ memset(subband_samples[k][l], 0, 8 * sizeof(subband_samples[0][0][0]));
1335+ } else {
1336+ /* Deal with transients */
1337+ int sfi = s->transition_mode[k][l] && subsubframe >= s->transition_mode[k][l];
1338+ float rscale = quant_step_size * s->scale_factor[k][l][sfi] *
1339+ s->scalefactor_adj[k][sel];
1340+
1341+ if (abits >= 11 || !dca_smpl_bitalloc[abits].vlc[sel].table) {
1342+ if (abits <= 7) {
1343+ /* Block code */
1344+ int block_code1, block_code2, size, levels, err;
1345+
1346+ size = abits_sizes[abits - 1];
1347+ levels = abits_levels[abits - 1];
1348+
1349+ block_code1 = get_bits(&s->gb, size);
1350+ block_code2 = get_bits(&s->gb, size);
1351+ err = decode_blockcodes(block_code1, block_code2,
1352+ levels, block);
1353+ if (err) {
1354+ av_log(s->avctx, AV_LOG_ERROR,
1355+ "ERROR: block code look-up failed\n");
1356+ return AVERROR_INVALIDDATA;
1357+ }
1358+ } else {
1359+ /* no coding */
1360+ for (m = 0; m < 8; m++)
1361+ block[m] = get_sbits(&s->gb, abits - 3);
1362+ }
1363+ } else {
1364+ /* Huffman coded */
1365+ for (m = 0; m < 8; m++)
1366+ block[m] = get_bitalloc(&s->gb,
1367+ &dca_smpl_bitalloc[abits], sel);
1368+ }
1369+
1370+ s->fmt_conv.int32_to_float_fmul_scalar(subband_samples[k][l],
1371+ block, rscale, 8);
1372+ }
1373+
1374+ /*
1375+ * Inverse ADPCM if in prediction mode
1376+ */
1377+ if (s->prediction_mode[k][l]) {
1378+ int n;
1379+ for (m = 0; m < 8; m++) {
1380+ for (n = 1; n <= 4; n++)
1381+ if (m >= n)
1382+ subband_samples[k][l][m] +=
1383+ (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1384+ subband_samples[k][l][m - n] / 8192);
1385+ else if (s->predictor_history)
1386+ subband_samples[k][l][m] +=
1387+ (adpcm_vb[s->prediction_vq[k][l]][n - 1] *
1388+ s->subband_samples_hist[k][l][m - n + 4] / 8192);
1389+ }
1390+ }
1391+ }
1392+
1393+ /*
1394+ * Decode VQ encoded high frequencies
1395+ */
1396+ for (l = s->vq_start_subband[k]; l < s->subband_activity[k]; l++) {
1397+ /* 1 vector -> 32 samples but we only need the 8 samples
1398+ * for this subsubframe. */
1399+ int hfvq = s->high_freq_vq[k][l];
1400+
1401+ if (!s->debug_flag & 0x01) {
1402+ av_log(s->avctx, AV_LOG_DEBUG,
1403+ "Stream with high frequencies VQ coding\n");
1404+ s->debug_flag |= 0x01;
1405+ }
1406+
1407+ int8x8_fmul_int32(subband_samples[k][l],
1408+ &high_freq_vq[hfvq][subsubframe * 8],
1409+ s->scale_factor[k][l][0]);
1410+ }
1411+ }
1412+
1413+ /* Check for DSYNC after subsubframe */
1414+ if (s->aspf || subsubframe == s->subsubframes[s->current_subframe] - 1) {
1415+ if (0xFFFF == get_bits(&s->gb, 16)) { /* 0xFFFF */
1416+#ifdef TRACE
1417+ av_log(s->avctx, AV_LOG_DEBUG, "Got subframe DSYNC\n");
1418+#endif
1419+ } else {
1420+ av_log(s->avctx, AV_LOG_ERROR, "Didn't get subframe DSYNC\n");
1421+ }
1422+ }
1423+
1424+ /* Backup predictor history for adpcm */
1425+ for (k = base_channel; k < s->prim_channels; k++)
1426+ for (l = 0; l < s->vq_start_subband[k]; l++)
1427+ memcpy(s->subband_samples_hist[k][l],
1428+ &subband_samples[k][l][4],
1429+ 4 * sizeof(subband_samples[0][0][0]));
1430+
1431+ return 0;
1432+}
1433+
1434+static int dca_filter_channels(DCAContext *s, int block_index)
1435+{
1436+ float (*subband_samples)[DCA_SUBBANDS][8] = s->subband_samples[block_index];
1437+ int k;
1438+
1439+ /* 32 subbands QMF */
1440+ for (k = 0; k < s->prim_channels; k++) {
1441+/* static float pcm_to_double[8] = { 32768.0, 32768.0, 524288.0, 524288.0,
1442+ 0, 8388608.0, 8388608.0 };*/
1443+ qmf_32_subbands(s, k, subband_samples[k],
1444+ &s->samples[256 * s->channel_order_tab[k]],
1445+ M_SQRT1_2 * s->scale_bias /* pcm_to_double[s->source_pcm_res] */);
1446+ }
1447+
1448+ /* Down mixing */
1449+ if (s->avctx->request_channels == 2 && s->prim_channels > 2) {
1450+ dca_downmix(s->samples, s->amode, s->downmix_coef, s->channel_order_tab);
1451+ }
1452+
1453+ /* Generate LFE samples for this subsubframe FIXME!!! */
1454+ if (s->output & DCA_LFE) {
1455+ lfe_interpolation_fir(s, s->lfe, 2 * s->lfe,
1456+ s->lfe_data + 2 * s->lfe * (block_index + 4),
1457+ &s->samples[256 * s->lfe_index],
1458+ (1.0 / 256.0) * s->scale_bias);
1459+ /* Outputs 20bits pcm samples */
1460+ }
1461+
1462+ return 0;
1463+}
1464+
1465+
1466+static int dca_subframe_footer(DCAContext *s, int base_channel)
1467+{
1468+ int aux_data_count = 0, i;
1469+
1470+ /*
1471+ * Unpack optional information
1472+ */
1473+
1474+ /* presumably optional information only appears in the core? */
1475+ if (!base_channel) {
1476+ if (s->timestamp)
1477+ skip_bits_long(&s->gb, 32);
1478+
1479+ if (s->aux_data)
1480+ aux_data_count = get_bits(&s->gb, 6);
1481+
1482+ for (i = 0; i < aux_data_count; i++)
1483+ get_bits(&s->gb, 8);
1484+
1485+ if (s->crc_present && (s->downmix || s->dynrange))
1486+ get_bits(&s->gb, 16);
1487+ }
1488+
1489+ return 0;
1490+}
1491+
1492+/**
1493+ * Decode a dca frame block
1494+ *
1495+ * @param s pointer to the DCAContext
1496+ */
1497+
1498+static int dca_decode_block(DCAContext *s, int base_channel, int block_index)
1499+{
1500+ int ret;
1501+
1502+ /* Sanity check */
1503+ if (s->current_subframe >= s->subframes) {
1504+ av_log(s->avctx, AV_LOG_DEBUG, "check failed: %i>%i",
1505+ s->current_subframe, s->subframes);
1506+ return AVERROR_INVALIDDATA;
1507+ }
1508+
1509+ if (!s->current_subsubframe) {
1510+#ifdef TRACE
1511+ av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_header\n");
1512+#endif
1513+ /* Read subframe header */
1514+ if ((ret = dca_subframe_header(s, base_channel, block_index)))
1515+ return ret;
1516+ }
1517+
1518+ /* Read subsubframe */
1519+#ifdef TRACE
1520+ av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subsubframe\n");
1521+#endif
1522+ if ((ret = dca_subsubframe(s, base_channel, block_index)))
1523+ return ret;
1524+
1525+ /* Update state */
1526+ s->current_subsubframe++;
1527+ if (s->current_subsubframe >= s->subsubframes[s->current_subframe]) {
1528+ s->current_subsubframe = 0;
1529+ s->current_subframe++;
1530+ }
1531+ if (s->current_subframe >= s->subframes) {
1532+#ifdef TRACE
1533+ av_log(s->avctx, AV_LOG_DEBUG, "DSYNC dca_subframe_footer\n");
1534+#endif
1535+ /* Read subframe footer */
1536+ if ((ret = dca_subframe_footer(s, base_channel)))
1537+ return ret;
1538+ }
1539+
1540+ return 0;
1541+}
1542+
1543+/**
1544+ * Return the number of channels in an ExSS speaker mask (HD)
1545+ */
1546+static int dca_exss_mask2count(int mask)
1547+{
1548+ /* count bits that mean speaker pairs twice */
1549+ return av_popcount(mask) +
1550+ av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
1551+ DCA_EXSS_FRONT_LEFT_RIGHT |
1552+ DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
1553+ DCA_EXSS_WIDE_LEFT_RIGHT |
1554+ DCA_EXSS_SIDE_LEFT_RIGHT |
1555+ DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
1556+ DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
1557+ DCA_EXSS_REAR_LEFT_RIGHT |
1558+ DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
1559+}
1560+
1561+/**
1562+ * Skip mixing coefficients of a single mix out configuration (HD)
1563+ */
1564+static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
1565+{
1566+ int i;
1567+
1568+ for (i = 0; i < channels; i++) {
1569+ int mix_map_mask = get_bits(gb, out_ch);
1570+ int num_coeffs = av_popcount(mix_map_mask);
1571+ skip_bits_long(gb, num_coeffs * 6);
1572+ }
1573+}
1574+
1575+/**
1576+ * Parse extension substream asset header (HD)
1577+ */
1578+static int dca_exss_parse_asset_header(DCAContext *s)
1579+{
1580+ int header_pos = get_bits_count(&s->gb);
1581+ int header_size;
1582+ int channels = 0;
1583+ int embedded_stereo = 0;
1584+ int embedded_6ch = 0;
1585+ int drc_code_present;
1586+ int av_uninit(extensions_mask);
1587+ int i, j;
1588+
1589+ if (get_bits_left(&s->gb) < 16)
1590+ return -1;
1591+
1592+ /* We will parse just enough to get to the extensions bitmask with which
1593+ * we can set the profile value. */
1594+
1595+ header_size = get_bits(&s->gb, 9) + 1;
1596+ skip_bits(&s->gb, 3); // asset index
1597+
1598+ if (s->static_fields) {
1599+ if (get_bits1(&s->gb))
1600+ skip_bits(&s->gb, 4); // asset type descriptor
1601+ if (get_bits1(&s->gb))
1602+ skip_bits_long(&s->gb, 24); // language descriptor
1603+
1604+ if (get_bits1(&s->gb)) {
1605+ /* How can one fit 1024 bytes of text here if the maximum value
1606+ * for the asset header size field above was 512 bytes? */
1607+ int text_length = get_bits(&s->gb, 10) + 1;
1608+ if (get_bits_left(&s->gb) < text_length * 8)
1609+ return -1;
1610+ skip_bits_long(&s->gb, text_length * 8); // info text
1611+ }
1612+
1613+ skip_bits(&s->gb, 5); // bit resolution - 1
1614+ skip_bits(&s->gb, 4); // max sample rate code
1615+ channels = get_bits(&s->gb, 8) + 1;
1616+
1617+ if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
1618+ int spkr_remap_sets;
1619+ int spkr_mask_size = 16;
1620+ int num_spkrs[7];
1621+
1622+ if (channels > 2)
1623+ embedded_stereo = get_bits1(&s->gb);
1624+ if (channels > 6)
1625+ embedded_6ch = get_bits1(&s->gb);
1626+
1627+ if (get_bits1(&s->gb)) {
1628+ spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
1629+ skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
1630+ }
1631+
1632+ spkr_remap_sets = get_bits(&s->gb, 3);
1633+
1634+ for (i = 0; i < spkr_remap_sets; i++) {
1635+ /* std layout mask for each remap set */
1636+ num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
1637+ }
1638+
1639+ for (i = 0; i < spkr_remap_sets; i++) {
1640+ int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
1641+ if (get_bits_left(&s->gb) < 0)
1642+ return -1;
1643+
1644+ for (j = 0; j < num_spkrs[i]; j++) {
1645+ int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
1646+ int num_dec_ch = av_popcount(remap_dec_ch_mask);
1647+ skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
1648+ }
1649+ }
1650+
1651+ } else {
1652+ skip_bits(&s->gb, 3); // representation type
1653+ }
1654+ }
1655+
1656+ drc_code_present = get_bits1(&s->gb);
1657+ if (drc_code_present)
1658+ get_bits(&s->gb, 8); // drc code
1659+
1660+ if (get_bits1(&s->gb))
1661+ skip_bits(&s->gb, 5); // dialog normalization code
1662+
1663+ if (drc_code_present && embedded_stereo)
1664+ get_bits(&s->gb, 8); // drc stereo code
1665+
1666+ if (s->mix_metadata && get_bits1(&s->gb)) {
1667+ skip_bits(&s->gb, 1); // external mix
1668+ skip_bits(&s->gb, 6); // post mix gain code
1669+
1670+ if (get_bits(&s->gb, 2) != 3) // mixer drc code
1671+ skip_bits(&s->gb, 3); // drc limit
1672+ else
1673+ skip_bits(&s->gb, 8); // custom drc code
1674+
1675+ if (get_bits1(&s->gb)) // channel specific scaling
1676+ for (i = 0; i < s->num_mix_configs; i++)
1677+ skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
1678+ else
1679+ skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
1680+
1681+ for (i = 0; i < s->num_mix_configs; i++) {
1682+ if (get_bits_left(&s->gb) < 0)
1683+ return -1;
1684+ dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
1685+ if (embedded_6ch)
1686+ dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
1687+ if (embedded_stereo)
1688+ dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
1689+ }
1690+ }
1691+
1692+ switch (get_bits(&s->gb, 2)) {
1693+ case 0: extensions_mask = get_bits(&s->gb, 12); break;
1694+ case 1: extensions_mask = DCA_EXT_EXSS_XLL; break;
1695+ case 2: extensions_mask = DCA_EXT_EXSS_LBR; break;
1696+ case 3: extensions_mask = 0; /* aux coding */ break;
1697+ }
1698+
1699+ /* not parsed further, we were only interested in the extensions mask */
1700+
1701+ if (get_bits_left(&s->gb) < 0)
1702+ return -1;
1703+
1704+ if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
1705+ av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
1706+ return -1;
1707+ }
1708+ skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
1709+
1710+ if (extensions_mask & DCA_EXT_EXSS_XLL)
1711+ s->profile = FF_PROFILE_DTS_HD_MA;
1712+ else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
1713+ DCA_EXT_EXSS_XXCH))
1714+ s->profile = FF_PROFILE_DTS_HD_HRA;
1715+
1716+ if (!(extensions_mask & DCA_EXT_CORE))
1717+ av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
1718+ if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
1719+ av_log(s->avctx, AV_LOG_WARNING,
1720+ "DTS extensions detection mismatch (%d, %d)\n",
1721+ extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
1722+
1723+ return 0;
1724+}
1725+
1726+static int dca_xbr_parse_frame(DCAContext *s)
1727+{
1728+ int scale_table_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS][2];
1729+ int active_bands[DCA_CHSETS_MAX][DCA_CHSET_CHANS_MAX];
1730+ int abits_high[DCA_CHSET_CHANS_MAX][DCA_SUBBANDS];
1731+ int anctemp[DCA_CHSET_CHANS_MAX];
1732+ int chset_fsize[DCA_CHSETS_MAX];
1733+ int n_xbr_ch[DCA_CHSETS_MAX];
1734+ int hdr_size, num_chsets, xbr_tmode, hdr_pos;
1735+ int i, j, k, l, chset, chan_base;
1736+
1737+ av_log(s->avctx, AV_LOG_DEBUG, "DTS-XBR: decoding XBR extension\n");
1738+
1739+ /* get bit position of sync header */
1740+ hdr_pos = get_bits_count(&s->gb) - 32;
1741+
1742+ hdr_size = get_bits(&s->gb, 6) + 1;
1743+ num_chsets = get_bits(&s->gb, 2) + 1;
1744+
1745+ for(i = 0; i < num_chsets; i++)
1746+ chset_fsize[i] = get_bits(&s->gb, 14) + 1;
1747+
1748+ xbr_tmode = get_bits1(&s->gb);
1749+
1750+ for(i = 0; i < num_chsets; i++) {
1751+ n_xbr_ch[i] = get_bits(&s->gb, 3) + 1;
1752+ k = get_bits(&s->gb, 2) + 5;
1753+ for(j = 0; j < n_xbr_ch[i]; j++)
1754+ active_bands[i][j] = get_bits(&s->gb, k) + 1;
1755+ }
1756+
1757+ /* skip to the end of the header */
1758+ i = get_bits_count(&s->gb);
1759+ if(hdr_pos + hdr_size * 8 > i)
1760+ skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1761+
1762+ /* loop over the channel data sets */
1763+ /* only decode as many channels as we've decoded base data for */
1764+ for(chset = 0, chan_base = 0;
1765+ chset < num_chsets && chan_base + n_xbr_ch[chset] <= s->prim_channels;
1766+ chan_base += n_xbr_ch[chset++]) {
1767+ int start_posn = get_bits_count(&s->gb);
1768+ int subsubframe = 0;
1769+ int subframe = 0;
1770+
1771+ /* loop over subframes */
1772+ for (k = 0; k < (s->sample_blocks / 8); k++) {
1773+ /* parse header if we're on first subsubframe of a block */
1774+ if(subsubframe == 0) {
1775+ /* Parse subframe header */
1776+ for(i = 0; i < n_xbr_ch[chset]; i++) {
1777+ anctemp[i] = get_bits(&s->gb, 2) + 2;
1778+ }
1779+
1780+ for(i = 0; i < n_xbr_ch[chset]; i++) {
1781+ get_array(&s->gb, abits_high[i], active_bands[chset][i], anctemp[i]);
1782+ }
1783+
1784+ for(i = 0; i < n_xbr_ch[chset]; i++) {
1785+ anctemp[i] = get_bits(&s->gb, 3);
1786+ if(anctemp[i] < 1) {
1787+ av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: SYNC ERROR\n");
1788+ return AVERROR_INVALIDDATA;
1789+ }
1790+ }
1791+
1792+ /* generate scale factors */
1793+ for(i = 0; i < n_xbr_ch[chset]; i++) {
1794+ const uint32_t *scale_table;
1795+ int nbits;
1796+
1797+ if (s->scalefactor_huffman[chan_base+i] == 6) {
1798+ scale_table = scale_factor_quant7;
1799+ } else {
1800+ scale_table = scale_factor_quant6;
1801+ }
1802+
1803+ nbits = anctemp[i];
1804+
1805+ for(j = 0; j < active_bands[chset][i]; j++) {
1806+ if(abits_high[i][j] > 0) {
1807+ scale_table_high[i][j][0] =
1808+ scale_table[get_bits(&s->gb, nbits)];
1809+
1810+ if(xbr_tmode && s->transition_mode[i][j]) {
1811+ scale_table_high[i][j][1] =
1812+ scale_table[get_bits(&s->gb, nbits)];
1813+ }
1814+ }
1815+ }
1816+ }
1817+ }
1818+
1819+ /* decode audio array for this block */
1820+ for(i = 0; i < n_xbr_ch[chset]; i++) {
1821+ for(j = 0; j < active_bands[chset][i]; j++) {
1822+ const int xbr_abits = abits_high[i][j];
1823+ const float quant_step_size = lossless_quant_d[xbr_abits];
1824+ const int sfi = xbr_tmode && s->transition_mode[i][j] && subsubframe >= s->transition_mode[i][j];
1825+ const float rscale = quant_step_size * scale_table_high[i][j][sfi];
1826+ float *subband_samples = s->subband_samples[k][chan_base+i][j];
1827+ int block[8];
1828+
1829+ if(xbr_abits <= 0)
1830+ continue;
1831+
1832+ if(xbr_abits > 7) {
1833+ get_array(&s->gb, block, 8, xbr_abits - 3);
1834+ } else {
1835+ int block_code1, block_code2, size, levels, err;
1836+
1837+ size = abits_sizes[xbr_abits - 1];
1838+ levels = abits_levels[xbr_abits - 1];
1839+
1840+ block_code1 = get_bits(&s->gb, size);
1841+ block_code2 = get_bits(&s->gb, size);
1842+ err = decode_blockcodes(block_code1, block_code2,
1843+ levels, block);
1844+ if (err) {
1845+ av_log(s->avctx, AV_LOG_ERROR,
1846+ "ERROR: DTS-XBR: block code look-up failed\n");
1847+ return AVERROR_INVALIDDATA;
1848+ }
1849+ }
1850+
1851+ /* scale & sum into subband */
1852+ for(l = 0; l < 8; l++)
1853+ subband_samples[l] += (float)block[l] * rscale;
1854+ }
1855+ }
1856+
1857+ /* check DSYNC marker */
1858+ if(s->aspf || subsubframe == s->subsubframes[subframe] - 1) {
1859+ if(get_bits(&s->gb, 16) != 0xffff) {
1860+ av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: Didn't get subframe DSYNC\n");
1861+ return AVERROR_INVALIDDATA;
1862+ }
1863+ }
1864+
1865+ /* advance sub-sub-frame index */
1866+ if(++subsubframe >= s->subsubframes[subframe]) {
1867+ subsubframe = 0;
1868+ subframe++;
1869+ }
1870+ }
1871+
1872+ /* skip to next channel set */
1873+ i = get_bits_count(&s->gb);
1874+ if(start_posn + chset_fsize[chset] * 8 != i) {
1875+ j = start_posn + chset_fsize[chset] * 8 - i;
1876+ if(j < 0 || j >= 8)
1877+ av_log(s->avctx, AV_LOG_ERROR, "DTS-XBR: end of channel set,"
1878+ " skipping further than expected (%d bits)\n", j);
1879+ skip_bits_long(&s->gb, j);
1880+ }
1881+ }
1882+
1883+ return 0;
1884+}
1885+
1886+/* parse initial header for XXCH and dump details */
1887+static int dca_xxch_decode_frame(DCAContext *s)
1888+{
1889+ int hdr_size, chhdr_crc, spkmsk_bits, num_chsets, core_spk, hdr_pos;
1890+ int i, chset, base_channel, chstart, fsize[8];
1891+
1892+ /* assume header word has already been parsed */
1893+ hdr_pos = get_bits_count(&s->gb) - 32;
1894+ hdr_size = get_bits(&s->gb, 6) + 1;
1895+ chhdr_crc = get_bits1(&s->gb);
1896+ spkmsk_bits = get_bits(&s->gb, 5) + 1;
1897+ num_chsets = get_bits(&s->gb, 2) + 1;
1898+
1899+ for (i = 0; i < num_chsets; i++)
1900+ fsize[i] = get_bits(&s->gb, 14) + 1;
1901+
1902+ core_spk = get_bits(&s->gb, spkmsk_bits);
1903+ s->xxch_core_spkmask = core_spk;
1904+ s->xxch_nbits_spk_mask = spkmsk_bits;
1905+ s->xxch_downmix = 0;
1906+ s->xxch_dmix_embedded = 0;
1907+
1908+ /* skip to the end of the header */
1909+ i = get_bits_count(&s->gb);
1910+ if (hdr_pos + hdr_size * 8 > i)
1911+ skip_bits_long(&s->gb, hdr_pos + hdr_size * 8 - i);
1912+
1913+ for (chset = 0; chset < num_chsets; chset++) {
1914+ chstart = get_bits_count(&s->gb);
1915+ base_channel = s->prim_channels;
1916+ s->xxch_chset = chset;
1917+
1918+ /* XXCH and Core headers differ, see 6.4.2 "XXCH Channel Set Header" vs.
1919+ 5.3.2 "Primary Audio Coding Header", DTS Spec 1.3.1 */
1920+ dca_parse_audio_coding_header(s, base_channel, 1);
1921+
1922+ /* decode channel data */
1923+ for (i = 0; i < (s->sample_blocks / 8); i++) {
1924+ if (dca_decode_block(s, base_channel, i)) {
1925+ av_log(s->avctx, AV_LOG_ERROR,
1926+ "Error decoding DTS-XXCH extension\n");
1927+ continue;
1928+ }
1929+ }
1930+
1931+ /* skip to end of this section */
1932+ i = get_bits_count(&s->gb);
1933+ if (chstart + fsize[chset] * 8 > i)
1934+ skip_bits_long(&s->gb, chstart + fsize[chset] * 8 - i);
1935+ }
1936+ s->xxch_chset = num_chsets;
1937+
1938+ return 0;
1939+}
1940+
1941+/**
1942+ * Parse extension substream header (HD)
1943+ */
1944+static void dca_exss_parse_header(DCAContext *s)
1945+{
1946+ int asset_size[8];
1947+ int ss_index;
1948+ int blownup;
1949+ int num_audiop = 1;
1950+ int num_assets = 1;
1951+ int active_ss_mask[8];
1952+ int i, j;
1953+ int start_posn;
1954+ int hdrsize;
1955+ uint32_t mkr;
1956+
1957+ if (get_bits_left(&s->gb) < 52)
1958+ return;
1959+
1960+ start_posn = get_bits_count(&s->gb) - 32;
1961+
1962+ skip_bits(&s->gb, 8); // user data
1963+ ss_index = get_bits(&s->gb, 2);
1964+
1965+ blownup = get_bits1(&s->gb);
1966+ hdrsize = get_bits(&s->gb, 8 + 4 * blownup) + 1; // header_size
1967+ skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
1968+
1969+ s->static_fields = get_bits1(&s->gb);
1970+ if (s->static_fields) {
1971+ skip_bits(&s->gb, 2); // reference clock code
1972+ skip_bits(&s->gb, 3); // frame duration code
1973+
1974+ if (get_bits1(&s->gb))
1975+ skip_bits_long(&s->gb, 36); // timestamp
1976+
1977+ /* a single stream can contain multiple audio assets that can be
1978+ * combined to form multiple audio presentations */
1979+
1980+ num_audiop = get_bits(&s->gb, 3) + 1;
1981+ if (num_audiop > 1) {
1982+ av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio presentations.");
1983+ /* ignore such streams for now */
1984+ return;
1985+ }
1986+
1987+ num_assets = get_bits(&s->gb, 3) + 1;
1988+ if (num_assets > 1) {
1989+ av_log_ask_for_sample(s->avctx, "Multiple DTS-HD audio assets.");
1990+ /* ignore such streams for now */
1991+ return;
1992+ }
1993+
1994+ for (i = 0; i < num_audiop; i++)
1995+ active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
1996+
1997+ for (i = 0; i < num_audiop; i++)
1998+ for (j = 0; j <= ss_index; j++)
1999+ if (active_ss_mask[i] & (1 << j))
2000+ skip_bits(&s->gb, 8); // active asset mask
2001+
2002+ s->mix_metadata = get_bits1(&s->gb);
2003+ if (s->mix_metadata) {
2004+ int mix_out_mask_size;
2005+
2006+ skip_bits(&s->gb, 2); // adjustment level
2007+ mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
2008+ s->num_mix_configs = get_bits(&s->gb, 2) + 1;
2009+
2010+ for (i = 0; i < s->num_mix_configs; i++) {
2011+ int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
2012+ s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
2013+ }
2014+ }
2015+ }
2016+
2017+ for (i = 0; i < num_assets; i++)
2018+ asset_size[i] = get_bits_long(&s->gb, 16 + 4 * blownup);
2019+
2020+ for (i = 0; i < num_assets; i++) {
2021+ if (dca_exss_parse_asset_header(s))
2022+ return;
2023+ }
2024+
2025+ /* not parsed further, we were only interested in the extensions mask
2026+ * from the asset header */
2027+
2028+ if (num_assets > 0) {
2029+ j = get_bits_count(&s->gb);
2030+ if (start_posn + hdrsize * 8 > j)
2031+ skip_bits_long(&s->gb, start_posn + hdrsize * 8 - j);
2032+
2033+ for (i = 0; i < num_assets; i++) {
2034+ start_posn = get_bits_count(&s->gb);
2035+ mkr = get_bits_long(&s->gb, 32);
2036+
2037+ /* parse extensions that we know about */
2038+ if (mkr == 0x655e315e) {
2039+ dca_xbr_parse_frame(s);
2040+ } else if (mkr == 0x47004a03) {
2041+ dca_xxch_decode_frame(s);
2042+ s->core_ext_mask |= DCA_EXT_XXCH; /* xxx use for chan reordering */
2043+ } else {
2044+ av_log(s->avctx, AV_LOG_DEBUG,
2045+ "DTS-ExSS: unknown marker = 0x%08x\n", mkr);
2046+ }
2047+
2048+ /* skip to end of block */
2049+ j = get_bits_count(&s->gb);
2050+ if (start_posn + asset_size[i] * 8 > j)
2051+ skip_bits_long(&s->gb, start_posn + asset_size[i] * 8 - j);
2052+ }
2053+ }
2054+}
2055+
2056+/**
2057+ * Main frame decoding function
2058+ * FIXME add arguments
2059+ */
2060+static int dca_decode_frame(AVCodecContext *avctx, void *data,
2061+ int *got_frame_ptr, AVPacket *avpkt)
2062+{
2063+ const uint8_t *buf = avpkt->data;
2064+ int buf_size = avpkt->size;
2065+ int channel_mask;
2066+ int channel_layout;
2067+ int lfe_samples;
2068+ int num_core_channels = 0;
2069+ int i, ret;
2070+ float *samples_flt;
2071+ float *src_chan;
2072+ float *dst_chan;
2073+ int16_t *samples_s16;
2074+ DCAContext *s = avctx->priv_data;
2075+ int core_ss_end;
2076+ int channels;
2077+ float scale;
2078+ int achan;
2079+ int chset;
2080+ int mask;
2081+ int lavc;
2082+ int posn;
2083+ int j, k;
2084+ int ch;
2085+ int endch;
2086+
2087+ s->xch_present = 0;
2088+
2089+ s->dca_buffer_size = ff_dca_convert_bitstream(buf, buf_size, s->dca_buffer,
2090+ DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE);
2091+ if (s->dca_buffer_size == AVERROR_INVALIDDATA) {
2092+ av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n");
2093+ return AVERROR_INVALIDDATA;
2094+ }
2095+
2096+ init_get_bits(&s->gb, s->dca_buffer, s->dca_buffer_size * 8);
2097+ if ((ret = dca_parse_frame_header(s)) < 0) {
2098+ //seems like the frame is corrupt, try with the next one
2099+ return ret;
2100+ }
2101+ //set AVCodec values with parsed data
2102+ avctx->sample_rate = s->sample_rate;
2103+ avctx->bit_rate = s->bit_rate;
2104+
2105+ s->profile = FF_PROFILE_DTS;
2106+
2107+ for (i = 0; i < (s->sample_blocks / 8); i++) {
2108+ if ((ret = dca_decode_block(s, 0, i))) {
2109+ av_log(avctx, AV_LOG_ERROR, "error decoding block\n");
2110+ return ret;
2111+ }
2112+ }
2113+
2114+ /* record number of core channels incase less than max channels are requested */
2115+ num_core_channels = s->prim_channels;
2116+
2117+ if (s->ext_coding)
2118+ s->core_ext_mask = dca_ext_audio_descr_mask[s->ext_descr];
2119+ else
2120+ s->core_ext_mask = 0;
2121+
2122+ core_ss_end = FFMIN(s->frame_size, s->dca_buffer_size) * 8;
2123+
2124+ /* only scan for extensions if ext_descr was unknown or indicated a
2125+ * supported XCh extension */
2126+ if (s->core_ext_mask < 0 || s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH)) {
2127+
2128+ /* if ext_descr was unknown, clear s->core_ext_mask so that the
2129+ * extensions scan can fill it up */
2130+ s->core_ext_mask = FFMAX(s->core_ext_mask, 0);
2131+
2132+ /* extensions start at 32-bit boundaries into bitstream */
2133+ skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2134+
2135+ while (core_ss_end - get_bits_count(&s->gb) >= 32) {
2136+ uint32_t bits = get_bits_long(&s->gb, 32);
2137+
2138+ switch (bits) {
2139+ case 0x5a5a5a5a: {
2140+ int ext_amode, xch_fsize;
2141+
2142+ s->xch_base_channel = s->prim_channels;
2143+
2144+ /* validate sync word using XCHFSIZE field */
2145+ xch_fsize = show_bits(&s->gb, 10);
2146+ if ((s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize) &&
2147+ (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + xch_fsize + 1))
2148+ continue;
2149+
2150+ /* skip length-to-end-of-frame field for the moment */
2151+ skip_bits(&s->gb, 10);
2152+
2153+ s->core_ext_mask |= DCA_EXT_XCH;
2154+
2155+ /* extension amode(number of channels in extension) should be 1 */
2156+ /* AFAIK XCh is not used for more channels */
2157+ if ((ext_amode = get_bits(&s->gb, 4)) != 1) {
2158+ av_log(avctx, AV_LOG_ERROR, "XCh extension amode %d not"
2159+ " supported!\n", ext_amode);
2160+ continue;
2161+ }
2162+
2163+ /* much like core primary audio coding header */
2164+ dca_parse_audio_coding_header(s, s->xch_base_channel, 0);
2165+
2166+ for (i = 0; i < (s->sample_blocks / 8); i++)
2167+ if ((ret = dca_decode_block(s, s->xch_base_channel, i))) {
2168+ av_log(avctx, AV_LOG_ERROR, "error decoding XCh extension\n");
2169+ continue;
2170+ }
2171+
2172+ s->xch_present = 1;
2173+ break;
2174+ }
2175+ case 0x47004a03:
2176+ /* XXCh: extended channels */
2177+ /* usually found either in core or HD part in DTS-HD HRA streams,
2178+ * but not in DTS-ES which contains XCh extensions instead */
2179+ s->core_ext_mask |= DCA_EXT_XXCH;
2180+ dca_xxch_decode_frame(s);
2181+ break;
2182+
2183+ case 0x1d95f262: {
2184+ int fsize96 = show_bits(&s->gb, 12) + 1;
2185+ if (s->frame_size != (get_bits_count(&s->gb) >> 3) - 4 + fsize96)
2186+ continue;
2187+
2188+ av_log(avctx, AV_LOG_DEBUG, "X96 extension found at %d bits\n",
2189+ get_bits_count(&s->gb));
2190+ skip_bits(&s->gb, 12);
2191+ av_log(avctx, AV_LOG_DEBUG, "FSIZE96 = %d bytes\n", fsize96);
2192+ av_log(avctx, AV_LOG_DEBUG, "REVNO = %d\n", get_bits(&s->gb, 4));
2193+
2194+ s->core_ext_mask |= DCA_EXT_X96;
2195+ break;
2196+ }
2197+ }
2198+
2199+ skip_bits_long(&s->gb, (-get_bits_count(&s->gb)) & 31);
2200+ }
2201+ } else {
2202+ /* no supported extensions, skip the rest of the core substream */
2203+ skip_bits_long(&s->gb, core_ss_end - get_bits_count(&s->gb));
2204+ }
2205+
2206+ if (s->core_ext_mask & DCA_EXT_X96)
2207+ s->profile = FF_PROFILE_DTS_96_24;
2208+ else if (s->core_ext_mask & (DCA_EXT_XCH | DCA_EXT_XXCH))
2209+ s->profile = FF_PROFILE_DTS_ES;
2210+
2211+ /* check for ExSS (HD part) */
2212+ if (s->dca_buffer_size - s->frame_size > 32 &&
2213+ get_bits_long(&s->gb, 32) == DCA_HD_MARKER)
2214+ dca_exss_parse_header(s);
2215+
2216+ avctx->profile = s->profile;
2217+
2218+ channels = s->prim_channels + !!s->lfe;
2219+
2220+ /* If we have XXCH then the channel layout is managed differently */
2221+ /* note that XLL will also have another way to do things */
2222+ if (!(s->core_ext_mask & DCA_EXT_XXCH)
2223+ || (s->core_ext_mask & DCA_EXT_XXCH && avctx->request_channels > 0
2224+ && avctx->request_channels
2225+ < num_core_channels + !!s->lfe + s->xxch_chset_nch[0]))
2226+ { /* xxx should also do MA extensions */
2227+ if (s->amode < 16) {
2228+ avctx->channel_layout = dca_core_channel_layout[s->amode];
2229+
2230+ if (s->xch_present && (!avctx->request_channels ||
2231+ avctx->request_channels
2232+ > num_core_channels + !!s->lfe)) {
2233+ avctx->channel_layout |= AV_CH_BACK_CENTER;
2234+ if (s->lfe) {
2235+ avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
2236+ s->channel_order_tab = dca_channel_reorder_lfe_xch[s->amode];
2237+ } else {
2238+ s->channel_order_tab = dca_channel_reorder_nolfe_xch[s->amode];
2239+ }
2240+ } else {
2241+ channels = num_core_channels + !!s->lfe;
2242+ s->xch_present = 0; /* disable further xch processing */
2243+ if (s->lfe) {
2244+ avctx->channel_layout |= AV_CH_LOW_FREQUENCY;
2245+ s->channel_order_tab = dca_channel_reorder_lfe[s->amode];
2246+ } else
2247+ s->channel_order_tab = dca_channel_reorder_nolfe[s->amode];
2248+ }
2249+
2250+ if (channels > !!s->lfe &&
2251+ s->channel_order_tab[channels - 1 - !!s->lfe] < 0)
2252+ return AVERROR_INVALIDDATA;
2253+
2254+ if (avctx->request_channels == 2 && s->prim_channels > 2) {
2255+ channels = 2;
2256+ s->output = DCA_STEREO;
2257+ avctx->channel_layout = AV_CH_LAYOUT_STEREO;
2258+ }
2259+ else if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
2260+ static const int8_t dca_channel_order_native[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
2261+ s->channel_order_tab = dca_channel_order_native;
2262+ }
2263+ s->lfe_index = dca_lfe_index[s->amode];
2264+ } else {
2265+ av_log(avctx, AV_LOG_ERROR,
2266+ "Non standard configuration %d !\n", s->amode);
2267+ return AVERROR_INVALIDDATA;
2268+ }
2269+
2270+ s->xxch_downmix = 0;
2271+ } else {
2272+ /* we only get here if an XXCH channel set can be added to the mix */
2273+ channel_mask = s->xxch_core_spkmask;
2274+
2275+ if (avctx->request_channels > 0
2276+ && avctx->request_channels < s->prim_channels) {
2277+ channels = num_core_channels + !!s->lfe;
2278+ for (i = 0; i < s->xxch_chset && channels + s->xxch_chset_nch[i]
2279+ <= avctx->request_channels; i++) {
2280+ channels += s->xxch_chset_nch[i];
2281+ channel_mask |= s->xxch_spk_masks[i];
2282+ }
2283+ } else {
2284+ channels = s->prim_channels + !!s->lfe;
2285+ for (i = 0; i < s->xxch_chset; i++) {
2286+ channel_mask |= s->xxch_spk_masks[i];
2287+ }
2288+ }
2289+
2290+ /* Given the DTS spec'ed channel mask, generate an avcodec version */
2291+ channel_layout = 0;
2292+ for (i = 0; i < s->xxch_nbits_spk_mask; ++i) {
2293+ if (channel_mask & (1 << i)) {
2294+ channel_layout |= map_xxch_to_native[i];
2295+ }
2296+ }
2297+
2298+ /* make sure that we have managed to get equivelant dts/avcodec channel
2299+ * masks in some sense -- unfortunately some channels could overlap */
2300+ if (av_popcount(channel_mask) != av_popcount(channel_layout)) {
2301+ av_log(avctx, AV_LOG_DEBUG,
2302+ "DTS-XXCH: Inconsistant avcodec/dts channel layouts\n");
2303+ return AVERROR_INVALIDDATA;
2304+ }
2305+
2306+ avctx->channel_layout = channel_layout;
2307+
2308+ if (!(avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE)) {
2309+ /* Estimate DTS --> avcodec ordering table */
2310+ for (chset = -1, j = 0; chset < s->xxch_chset; ++chset) {
2311+ mask = chset >= 0 ? s->xxch_spk_masks[chset]
2312+ : s->xxch_core_spkmask;
2313+ for (i = 0; i < s->xxch_nbits_spk_mask; i++) {
2314+ if (mask & ~(DCA_XXCH_LFE1 | DCA_XXCH_LFE2) & (1 << i)) {
2315+ lavc = map_xxch_to_native[i];
2316+ posn = av_popcount(channel_layout & (lavc - 1));
2317+ s->xxch_order_tab[j++] = posn;
2318+ }
2319+ }
2320+ }
2321+
2322+ s->lfe_index = av_popcount(channel_layout & (AV_CH_LOW_FREQUENCY-1));
2323+ } else { /* native ordering */
2324+ for (i = 0; i < channels; i++)
2325+ s->xxch_order_tab[i] = i;
2326+
2327+ s->lfe_index = channels - 1;
2328+ }
2329+
2330+ s->channel_order_tab = s->xxch_order_tab;
2331+ }
2332+
2333+ if (avctx->channels != channels) {
2334+ if (avctx->channels)
2335+ av_log(avctx, AV_LOG_INFO, "Number of channels changed in DCA decoder (%d -> %d)\n", avctx->channels, channels);
2336+ avctx->channels = channels;
2337+ }
2338+
2339+ /* get output buffer */
2340+ s->frame.nb_samples = 256 * (s->sample_blocks / 8);
2341+ if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
2342+ av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
2343+ return ret;
2344+ }
2345+ samples_flt = (float *) s->frame.data[0];
2346+ samples_s16 = (int16_t *) s->frame.data[0];
2347+
2348+ /* filter to get final output */
2349+ for (i = 0; i < (s->sample_blocks / 8); i++) {
2350+ dca_filter_channels(s, i);
2351+
2352+ /* If this was marked as a DTS-ES stream we need to subtract back- */
2353+ /* channel from SL & SR to remove matrixed back-channel signal */
2354+ if ((s->source_pcm_res & 1) && s->xch_present) {
2355+ float *back_chan = s->samples + s->channel_order_tab[s->xch_base_channel] * 256;
2356+ float *lt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 2] * 256;
2357+ float *rt_chan = s->samples + s->channel_order_tab[s->xch_base_channel - 1] * 256;
2358+ s->fdsp.vector_fmac_scalar(lt_chan, back_chan, -M_SQRT1_2, 256);
2359+ s->fdsp.vector_fmac_scalar(rt_chan, back_chan, -M_SQRT1_2, 256);
2360+ }
2361+
2362+ /* If stream contains XXCH, we might need to undo an embedded downmix */
2363+ if (s->xxch_dmix_embedded) {
2364+ /* Loop over channel sets in turn */
2365+ ch = num_core_channels;
2366+ for (chset = 0; chset < s->xxch_chset; chset++) {
2367+ endch = ch + s->xxch_chset_nch[chset];
2368+ mask = s->xxch_dmix_embedded;
2369+
2370+ /* undo downmix */
2371+ for (j = ch; j < endch; j++) {
2372+ if (mask & (1 << j)) { /* this channel has been mixed-out */
2373+ src_chan = s->samples + s->channel_order_tab[j] * 256;
2374+ for (k = 0; k < endch; k++) {
2375+ achan = s->channel_order_tab[k];
2376+ scale = s->xxch_dmix_coeff[j][k];
2377+ if (scale != 0.0) {
2378+ dst_chan = s->samples + achan * 256;
2379+ s->fdsp.vector_fmac_scalar(dst_chan, src_chan,
2380+ -scale, 256);
2381+ }
2382+ }
2383+ }
2384+ }
2385+
2386+ /* if a downmix has been embedded then undo the pre-scaling */
2387+ if ((mask & (1 << ch)) && s->xxch_dmix_sf[chset] != 1.0f) {
2388+ scale = s->xxch_dmix_sf[chset];
2389+
2390+ for (j = 0; j < ch; j++) {
2391+ src_chan = s->samples + s->channel_order_tab[j] * 256;
2392+ for (k = 0; k < 256; k++)
2393+ src_chan[k] *= scale;
2394+ }
2395+
2396+ /* LFE channel is always part of core, scale if it exists */
2397+ if (s->lfe) {
2398+ src_chan = s->samples + s->lfe_index * 256;
2399+ for (k = 0; k < 256; k++)
2400+ src_chan[k] *= scale;
2401+ }
2402+ }
2403+
2404+ ch = endch;
2405+ }
2406+
2407+ }
2408+
2409+ if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
2410+ s->fmt_conv.float_interleave(samples_flt, s->samples_chanptr, 256,
2411+ channels);
2412+ samples_flt += 256 * channels;
2413+ } else {
2414+ s->fmt_conv.float_to_int16_interleave(samples_s16,
2415+ s->samples_chanptr, 256,
2416+ channels);
2417+ samples_s16 += 256 * channels;
2418+ }
2419+ }
2420+
2421+ /* update lfe history */
2422+ lfe_samples = 2 * s->lfe * (s->sample_blocks / 8);
2423+ for (i = 0; i < 2 * s->lfe * 4; i++)
2424+ s->lfe_data[i] = s->lfe_data[i + lfe_samples];
2425+
2426+ *got_frame_ptr = 1;
2427+ *(AVFrame *) data = s->frame;
2428+
2429+ return buf_size;
2430+}
2431+
2432+
2433+
2434+/**
2435+ * DCA initialization
2436+ *
2437+ * @param avctx pointer to the AVCodecContext
2438+ */
2439+
2440+static av_cold int dca_decode_init(AVCodecContext *avctx)
2441+{
2442+ DCAContext *s = avctx->priv_data;
2443+ int i;
2444+
2445+ s->avctx = avctx;
2446+ dca_init_vlcs();
2447+
2448+ avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
2449+ ff_mdct_init(&s->imdct, 6, 1, 1.0);
2450+ ff_synth_filter_init(&s->synth);
2451+ ff_dcadsp_init(&s->dcadsp);
2452+ ff_fmt_convert_init(&s->fmt_conv, avctx);
2453+
2454+ for (i = 0; i < DCA_PRIM_CHANNELS_MAX + 1; i++)
2455+ s->samples_chanptr[i] = s->samples + i * 256;
2456+
2457+ if (avctx->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
2458+ avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
2459+ s->scale_bias = 1.0 / 32768.0;
2460+ } else {
2461+ avctx->sample_fmt = AV_SAMPLE_FMT_S16;
2462+ s->scale_bias = 1.0;
2463+ }
2464+
2465+ /* allow downmixing to stereo */
2466+ if (avctx->channels > 0 && avctx->request_channels < avctx->channels &&
2467+ avctx->request_channels == 2) {
2468+ avctx->channels = avctx->request_channels;
2469+ }
2470+
2471+ avcodec_get_frame_defaults(&s->frame);
2472+ avctx->coded_frame = &s->frame;
2473+
2474+ return 0;
2475+}
2476+
2477+static av_cold int dca_decode_end(AVCodecContext *avctx)
2478+{
2479+ DCAContext *s = avctx->priv_data;
2480+ ff_mdct_end(&s->imdct);
2481+ return 0;
2482+}
2483+
2484+static const AVProfile profiles[] = {
2485+ { FF_PROFILE_DTS, "DTS" },
2486+ { FF_PROFILE_DTS_ES, "DTS-ES" },
2487+ { FF_PROFILE_DTS_96_24, "DTS 96/24" },
2488+ { FF_PROFILE_DTS_HD_HRA, "DTS-HD HRA" },
2489+ { FF_PROFILE_DTS_HD_MA, "DTS-HD MA" },
2490+ { FF_PROFILE_UNKNOWN },
2491+};
2492+
2493+AVCodec ff_dca_decoder = {
2494+ .name = "dca",
2495+ .type = AVMEDIA_TYPE_AUDIO,
2496+ .id = CODEC_ID_DTS,
2497+ .priv_data_size = sizeof(DCAContext),
2498+ .init = dca_decode_init,
2499+ .decode = dca_decode_frame,
2500+ .close = dca_decode_end,
2501+ .long_name = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
2502+ .capabilities = CODEC_CAP_CHANNEL_CONF | CODEC_CAP_DR1,
2503+ .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
2504+ AV_SAMPLE_FMT_S16,
2505+ AV_SAMPLE_FMT_NONE },
2506+ .profiles = NULL_IF_CONFIG_SMALL(profiles),
2507+};
libavcodec/dcaenc.c+3−2
@@ -30,6 +30,7 @@ · 29 unmodified lines
3030 #include "put_bits.h"
3131 #include "dcaenc.h"
3232 #include "dcadata.h"
33+#include "dca.h"
3334
3435 #undef NDEBUG
3536
@@ -569,13 +570,13 @@ static int encode_init(AVCodecContext *avctx) · 533 unmodified lines
569570 }
570571
571572 for (i = 0; i < 16; i++) {
572− if (dca_sample_rates[i] && (dca_sample_rates[i] == avctx->sample_rate))
573+ if (avpriv_dca_sample_rates[i] && (avpriv_dca_sample_rates[i] == avctx->sample_rate))
573574 break;
574575 }
575576 if (i == 16) {
576577 av_log(avctx, AV_LOG_ERROR, "Sample rate %iHz not supported, only ", avctx->sample_rate);
577578 for (i = 0; i < 16; i++)
578− av_log(avctx, AV_LOG_ERROR, "%d, ", dca_sample_rates[i]);
579+ av_log(avctx, AV_LOG_ERROR, "%d, ", avpriv_dca_sample_rates[i]);
579580 av_log(avctx, AV_LOG_ERROR, "supported.\n");
580581 return -1;
581582 }
libavcodec/dct-test.c+1−1
@@ -85,7 +85,7 @@ static const struct algo fdct_tab[] = { · 84 unmodified lines
8585 { "IJG-AAN-INT", ff_fdct_ifast, SCALE_PERM },
8686 { "IJG-LLM-INT", ff_jpeg_fdct_islow_8, NO_PERM },
8787
88−#if HAVE_MMX
88+#if HAVE_MMX && HAVE_INLINE_ASM
8989 { "MMX", ff_fdct_mmx, NO_PERM, AV_CPU_FLAG_MMX },
9090 { "MMX2", ff_fdct_mmx2, NO_PERM, AV_CPU_FLAG_MMX2 },
9191 { "SSE2", ff_fdct_sse2, NO_PERM, AV_CPU_FLAG_SSE2 },
libavcodec/x86/dsputilenc_mmx.c+47−33
@@ -30,6 +30,8 @@ · 29 unmodified lines
3030 #include "dsputil_mmx.h"
3131
3232
33+#if HAVE_INLINE_ASM
34+
3335 static void get_pixels_mmx(DCTELEM *block, const uint8_t *pixels, int line_size)
3436 {
3537 __asm__ volatile(
@@ -323,8 +325,6 @@ static int sse16_mmx(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int · 287 unmodified lines
323325 return tmp;
324326 }
325327
326−int ff_sse16_sse2(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h);
327−
328328 static int hf_noise8_mmx(uint8_t * pix1, int line_size, int h) {
329329 int tmp;
330330 __asm__ volatile (
@@ -926,17 +926,6 @@ static void sub_hfyu_median_prediction_mmx2(uint8_t *dst, const uint8_t *src1, c · 595 unmodified lines
926926 "paddusw "#t", "#a" \n\t"\
927927 "movd "#a", "#dst" \n\t"\
928928
929−#define hadamard_func(cpu) \
930−int ff_hadamard8_diff_##cpu (void *s, uint8_t *src1, uint8_t *src2, \
931− int stride, int h); \
932−int ff_hadamard8_diff16_##cpu(void *s, uint8_t *src1, uint8_t *src2, \
933− int stride, int h);
934−
935−hadamard_func(mmx)
936−hadamard_func(mmx2)
937−hadamard_func(sse2)
938−hadamard_func(ssse3)
939−
940929 #define DCT_SAD4(m,mm,o)\
941930 "mov"#m" "#o"+ 0(%1), "#mm"2 \n\t"\
942931 "mov"#m" "#o"+16(%1), "#mm"3 \n\t"\
@@ -1095,10 +1084,26 @@ static int ssd_int8_vs_int16_mmx(const int8_t *pix1, const int16_t *pix2, int si · 152 unmodified lines
10951084 #undef PHADDD
10961085 #endif //HAVE_SSSE3
10971086
1087+#endif /* HAVE_INLINE_ASM */
1088+
1089+int ff_sse16_sse2(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h);
1090+
1091+#define hadamard_func(cpu) \
1092+int ff_hadamard8_diff_##cpu (void *s, uint8_t *src1, uint8_t *src2, \
1093+ int stride, int h); \
1094+int ff_hadamard8_diff16_##cpu(void *s, uint8_t *src1, uint8_t *src2, \
1095+ int stride, int h);
1096+
1097+hadamard_func(mmx)
1098+hadamard_func(mmx2)
1099+hadamard_func(sse2)
1100+hadamard_func(ssse3)
10981101
10991102 void ff_dsputilenc_init_mmx(DSPContext* c, AVCodecContext *avctx)
11001103 {
11011104 int mm_flags = av_get_cpu_flags();
1105+
1106+#if HAVE_INLINE_ASM
11021107 int bit_depth = avctx->bits_per_raw_sample;
11031108
11041109 if (mm_flags & AV_CPU_FLAG_MMX) {
@@ -1122,11 +1127,6 @@ void ff_dsputilenc_init_mmx(DSPContext* c, AVCodecContext *avctx) · 17 unmodified lines
11221127 c->diff_bytes= diff_bytes_mmx;
11231128 c->sum_abs_dctelem= sum_abs_dctelem_mmx;
11241129
1125−#if HAVE_YASM
1126− c->hadamard8_diff[0]= ff_hadamard8_diff16_mmx;
1127− c->hadamard8_diff[1]= ff_hadamard8_diff_mmx;
1128−#endif
1129−
11301130 c->pix_norm1 = pix_norm1_mmx;
11311131 c->sse[0] = sse16_mmx;
11321132 c->sse[1] = sse8_mmx;
@@ -1147,10 +1147,6 @@ void ff_dsputilenc_init_mmx(DSPContext* c, AVCodecContext *avctx) · 14 unmodified lines
11471147
11481148
11491149 if (mm_flags & AV_CPU_FLAG_MMX2) {
1150−#if HAVE_YASM
1151− c->hadamard8_diff[0]= ff_hadamard8_diff16_mmx2;
1152− c->hadamard8_diff[1]= ff_hadamard8_diff_mmx2;
1153−#endif
11541150 c->sum_abs_dctelem= sum_abs_dctelem_mmx2;
11551151 c->vsad[4]= vsad_intra16_mmx2;
11561152
@@ -1165,13 +1161,6 @@ void ff_dsputilenc_init_mmx(DSPContext* c, AVCodecContext *avctx) · 8 unmodified lines
11651161 if (bit_depth <= 8)
11661162 c->get_pixels = get_pixels_sse2;
11671163 c->sum_abs_dctelem= sum_abs_dctelem_sse2;
1168−#if HAVE_YASM
1169− c->sse[0] = ff_sse16_sse2;
1170−#if HAVE_ALIGNED_STACK
1171− c->hadamard8_diff[0]= ff_hadamard8_diff16_sse2;
1172− c->hadamard8_diff[1]= ff_hadamard8_diff_sse2;
1173−#endif
1174−#endif
11751164 }
11761165
11771166 #if HAVE_SSSE3
@@ -1181,10 +1170,6 @@ void ff_dsputilenc_init_mmx(DSPContext* c, AVCodecContext *avctx) · 3 unmodified lines
11811170 }
11821171 c->add_8x8basis= add_8x8basis_ssse3;
11831172 c->sum_abs_dctelem= sum_abs_dctelem_ssse3;
1184−#if HAVE_YASM && HAVE_ALIGNED_STACK
1185− c->hadamard8_diff[0]= ff_hadamard8_diff16_ssse3;
1186− c->hadamard8_diff[1]= ff_hadamard8_diff_ssse3;
1187−#endif
11881173 }
11891174 #endif
11901175
@@ -1195,6 +1180,35 @@ void ff_dsputilenc_init_mmx(DSPContext* c, AVCodecContext *avctx) · 4 unmodified lines
11951180 c->add_8x8basis= add_8x8basis_3dnow;
11961181 }
11971182 }
1183+#endif /* HAVE_INLINE_ASM */
1184+
1185+#if HAVE_YASM
1186+ if (mm_flags & AV_CPU_FLAG_MMX) {
1187+ c->hadamard8_diff[0] = ff_hadamard8_diff16_mmx;
1188+ c->hadamard8_diff[1] = ff_hadamard8_diff_mmx;
1189+
1190+ if (mm_flags & AV_CPU_FLAG_MMX2) {
1191+ c->hadamard8_diff[0] = ff_hadamard8_diff16_mmx2;
1192+ c->hadamard8_diff[1] = ff_hadamard8_diff_mmx2;
1193+ }
1194+
1195+ if (mm_flags & AV_CPU_FLAG_SSE2){
1196+ c->sse[0] = ff_sse16_sse2;
1197+
1198+#if HAVE_ALIGNED_STACK
1199+ c->hadamard8_diff[0] = ff_hadamard8_diff16_sse2;
1200+ c->hadamard8_diff[1] = ff_hadamard8_diff_sse2;
1201+#endif
1202+ }
1203+
1204+#if HAVE_SSSE3 && HAVE_ALIGNED_STACK
1205+ if (mm_flags & AV_CPU_FLAG_SSSE3) {
1206+ c->hadamard8_diff[0] = ff_hadamard8_diff16_ssse3;
1207+ c->hadamard8_diff[1] = ff_hadamard8_diff_ssse3;
1208+ }
1209+#endif
1210+ }
1211+#endif /* HAVE_YASM */
11981212
11991213 ff_dsputil_init_pix_mmx(c, avctx);
12001214 }
libavcodec/x86/fdct_mmx.c+4−0
@@ -34,6 +34,8 @@ · 33 unmodified lines
3434 #include "libavutil/x86_cpu.h"
3535 #include "libavcodec/dsputil.h"
3636
37+#if HAVE_INLINE_ASM
38+
3739 //////////////////////////////////////////////////////////////////////
3840 //
3941 // constants for the forward DCT
@@ -579,3 +581,5 @@ void ff_fdct_sse2(int16_t *block) · 539 unmodified lines
579581 fdct_col_sse2(block, block1, 0);
580582 fdct_row_sse2(block1, block);
581583 }
584+
585+#endif /* HAVE_INLINE_ASM */
libavcodec/x86/fft.c+2−0
@@ -25,6 +25,7 @@ av_cold void ff_fft_init_mmx(FFTContext *s) · 24 unmodified lines
2525 {
2626 #if HAVE_YASM
2727 int has_vectors = av_get_cpu_flags();
28+#if ARCH_X86_32
2829 if (has_vectors & AV_CPU_FLAG_3DNOW && HAVE_AMD3DNOW) {
2930 /* 3DNow! for K6-2/3 */
3031 s->imdct_calc = ff_imdct_calc_3dnow;
@@ -37,6 +38,7 @@ av_cold void ff_fft_init_mmx(FFTContext *s) · 6 unmodified lines
3738 s->imdct_half = ff_imdct_half_3dnow2;
3839 s->fft_calc = ff_fft_calc_3dnow2;
3940 }
41+#endif
4042 if (has_vectors & AV_CPU_FLAG_SSE && HAVE_SSE) {
4143 /* SSE for P3/P4/K8 */
4244 s->imdct_calc = ff_imdct_calc_sse;
libavcodec/x86/h264dsp_mmx.c+0−2
@@ -23,8 +23,6 @@ · 22 unmodified lines
2323 #include "libavcodec/h264dsp.h"
2424 #include "dsputil_mmx.h"
2525
26−DECLARE_ALIGNED(8, static const uint64_t, ff_pb_3_1 ) = 0x0103010301030103ULL;
27−
2826 /***********************************/
2927 /* IDCT */
3028 #define IDCT_ADD_FUNC(NUM, DEPTH, OPT) \
libavcodec/x86/motion_est_mmx.c+6−0
@@ -26,6 +26,8 @@ · 25 unmodified lines
2626 #include "libavcodec/dsputil.h"
2727 #include "dsputil_mmx.h"
2828
29+#if HAVE_INLINE_ASM
30+
2931 DECLARE_ASM_CONST(8, uint64_t, round_tab)[3]={
3032 0x0000000000000000ULL,
3133 0x0001000100010001ULL,
@@ -422,8 +424,11 @@ static int sad16_xy2_ ## suf(void *v, uint8_t *blk2, uint8_t *blk1, int stride, · 390 unmodified lines
422424 PIX_SAD(mmx)
423425 PIX_SAD(mmx2)
424426
427+#endif /* HAVE_INLINE_ASM */
428+
425429 void ff_dsputil_init_pix_mmx(DSPContext* c, AVCodecContext *avctx)
426430 {
431+#if HAVE_INLINE_ASM
427432 int mm_flags = av_get_cpu_flags();
428433
429434 if (mm_flags & AV_CPU_FLAG_MMX) {
@@ -458,4 +463,5 @@ void ff_dsputil_init_pix_mmx(DSPContext* c, AVCodecContext *avctx) · 28 unmodified lines
458463 if ((mm_flags & AV_CPU_FLAG_SSE2) && !(mm_flags & AV_CPU_FLAG_3DNOW) && avctx->codec_id != CODEC_ID_SNOW) {
459464 c->sad[0]= sad16_sse2;
460465 }
466+#endif /* HAVE_INLINE_ASM */
461467 }
libavcodec/x86/mpegvideo_mmx.c+6−0
@@ -29,6 +29,8 @@ · 28 unmodified lines
2929 #include "libavcodec/mpegvideo.h"
3030 #include "dsputil_mmx.h"
3131
32+#if HAVE_INLINE_ASM
33+
3234 extern uint16_t ff_inv_zigzag_direct16[64];
3335
3436
@@ -626,8 +628,11 @@ static void denoise_dct_sse2(MpegEncContext *s, DCTELEM *block){ · 591 unmodified lines
626628 #include "mpegvideo_mmx_template.c"
627629 #endif
628630
631+#endif /* HAVE_INLINE_ASM */
632+
629633 void ff_MPV_common_init_mmx(MpegEncContext *s)
630634 {
635+#if HAVE_INLINE_ASM
631636 int mm_flags = av_get_cpu_flags();
632637
633638 if (mm_flags & AV_CPU_FLAG_MMX) {
@@ -662,4 +667,5 @@ void ff_MPV_common_init_mmx(MpegEncContext *s) · 28 unmodified lines
662667 }
663668 }
664669 }
670+#endif /* HAVE_INLINE_ASM */
665671 }
libavformat/spdifenc.c+1−2
@@ -49,7 +49,6 @@ · 48 unmodified lines
4949 #include "spdif.h"
5050 #include "libavcodec/ac3.h"
5151 #include "libavcodec/dca.h"
52−#include "libavcodec/dcadata.h"
5352 #include "libavcodec/aacadtsdec.h"
5453 #include "libavutil/opt.h"
5554
@@ -253,7 +252,7 @@ static int spdif_header_dts(AVFormatContext *s, AVPacket *pkt) · 197 unmodified lines
253252 case DCA_MARKER_RAW_BE:
254253 blocks = (AV_RB16(pkt->data + 4) >> 2) & 0x7f;
255254 core_size = ((AV_RB24(pkt->data + 5) >> 4) & 0x3fff) + 1;
256− sample_rate = dca_sample_rates[(pkt->data[8] >> 2) & 0x0f];
255+ sample_rate = avpriv_dca_sample_rates[(pkt->data[8] >> 2) & 0x0f];
257256 break;
258257 case DCA_MARKER_RAW_LE:
259258 blocks = (AV_RL16(pkt->data + 4) >> 2) & 0x7f;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t