summaryrefslogtreecommitdiff
path: root/apps/codecs/libcook/cook.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libcook/cook.c')
-rw-r--r--apps/codecs/libcook/cook.c264
1 files changed, 170 insertions, 94 deletions
diff --git a/apps/codecs/libcook/cook.c b/apps/codecs/libcook/cook.c
index cee69fe14a..856004f31b 100644
--- a/apps/codecs/libcook/cook.c
+++ b/apps/codecs/libcook/cook.c
@@ -45,15 +45,30 @@
45#include <math.h> 45#include <math.h>
46#include <stddef.h> 46#include <stddef.h>
47#include <stdio.h> 47#include <stdio.h>
48#include <sys/types.h>
49#include <sys/stat.h>
50#include <fcntl.h>
51#include <unistd.h>
48 52
49#include "libavutil/lfg.h" 53#include "libavutil/lfg.h"
50#include "libavutil/random_seed.h"
51#include "avcodec.h"
52#include "bitstream.h" 54#include "bitstream.h"
53#include "dsputil.h" 55#include "dsputil.h"
54#include "bytestream.h" 56#include "bytestream.h"
55 57
56#include "cookdata.h" 58#include "cookdata.h"
59#include "rm2wav.h"
60
61/* The following table is taken from libavutil/mathematics.c */
62const uint8_t ff_log2_tab[256]={
63 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
64 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
65 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
66 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
67 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
68 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
69 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
70 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
71};
57 72
58/* the different Cook versions */ 73/* the different Cook versions */
59#define MONO 0x1000001 74#define MONO 0x1000001
@@ -64,6 +79,8 @@
64#define SUBBAND_SIZE 20 79#define SUBBAND_SIZE 20
65#define MAX_SUBPACKETS 5 80#define MAX_SUBPACKETS 5
66//#define COOKDEBUG 81//#define COOKDEBUG
82//#define DUMP_RAW_FRAMES
83#define DEBUGF(message,args ...) av_log(NULL,AV_LOG_ERROR,message,## args)
67 84
68typedef struct { 85typedef struct {
69 int *now; 86 int *now;
@@ -93,8 +110,10 @@ typedef struct cook {
93 110
94 void (* saturate_output) (struct cook *q, int chan, int16_t *out); 111 void (* saturate_output) (struct cook *q, int chan, int16_t *out);
95 112
96 AVCodecContext* avctx;
97 GetBitContext gb; 113 GetBitContext gb;
114 int frame_number;
115 int block_align;
116 int extradata_size;
98 /* stream data */ 117 /* stream data */
99 int nb_channels; 118 int nb_channels;
100 int joint_stereo; 119 int joint_stereo;
@@ -112,7 +131,6 @@ typedef struct cook {
112 int cookversion; 131 int cookversion;
113 /* states */ 132 /* states */
114 AVLFG random_state; 133 AVLFG random_state;
115
116 /* transform data */ 134 /* transform data */
117 MDCTContext mdct_ctx; 135 MDCTContext mdct_ctx;
118 float* mlt_window; 136 float* mlt_window;
@@ -138,7 +156,7 @@ typedef struct cook {
138 /* data buffers */ 156 /* data buffers */
139 157
140 uint8_t* decoded_bytes_buffer; 158 uint8_t* decoded_bytes_buffer;
141 DECLARE_ALIGNED_16(float,mono_mdct_output[2048]); 159 float mono_mdct_output[2048] __attribute__ ((aligned(16))); //DECLARE_ALIGNED_16(float,mono_mdct_output[2048]);
142 float mono_previous_buffer1[1024]; 160 float mono_previous_buffer1[1024];
143 float mono_previous_buffer2[1024]; 161 float mono_previous_buffer2[1024];
144 float decode_buffer_1[1024]; 162 float decode_buffer_1[1024];
@@ -214,7 +232,7 @@ static av_cold int init_cook_vlc_tables(COOKContext *q) {
214 envelope_quant_index_huffbits[i], 1, 1, 232 envelope_quant_index_huffbits[i], 1, 1,
215 envelope_quant_index_huffcodes[i], 2, 2, 0); 233 envelope_quant_index_huffcodes[i], 2, 2, 0);
216 } 234 }
217 av_log(q->avctx,AV_LOG_DEBUG,"sqvh VLC init\n"); 235 av_log(NULL,AV_LOG_DEBUG,"sqvh VLC init\n");
218 for (i=0 ; i<7 ; i++) { 236 for (i=0 ; i<7 ; i++) {
219 result |= init_vlc (&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i], 237 result |= init_vlc (&q->sqvh[i], vhvlcsize_tab[i], vhsize_tab[i],
220 cvh_huffbits[i], 1, 1, 238 cvh_huffbits[i], 1, 1,
@@ -225,10 +243,10 @@ static av_cold int init_cook_vlc_tables(COOKContext *q) {
225 result |= init_vlc (&q->ccpl, 6, (1<<q->js_vlc_bits)-1, 243 result |= init_vlc (&q->ccpl, 6, (1<<q->js_vlc_bits)-1,
226 ccpl_huffbits[q->js_vlc_bits-2], 1, 1, 244 ccpl_huffbits[q->js_vlc_bits-2], 1, 1,
227 ccpl_huffcodes[q->js_vlc_bits-2], 2, 2, 0); 245 ccpl_huffcodes[q->js_vlc_bits-2], 2, 2, 0);
228 av_log(q->avctx,AV_LOG_DEBUG,"Joint-stereo VLC used.\n"); 246 av_log(NULL,AV_LOG_DEBUG,"Joint-stereo VLC used.\n");
229 } 247 }
230 248
231 av_log(q->avctx,AV_LOG_DEBUG,"VLC tables initialized.\n"); 249 av_log(NULL,AV_LOG_ERROR,"VLC tables initialized. Result = %d\n",result);
232 return result; 250 return result;
233} 251}
234 252
@@ -249,8 +267,8 @@ static av_cold int init_cook_mlt(COOKContext *q) {
249 av_free(q->mlt_window); 267 av_free(q->mlt_window);
250 return -1; 268 return -1;
251 } 269 }
252 av_log(q->avctx,AV_LOG_DEBUG,"MDCT initialized, order = %d.\n", 270 av_log(NULL,AV_LOG_ERROR,"MDCT initialized, order = %d. mlt_window = %d\n",
253 av_log2(mlt_size)+1); 271 av_log2(mlt_size)+1,sizeof(q->mlt_window)*mlt_size);
254 272
255 return 0; 273 return 0;
256} 274}
@@ -317,11 +335,11 @@ static inline int decode_bytes(const uint8_t* inbuffer, uint8_t* out, int bytes)
317 * Cook uninit 335 * Cook uninit
318 */ 336 */
319 337
320static av_cold int cook_decode_close(AVCodecContext *avctx) 338static av_cold int cook_decode_close(COOKContext *q)
321{ 339{
322 int i; 340 int i;
323 COOKContext *q = avctx->priv_data; 341 //COOKContext *q = avctx->priv_data;
324 av_log(avctx,AV_LOG_DEBUG, "Deallocating memory.\n"); 342 av_log(NULL,AV_LOG_ERROR, "Deallocating memory.\n");
325 343
326 /* Free allocated memory buffers. */ 344 /* Free allocated memory buffers. */
327 av_free(q->mlt_window); 345 av_free(q->mlt_window);
@@ -341,7 +359,7 @@ static av_cold int cook_decode_close(AVCodecContext *avctx)
341 free_vlc(&q->ccpl); 359 free_vlc(&q->ccpl);
342 } 360 }
343 361
344 av_log(avctx,AV_LOG_DEBUG,"Memory deallocated.\n"); 362 av_log(NULL,AV_LOG_ERROR,"Memory deallocated.\n");
345 363
346 return 0; 364 return 0;
347} 365}
@@ -864,6 +882,8 @@ static void joint_decode(COOKContext *q, float* mlt_buffer1,
864 * @param gain_ptr array of current/prev gain pointers 882 * @param gain_ptr array of current/prev gain pointers
865 */ 883 */
866 884
885#define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
886
867static inline void 887static inline void
868decode_bytes_and_gain(COOKContext *q, const uint8_t *inbuffer, 888decode_bytes_and_gain(COOKContext *q, const uint8_t *inbuffer,
869 cook_gains *gains_ptr) 889 cook_gains *gains_ptr)
@@ -938,9 +958,9 @@ static int decode_subpacket(COOKContext *q, const uint8_t *inbuffer,
938 int sub_packet_size, int16_t *outbuffer) { 958 int sub_packet_size, int16_t *outbuffer) {
939 /* packet dump */ 959 /* packet dump */
940// for (i=0 ; i<sub_packet_size ; i++) { 960// for (i=0 ; i<sub_packet_size ; i++) {
941// av_log(q->avctx, AV_LOG_ERROR, "%02x", inbuffer[i]); 961// av_log(NULL, AV_LOG_ERROR, "%02x", inbuffer[i]);
942// } 962// }
943// av_log(q->avctx, AV_LOG_ERROR, "\n"); 963// av_log(NULL, AV_LOG_ERROR, "\n");
944 964
945 decode_bytes_and_gain(q, inbuffer, &q->gains1); 965 decode_bytes_and_gain(q, inbuffer, &q->gains1);
946 966
@@ -974,37 +994,37 @@ static int decode_subpacket(COOKContext *q, const uint8_t *inbuffer,
974/** 994/**
975 * Cook frame decoding 995 * Cook frame decoding
976 * 996 *
977 * @param avctx pointer to the AVCodecContext 997 * @param rmctx pointer to the RMContext
978 */ 998 */
979 999
980static int cook_decode_frame(AVCodecContext *avctx, 1000static int cook_decode_frame(RMContext *rmctx,COOKContext *q,
981 void *data, int *data_size, 1001 int16_t *outbuffer, int *data_size,
982 const uint8_t *buf, int buf_size) { 1002 const uint8_t *inbuffer, int buf_size) {
983 COOKContext *q = avctx->priv_data; 1003 //COOKContext *q = avctx->priv_data;
1004 //COOKContext *q;
984 1005
985 if (buf_size < avctx->block_align) 1006 if (buf_size < rmctx->block_align)
986 return buf_size; 1007 return buf_size;
987 1008
988 *data_size = decode_subpacket(q, buf, avctx->block_align, data); 1009 *data_size = decode_subpacket(q, inbuffer, rmctx->block_align, outbuffer);
989 1010
990 /* Discard the first two frames: no valid audio. */ 1011 /* Discard the first two frames: no valid audio. */
991 if (avctx->frame_number < 2) *data_size = 0; 1012 if (rmctx->frame_number < 2) *data_size = 0;
992 1013
993 return avctx->block_align; 1014 return rmctx->block_align;
994} 1015}
995 1016
996#ifdef COOKDEBUG 1017#ifdef COOKDEBUG
997static void dump_cook_context(COOKContext *q) 1018static void dump_cook_context(COOKContext *q)
998{ 1019{
999 //int i=0; 1020 //int i=0;
1000#define PRINT(a,b) av_log(q->avctx,AV_LOG_ERROR," %s = %d\n", a, b); 1021#define PRINT(a,b) av_log(NULL,AV_LOG_ERROR," %s = %d\n", a, b);
1001 av_log(q->avctx,AV_LOG_ERROR,"COOKextradata\n"); 1022 av_log(NULL,AV_LOG_ERROR,"COOKextradata\n");
1002 av_log(q->avctx,AV_LOG_ERROR,"cookversion=%x\n",q->cookversion); 1023 av_log(NULL,AV_LOG_ERROR,"cookversion=%x\n",q->cookversion);
1003 if (q->cookversion > STEREO) { 1024 if (q->cookversion > STEREO) {
1004 PRINT("js_subband_start",q->js_subband_start); 1025 PRINT("js_subband_start",q->js_subband_start);
1005 PRINT("js_vlc_bits",q->js_vlc_bits); 1026 PRINT("js_vlc_bits",q->js_vlc_bits);
1006 } 1027 }
1007 av_log(q->avctx,AV_LOG_ERROR,"COOKContext\n");
1008 PRINT("nb_channels",q->nb_channels); 1028 PRINT("nb_channels",q->nb_channels);
1009 PRINT("bit_rate",q->bit_rate); 1029 PRINT("bit_rate",q->bit_rate);
1010 PRINT("sample_rate",q->sample_rate); 1030 PRINT("sample_rate",q->sample_rate);
@@ -1031,76 +1051,60 @@ static av_cold int cook_count_channels(unsigned int mask){
1031 1051
1032/** 1052/**
1033 * Cook initialization 1053 * Cook initialization
1034 *
1035 * @param avctx pointer to the AVCodecContext
1036 */ 1054 */
1037 1055
1038static av_cold int cook_decode_init(AVCodecContext *avctx) 1056static av_cold int cook_decode_init(RMContext *rmctx, COOKContext *q)
1039{ 1057{
1040 COOKContext *q = avctx->priv_data; 1058 /* cook extradata */
1041 const uint8_t *edata_ptr = avctx->extradata; 1059 q->cookversion = rmctx->cook_version;
1042 q->avctx = avctx; 1060 q->samples_per_frame = rmctx->samples_pf_pc;
1043 1061 q->subbands = rmctx->nb_subbands;
1044 /* Take care of the codec specific extradata. */ 1062 q->extradata_size = rmctx->extradata_size;
1045 if (avctx->extradata_size <= 0) { 1063 if (q->extradata_size >= 16){
1046 av_log(avctx,AV_LOG_ERROR,"Necessary extradata missing!\n"); 1064 q->js_subband_start = rmctx->js_subband_start;
1047 return -1; 1065 q->js_vlc_bits = rmctx->js_vlc_bits;
1048 } else {
1049 /* 8 for mono, 16 for stereo, ? for multichannel
1050 Swap to right endianness so we don't need to care later on. */
1051 av_log(avctx,AV_LOG_DEBUG,"codecdata_length=%d\n",avctx->extradata_size);
1052 if (avctx->extradata_size >= 8){
1053 q->cookversion = bytestream_get_be32(&edata_ptr);
1054 q->samples_per_frame = bytestream_get_be16(&edata_ptr);
1055 q->subbands = bytestream_get_be16(&edata_ptr);
1056 }
1057 if (avctx->extradata_size >= 16){
1058 bytestream_get_be32(&edata_ptr); //Unknown unused
1059 q->js_subband_start = bytestream_get_be16(&edata_ptr);
1060 q->js_vlc_bits = bytestream_get_be16(&edata_ptr);
1061 }
1062 } 1066 }
1063 1067
1064 /* Take data from the AVCodecContext (RM container). */ 1068 /* Take data from the RMContext (RM container). */
1065 q->sample_rate = avctx->sample_rate; 1069 q->sample_rate = rmctx->sample_rate;
1066 q->nb_channels = avctx->channels; 1070 q->nb_channels = rmctx->nb_channels;
1067 q->bit_rate = avctx->bit_rate; 1071 q->bit_rate = rmctx->bit_rate;
1068 1072
1069 /* Initialize RNG. */ 1073 /* Initialize RNG. */
1070 av_lfg_init(&q->random_state, ff_random_get_seed()); 1074 av_lfg_init(&q->random_state, 1);
1071 1075
1072 /* Initialize extradata related variables. */ 1076 /* Initialize extradata related variables. */
1073 q->samples_per_channel = q->samples_per_frame / q->nb_channels; 1077 q->samples_per_channel = q->samples_per_frame / q->nb_channels;
1074 q->bits_per_subpacket = avctx->block_align * 8; 1078 q->bits_per_subpacket = rmctx->block_align * 8;
1075 1079
1076 /* Initialize default data states. */ 1080 /* Initialize default data states. */
1077 q->log2_numvector_size = 5; 1081 q->log2_numvector_size = 5;
1078 q->total_subbands = q->subbands; 1082 q->total_subbands = q->subbands;
1079 1083
1080 /* Initialize version-dependent variables */ 1084 /* Initialize version-dependent variables */
1081 av_log(avctx,AV_LOG_DEBUG,"q->cookversion=%x\n",q->cookversion); 1085 av_log(NULL,AV_LOG_DEBUG,"q->cookversion=%x\n",q->cookversion);
1082 q->joint_stereo = 0; 1086 q->joint_stereo = 0;
1083 switch (q->cookversion) { 1087 switch (q->cookversion) {
1084 case MONO: 1088 case MONO:
1085 if (q->nb_channels != 1) { 1089 if (q->nb_channels != 1) {
1086 av_log(avctx,AV_LOG_ERROR,"Container channels != 1, report sample!\n"); 1090 av_log(NULL,AV_LOG_ERROR,"Container channels != 1, report sample!\n");
1087 return -1; 1091 return -1;
1088 } 1092 }
1089 av_log(avctx,AV_LOG_DEBUG,"MONO\n"); 1093 av_log(NULL,AV_LOG_DEBUG,"MONO\n");
1090 break; 1094 break;
1091 case STEREO: 1095 case STEREO:
1092 if (q->nb_channels != 1) { 1096 if (q->nb_channels != 1) {
1093 q->bits_per_subpacket = q->bits_per_subpacket/2; 1097 q->bits_per_subpacket = q->bits_per_subpacket/2;
1094 } 1098 }
1095 av_log(avctx,AV_LOG_DEBUG,"STEREO\n"); 1099 av_log(NULL,AV_LOG_DEBUG,"STEREO\n");
1096 break; 1100 break;
1097 case JOINT_STEREO: 1101 case JOINT_STEREO:
1098 if (q->nb_channels != 2) { 1102 if (q->nb_channels != 2) {
1099 av_log(avctx,AV_LOG_ERROR,"Container channels != 2, report sample!\n"); 1103 av_log(NULL,AV_LOG_ERROR,"Container channels != 2, report sample!\n");
1100 return -1; 1104 return -1;
1101 } 1105 }
1102 av_log(avctx,AV_LOG_DEBUG,"JOINT_STEREO\n"); 1106 av_log(NULL,AV_LOG_ERROR,"JOINT_STEREO\n");
1103 if (avctx->extradata_size >= 16){ 1107 if (q->extradata_size >= 16){
1104 q->total_subbands = q->subbands + q->js_subband_start; 1108 q->total_subbands = q->subbands + q->js_subband_start;
1105 q->joint_stereo = 1; 1109 q->joint_stereo = 1;
1106 } 1110 }
@@ -1112,11 +1116,11 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
1112 } 1116 }
1113 break; 1117 break;
1114 case MC_COOK: 1118 case MC_COOK:
1115 av_log(avctx,AV_LOG_ERROR,"MC_COOK not supported!\n"); 1119 av_log(NULL,AV_LOG_ERROR,"MC_COOK not supported!\n");
1116 return -1; 1120 return -1;
1117 break; 1121 break;
1118 default: 1122 default:
1119 av_log(avctx,AV_LOG_ERROR,"Unknown Cook version, report sample!\n"); 1123 av_log(NULL,AV_LOG_ERROR,"Unknown Cook version, report sample!\n");
1120 return -1; 1124 return -1;
1121 break; 1125 break;
1122 } 1126 }
@@ -1133,22 +1137,24 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
1133 return -1; 1137 return -1;
1134 1138
1135 1139
1136 if(avctx->block_align >= UINT_MAX/2) 1140 if(q->block_align >= UINT_MAX/2)
1137 return -1; 1141 return -1;
1138 1142
1139 /* Pad the databuffer with: 1143 /* Pad the databuffer with:
1140 DECODE_BYTES_PAD1 or DECODE_BYTES_PAD2 for decode_bytes(), 1144 DECODE_BYTES_PAD1 or DECODE_BYTES_PAD2 for decode_bytes(),
1141 FF_INPUT_BUFFER_PADDING_SIZE, for the bitstreamreader. */ 1145 INPUT_BUFFER_PADDING_SIZE, for the bitstreamreader. */
1146
1147#define INPUT_BUFFER_PADDING_SIZE 8
1142 if (q->nb_channels==2 && q->joint_stereo==0) { 1148 if (q->nb_channels==2 && q->joint_stereo==0) {
1143 q->decoded_bytes_buffer = 1149 q->decoded_bytes_buffer =
1144 av_mallocz(avctx->block_align/2 1150 av_mallocz(rmctx->block_align/2
1145 + DECODE_BYTES_PAD2(avctx->block_align/2) 1151 + DECODE_BYTES_PAD2(q->block_align/2)
1146 + FF_INPUT_BUFFER_PADDING_SIZE); 1152 + INPUT_BUFFER_PADDING_SIZE);
1147 } else { 1153 } else {
1148 q->decoded_bytes_buffer = 1154 q->decoded_bytes_buffer =
1149 av_mallocz(avctx->block_align 1155 av_mallocz(rmctx->block_align
1150 + DECODE_BYTES_PAD1(avctx->block_align) 1156 + DECODE_BYTES_PAD1(q->block_align)
1151 + FF_INPUT_BUFFER_PADDING_SIZE); 1157 + INPUT_BUFFER_PADDING_SIZE);
1152 } 1158 }
1153 if (q->decoded_bytes_buffer == NULL) 1159 if (q->decoded_bytes_buffer == NULL)
1154 return -1; 1160 return -1;
@@ -1173,25 +1179,23 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
1173 1179
1174 /* Try to catch some obviously faulty streams, othervise it might be exploitable */ 1180 /* Try to catch some obviously faulty streams, othervise it might be exploitable */
1175 if (q->total_subbands > 53) { 1181 if (q->total_subbands > 53) {
1176 av_log(avctx,AV_LOG_ERROR,"total_subbands > 53, report sample!\n"); 1182 av_log(NULL,AV_LOG_ERROR,"total_subbands > 53, report sample!\n");
1177 return -1; 1183 return -1;
1178 } 1184 }
1179 if (q->subbands > 50) { 1185 if (q->subbands > 50) {
1180 av_log(avctx,AV_LOG_ERROR,"subbands > 50, report sample!\n"); 1186 av_log(NULL,AV_LOG_ERROR,"subbands > 50, report sample!\n");
1181 return -1; 1187 return -1;
1182 } 1188 }
1183 if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512) || (q->samples_per_channel == 1024)) { 1189 if ((q->samples_per_channel == 256) || (q->samples_per_channel == 512) || (q->samples_per_channel == 1024)) {
1184 } else { 1190 } else {
1185 av_log(avctx,AV_LOG_ERROR,"unknown amount of samples_per_channel = %d, report sample!\n",q->samples_per_channel); 1191 av_log(NULL,AV_LOG_ERROR,"unknown amount of samples_per_channel = %d, report sample!\n",q->samples_per_channel);
1186 return -1; 1192 return -1;
1187 } 1193 }
1188 if ((q->js_vlc_bits > 6) || (q->js_vlc_bits < 0)) { 1194 if ((q->js_vlc_bits > 6) || (q->js_vlc_bits < 0)) {
1189 av_log(avctx,AV_LOG_ERROR,"q->js_vlc_bits = %d, only >= 0 and <= 6 allowed!\n",q->js_vlc_bits); 1195 av_log(NULL,AV_LOG_ERROR,"q->js_vlc_bits = %d, only >= 0 and <= 6 allowed!\n",q->js_vlc_bits);
1190 return -1; 1196 return -1;
1191 } 1197 }
1192 1198
1193 avctx->sample_fmt = SAMPLE_FMT_S16;
1194 avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
1195 1199
1196#ifdef COOKDEBUG 1200#ifdef COOKDEBUG
1197 dump_cook_context(q); 1201 dump_cook_context(q);
@@ -1200,14 +1204,86 @@ static av_cold int cook_decode_init(AVCodecContext *avctx)
1200} 1204}
1201 1205
1202 1206
1203AVCodec cook_decoder = 1207int main(int argc, char *argv[])
1204{ 1208{
1205 .name = "cook", 1209 int fd, fd_dec;
1206 .type = CODEC_TYPE_AUDIO, 1210 int res, datasize,x,i;
1207 .id = CODEC_ID_COOK, 1211 int nb_frames = 0;
1208 .priv_data_size = sizeof(COOKContext), 1212#ifdef DUMP_RAW_FRAMES
1209 .init = cook_decode_init, 1213 char filename[15];
1210 .close = cook_decode_close, 1214 int fd_out;
1211 .decode = cook_decode_frame, 1215#endif
1212 .long_name = NULL_IF_CONFIG_SMALL("COOK"), 1216 int16_t outbuf[2048];
1213}; 1217 uint8_t inbuf[1024];
1218 uint16_t fs,sps,h;
1219 uint32_t packet_count;
1220 COOKContext q;
1221 RMContext rmctx;
1222 RMPacket pkt;
1223
1224 memset(&q,0,sizeof(COOKContext));
1225 memset(&rmctx,0,sizeof(RMContext));
1226 memset(&pkt,0,sizeof(RMPacket));
1227
1228 if (argc != 2) {
1229 av_log(NULL,AV_LOG_ERROR,"Incorrect number of arguments\n");
1230 return -1;
1231 }
1232
1233 fd = open(argv[1],O_RDONLY);
1234 if (fd < 0) {
1235 av_log(NULL,AV_LOG_ERROR,"Error opening file %s\n", argv[1]);
1236 return -1;
1237 }
1238
1239 fd_dec = open_wav("output.wav");
1240 if (fd_dec < 0) {
1241 av_log(NULL,AV_LOG_ERROR,"Error creating output file\n");
1242 return -1;
1243 }
1244 res = real_parse_header(fd, &rmctx);
1245 packet_count = rmctx.nb_packets;
1246 rmctx.audio_framesize = rmctx.block_align;
1247 rmctx.block_align = rmctx.sub_packet_size;
1248 fs = rmctx.audio_framesize;
1249 sps= rmctx.block_align;
1250 h = rmctx.sub_packet_h;
1251 cook_decode_init(&rmctx,&q);
1252 av_log(NULL,AV_LOG_ERROR,"nb_frames = %d\n",nb_frames);
1253 x = 0;
1254 if(packet_count % h)
1255 {
1256 packet_count += h - (packet_count % h);
1257 rmctx.nb_packets = packet_count;
1258 }
1259 while(packet_count)
1260 {
1261
1262 memset(pkt.data,0,sizeof(pkt.data));
1263 rm_get_packet(fd, &rmctx, &pkt);
1264 DEBUGF("total frames = %d packet count = %d output counter = %d \n",rmctx.audio_pkt_cnt*(fs/sps), packet_count,rmctx.audio_pkt_cnt);
1265 for(i = 0; i < rmctx.audio_pkt_cnt*(fs/sps) ; i++)
1266 {
1267 /* output raw audio frames that are sent to the decoder into separate files */
1268 #ifdef DUMP_RAW_FRAMES
1269 snprintf(filename,sizeof(filename),"dump%d.raw",++x);
1270 fd_out = open(filename,O_WRONLY|O_CREAT|O_APPEND);
1271 write(fd_out,pkt.data+i*sps,sps);
1272 close(fd_out);
1273 #endif
1274
1275 memcpy(inbuf,pkt.data+i*sps,sps);
1276 nb_frames = cook_decode_frame(&rmctx,&q, outbuf, &datasize, inbuf , rmctx.block_align);
1277 rmctx.frame_number++;
1278 write(fd_dec,outbuf,datasize);
1279 }
1280 packet_count -= rmctx.audio_pkt_cnt;
1281 rmctx.audio_pkt_cnt = 0;
1282 }
1283 cook_decode_close(&q);
1284 close_wav(fd_dec,&rmctx);
1285 close(fd);
1286
1287
1288 return 0;
1289}