annotate roqaudioenc.c @ 4977:9d7c1fbd3359 libavcodec

move mjpeg parser in its own file
author aurel
date Thu, 10 May 2007 22:37:36 +0000
parents 7c6a0470eb63
children 48759bfbd073
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4932
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
1 /*
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
2 * RoQ audio encoder
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
3 *
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
4 * Copyright (c) 2005 Eric Lasota
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
5 * Based on RoQ specs (c)2001 Tim Ferguson
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
6 *
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
7 * This file is part of FFmpeg.
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
8 *
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
9 * FFmpeg is free software; you can redistribute it and/or
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
10 * modify it under the terms of the GNU Lesser General Public
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
11 * License as published by the Free Software Foundation; either
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
12 * version 2.1 of the License, or (at your option) any later version.
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
13 *
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
14 * FFmpeg is distributed in the hope that it will be useful,
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
17 * Lesser General Public License for more details.
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
18 *
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
19 * You should have received a copy of the GNU Lesser General Public
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
20 * License along with FFmpeg; if not, write to the Free Software
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
22 */
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
23
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
24 #include "avcodec.h"
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
25 #include "bytestream.h"
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
26
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
27 #define ROQ_FIRST_FRAME_SIZE (735*8)
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
28 #define ROQ_FRAME_SIZE 735
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
29
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
30
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
31 #define MAX_DPCM (127*127)
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
32 static unsigned char dpcmValues[MAX_DPCM];
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
33
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
34
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
35 typedef struct
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
36 {
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
37 short lastSample[2];
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
38 } ROQDPCMContext_t;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
39
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
40 static void roq_dpcm_table_init(void)
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
41 {
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
42 int i;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
43
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
44 /* Create a table of quick DPCM values */
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
45 for (i=0; i<MAX_DPCM; i++) {
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
46 int s= ff_sqrt(i);
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
47 int mid= s*s + s;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
48 dpcmValues[i]= s + (i>mid);
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
49 }
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
50 }
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
51
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
52 static int roq_dpcm_encode_init(AVCodecContext *avctx)
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
53 {
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
54 ROQDPCMContext_t *context = avctx->priv_data;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
55
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
56 if (avctx->channels > 2) {
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
57 av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
58 return -1;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
59 }
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
60 if (avctx->sample_rate != 22050) {
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
61 av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
62 return -1;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
63 }
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
64 if (avctx->sample_fmt != SAMPLE_FMT_S16) {
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
65 av_log(avctx, AV_LOG_ERROR, "Audio must be signed 16-bit\n");
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
66 return -1;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
67 }
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
68
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
69 roq_dpcm_table_init();
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
70
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
71 avctx->frame_size = ROQ_FIRST_FRAME_SIZE;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
72
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
73 context->lastSample[0] = context->lastSample[1] = 0;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
74
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
75 avctx->coded_frame= avcodec_alloc_frame();
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
76 avctx->coded_frame->key_frame= 1;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
77
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
78 return 0;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
79 }
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
80
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
81 static unsigned char dpcm_predict(short *previous, short current)
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
82 {
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
83 int diff;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
84 int negative;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
85 int result;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
86 int predicted;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
87
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
88 diff = current - *previous;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
89
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
90 negative = diff<0;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
91 diff = FFABS(diff);
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
92
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
93 if (diff >= MAX_DPCM)
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
94 result = 127;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
95 else
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
96 result = dpcmValues[diff];
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
97
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
98 /* See if this overflows */
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
99 retry:
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
100 diff = result*result;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
101 if (negative)
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
102 diff = -diff;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
103 predicted = *previous + diff;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
104
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
105 /* If it overflows, back off a step */
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
106 if (predicted > 32767 || predicted < -32768) {
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
107 result--;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
108 goto retry;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
109 }
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
110
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
111 /* Add the sign bit */
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
112 result |= negative << 7; //if (negative) result |= 128;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
113
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
114 *previous = predicted;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
115
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
116 return result;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
117 }
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
118
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
119 static int roq_dpcm_encode_frame(AVCodecContext *avctx,
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
120 unsigned char *frame, int buf_size, void *data)
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
121 {
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
122 int i, samples, stereo, ch;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
123 short *in;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
124 unsigned char *out;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
125
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
126 ROQDPCMContext_t *context = avctx->priv_data;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
127
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
128 stereo = (avctx->channels == 2);
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
129
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
130 if (stereo) {
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
131 context->lastSample[0] &= 0xFF00;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
132 context->lastSample[1] &= 0xFF00;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
133 }
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
134
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
135 out = frame;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
136 in = data;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
137
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
138 bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
139 bytestream_put_byte(&out, 0x10);
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
140 bytestream_put_le32(&out, avctx->frame_size*avctx->channels);
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
141
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
142 if (stereo) {
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
143 bytestream_put_byte(&out, (context->lastSample[1])>>8);
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
144 bytestream_put_byte(&out, (context->lastSample[0])>>8);
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
145 } else
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
146 bytestream_put_le16(&out, context->lastSample[0]);
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
147
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
148 /* Write the actual samples */
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
149 samples = avctx->frame_size;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
150 for (i=0; i<samples; i++)
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
151 for (ch=0; ch<avctx->channels; ch++)
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
152 *out++ = dpcm_predict(&context->lastSample[ch], *in++);
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
153
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
154 /* Use smaller frames from now on */
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
155 avctx->frame_size = ROQ_FRAME_SIZE;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
156
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
157 /* Return the result size */
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
158 return out - frame;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
159 }
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
160
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
161 static int roq_dpcm_encode_close(AVCodecContext *avctx)
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
162 {
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
163 av_freep(&avctx->coded_frame);
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
164
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
165 return 0;
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
166 }
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
167
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
168 AVCodec roq_dpcm_encoder = {
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
169 "roq_dpcm",
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
170 CODEC_TYPE_AUDIO,
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
171 CODEC_ID_ROQ_DPCM,
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
172 sizeof(ROQDPCMContext_t),
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
173 roq_dpcm_encode_init,
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
174 roq_dpcm_encode_frame,
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
175 roq_dpcm_encode_close,
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
176 NULL,
7c6a0470eb63 RoQ audio encoder
benoit
parents:
diff changeset
177 };