summaryrefslogtreecommitdiff
path: root/apps/codecs/libwmapro/wmaprodec.c
diff options
context:
space:
mode:
authorMohamed Tarek <mt@rockbox.org>2010-07-15 05:38:09 +0000
committerMohamed Tarek <mt@rockbox.org>2010-07-15 05:38:09 +0000
commit387af97a26105fce79e6a8726752cf183d40939e (patch)
tree25c7898bbf08963b9c7688c8937061b4b3491dec /apps/codecs/libwmapro/wmaprodec.c
parent5d2969c94e97c81e3eb88211b921be80b66bd691 (diff)
downloadrockbox-387af97a26105fce79e6a8726752cf183d40939e.tar.gz
rockbox-387af97a26105fce79e6a8726752cf183d40939e.zip
Initial cleanup for libwmapro.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27425 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/codecs/libwmapro/wmaprodec.c')
-rw-r--r--apps/codecs/libwmapro/wmaprodec.c100
1 files changed, 53 insertions, 47 deletions
diff --git a/apps/codecs/libwmapro/wmaprodec.c b/apps/codecs/libwmapro/wmaprodec.c
index 2dd2a6a1e4..1fc4386bd5 100644
--- a/apps/codecs/libwmapro/wmaprodec.c
+++ b/apps/codecs/libwmapro/wmaprodec.c
@@ -86,8 +86,6 @@
86 * subframe in order to reconstruct the output samples. 86 * subframe in order to reconstruct the output samples.
87 */ 87 */
88 88
89#include "avcodec.h"
90#include "internal.h"
91#include "get_bits.h" 89#include "get_bits.h"
92#include "put_bits.h" 90#include "put_bits.h"
93#include "wmaprodata.h" 91#include "wmaprodata.h"
@@ -99,6 +97,8 @@
99#include "types.h" 97#include "types.h"
100#include "wmapro_math.h" 98#include "wmapro_math.h"
101#include "codecs.h" 99#include "codecs.h"
100#include "codeclib.h"
101#include "../libasf/asf.h"
102 102
103/* Uncomment the following line to enable some debug output */ 103/* Uncomment the following line to enable some debug output */
104//#define WMAPRO_DUMP_CTX_EN 104//#define WMAPRO_DUMP_CTX_EN
@@ -115,6 +115,16 @@
115#define AVERROR_PATCHWELCOME -2 115#define AVERROR_PATCHWELCOME -2
116#define av_log_ask_for_sample(...) 116#define av_log_ask_for_sample(...)
117 117
118/* Taken from avcodec.h */
119#define FF_INPUT_BUFFER_PADDING_SIZE 8
120
121/* Taken from libavutil/mem.h */
122#define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
123
124/* Taken from libavutil/common.h */
125#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
126#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
127
118/** current decoder limitations */ 128/** current decoder limitations */
119#define WMAPRO_MAX_CHANNELS 8 ///< max number of handled channels 129#define WMAPRO_MAX_CHANNELS 8 ///< max number of handled channels
120#define MAX_SUBFRAMES 32 ///< max number of subframes per channel 130#define MAX_SUBFRAMES 32 ///< max number of subframes per channel
@@ -183,7 +193,6 @@ typedef struct {
183 */ 193 */
184typedef struct WMAProDecodeCtx { 194typedef struct WMAProDecodeCtx {
185 /* generic decoder variables */ 195 /* generic decoder variables */
186 AVCodecContext* avctx; ///< codec context for av_log
187 uint8_t frame_data[MAX_FRAMESIZE + 196 uint8_t frame_data[MAX_FRAMESIZE +
188 FF_INPUT_BUFFER_PADDING_SIZE];///< compressed frame data 197 FF_INPUT_BUFFER_PADDING_SIZE];///< compressed frame data
189 PutBitContext pb; ///< context for filling the frame_data buffer 198 PutBitContext pb; ///< context for filling the frame_data buffer
@@ -218,7 +227,7 @@ typedef struct WMAProDecodeCtx {
218 uint8_t packet_done; ///< set when a packet is fully decoded 227 uint8_t packet_done; ///< set when a packet is fully decoded
219 228
220 /* frame decode state */ 229 /* frame decode state */
221 uint32_t frame_num; ///< current frame number (not used for decoding) 230 uint32_t frame_num; ///< current frame number
222 GetBitContext gb; ///< bitstream reader context 231 GetBitContext gb; ///< bitstream reader context
223 int buf_bit_size; ///< buffer size in bits 232 int buf_bit_size; ///< buffer size in bits
224 FIXED* samples; 233 FIXED* samples;
@@ -250,7 +259,7 @@ static WMAProDecodeCtx globWMAProDecCtx;
250 *@param s context 259 *@param s context
251 */ 260 */
252#ifdef WMAPRO_DUMP_CTX_EN 261#ifdef WMAPRO_DUMP_CTX_EN
253static void av_cold dump_context(WMAProDecodeCtx *s) 262static void dump_context(WMAProDecodeCtx *s)
254{ 263{
255#define PRINT(a, b) printf(" %s = %d\n", a, b); 264#define PRINT(a, b) printf(" %s = %d\n", a, b);
256#define PRINT_HEX(a, b) printf(" %s = %x\n", a, b); 265#define PRINT_HEX(a, b) printf(" %s = %x\n", a, b);
@@ -270,28 +279,26 @@ static void av_cold dump_context(WMAProDecodeCtx *s)
270 *@param avctx codec context 279 *@param avctx codec context
271 *@return 0 on success, -1 otherwise 280 *@return 0 on success, -1 otherwise
272 */ 281 */
273av_cold int decode_init(AVCodecContext *avctx) 282int decode_init(asf_waveformatex_t *wfx)
274{ 283{
275 avctx->priv_data = &globWMAProDecCtx; 284 memset(&globWMAProDecCtx, 0, sizeof(WMAProDecodeCtx));
276 memset(avctx->priv_data, 0, sizeof(WMAProDecodeCtx)); 285 WMAProDecodeCtx *s = &globWMAProDecCtx;
277 WMAProDecodeCtx *s = avctx->priv_data; 286 uint8_t *edata_ptr = wfx->data;
278 uint8_t *edata_ptr = avctx->extradata;
279 unsigned int channel_mask; 287 unsigned int channel_mask;
280 int i; 288 int i;
281 int log2_max_num_subframes; 289 int log2_max_num_subframes;
282 int num_possible_block_sizes; 290 int num_possible_block_sizes;
283 291
284 s->avctx = avctx;
285 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE); 292 init_put_bits(&s->pb, s->frame_data, MAX_FRAMESIZE);
286 293
287 if (avctx->extradata_size >= 18) { 294 if (wfx->datalen >= 18) {
288 s->decode_flags = AV_RL16(edata_ptr+14); 295 s->decode_flags = AV_RL16(edata_ptr+14);
289 channel_mask = AV_RL32(edata_ptr+2); 296 channel_mask = AV_RL32(edata_ptr+2);
290 s->bits_per_sample = AV_RL16(edata_ptr); 297 s->bits_per_sample = AV_RL16(edata_ptr);
291 /** dump the extradata */ 298 /** dump the extradata */
292 for (i = 0; i < avctx->extradata_size; i++) 299 for (i = 0; i < wfx->datalen; i++)
293 dprintf(avctx, "[%x] ", avctx->extradata[i]); 300 DEBUGF("[%x] ", wfx->data[i]);
294 dprintf(avctx, "\n"); 301 DEBUGF("\n");
295 302
296 } else { 303 } else {
297 DEBUGF("Unknown extradata size\n"); 304 DEBUGF("Unknown extradata size\n");
@@ -299,7 +306,7 @@ av_cold int decode_init(AVCodecContext *avctx)
299 } 306 }
300 307
301 /** generic init */ 308 /** generic init */
302 s->log2_frame_size = av_log2(avctx->block_align) + 4; 309 s->log2_frame_size = av_log2(wfx->blockalign) + 4;
303 310
304 /** frame info */ 311 /** frame info */
305 s->skip_frame = 1; /** skip first frame */ 312 s->skip_frame = 1; /** skip first frame */
@@ -312,11 +319,11 @@ av_cold int decode_init(AVCodecContext *avctx)
312 } 319 }
313 320
314 /** get frame len */ 321 /** get frame len */
315 s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(avctx->sample_rate, 322 s->samples_per_frame = 1 << ff_wma_get_frame_len_bits(wfx->rate,
316 3, s->decode_flags); 323 3, s->decode_flags);
317 324
318 /** init previous block len */ 325 /** init previous block len */
319 for (i = 0; i < avctx->channels; i++) 326 for (i = 0; i < wfx->channels; i++)
320 s->channel[i].prev_block_len = s->samples_per_frame; 327 s->channel[i].prev_block_len = s->samples_per_frame;
321 328
322 /** subframe info */ 329 /** subframe info */
@@ -336,7 +343,7 @@ av_cold int decode_init(AVCodecContext *avctx)
336 return AVERROR_INVALIDDATA; 343 return AVERROR_INVALIDDATA;
337 } 344 }
338 345
339 s->num_channels = avctx->channels; 346 s->num_channels = wfx->channels;
340 347
341 /** extract lfe channel position */ 348 /** extract lfe channel position */
342 s->lfe_channel = -1; 349 s->lfe_channel = -1;
@@ -396,7 +403,7 @@ av_cold int decode_init(AVCodecContext *avctx)
396 403
397 for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band - 1] < subframe_len; x++) { 404 for (x = 0; x < MAX_BANDS-1 && s->sfb_offsets[i][band - 1] < subframe_len; x++) {
398 int offset = (subframe_len * 2 * critical_freq[x]) 405 int offset = (subframe_len * 2 * critical_freq[x])
399 / s->avctx->sample_rate + 2; 406 / wfx->rate + 2;
400 offset &= ~3; 407 offset &= ~3;
401 if (offset > s->sfb_offsets[i][band - 1]) 408 if (offset > s->sfb_offsets[i][band - 1])
402 s->sfb_offsets[i][band++] = offset; 409 s->sfb_offsets[i][band++] = offset;
@@ -429,8 +436,8 @@ av_cold int decode_init(AVCodecContext *avctx)
429 /** calculate subwoofer cutoff values */ 436 /** calculate subwoofer cutoff values */
430 for (i = 0; i < num_possible_block_sizes; i++) { 437 for (i = 0; i < num_possible_block_sizes; i++) {
431 int block_size = s->samples_per_frame >> i; 438 int block_size = s->samples_per_frame >> i;
432 int cutoff = (440*block_size + 3 * (s->avctx->sample_rate >> 1) - 1) 439 int cutoff = (440*block_size + 3 * (wfx->rate >> 1) - 1)
433 / s->avctx->sample_rate; 440 / wfx->rate;
434 s->subwoofer_cutoffs[i] = av_clip(cutoff, 4, block_size); 441 s->subwoofer_cutoffs[i] = av_clip(cutoff, 4, block_size);
435 } 442 }
436 443
@@ -576,7 +583,7 @@ static int decode_tilehdr(WMAProDecodeCtx *s)
576 int i; 583 int i;
577 int offset = 0; 584 int offset = 0;
578 for (i = 0; i < s->channel[c].num_subframes; i++) { 585 for (i = 0; i < s->channel[c].num_subframes; i++) {
579 dprintf(s->avctx, "frame[%i] channel[%i] subframe[%i]" 586 DEBUGF("frame[%i] channel[%i] subframe[%i]"
580 " len %i\n", s->frame_num, c, i, 587 " len %i\n", s->frame_num, c, i,
581 s->channel[c].subframe_len[i]); 588 s->channel[c].subframe_len[i]);
582 s->channel[c].subframe_offset[i] = offset; 589 s->channel[c].subframe_offset[i] = offset;
@@ -789,7 +796,7 @@ static int decode_coeffs(WMAProDecodeCtx *s, int c)
789 const uint16_t* run; 796 const uint16_t* run;
790 const FIXED* level; 797 const FIXED* level;
791 798
792 dprintf(s->avctx, "decode coefficients for channel %i\n", c); 799 DEBUGF("decode coefficients for channel %i\n", c);
793 800
794 vlctable = get_bits1(&s->gb); 801 vlctable = get_bits1(&s->gb);
795 vlc = &coef_vlc[vlctable]; 802 vlc = &coef_vlc[vlctable];
@@ -1080,8 +1087,7 @@ static int decode_subframe(WMAProDecodeCtx *s)
1080 } 1087 }
1081 } 1088 }
1082 1089
1083 dprintf(s->avctx, 1090 DEBUGF("processing subframe with offset %i len %i\n", offset, subframe_len);
1084 "processing subframe with offset %i len %i\n", offset, subframe_len);
1085 1091
1086 /** get a list of all channels that contain the estimated block */ 1092 /** get a list of all channels that contain the estimated block */
1087 s->channels_for_cur_subframe = 0; 1093 s->channels_for_cur_subframe = 0;
@@ -1107,8 +1113,7 @@ static int decode_subframe(WMAProDecodeCtx *s)
1107 s->parsed_all_subframes = 1; 1113 s->parsed_all_subframes = 1;
1108 1114
1109 1115
1110 dprintf(s->avctx, "subframe is part of %i channels\n", 1116 DEBUGF("subframe is part of %i channels\n", s->channels_for_cur_subframe);
1111 s->channels_for_cur_subframe);
1112 1117
1113 /** calculate number of scale factor bands and their offsets */ 1118 /** calculate number of scale factor bands and their offsets */
1114 s->table_idx = av_log2(s->samples_per_frame/subframe_len); 1119 s->table_idx = av_log2(s->samples_per_frame/subframe_len);
@@ -1207,7 +1212,7 @@ static int decode_subframe(WMAProDecodeCtx *s)
1207 return AVERROR_INVALIDDATA; 1212 return AVERROR_INVALIDDATA;
1208 } 1213 }
1209 1214
1210 dprintf(s->avctx, "BITSTREAM: subframe header length was %i\n", 1215 DEBUGF("BITSTREAM: subframe header length was %i\n",
1211 get_bits_count(&s->gb) - s->subframe_offset); 1216 get_bits_count(&s->gb) - s->subframe_offset);
1212 1217
1213 /** parse coefficients */ 1218 /** parse coefficients */
@@ -1222,7 +1227,7 @@ static int decode_subframe(WMAProDecodeCtx *s)
1222 } 1227 }
1223 } 1228 }
1224 1229
1225 dprintf(s->avctx, "BITSTREAM: subframe length was %i\n", 1230 DEBUGF("BITSTREAM: subframe length was %i\n",
1226 get_bits_count(&s->gb) - s->subframe_offset); 1231 get_bits_count(&s->gb) - s->subframe_offset);
1227 1232
1228 if (transmit_coeffs) { 1233 if (transmit_coeffs) {
@@ -1306,7 +1311,7 @@ static int decode_frame(WMAProDecodeCtx *s)
1306 if (s->len_prefix) 1311 if (s->len_prefix)
1307 len = get_bits(gb, s->log2_frame_size); 1312 len = get_bits(gb, s->log2_frame_size);
1308 1313
1309 dprintf(s->avctx, "decoding frame with length %x\n", len); 1314 DEBUGF("decoding frame with length %x\n", len);
1310 1315
1311 /** decode tile information */ 1316 /** decode tile information */
1312 if (decode_tilehdr(s)) { 1317 if (decode_tilehdr(s)) {
@@ -1324,7 +1329,7 @@ static int decode_frame(WMAProDecodeCtx *s)
1324 /** read drc info */ 1329 /** read drc info */
1325 if (s->dynamic_range_compression) { 1330 if (s->dynamic_range_compression) {
1326 s->drc_gain = get_bits(gb, 8); 1331 s->drc_gain = get_bits(gb, 8);
1327 dprintf(s->avctx, "drc_gain %i\n", s->drc_gain); 1332 DEBUGF("drc_gain %i\n", s->drc_gain);
1328 } 1333 }
1329 1334
1330 /** no idea what these are for, might be the number of samples 1335 /** no idea what these are for, might be the number of samples
@@ -1335,18 +1340,18 @@ static int decode_frame(WMAProDecodeCtx *s)
1335 /** usually true for the first frame */ 1340 /** usually true for the first frame */
1336 if (get_bits1(gb)) { 1341 if (get_bits1(gb)) {
1337 skip = get_bits(gb, av_log2(s->samples_per_frame * 2)); 1342 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1338 dprintf(s->avctx, "start skip: %i\n", skip); 1343 DEBUGF("start skip: %i\n", skip);
1339 } 1344 }
1340 1345
1341 /** sometimes true for the last frame */ 1346 /** sometimes true for the last frame */
1342 if (get_bits1(gb)) { 1347 if (get_bits1(gb)) {
1343 skip = get_bits(gb, av_log2(s->samples_per_frame * 2)); 1348 skip = get_bits(gb, av_log2(s->samples_per_frame * 2));
1344 dprintf(s->avctx, "end skip: %i\n", skip); 1349 DEBUGF("end skip: %i\n", skip);
1345 } 1350 }
1346 1351
1347 } 1352 }
1348 1353
1349 dprintf(s->avctx, "BITSTREAM: frame header length was %i\n", 1354 DEBUGF("BITSTREAM: frame header length was %i\n",
1350 get_bits_count(gb) - s->frame_offset); 1355 get_bits_count(gb) - s->frame_offset);
1351 1356
1352 /** reset subframe states */ 1357 /** reset subframe states */
@@ -1477,13 +1482,13 @@ static void save_bits(WMAProDecodeCtx *s, GetBitContext* gb, int len,
1477 *@param avpkt input packet 1482 *@param avpkt input packet
1478 *@return number of bytes that were read from the input buffer 1483 *@return number of bytes that were read from the input buffer
1479 */ 1484 */
1480int decode_packet(AVCodecContext *avctx, 1485int decode_packet(asf_waveformatex_t *wfx, void *data, int *data_size,
1481 void *data, int *data_size, AVPacket* avpkt) 1486 void* pktdata, int size)
1482{ 1487{
1483 WMAProDecodeCtx *s = avctx->priv_data; 1488 WMAProDecodeCtx *s = &globWMAProDecCtx;
1484 GetBitContext* gb = &s->pgb; 1489 GetBitContext* gb = &s->pgb;
1485 const uint8_t* buf = avpkt->data; 1490 const uint8_t* buf = pktdata;
1486 int buf_size = avpkt->size; 1491 int buf_size = size;
1487 int num_bits_prev_frame; 1492 int num_bits_prev_frame;
1488 int packet_sequence_number; 1493 int packet_sequence_number;
1489 1494
@@ -1496,10 +1501,10 @@ int decode_packet(AVCodecContext *avctx,
1496 s->buf_bit_size = buf_size << 3; 1501 s->buf_bit_size = buf_size << 3;
1497 1502
1498 /** sanity check for the buffer length */ 1503 /** sanity check for the buffer length */
1499 if (buf_size < avctx->block_align) 1504 if (buf_size < wfx->blockalign)
1500 return 0; 1505 return 0;
1501 1506
1502 buf_size = avctx->block_align; 1507 buf_size = wfx->blockalign;
1503 1508
1504 /** parse packet header */ 1509 /** parse packet header */
1505 init_get_bits(gb, buf, s->buf_bit_size); 1510 init_get_bits(gb, buf, s->buf_bit_size);
@@ -1508,7 +1513,7 @@ int decode_packet(AVCodecContext *avctx,
1508 1513
1509 /** get number of bits that need to be added to the previous frame */ 1514 /** get number of bits that need to be added to the previous frame */
1510 num_bits_prev_frame = get_bits(gb, s->log2_frame_size); 1515 num_bits_prev_frame = get_bits(gb, s->log2_frame_size);
1511 dprintf(avctx, "packet[%d]: nbpf %x\n", avctx->frame_number, 1516 DEBUGF("packet[%d]: nbpf %x\n", s->frame_num,
1512 num_bits_prev_frame); 1517 num_bits_prev_frame);
1513 1518
1514 /** check for packet loss */ 1519 /** check for packet loss */
@@ -1524,14 +1529,14 @@ int decode_packet(AVCodecContext *avctx,
1524 /** append the previous frame data to the remaining data from the 1529 /** append the previous frame data to the remaining data from the
1525 previous packet to create a full frame */ 1530 previous packet to create a full frame */
1526 save_bits(s, gb, num_bits_prev_frame, 1); 1531 save_bits(s, gb, num_bits_prev_frame, 1);
1527 dprintf(avctx, "accumulated %x bits of frame data\n", 1532 DEBUGF("accumulated %x bits of frame data\n",
1528 s->num_saved_bits - s->frame_offset); 1533 s->num_saved_bits - s->frame_offset);
1529 1534
1530 /** decode the cross packet frame if it is valid */ 1535 /** decode the cross packet frame if it is valid */
1531 if (!s->packet_loss) 1536 if (!s->packet_loss)
1532 decode_frame(s); 1537 decode_frame(s);
1533 } else if (s->num_saved_bits - s->frame_offset) { 1538 } else if (s->num_saved_bits - s->frame_offset) {
1534 dprintf(avctx, "ignoring %x previously saved bits\n", 1539 DEBUGF("ignoring %x previously saved bits\n",
1535 s->num_saved_bits - s->frame_offset); 1540 s->num_saved_bits - s->frame_offset);
1536 } 1541 }
1537 1542
@@ -1539,8 +1544,8 @@ int decode_packet(AVCodecContext *avctx,
1539 1544
1540 } else { 1545 } else {
1541 int frame_size; 1546 int frame_size;
1542 s->buf_bit_size = avpkt->size << 3; 1547 s->buf_bit_size = size << 3;
1543 init_get_bits(gb, avpkt->data, s->buf_bit_size); 1548 init_get_bits(gb, pktdata, s->buf_bit_size);
1544 skip_bits(gb, s->packet_offset); 1549 skip_bits(gb, s->packet_offset);
1545 if (remaining_bits(s, gb) > s->log2_frame_size && 1550 if (remaining_bits(s, gb) > s->log2_frame_size &&
1546 (frame_size = show_bits(gb, s->log2_frame_size)) && 1551 (frame_size = show_bits(gb, s->log2_frame_size)) &&
@@ -1561,6 +1566,7 @@ int decode_packet(AVCodecContext *avctx,
1561 *data_size = (int8_t *)s->samples - (int8_t *)data; 1566 *data_size = (int8_t *)s->samples - (int8_t *)data;
1562 s->packet_offset = get_bits_count(gb) & 7; 1567 s->packet_offset = get_bits_count(gb) & 7;
1563 1568
1569 s->frame_num++;
1564 return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3; 1570 return (s->packet_loss) ? AVERROR_INVALIDDATA : get_bits_count(gb) >> 3;
1565} 1571}
1566 1572