4 * Copyright (c) 2005 Eric Lasota
5 * Based on RoQ specs (c)2001 Tim Ferguson
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "bytestream.h"
29 #define ROQ_FRAME_SIZE 735
30 #define ROQ_HEADER_SIZE 8
32 #define MAX_DPCM (127*127)
40 int16_t *frame_buffer
;
45 static av_cold
int roq_dpcm_encode_close(AVCodecContext
*avctx
)
47 ROQDPCMContext
*context
= avctx
->priv_data
;
49 av_freep(&context
->frame_buffer
);
54 static av_cold
int roq_dpcm_encode_init(AVCodecContext
*avctx
)
56 ROQDPCMContext
*context
= avctx
->priv_data
;
59 if (avctx
->channels
> 2) {
60 av_log(avctx
, AV_LOG_ERROR
, "Audio must be mono or stereo\n");
61 return AVERROR(EINVAL
);
63 if (avctx
->sample_rate
!= 22050) {
64 av_log(avctx
, AV_LOG_ERROR
, "Audio must be 22050 Hz\n");
65 return AVERROR(EINVAL
);
68 avctx
->frame_size
= ROQ_FRAME_SIZE
;
69 avctx
->bit_rate
= (ROQ_HEADER_SIZE
+ ROQ_FRAME_SIZE
* avctx
->channels
) *
70 (22050 / ROQ_FRAME_SIZE
) * 8;
72 context
->frame_buffer
= av_malloc(8 * ROQ_FRAME_SIZE
* avctx
->channels
*
73 sizeof(*context
->frame_buffer
));
74 if (!context
->frame_buffer
) {
75 ret
= AVERROR(ENOMEM
);
79 context
->lastSample
[0] = context
->lastSample
[1] = 0;
83 roq_dpcm_encode_close(avctx
);
87 static unsigned char dpcm_predict(short *previous
, short current
)
94 diff
= current
- *previous
;
102 result
= ff_sqrt(diff
);
103 result
+= diff
> result
*result
+result
;
106 /* See if this overflows */
108 diff
= result
*result
;
111 predicted
= *previous
+ diff
;
113 /* If it overflows, back off a step */
114 if (predicted
> 32767 || predicted
< -32768) {
119 /* Add the sign bit */
120 result
|= negative
<< 7; //if (negative) result |= 128;
122 *previous
= predicted
;
127 static int roq_dpcm_encode_frame(AVCodecContext
*avctx
, AVPacket
*avpkt
,
128 const AVFrame
*frame
, int *got_packet_ptr
)
130 int i
, stereo
, data_size
, ret
;
131 const int16_t *in
= frame
? (const int16_t *)frame
->data
[0] : NULL
;
133 ROQDPCMContext
*context
= avctx
->priv_data
;
135 stereo
= (avctx
->channels
== 2);
137 if (!in
&& context
->input_frames
>= 8)
140 if (in
&& context
->input_frames
< 8) {
141 memcpy(&context
->frame_buffer
[context
->buffered_samples
* avctx
->channels
],
142 in
, avctx
->frame_size
* avctx
->channels
* sizeof(*in
));
143 context
->buffered_samples
+= avctx
->frame_size
;
144 if (context
->input_frames
== 0)
145 context
->first_pts
= frame
->pts
;
146 if (context
->input_frames
< 7) {
147 context
->input_frames
++;
151 if (context
->input_frames
< 8) {
152 in
= context
->frame_buffer
;
156 context
->lastSample
[0] &= 0xFF00;
157 context
->lastSample
[1] &= 0xFF00;
160 if (context
->input_frames
== 7)
161 data_size
= avctx
->channels
* context
->buffered_samples
;
163 data_size
= avctx
->channels
* avctx
->frame_size
;
165 if ((ret
= ff_alloc_packet2(avctx
, avpkt
, ROQ_HEADER_SIZE
+ data_size
)) < 0)
169 bytestream_put_byte(&out
, stereo
? 0x21 : 0x20);
170 bytestream_put_byte(&out
, 0x10);
171 bytestream_put_le32(&out
, data_size
);
174 bytestream_put_byte(&out
, (context
->lastSample
[1])>>8);
175 bytestream_put_byte(&out
, (context
->lastSample
[0])>>8);
177 bytestream_put_le16(&out
, context
->lastSample
[0]);
179 /* Write the actual samples */
180 for (i
= 0; i
< data_size
; i
++)
181 *out
++ = dpcm_predict(&context
->lastSample
[i
& 1], *in
++);
183 avpkt
->pts
= context
->input_frames
<= 7 ? context
->first_pts
: frame
->pts
;
184 avpkt
->duration
= data_size
/ avctx
->channels
;
186 context
->input_frames
++;
188 context
->input_frames
= FFMAX(context
->input_frames
, 8);
194 AVCodec ff_roq_dpcm_encoder
= {
196 .long_name
= NULL_IF_CONFIG_SMALL("id RoQ DPCM"),
197 .type
= AVMEDIA_TYPE_AUDIO
,
198 .id
= AV_CODEC_ID_ROQ_DPCM
,
199 .priv_data_size
= sizeof(ROQDPCMContext
),
200 .init
= roq_dpcm_encode_init
,
201 .encode2
= roq_dpcm_encode_frame
,
202 .close
= roq_dpcm_encode_close
,
203 .capabilities
= CODEC_CAP_DELAY
,
204 .sample_fmts
= (const enum AVSampleFormat
[]){ AV_SAMPLE_FMT_S16
,
205 AV_SAMPLE_FMT_NONE
},