Mercurial > libavcodec.hg
comparison libgsm.c @ 4551:c0bf618fbe7e libavcodec
Add support for MS-GSM codec
author | mbardiaux |
---|---|
date | Tue, 20 Feb 2007 11:09:47 +0000 |
parents | c8c591fe26f8 |
children | 4323e587708d |
comparison
equal
deleted
inserted
replaced
4550:03b09678398d | 4551:c0bf618fbe7e |
---|---|
1 /* | 1 /* |
2 * Interface to libgsm for gsm encoding/decoding | 2 * Interface to libgsm for gsm encoding/decoding |
3 * Copyright (c) 2005 Alban Bedel <albeu@free.fr> | 3 * Copyright (c) 2005 Alban Bedel <albeu@free.fr> |
4 * Copyright (c) 2006, 2007 Michel Bardiaux <mbardiaux@mediaxim.be> | |
4 * | 5 * |
5 * This file is part of FFmpeg. | 6 * This file is part of FFmpeg. |
6 * | 7 * |
7 * FFmpeg is free software; you can redistribute it and/or | 8 * FFmpeg is free software; you can redistribute it and/or |
8 * modify it under the terms of the GNU Lesser General Public | 9 * modify it under the terms of the GNU Lesser General Public |
22 /** | 23 /** |
23 * @file libgsm.c | 24 * @file libgsm.c |
24 * Interface to libgsm for gsm encoding/decoding | 25 * Interface to libgsm for gsm encoding/decoding |
25 */ | 26 */ |
26 | 27 |
28 // The idiosyncrasies of GSM-in-WAV are explained at http://kbs.cs.tu-berlin.de/~jutta/toast.html | |
29 | |
27 #include "avcodec.h" | 30 #include "avcodec.h" |
28 #include <gsm.h> | 31 #include <gsm.h> |
29 | 32 |
30 // gsm.h miss some essential constants | 33 // gsm.h miss some essential constants |
31 #define GSM_BLOCK_SIZE 33 | 34 #define GSM_BLOCK_SIZE 33 |
35 #define GSM_MS_BLOCK_SIZE 65 | |
32 #define GSM_FRAME_SIZE 160 | 36 #define GSM_FRAME_SIZE 160 |
33 | 37 |
34 static int libgsm_init(AVCodecContext *avctx) { | 38 static int libgsm_init(AVCodecContext *avctx) { |
35 if (avctx->channels > 1 || avctx->sample_rate != 8000) | 39 if (avctx->channels > 1 || avctx->sample_rate != 8000 || avctx->bit_rate != 13000) |
36 return -1; | 40 return -1; |
37 | 41 |
38 avctx->frame_size = GSM_FRAME_SIZE; | 42 avctx->priv_data = gsm_create(); |
39 avctx->block_align = GSM_BLOCK_SIZE; | |
40 | 43 |
41 avctx->priv_data = gsm_create(); | 44 switch(avctx->codec_id) { |
45 case CODEC_ID_GSM: | |
46 avctx->frame_size = GSM_FRAME_SIZE; | |
47 avctx->block_align = GSM_BLOCK_SIZE; | |
48 break; | |
49 case CODEC_ID_GSM_MS: { | |
50 int one = 1; | |
51 gsm_option(avctx->priv_data, GSM_OPT_WAV49, &one); | |
52 avctx->frame_size = 2*GSM_FRAME_SIZE; | |
53 avctx->block_align = GSM_MS_BLOCK_SIZE; | |
54 } | |
55 } | |
42 | 56 |
43 avctx->coded_frame= avcodec_alloc_frame(); | 57 avctx->coded_frame= avcodec_alloc_frame(); |
44 avctx->coded_frame->key_frame= 1; | 58 avctx->coded_frame->key_frame= 1; |
45 | 59 |
46 return 0; | 60 return 0; |
53 } | 67 } |
54 | 68 |
55 static int libgsm_encode_frame(AVCodecContext *avctx, | 69 static int libgsm_encode_frame(AVCodecContext *avctx, |
56 unsigned char *frame, int buf_size, void *data) { | 70 unsigned char *frame, int buf_size, void *data) { |
57 // we need a full block | 71 // we need a full block |
58 if(buf_size < GSM_BLOCK_SIZE) return 0; | 72 if(buf_size < avctx->block_align) return 0; |
59 | 73 |
60 gsm_encode(avctx->priv_data,data,frame); | 74 switch(avctx->codec_id) { |
61 | 75 case CODEC_ID_GSM: |
62 return GSM_BLOCK_SIZE; | 76 gsm_encode(avctx->priv_data,data,frame); |
77 break; | |
78 case CODEC_ID_GSM_MS: | |
79 gsm_encode(avctx->priv_data,data,frame); | |
80 gsm_encode(avctx->priv_data,((short*)data)+GSM_FRAME_SIZE,frame+32); | |
81 } | |
82 return avctx->block_align; | |
63 } | 83 } |
64 | 84 |
65 | 85 |
66 AVCodec libgsm_encoder = { | 86 AVCodec libgsm_encoder = { |
67 "gsm", | 87 "gsm", |
71 libgsm_init, | 91 libgsm_init, |
72 libgsm_encode_frame, | 92 libgsm_encode_frame, |
73 libgsm_close, | 93 libgsm_close, |
74 }; | 94 }; |
75 | 95 |
96 AVCodec libgsm_ms_encoder = { | |
97 "gsm", | |
98 CODEC_TYPE_AUDIO, | |
99 CODEC_ID_GSM_MS, | |
100 0, | |
101 libgsm_init, | |
102 libgsm_encode_frame, | |
103 libgsm_close, | |
104 }; | |
105 | |
76 static int libgsm_decode_frame(AVCodecContext *avctx, | 106 static int libgsm_decode_frame(AVCodecContext *avctx, |
77 void *data, int *data_size, | 107 void *data, int *data_size, |
78 uint8_t *buf, int buf_size) { | 108 uint8_t *buf, int buf_size) { |
79 | 109 |
80 if(buf_size < GSM_BLOCK_SIZE) return 0; | 110 if(buf_size < avctx->block_align) return 0; |
81 | 111 |
82 if(gsm_decode(avctx->priv_data,buf,data)) return -1; | 112 switch(avctx->codec_id) { |
83 | 113 case CODEC_ID_GSM: |
84 *data_size = GSM_FRAME_SIZE*2; | 114 if(gsm_decode(avctx->priv_data,buf,data)) return -1; |
85 return GSM_BLOCK_SIZE; | 115 *data_size = GSM_FRAME_SIZE*sizeof(int16_t); |
116 break; | |
117 case CODEC_ID_GSM_MS: | |
118 if(gsm_decode(avctx->priv_data,buf,data) || | |
119 gsm_decode(avctx->priv_data,buf+33,((int16_t*)data)+GSM_FRAME_SIZE)) return -1; | |
120 *data_size = GSM_FRAME_SIZE*sizeof(int16_t)*2; | |
121 } | |
122 return avctx->block_align; | |
86 } | 123 } |
87 | 124 |
88 AVCodec libgsm_decoder = { | 125 AVCodec libgsm_decoder = { |
89 "gsm", | 126 "gsm", |
90 CODEC_TYPE_AUDIO, | 127 CODEC_TYPE_AUDIO, |
93 libgsm_init, | 130 libgsm_init, |
94 NULL, | 131 NULL, |
95 libgsm_close, | 132 libgsm_close, |
96 libgsm_decode_frame, | 133 libgsm_decode_frame, |
97 }; | 134 }; |
135 | |
136 AVCodec libgsm_ms_decoder = { | |
137 "gsm_ms", | |
138 CODEC_TYPE_AUDIO, | |
139 CODEC_ID_GSM_MS, | |
140 0, | |
141 libgsm_init, | |
142 NULL, | |
143 libgsm_close, | |
144 libgsm_decode_frame, | |
145 }; |