roqaudioenc.c
Go to the documentation of this file.
1 /*
2  * RoQ audio encoder
3  *
4  * Copyright (c) 2005 Eric Lasota
5  * Based on RoQ specs (c)2001 Tim Ferguson
6  *
7  * This file is part of Libav.
8  *
9  * Libav 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.
13  *
14  * Libav 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.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with Libav; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27 #include "mathops.h"
28 
29 #define ROQ_FRAME_SIZE 735
30 #define ROQ_HEADER_SIZE 8
31 
32 #define MAX_DPCM (127*127)
33 
34 
35 typedef struct
36 {
37  short lastSample[2];
40  int16_t *frame_buffer;
41  int64_t first_pts;
43 
44 
46 {
47  ROQDPCMContext *context = avctx->priv_data;
48 
49 #if FF_API_OLD_ENCODE_AUDIO
50  av_freep(&avctx->coded_frame);
51 #endif
52  av_freep(&context->frame_buffer);
53 
54  return 0;
55 }
56 
58 {
59  ROQDPCMContext *context = avctx->priv_data;
60  int ret;
61 
62  if (avctx->channels > 2) {
63  av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
64  return AVERROR(EINVAL);
65  }
66  if (avctx->sample_rate != 22050) {
67  av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
68  return AVERROR(EINVAL);
69  }
70 
71  avctx->frame_size = ROQ_FRAME_SIZE;
72  avctx->bit_rate = (ROQ_HEADER_SIZE + ROQ_FRAME_SIZE * avctx->channels) *
73  (22050 / ROQ_FRAME_SIZE) * 8;
74 
75  context->frame_buffer = av_malloc(8 * ROQ_FRAME_SIZE * avctx->channels *
76  sizeof(*context->frame_buffer));
77  if (!context->frame_buffer) {
78  ret = AVERROR(ENOMEM);
79  goto error;
80  }
81 
82  context->lastSample[0] = context->lastSample[1] = 0;
83 
84 #if FF_API_OLD_ENCODE_AUDIO
86  if (!avctx->coded_frame) {
87  ret = AVERROR(ENOMEM);
88  goto error;
89  }
90 #endif
91 
92  return 0;
93 error:
94  roq_dpcm_encode_close(avctx);
95  return ret;
96 }
97 
98 static unsigned char dpcm_predict(short *previous, short current)
99 {
100  int diff;
101  int negative;
102  int result;
103  int predicted;
104 
105  diff = current - *previous;
106 
107  negative = diff<0;
108  diff = FFABS(diff);
109 
110  if (diff >= MAX_DPCM)
111  result = 127;
112  else {
113  result = ff_sqrt(diff);
114  result += diff > result*result+result;
115  }
116 
117  /* See if this overflows */
118  retry:
119  diff = result*result;
120  if (negative)
121  diff = -diff;
122  predicted = *previous + diff;
123 
124  /* If it overflows, back off a step */
125  if (predicted > 32767 || predicted < -32768) {
126  result--;
127  goto retry;
128  }
129 
130  /* Add the sign bit */
131  result |= negative << 7; //if (negative) result |= 128;
132 
133  *previous = predicted;
134 
135  return result;
136 }
137 
139  const AVFrame *frame, int *got_packet_ptr)
140 {
141  int i, stereo, data_size, ret;
142  const int16_t *in = frame ? (const int16_t *)frame->data[0] : NULL;
143  uint8_t *out;
144  ROQDPCMContext *context = avctx->priv_data;
145 
146  stereo = (avctx->channels == 2);
147 
148  if (!in && context->input_frames >= 8)
149  return 0;
150 
151  if (in && context->input_frames < 8) {
152  memcpy(&context->frame_buffer[context->buffered_samples * avctx->channels],
153  in, avctx->frame_size * avctx->channels * sizeof(*in));
154  context->buffered_samples += avctx->frame_size;
155  if (context->input_frames == 0)
156  context->first_pts = frame->pts;
157  if (context->input_frames < 7) {
158  context->input_frames++;
159  return 0;
160  }
161  in = context->frame_buffer;
162  }
163 
164  if (stereo) {
165  context->lastSample[0] &= 0xFF00;
166  context->lastSample[1] &= 0xFF00;
167  }
168 
169  if (context->input_frames == 7 || !in)
170  data_size = avctx->channels * context->buffered_samples;
171  else
172  data_size = avctx->channels * avctx->frame_size;
173 
174  if ((ret = ff_alloc_packet(avpkt, ROQ_HEADER_SIZE + data_size))) {
175  av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
176  return ret;
177  }
178  out = avpkt->data;
179 
180  bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
181  bytestream_put_byte(&out, 0x10);
182  bytestream_put_le32(&out, data_size);
183 
184  if (stereo) {
185  bytestream_put_byte(&out, (context->lastSample[1])>>8);
186  bytestream_put_byte(&out, (context->lastSample[0])>>8);
187  } else
188  bytestream_put_le16(&out, context->lastSample[0]);
189 
190  /* Write the actual samples */
191  for (i = 0; i < data_size; i++)
192  *out++ = dpcm_predict(&context->lastSample[i & 1], *in++);
193 
194  avpkt->pts = context->input_frames <= 7 ? context->first_pts : frame->pts;
195  avpkt->duration = data_size / avctx->channels;
196 
197  context->input_frames++;
198  if (!in)
199  context->input_frames = FFMAX(context->input_frames, 8);
200 
201  *got_packet_ptr = 1;
202  return 0;
203 }
204 
206  .name = "roq_dpcm",
207  .type = AVMEDIA_TYPE_AUDIO,
208  .id = AV_CODEC_ID_ROQ_DPCM,
209  .priv_data_size = sizeof(ROQDPCMContext),
211  .encode2 = roq_dpcm_encode_frame,
213  .capabilities = CODEC_CAP_DELAY,
214  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
216  .long_name = NULL_IF_CONFIG_SMALL("id RoQ DPCM"),
217 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
signed 16 bits
Definition: samplefmt.h:52
AVCodec.
Definition: avcodec.h:2960
short lastSample[2]
Definition: roqaudioenc.c:37
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
int64_t first_pts
Definition: roqaudioenc.c:41
uint8_t
static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx)
Definition: roqaudioenc.c:45
int64_t pts
presentation timestamp in time_base units (time when frame should be shown to user) If AV_NOPTS_VALUE...
Definition: avcodec.h:1088
int buffered_samples
Definition: roqaudioenc.c:39
uint8_t * data
Definition: avcodec.h:915
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:937
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
sample_fmts
Definition: avconv_filter.c:63
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
int16_t * frame_buffer
Definition: roqaudioenc.c:40
static int roq_dpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: roqaudioenc.c:138
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:618
int bit_rate
the average bitrate
Definition: avcodec.h:1404
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:880
static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx)
Definition: roqaudioenc.c:57
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2124
NULL
Definition: eval.c:52
external API header
static av_const unsigned int ff_sqrt(unsigned int a)
Definition: mathops.h:198
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:63
int sample_rate
samples per second
Definition: avcodec.h:2104
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
static unsigned char dpcm_predict(short *previous, short current)
Definition: roqaudioenc.c:98
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
common internal api header.
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
void * priv_data
Definition: avcodec.h:1382
int channels
number of audio channels
Definition: avcodec.h:2105
#define ROQ_FRAME_SIZE
Definition: roqaudioenc.c:29
AVCodec ff_roq_dpcm_encoder
Definition: roqaudioenc.c:205
#define ROQ_HEADER_SIZE
Definition: roqaudioenc.c:30
#define MAX_DPCM
Definition: roqaudioenc.c:32
This structure stores compressed data.
Definition: avcodec.h:898
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:908