FFmpeg
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 FFmpeg.
8  *
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.
13  *
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.
18  *
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
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 ROQDPCMContext {
36  short lastSample[2];
39  int16_t *frame_buffer;
40  int64_t first_pts;
42 
43 
45 {
47 
48  av_freep(&context->frame_buffer);
49 
50  return 0;
51 }
52 
54 {
56 
57  if (avctx->channels > 2) {
58  av_log(avctx, AV_LOG_ERROR, "Audio must be mono or stereo\n");
59  return AVERROR(EINVAL);
60  }
61  if (avctx->sample_rate != 22050) {
62  av_log(avctx, AV_LOG_ERROR, "Audio must be 22050 Hz\n");
63  return AVERROR(EINVAL);
64  }
65 
66  avctx->frame_size = ROQ_FRAME_SIZE;
67  avctx->bit_rate = (ROQ_HEADER_SIZE + ROQ_FRAME_SIZE * avctx->channels) *
68  (22050 / ROQ_FRAME_SIZE) * 8;
69 
70  context->frame_buffer = av_malloc(8 * ROQ_FRAME_SIZE * avctx->channels *
71  sizeof(*context->frame_buffer));
72  if (!context->frame_buffer)
73  return AVERROR(ENOMEM);
74 
75  context->lastSample[0] = context->lastSample[1] = 0;
76 
77  return 0;
78 }
79 
80 static unsigned char dpcm_predict(short *previous, short current)
81 {
82  int diff;
83  int negative;
84  int result;
85  int predicted;
86 
87  diff = current - *previous;
88 
89  negative = diff<0;
90  diff = FFABS(diff);
91 
92  if (diff >= MAX_DPCM)
93  result = 127;
94  else {
95  result = ff_sqrt(diff);
97  }
98 
99  /* See if this overflows */
100  retry:
101  diff = result*result;
102  if (negative)
103  diff = -diff;
104  predicted = *previous + diff;
105 
106  /* If it overflows, back off a step */
107  if (predicted > 32767 || predicted < -32768) {
108  result--;
109  goto retry;
110  }
111 
112  /* Add the sign bit */
113  result |= negative << 7; //if (negative) result |= 128;
114 
115  *previous = predicted;
116 
117  return result;
118 }
119 
121  const AVFrame *frame, int *got_packet_ptr)
122 {
123  int i, stereo, data_size, ret;
124  const int16_t *in = frame ? (const int16_t *)frame->data[0] : NULL;
125  uint8_t *out;
126  ROQDPCMContext *context = avctx->priv_data;
127 
128  stereo = (avctx->channels == 2);
129 
130  if (!in && context->input_frames >= 8)
131  return 0;
132 
133  if (in && context->input_frames < 8) {
134  memcpy(&context->frame_buffer[context->buffered_samples * avctx->channels],
135  in, avctx->frame_size * avctx->channels * sizeof(*in));
136  context->buffered_samples += avctx->frame_size;
137  if (context->input_frames == 0)
138  context->first_pts = frame->pts;
139  if (context->input_frames < 7) {
140  context->input_frames++;
141  return 0;
142  }
143  }
144  if (context->input_frames < 8)
145  in = context->frame_buffer;
146 
147  if (stereo) {
148  context->lastSample[0] &= 0xFF00;
149  context->lastSample[1] &= 0xFF00;
150  }
151 
152  if (context->input_frames == 7)
153  data_size = avctx->channels * context->buffered_samples;
154  else
155  data_size = avctx->channels * avctx->frame_size;
156 
157  if ((ret = ff_alloc_packet2(avctx, avpkt, ROQ_HEADER_SIZE + data_size, 0)) < 0)
158  return ret;
159  out = avpkt->data;
160 
161  bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
162  bytestream_put_byte(&out, 0x10);
163  bytestream_put_le32(&out, data_size);
164 
165  if (stereo) {
166  bytestream_put_byte(&out, (context->lastSample[1])>>8);
167  bytestream_put_byte(&out, (context->lastSample[0])>>8);
168  } else
169  bytestream_put_le16(&out, context->lastSample[0]);
170 
171  /* Write the actual samples */
172  for (i = 0; i < data_size; i++)
173  *out++ = dpcm_predict(&context->lastSample[i & 1], *in++);
174 
175  avpkt->pts = context->input_frames <= 7 ? context->first_pts : frame->pts;
176  avpkt->duration = data_size / avctx->channels;
177 
178  context->input_frames++;
179  if (!in)
180  context->input_frames = FFMAX(context->input_frames, 8);
181 
182  *got_packet_ptr = 1;
183  return 0;
184 }
185 
187  .name = "roq_dpcm",
188  .long_name = NULL_IF_CONFIG_SMALL("id RoQ DPCM"),
189  .type = AVMEDIA_TYPE_AUDIO,
190  .id = AV_CODEC_ID_ROQ_DPCM,
191  .priv_data_size = sizeof(ROQDPCMContext),
193  .encode2 = roq_dpcm_encode_frame,
194  .close = roq_dpcm_encode_close,
195  .capabilities = AV_CODEC_CAP_DELAY,
196  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
198 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1216
AVCodec
AVCodec.
Definition: codec.h:197
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
out
FILE * out
Definition: movenc.c:54
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1196
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:925
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:369
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
ROQDPCMContext
Definition: roqaudioenc.c:35
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
ff_sqrt
#define ff_sqrt
Definition: mathops.h:206
ROQDPCMContext::frame_buffer
int16_t * frame_buffer
Definition: roqaudioenc.c:39
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
av_cold
#define av_cold
Definition: attributes.h:90
ROQ_HEADER_SIZE
#define ROQ_HEADER_SIZE
Definition: roqaudioenc.c:30
ROQDPCMContext::input_frames
int input_frames
Definition: roqaudioenc.c:37
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
context
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your context
Definition: writing_filters.txt:91
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:586
mathops.h
ROQDPCMContext::first_pts
int64_t first_pts
Definition: roqaudioenc.c:40
AV_CODEC_ID_ROQ_DPCM
@ AV_CODEC_ID_ROQ_DPCM
Definition: codec_id.h:414
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
roq_dpcm_encode_init
static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx)
Definition: roqaudioenc.c:53
FFMAX
#define FFMAX(a, b)
Definition: common.h:103
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
roq_dpcm_encode_frame
static int roq_dpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: roqaudioenc.c:120
dpcm_predict
static unsigned char dpcm_predict(short *previous, short current)
Definition: roqaudioenc.c:80
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:1197
in
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;} return ac;} 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;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->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);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Definition: audio_convert.c:326
i
int i
Definition: input.c:407
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
uint8_t
uint8_t
Definition: audio_convert.c:194
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:204
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
MAX_DPCM
#define MAX_DPCM
Definition: roqaudioenc.c:32
AVCodecContext
main external API structure.
Definition: avcodec.h:536
ROQDPCMContext::buffered_samples
int buffered_samples
Definition: roqaudioenc.c:38
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:77
roq_dpcm_encode_close
static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx)
Definition: roqaudioenc.c:44
ff_roq_dpcm_encoder
AVCodec ff_roq_dpcm_encoder
Definition: roqaudioenc.c:186
diff
static av_always_inline int diff(const uint32_t a, const uint32_t b)
Definition: vf_palettegen.c:136
AVPacket
This structure stores compressed data.
Definition: packet.h:346
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:563
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
bytestream.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ROQ_FRAME_SIZE
#define ROQ_FRAME_SIZE
Definition: roqaudioenc.c:29
ff_alloc_packet2
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:33
ROQDPCMContext::lastSample
short lastSample[2]
Definition: roqaudioenc.c:36