summaryrefslogtreecommitdiffstats
path: root/tinyDAV/src/codecs/amr/tdav_codec_amr.c
blob: 9304f85ec7df52dfd7e8443408b7a58c0a85ec3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.org>
*	
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*	
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*	
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/

/**@file tdav_codec_amr.c
 * @brief AMR-NB and AMR-WB codecs.
 * RTP payloader/depayloader are based on RFC 4867
 *
 * @author Mamadou Diop <diopmamadou(at)doubango.org>
 *

 */
#include "tinydav/codecs/amr/tdav_codec_amr.h"

#include "tsk_params.h"
#include "tsk_string.h"
#include "tsk_memory.h"
#include "tsk_debug.h"

#include <stdlib.h> /* atoi() */

#if HAVE_OPENCORE_AMR

#if defined(_MSC_VER)
#	pragma comment(lib, "..\\thirdparties\\win32\\lib\\opencore\\libopencore-amrnb.a")
#endif

#define NO_DATA 15
#define DEFAULT_ENC_MODE ((enum Mode)MR122) /* Higher, could be changed by remote party by using CMR */

/* From WmfDecBytesPerFrame in dec_input_format_tab.cpp */
static const int tdav_codec_amr_nb_sizes[] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 6, 5, 5, 0, 0, 0, 0 };
/* From pvamrwbdecoder_api.h, by dividing by 8 and rounding up */
static const int tdav_codec_amr_wb_sizes[] = { 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, -1, -1, -1, -1, -1, -1 };

/* ============ Common ================= */
static int tdav_codec_amr_init(tdav_codec_amr_t* self, tdav_codec_amr_type_t type, tdav_codec_amr_mode_t mode);
static int tdav_codec_amr_deinit(tdav_codec_amr_t* self);
static tdav_codec_amr_mode_t tdav_codec_amr_get_mode(const char* fmtp);
static int tdav_codec_amr_parse_fmtp(tdav_codec_amr_t* self, const char* fmtp);
static tsk_size_t tdav_codec_amr_oa_decode(tdav_codec_amr_t* self, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size, const tsk_object_t* proto_hdr);
static tsk_size_t tdav_codec_amr_be_decode(tdav_codec_amr_t* self, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size, const tsk_object_t* proto_hdr);
static tsk_size_t tdav_codec_amr_be_encode(tdav_codec_amr_t* amr, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size);
static tsk_size_t tdav_codec_amr_oa_encode(tdav_codec_amr_t* amr, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size);
static uint8_t tdav_codec_amr_bitbuffer_read(const void* bits, tsk_size_t size, tsk_size_t start, tsk_size_t count);

/* ============ AMR-NB Plugin interface ================= 
	The AMR codec was originally developed and standardized by the
	European Telecommunications Standards Institute (ETSI) for GSM
	cellular systems.  It is now chosen by the Third Generation
	Partnership Project (3GPP) as the mandatory codec for third
	generation (3G) cellular systems [1].

	The AMR codec is a multi-mode codec that supports eight narrow band
	speech encoding modes with bit rates between 4.75 and 12.2 kbps.  The
	sampling frequency used in AMR is 8000 Hz and the speech encoding is
	performed on 20 ms speech frames.  Therefore, each encoded AMR speech
	frame represents 160 samples of the original speech.

	Among the eight AMR encoding modes, three are already separately
	adopted as standards of their own.  Particularly, the 6.7 kbps mode
	is adopted as PDC-EFR [18], the 7.4 kbps mode as IS-641 codec in TDMA
	[17], and the 12.2 kbps mode as GSM-EFR [16].
*/

int tdav_codec_amrnb_open(tmedia_codec_t* self)
{
	tdav_codec_amr_t* amrnb = (tdav_codec_amr_t*)self;

	if(!TDAV_CODEC_AMR(amrnb)->encoder){
		if(!(TDAV_CODEC_AMR(amrnb)->encoder = Encoder_Interface_init(0))){
			TSK_DEBUG_ERROR("Failed to initialize AMR-NB encoder");
			return -2;
		}
	}

	if(!TDAV_CODEC_AMR(amrnb)->decoder){
		if(!(TDAV_CODEC_AMR(amrnb)->decoder = Decoder_Interface_init())){
			TSK_DEBUG_ERROR("Failed to initialize AMR-NB encoder");
			return -2;
		}
	}

	return 0;
}

int tdav_codec_amrnb_close(tmedia_codec_t* self)
{
	tdav_codec_amr_t* amrnb = (tdav_codec_amr_t*)self;

	if(TDAV_CODEC_AMR(amrnb)->encoder){
		Encoder_Interface_exit(TDAV_CODEC_AMR(amrnb)->encoder);
		TDAV_CODEC_AMR(amrnb)->encoder = tsk_null;
	}

	if(TDAV_CODEC_AMR(amrnb)->decoder){
		Decoder_Interface_exit(TDAV_CODEC_AMR(amrnb)->decoder);
		TDAV_CODEC_AMR(amrnb)->decoder = tsk_null;
	}

	return 0;
}

tsk_size_t tdav_codec_amrnb_encode(tmedia_codec_t* self, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size)
{	
	tdav_codec_amr_t* amr = (tdav_codec_amr_t*)self;
	
	switch(amr->mode){
		case tdav_codec_amr_mode_be:
			return tdav_codec_amr_be_encode(amr, in_data, in_size, out_data, out_max_size);
		default:
			return tdav_codec_amr_oa_encode(amr, in_data, in_size, out_data, out_max_size); 
	}
	
	return 0;
}

tsk_size_t tdav_codec_amrnb_decode(tmedia_codec_t* self, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size, const tsk_object_t* proto_hdr)
{
	tdav_codec_amr_t* amr = (tdav_codec_amr_t*)self;

	switch(amr->mode){
		case tdav_codec_amr_mode_be:
			return tdav_codec_amr_be_decode(amr, in_data, in_size, out_data, out_max_size, proto_hdr);
		default:
			return tdav_codec_amr_oa_decode(amr, in_data, in_size, out_data, out_max_size, proto_hdr); 
	}
}

char* tdav_codec_amrnb_sdp_att_get(const tmedia_codec_t* codec, const char* att_name)
{
	const tdav_codec_amr_t* amr = (const tdav_codec_amr_t*)codec;

	/* We support all modes, all ... */
	if(amr){
		switch(amr->mode){
			case tdav_codec_amr_mode_be:
				return tsk_strdup("octet-align=0");
			default:
				return tsk_strdup("octet-align=1");
		}
	}
	return tsk_null;
}

tsk_bool_t tdav_codec_amrnb_sdp_att_match(const tmedia_codec_t* codec, const char* att_name, const char* att_value)
{	
	tdav_codec_amr_t* amr;
	if(!(amr = (tdav_codec_amr_t*)codec) || !att_name){
		TSK_DEBUG_ERROR("Invalid parameter");
		return tsk_false;
	}

	if(amr && tsk_striequals(att_name, "fmtp")){
		/* Match mode */
		if(tdav_codec_amr_get_mode(att_value) != amr->mode){
			TSK_DEBUG_INFO("Failed to match [%s]", att_value);
			return tsk_false;
		}
		/* check parameters validity */
		if(tdav_codec_amr_parse_fmtp(amr, att_value)){
			TSK_DEBUG_INFO("Failed to match [%s]", att_value);
			return tsk_false;
		}

		return tsk_true;
	}
	return tsk_false;
}


//
//	AMR-NB OA Plugin definition
//

/* constructor */
static tsk_object_t* tdav_codec_amrnb_oa_ctor(tsk_object_t * self, va_list * app)
{
	tdav_codec_amr_t *amrnb_oa = self;
	if(amrnb_oa){
		/* init base: called by tmedia_codec_create() */
		/* init self */
		tdav_codec_amr_init(TDAV_CODEC_AMR(amrnb_oa), tdav_codec_amr_type_nb, tdav_codec_amr_mode_oa);
	}
	return self;
}
/* destructor */
static tsk_object_t* tdav_codec_amrnb_oa_dtor(tsk_object_t * self)
{ 
	tdav_codec_amr_t *amrnb_oa = self;
	if(amrnb_oa){
		/* deinit base */
		tmedia_codec_audio_deinit(amrnb_oa);
		/* deinit self */
		tdav_codec_amr_deinit(TDAV_CODEC_AMR(amrnb_oa));
	}

	return self;
}
/* object definition */
static const tsk_object_def_t tdav_codec_amrnb_oa_def_s = 
{
	sizeof(tdav_codec_amr_t),
	tdav_codec_amrnb_oa_ctor, 
	tdav_codec_amrnb_oa_dtor,
	tmedia_codec_cmp, 
};
/* plugin definition*/
static const tmedia_codec_plugin_def_t tdav_codec_amrnb_oa_plugin_def_s = 
{
	&tdav_codec_amrnb_oa_def_s,

	tmedia_audio,
	tmedia_codec_id_amr_nb_oa,
	"AMR",
	"AMR Narrow Band - Octet Aligned (libopencore-amr)",
	TMEDIA_CODEC_FORMAT_AMR_NB_OA,
	tsk_true,
	8000, // rate
	
	{ /* audio */
		1, // channels
		20 // ptime
	},

	/* video */
	{0},

	tsk_null, // set()
	tdav_codec_amrnb_open,
	tdav_codec_amrnb_close,
	tdav_codec_amrnb_encode,
	tdav_codec_amrnb_decode,
	tdav_codec_amrnb_sdp_att_match,
	tdav_codec_amrnb_sdp_att_get
};
const tmedia_codec_plugin_def_t *tdav_codec_amrnb_oa_plugin_def_t = &tdav_codec_amrnb_oa_plugin_def_s;

//
//	AMR-NB BE Plugin definition
//

/* constructor */
static tsk_object_t* tdav_codec_amrnb_be_ctor(tsk_object_t * self, va_list * app)
{
	tdav_codec_amr_t *amrnb_be = self;
	if(amrnb_be){
		/* init base: called by tmedia_codec_create() */
		/* init self */
		tdav_codec_amr_init(TDAV_CODEC_AMR(amrnb_be), tdav_codec_amr_type_nb, tdav_codec_amr_mode_be);
	}
	return self;
}
/* destructor */
static tsk_object_t* tdav_codec_amrnb_be_dtor(tsk_object_t * self)
{ 
	tdav_codec_amr_t *amrnb_be = self;
	if(amrnb_be){
		/* deinit base */
		tmedia_codec_audio_deinit(amrnb_be);
		/* deinit self */
		tdav_codec_amr_deinit(TDAV_CODEC_AMR(amrnb_be));
	}

	return self;
}
/* object definition */
static const tsk_object_def_t tdav_codec_amrnb_be_def_s = 
{
	sizeof(tdav_codec_amr_t),
	tdav_codec_amrnb_be_ctor, 
	tdav_codec_amrnb_be_dtor,
	tmedia_codec_cmp, 
};
/* plugin definition*/
static const tmedia_codec_plugin_def_t tdav_codec_amrnb_be_plugin_def_s = 
{
	&tdav_codec_amrnb_be_def_s,

	tmedia_audio,
	tmedia_codec_id_amr_nb_be,
	"AMR",
	"AMR Narrow Band - Bandwidth-Efficient (libopencore-amr)",
	TMEDIA_CODEC_FORMAT_AMR_NB_BE,
	tsk_true,
	8000, // rate
	
	{ /* audio */
		1, // channels
		0 // ptime @deprecated
	},

	/* video */
	{0},

	tsk_null, // set()
	tdav_codec_amrnb_open,
	tdav_codec_amrnb_close,
	tdav_codec_amrnb_encode,
	tdav_codec_amrnb_decode,
	tdav_codec_amrnb_sdp_att_match,
	tdav_codec_amrnb_sdp_att_get
};
const tmedia_codec_plugin_def_t *tdav_codec_amrnb_be_plugin_def_t = &tdav_codec_amrnb_be_plugin_def_s;









//
// Common functions
//

static int tdav_codec_amr_init(tdav_codec_amr_t* self, tdav_codec_amr_type_t type, tdav_codec_amr_mode_t mode)
{
	if(self){
		self->type = type;
		self->mode = mode;
		self->encoder_mode = DEFAULT_ENC_MODE;

		return 0;
	}
	else{
		TSK_DEBUG_ERROR("Invalid Parameter");
		return -1;
	}
}

static int tdav_codec_amr_deinit(tdav_codec_amr_t* self)
{
	if(self){
		switch(self->type){
			case tdav_codec_amr_type_nb:
				{	/* AMR-NB */
					if(self->encoder){
						Encoder_Interface_exit(self->encoder);
						self->encoder = tsk_null;
					}
					if(self->decoder){
						Decoder_Interface_exit(self->decoder);
						self->decoder = tsk_null;
					}
					break;
				}
			case tdav_codec_amr_type_wb:
				{	/* AMR-WB */
					break;
				}
		}

		return 0;
	}
	else{
		TSK_DEBUG_ERROR("Invalid Parameter");
		return -1;
	}
}

static tdav_codec_amr_mode_t tdav_codec_amr_get_mode(const char* fmtp)
{
	/* RFC 4867 - 8.1.  AMR Media Type Registration
		octet-align: Permissible values are 0 and 1.  If 1, octet-aligned
		operation SHALL be used.  If 0 or if not present, bandwidth-efficient operation is employed.
	*/
	tdav_codec_amr_mode_t mode = tdav_codec_amr_mode_be;
	tsk_size_t size = tsk_strlen(fmtp);
	int start, end;
	
	if((start = tsk_strindexOf(fmtp, size, "octet-align")) !=-1){
		tsk_param_t* param;
		if((end = tsk_strindexOf((fmtp+start), (size-start), ";")) == -1){
			end = size;
		}
		if((param = tsk_params_parse_param((fmtp+start), (end-start)))){
			if(param->value && tsk_strequals(param->value, "1")){
				mode = tdav_codec_amr_mode_oa;
			}
			TSK_OBJECT_SAFE_FREE(param);
		}
	}
	return mode;
}

int tdav_codec_amr_parse_fmtp(tdav_codec_amr_t* self, const char* fmtp)
{
	int ret = 0;
	int val_int;
	const char* val_str;
	//--tdav_codec_amr_mode_t mode = self->mode;
	tsk_params_L_t* params = tsk_null;

	if((params = tsk_params_fromstring(fmtp, ";", tsk_true))){
		/* Do not check "octet-align" => already done by the caller of this function */
		
		/* === mode-set ===*/
		if((val_str = tsk_params_get_param_value(params, "mode-set"))){
			char* modes = tsk_strdup(val_str);
			char *pch, *saveptr;
			int mode_int;
			pch = tsk_strtok_r(modes, ", ", &saveptr);
			while(pch){
				mode_int = atoi(pch);
				self->modes |= 0x0001 << mode_int;
				pch = tsk_strtok_r(tsk_null, ", ", &saveptr);
			}
			
			TSK_FREE(modes);
		}
		else{
			self->modes = 0xFFFF;
		}

		/* === interleaving ===*/
		if((val_int = tsk_params_get_param_value_as_int(params, "interleaving")) != -1){
			TSK_DEBUG_WARN("interleaving not supported");
			ret = -1; goto bail;
		}
		/* === mode-change-period ===*/
		if((val_int = tsk_params_get_param_value_as_int(params, "mode-change-period")) != -1){
			if(val_int != 1 && val_int != 2){
				TSK_DEBUG_ERROR("Invalid [mode-change-period]");
				ret = -1; goto bail;
			}
			self->mcp = (unsigned)val_int;
		}
		/* === mode-change-capability ===*/
		if((val_int = tsk_params_get_param_value_as_int(params, "mode-change-capability")) != -1){
			if(val_int != 1 && val_int != 2){
				TSK_DEBUG_ERROR("Invalid [mode-change-capability]");
				ret = -1; goto bail;
			}
			self->mcc = (unsigned)val_int;
		}
		/* === mode-change-neighbor ===*/
		if((val_int = tsk_params_get_param_value_as_int(params, "mode-change-neighbor")) != -1){
			if(val_int != 0 && val_int != 1){
				TSK_DEBUG_ERROR("Invalid [mode-change-neighbor]");
				ret = -1; goto bail;
			}
			self->mcn = (unsigned)val_int;
		}
	}

bail:
	TSK_OBJECT_SAFE_FREE(params);
	return ret;
}


/* RFC 4867 - 4.2.  Payload Structure
	+----------------+-------------------+----------------
	| payload header | table of contents | speech data ...
	+----------------+-------------------+----------------
*/
/*	RFC 4867 - 4.4.2.  The Payload Table of Contents and Frame CRCs
		The table of contents (ToC) consists of a list of ToC entries, each representing a speech frame.
		+---------------------+
		| list of ToC entries |
		+---------------------+
		| list of frame CRCs  | (optional)
		- - - - - - - - - - -
		Note, for ToC entries with FT=14 or 15, there will be no
		corresponding speech frame or frame CRC present in the payload.
*/


static tsk_size_t tdav_codec_amr_be_encode(tdav_codec_amr_t* amr, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size)
{	
	tsk_size_t out_size = 0, i;
	int ret_size;
	uint8_t ToC;
	static uint8_t CMR = NO_DATA /* No interleaving */;

	uint8_t outbuf[60 + 1]; /* enought for both NB and WB at ptime=20ms */
	if(!amr || !in_data || !in_size || !out_data || (amr->mode != tdav_codec_amr_mode_be)){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}

	/* Encode */
	if((ret_size = Encoder_Interface_Encode(amr->encoder, amr->encoder_mode, in_data, outbuf, 0)) <= 0){
		TSK_DEBUG_ERROR("Encoder_Interface_Encode() failed");
		goto bail;
	}
	
	
	/* allocate output buffer */
	if((int)*out_max_size <ret_size){
		if(!(*out_data = tsk_realloc(*out_data, ret_size))){
			*out_max_size = 0;
			TSK_DEBUG_ERROR("Failed to allocate new buffer");
			goto bail;
		}
		*out_max_size = ret_size;
	}

	out_size = ret_size;

	/* CMR (4bits) */
	((uint8_t*)*out_data)[0] = (CMR<<4);
	/* ToC (Always ONE Frame, don't need to test for [F]) (6bits)*/
	ToC = outbuf[0]>>2/*2*[P]*/;
	((uint8_t*)*out_data)[0] |=  (ToC >> 2/*[Q],[1-FT]*/) & 0xF; /* 4bits */
	((uint8_t*)*out_data)[1] = (ToC & 0x3/*[1-FT],[Q]*/)<<6; /* 2bits */

	/* === THERE ARE 2 EXTRA BITS === */

	for(i=1; i<out_size-1; i++){
		((uint8_t*)*out_data)[i] |= outbuf[i]>>2;/* 6bits */
		((uint8_t*)*out_data)[i+1] = outbuf[i]<<6;/* 2bits */
	}

bail:
	return out_size;
}

tsk_size_t tdav_codec_amr_be_decode(tdav_codec_amr_t* amr, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size, const tsk_object_t* proto_hdr)
{
	tsk_size_t out_size = 0, pcm_frame_size = 0, index = 0;
	const uint8_t* pdata = (const uint8_t*)in_data;
	//--const uint8_t* pend = (pdata + in_size);
	uint8_t CMR;
	int toc_entries = 0, i, k; // ToC entries count

	if(!amr || !in_data || !in_size || !out_data || (amr->mode != tdav_codec_amr_mode_be)){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}

	/* compute PCM frame size */
	switch(TDAV_CODEC_AMR(amr)->type){
		case tdav_codec_amr_type_nb:
			pcm_frame_size = 160 * sizeof(short);
			break;
		case tdav_codec_amr_type_wb:
			pcm_frame_size = 320 * sizeof(short);
			break;
		default:
			TSK_DEBUG_ERROR("Invalid AMR type");
			return 0;
	}

	/* CMR (4bits) */
	CMR = tdav_codec_amr_bitbuffer_read(in_data, (in_size*8), index, 4);
	index += 4;
	if(CMR != NO_DATA){
		amr->encoder_mode = (enum Mode)CMR;
	}
	
	/* F(1bit), FT(4bits), Q(1bit) */
	/* count ToC entries */
	do{	/* At least ONE ToC */
		++toc_entries;
		++pdata;
		index += 6;
	}
	while((index < (in_size*8)) && (tdav_codec_amr_bitbuffer_read(in_data, (in_size*8), (index-6), 1)/* F */));
	
	for(i = 0; (i<toc_entries && (in_size < (in_size*8))) ; i++){
		int size = -1;
		uint8_t* speech_data = tsk_null;
		//--int speech_data_size = 0;
		uint8_t ToC = tdav_codec_amr_bitbuffer_read(in_data, (in_size*8), 4/*CMR*/ + (i*6), 6);
		
		switch(TDAV_CODEC_AMR(amr)->type){
			case tdav_codec_amr_type_nb:
				size = tdav_codec_amr_nb_sizes[(ToC>>1)&0x0F/* FT */];
				break;
			case tdav_codec_amr_type_wb:
				size = tdav_codec_amr_wb_sizes[(ToC>>1)&0x0F/* FT */];
				break;
		}		
		
		if((speech_data = tsk_calloc((size + 2/* ToC + '\0' */), sizeof(uint8_t)))){
			/* copy ToC */
			speech_data[0] = (ToC & 0x1F)<<2/* 2*[P] */;  /* ToC as OA layout */
			/* copy speech data */
			for(k=0; k<size; k++){
				speech_data[1 + k] = tdav_codec_amr_bitbuffer_read(in_data, (in_size*8), index, 8);
				index+=8;
				if((k==size-1) && (index%8)){
					speech_data[1 + k] <<= (8-(index%8)); //clean
				}
			}
			
			/* allocate/reallocate speech data */
			if(*out_max_size <(out_size + pcm_frame_size)){
				if(!(*out_data = tsk_realloc(*out_data, (out_size + pcm_frame_size)))){
					TSK_DEBUG_ERROR("Failed to allocate new buffer");
					*out_max_size = 0;
					TSK_FREE(speech_data);
					goto bail;
				}
				*out_max_size = out_size + pcm_frame_size;
			}

			/* decode speech data */
			Decoder_Interface_Decode(amr->decoder, speech_data, &((short*)*out_data)[out_size/sizeof(short)], 0);
			out_size += pcm_frame_size, pdata+= size;

			TSK_FREE(speech_data);
		}
	}

bail:
	return out_size;
}

static tsk_size_t tdav_codec_amr_oa_encode(tdav_codec_amr_t* amr, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size)
{	
	tsk_size_t out_size = 0;
	int ret_size;
	static uint8_t CMR = NO_DATA /* No interleaving */;

	uint8_t outbuf[60 + 1]; /* enought for both NB and WB at ptime=20ms */
	if(!amr || !in_data || !in_size || !out_data || (amr->mode != tdav_codec_amr_mode_oa)){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}

	/* Encode */
	if((ret_size = Encoder_Interface_Encode(amr->encoder, amr->encoder_mode, in_data, outbuf, 0)) <= 0){
		TSK_DEBUG_ERROR("Encoder_Interface_Encode() failed");
		goto bail;
	}
	
	out_size = ret_size + 1 /* CMR without interleaving */;
	/* allocate output buffer */
	if(*out_max_size <out_size){
		if(!(*out_data = tsk_realloc(*out_data, out_size))){
			TSK_DEBUG_ERROR("Failed to allocate new buffer");
			*out_max_size = out_size = 0;
			goto bail;
		}
		*out_max_size = out_size;
	}

	/* CMR */
	((uint8_t*)*out_data)[0] = (CMR << 4);
	/* Only ONE ToC --> believe me */
	memcpy(&((uint8_t*)*out_data)[1], outbuf, ret_size);

bail:
	return out_size;
}

static tsk_size_t tdav_codec_amr_oa_decode(tdav_codec_amr_t* amr, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size, const tsk_object_t* proto_hdr)
{
	tsk_size_t out_size = 0, pcm_frame_size = 0;
	const uint8_t* pdata = (const uint8_t*)in_data;
	const uint8_t* pend = (pdata + in_size);
	uint8_t CMR;
	int toc_entries = 0, i; // ToC entries count

	if(!amr || !in_data || !in_size || !out_data || (amr->mode != tdav_codec_amr_mode_oa)){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}

	/* compute PCM frame size */
	switch(TDAV_CODEC_AMR(amr)->type){
		case tdav_codec_amr_type_nb:
			pcm_frame_size = 160 * sizeof(short);
			break;
		case tdav_codec_amr_type_wb:
			pcm_frame_size = 320 * sizeof(short);
			break;
		default:
			TSK_DEBUG_ERROR("Invalid AMR type");
			return 0;
	}	

	/* RFC 4867 - 4.4.  Octet-Aligned Mode
		In octet-aligned mode, the payload header consists of a 4-bit CMR, 4
	   reserved bits, and optionally, an 8-bit interleaving header, as shown
	   below:

		0                   1
		0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
	   +-+-+-+-+-+-+-+-+- - - - - - - -
	   |  CMR  |R|R|R|R|  ILL  |  ILP  |
	   +-+-+-+-+-+-+-+-+- - - - - - - -

	   CMR (4 bits): same as defined in Section 4.3.1.

	   "interleaving" not supported ==> could ignore ILL and ILP (wich are optional)
	*/
	
	CMR = (*pdata++ >> 4);
	if(CMR != NO_DATA){
		/* The codec mode request received in the CMR field is valid until the
		next codec mode request is received, i.e., a newly received CMR value
		corresponding to a speech mode, or NO_DATA overrides the previously
		received CMR value corresponding to a speech mode or NO_DATA. */
		amr->encoder_mode = (enum Mode)CMR; // As we support all modes, do not check for validity
	}
	
	/*	
		A ToC entry takes the following format in octet-aligned mode:
		0 1 2 3 4 5 6 7
		+-+-+-+-+-+-+-+-+
		|F|  FT   |Q|P|P|
		+-+-+-+-+-+-+-+-+

		F (1 bit): see definition in Section 4.3.2.
		FT (4 bits, unsigned integer): see definition in Section 4.3.2.
		Q (1 bit): see definition in Section 4.3.2.
		P bits: padding bits, MUST be set to zero, and MUST be ignored on reception.
	*/	

	/* count ToC entries */
	do{	/* At least ONE ToC */
		++toc_entries;
		++pdata;
	}
	while(pdata && (pdata < pend) && (pdata[-1] >> 7/* F */));

	for(i = 0; (i<toc_entries && (pdata < pend)) ; i++){
		int size = -1;
		uint8_t* speech_data = tsk_null;
		//--int speech_data_size = 0;
		uint8_t ToC = ((const uint8_t*)in_data)[1/*CMR...*/ + i];
		switch(TDAV_CODEC_AMR(amr)->type){
			case tdav_codec_amr_type_nb:
				size = tdav_codec_amr_nb_sizes[(ToC>>3) & 0x0F/* FT */];
				break;
			case tdav_codec_amr_type_wb:
				size = tdav_codec_amr_wb_sizes[(ToC>>3) & 0x0F/* FT */];
				break;
		}
		
		/* check size */
		if(size <0 || ((pdata + size) > pend)){
			TSK_DEBUG_ERROR("Invalid size");
			break;
		}
		
		if((speech_data = tsk_calloc((size + 2/* ToC + '\0' */), sizeof(uint8_t)))){
			/* copy ToC */
			*speech_data = ToC & 0x7F/* with 'F'=0 */;
			/* copy speech data */
			memcpy((speech_data + 1), pdata, size);
			/* allocate/reallocate speech data */
			if(*out_max_size <(out_size + pcm_frame_size)){
				if(!(*out_data = tsk_realloc(*out_data, (out_size + pcm_frame_size)))){
					TSK_DEBUG_ERROR("Failed to allocate new buffer");
					*out_max_size = 0;
					TSK_FREE(speech_data);
					goto bail;
				}
				*out_max_size = (out_size + pcm_frame_size);
			}
			/* decode speech data */
			Decoder_Interface_Decode(amr->decoder, speech_data, &((short*)*out_data)[out_size/sizeof(short)], 0);
			out_size += pcm_frame_size, pdata+= size;

			TSK_FREE(speech_data);
		}
	}

bail:
	return out_size;
}


static uint8_t tdav_codec_amr_bitbuffer_read(const void* bits, tsk_size_t size, tsk_size_t start, tsk_size_t count)
{
	uint8_t byte, left, right, pad;

	if(!bits || !size || count>8){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}
	
	if((start + count) > size){
		count = (size - start);
	}

	pad = start ? (8 - (start % 8)) : count;
	left = ((uint8_t*)bits)[start/8] << (8-pad);
	right = ((uint8_t*)bits)[((start+count)<size ? (start+count) : start)/8] >> pad;

	if((start && (start % 8) != ((start+count)%8)) || (!start && count>8)){
		/* overlap */
		byte = (left | right) >> (8-count);
	}
	else{
		byte = (left | right) & (0xFF >> (8-count));
	}

	return byte;
}

#endif /* HAVE_OPENCORE_AMR */
OpenPOWER on IntegriCloud