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 "codec_internal.h"
27 #include "encode.h"
28 #include "mathops.h"
29 
30 #define ROQ_FRAME_SIZE 735
31 #define ROQ_HEADER_SIZE 8
32 
33 #define MAX_DPCM (127*127)
34 
35 
36 typedef struct ROQDPCMContext {
37  short lastSample[2];
40  int16_t *frame_buffer;
41  int64_t first_pts;
43 
44 
46 {
48 
49  av_freep(&context->frame_buffer);
50 
51  return 0;
52 }
53 
55 {
57  int channels = avctx->ch_layout.nb_channels;
58 
59  if (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;
70  (22050 / ROQ_FRAME_SIZE) * 8;
71 
72  context->frame_buffer = av_malloc(8 * ROQ_FRAME_SIZE * channels *
73  sizeof(*context->frame_buffer));
74  if (!context->frame_buffer)
75  return AVERROR(ENOMEM);
76 
77  context->lastSample[0] = context->lastSample[1] = 0;
78 
79  return 0;
80 }
81 
82 static unsigned char dpcm_predict(short *previous, short current)
83 {
84  int diff;
85  int negative;
86  int result;
87  int predicted;
88 
89  diff = current - *previous;
90 
91  negative = diff<0;
92  diff = FFABS(diff);
93 
94  if (diff >= MAX_DPCM)
95  result = 127;
96  else {
97  result = ff_sqrt(diff);
99  }
100 
101  /* See if this overflows */
102  retry:
103  diff = result*result;
104  if (negative)
105  diff = -diff;
106  predicted = *previous + diff;
107 
108  /* If it overflows, back off a step */
109  if (predicted > 32767 || predicted < -32768) {
110  result--;
111  goto retry;
112  }
113 
114  /* Add the sign bit */
115  result |= negative << 7; //if (negative) result |= 128;
116 
117  *previous = predicted;
118 
119  return result;
120 }
121 
123  const AVFrame *frame, int *got_packet_ptr)
124 {
125  int i, stereo, data_size, ret;
126  const int16_t *in = frame ? (const int16_t *)frame->data[0] : NULL;
127  int channels = avctx->ch_layout.nb_channels;
128  uint8_t *out;
129  ROQDPCMContext *context = avctx->priv_data;
130 
131  stereo = (channels == 2);
132 
133  if (!in && context->input_frames >= 8)
134  return 0;
135 
136  if (in && context->input_frames < 8) {
137  memcpy(&context->frame_buffer[context->buffered_samples * channels],
138  in, avctx->frame_size * channels * sizeof(*in));
139  context->buffered_samples += avctx->frame_size;
140  if (context->input_frames == 0)
141  context->first_pts = frame->pts;
142  if (context->input_frames < 7) {
143  context->input_frames++;
144  return 0;
145  }
146  }
147  if (context->input_frames < 8)
148  in = context->frame_buffer;
149 
150  if (stereo) {
151  context->lastSample[0] &= 0xFF00;
152  context->lastSample[1] &= 0xFF00;
153  }
154 
155  if (context->input_frames == 7)
156  data_size = channels * context->buffered_samples;
157  else
158  data_size = channels * avctx->frame_size;
159 
160  ret = ff_get_encode_buffer(avctx, avpkt, ROQ_HEADER_SIZE + data_size, 0);
161  if (ret < 0)
162  return ret;
163  out = avpkt->data;
164 
165  bytestream_put_byte(&out, stereo ? 0x21 : 0x20);
166  bytestream_put_byte(&out, 0x10);
167  bytestream_put_le32(&out, data_size);
168 
169  if (stereo) {
170  bytestream_put_byte(&out, (context->lastSample[1])>>8);
171  bytestream_put_byte(&out, (context->lastSample[0])>>8);
172  } else
173  bytestream_put_le16(&out, context->lastSample[0]);
174 
175  /* Write the actual samples */
176  for (i = 0; i < data_size; i++)
177  *out++ = dpcm_predict(&context->lastSample[(i & 1) & stereo], *in++);
178 
179  avpkt->pts = context->input_frames <= 7 ? context->first_pts : frame->pts;
180  avpkt->duration = data_size / channels;
181 
182  context->input_frames++;
183  if (!in)
184  context->input_frames = FFMAX(context->input_frames, 8);
185 
186  *got_packet_ptr = 1;
187  return 0;
188 }
189 
191  .p.name = "roq_dpcm",
192  CODEC_LONG_NAME("id RoQ DPCM"),
193  .p.type = AVMEDIA_TYPE_AUDIO,
194  .p.id = AV_CODEC_ID_ROQ_DPCM,
195  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
196  .priv_data_size = sizeof(ROQDPCMContext),
199  .close = roq_dpcm_encode_close,
200  .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
202 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1062
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:1034
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
AVPacket::data
uint8_t * data
Definition: packet.h:374
encode.h
FFCodec
Definition: codec_internal.h:127
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
ROQDPCMContext
Definition: roqaudioenc.c:36
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2054
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:315
ff_sqrt
#define ff_sqrt
Definition: mathops.h:218
ff_roq_dpcm_encoder
const FFCodec ff_roq_dpcm_encoder
Definition: roqaudioenc.c:190
ROQDPCMContext::frame_buffer
int16_t * frame_buffer
Definition: roqaudioenc.c:40
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
ROQ_HEADER_SIZE
#define ROQ_HEADER_SIZE
Definition: roqaudioenc.c:31
ROQDPCMContext::input_frames
int input_frames
Definition: roqaudioenc.c:38
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:365
channels
channels
Definition: aptx.h:31
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:64
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:476
mathops.h
ROQDPCMContext::first_pts
int64_t first_pts
Definition: roqaudioenc.c:41
AV_CODEC_ID_ROQ_DPCM
@ AV_CODEC_ID_ROQ_DPCM
Definition: codec_id.h:427
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
codec_internal.h
roq_dpcm_encode_init
static av_cold int roq_dpcm_encode_init(AVCodecContext *avctx)
Definition: roqaudioenc.c:54
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
roq_dpcm_encode_frame
static int roq_dpcm_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: roqaudioenc.c:122
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:162
dpcm_predict
static unsigned char dpcm_predict(short *previous, short current)
Definition: roqaudioenc.c:82
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:191
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:33
AVCodecContext
main external API structure.
Definition: avcodec.h:426
ROQDPCMContext::buffered_samples
int buffered_samples
Definition: roqaudioenc.c:39
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:79
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:76
roq_dpcm_encode_close
static av_cold int roq_dpcm_encode_close(AVCodecContext *avctx)
Definition: roqaudioenc.c:45
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:453
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
bytestream.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ROQ_FRAME_SIZE
#define ROQ_FRAME_SIZE
Definition: roqaudioenc.c:30
ROQDPCMContext::lastSample
short lastSample[2]
Definition: roqaudioenc.c:37