Libav
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  av_freep(&context->frame_buffer);
50 
51  return 0;
52 }
53 
55 {
56  ROQDPCMContext *context = avctx->priv_data;
57  int ret;
58 
59  if (avctx->channels > 2) {
60  av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
61  return AVERROR(EINVAL);
62  }
63  if (avctx->sample_rate != 22050) {
64  av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
65  return AVERROR(EINVAL);
66  }
67 
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;
71 
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);
76  goto error;
77  }
78 
79  context->lastSample[0] = context->lastSample[1] = 0;
80 
81  return 0;
82 error:
83  roq_dpcm_encode_close(avctx);
84  return ret;
85 }
86 
87 static unsigned char dpcm_predict(short *previous, short current)
88 {
89  int diff;
90  int negative;
91  int result;
92  int predicted;
93 
94  diff = current - *previous;
95 
96  negative = diff<0;
97  diff = FFABS(diff);
98 
99  if (diff >= MAX_DPCM)
100  result = 127;
101  else {
102  result = ff_sqrt(diff);
103  result += diff > result*result+result;
104  }
105 
106  /* See if this overflows */
107  retry:
108  diff = result*result;
109  if (negative)
110  diff = -diff;
111  predicted = *previous + diff;
112 
113  /* If it overflows, back off a step */
114  if (predicted > 32767 || predicted < -32768) {
115  result--;
116  goto retry;
117  }
118 
119  /* Add the sign bit */
120  result |= negative << 7; //if (negative) result |= 128;
121 
122  *previous = predicted;
123 
124  return result;
125 }
126 
128  const AVFrame *frame, int *got_packet_ptr)
129 {
130  int i, stereo, data_size, ret;
131  const int16_t *in = frame ? (const int16_t *)frame->data[0] : NULL;
132  uint8_t *out;
133  ROQDPCMContext *context = avctx->priv_data;
134 
135  stereo = (avctx->channels == 2);
136 
137  if (!in && context->input_frames >= 8)
138  return 0;
139 
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++;
148  return 0;
149  }
150  in = context->frame_buffer;
151  }
152 
153  if (stereo) {
154  context->lastSample[0] &= 0xFF00;
155  context->lastSample[1] &= 0xFF00;
156  }
157 
158  if (context->input_frames == 7 || !in)
159  data_size = avctx->channels * context->buffered_samples;
160  else
161  data_size = avctx->channels * avctx->frame_size;
162 
163  if ((ret = ff_alloc_packet(avpkt, ROQ_HEADER_SIZE + data_size))) {
164  av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
165  return ret;
166  }
167  out = avpkt->data;
168 
169  bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
170  bytestream_put_byte(&out, 0x10);
171  bytestream_put_le32(&out, data_size);
172 
173  if (stereo) {
174  bytestream_put_byte(&out, (context->lastSample[1])>>8);
175  bytestream_put_byte(&out, (context->lastSample[0])>>8);
176  } else
177  bytestream_put_le16(&out, context->lastSample[0]);
178 
179  /* Write the actual samples */
180  for (i = 0; i < data_size; i++)
181  *out++ = dpcm_predict(&context->lastSample[i & 1], *in++);
182 
183  avpkt->pts = context->input_frames <= 7 ? context->first_pts : frame->pts;
184  avpkt->duration = data_size / avctx->channels;
185 
186  context->input_frames++;
187  if (!in)
188  context->input_frames = FFMAX(context->input_frames, 8);
189 
190  *got_packet_ptr = 1;
191  return 0;
192 }
193 
195  .name = "roq_dpcm",
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),
201  .encode2 = roq_dpcm_encode_frame,
203  .capabilities = CODEC_CAP_DELAY,
204  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
206 };
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:62
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
AVCodec.
Definition: avcodec.h:2790
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:198
int64_t first_pts
Definition: roqaudioenc.c:41
uint8_t
#define av_cold
Definition: attributes.h:66
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).
Definition: frame.h:211
int buffered_samples
Definition: roqaudioenc.c:39
uint8_t * data
Definition: avcodec.h:967
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:985
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:713
#define AVERROR(e)
Definition: error.h:43
sample_fmts
Definition: avconv_filter.c:68
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
const char * name
Name of the codec implementation.
Definition: avcodec.h:2797
#define FFMAX(a, b)
Definition: common.h:55
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:127
int bit_rate
the average bitrate
Definition: avcodec.h:1108
#define FFABS(a)
Definition: common.h:52
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1236
static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx)
Definition: roqaudioenc.c:54
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1805
NULL
Definition: eval.c:55
Libavcodec external API header.
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:61
static av_const unsigned int ff_sqrt(unsigned int a)
Definition: mathops.h:202
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:68
int sample_rate
samples per second
Definition: avcodec.h:1785
main external API structure.
Definition: avcodec.h:1044
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
static unsigned char dpcm_predict(short *previous, short current)
Definition: roqaudioenc.c:87
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
common internal api header.
signed 16 bits
Definition: samplefmt.h:64
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1086
int channels
number of audio channels
Definition: avcodec.h:1786
#define ROQ_FRAME_SIZE
Definition: roqaudioenc.c:29
AVCodec ff_roq_dpcm_encoder
Definition: roqaudioenc.c:194
#define ROQ_HEADER_SIZE
Definition: roqaudioenc.c:30
#define MAX_DPCM
Definition: roqaudioenc.c:32
This structure stores compressed data.
Definition: avcodec.h:944
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:960