FFmpeg
libwavpackenc.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <wavpack/wavpack.h>
20 #include <string.h>
21 
22 #include "libavutil/attributes.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/samplefmt.h"
25 
26 #include "audio_frame_queue.h"
27 #include "avcodec.h"
28 #include "internal.h"
29 
30 #define WV_DEFAULT_BLOCK_SIZE 32768
31 
32 typedef struct LibWavpackContext {
33  const AVClass *class;
36 
38  int user_size;
39 
42 
44  const AVFrame *frame, int *got_output)
45 {
46  LibWavpackContext *s = avctx->priv_data;
47  int ret;
48 
49  s->got_output = 0;
50  s->pkt = pkt;
51  s->user_size = pkt->size;
52 
53  if (frame) {
54  ret = ff_af_queue_add(&s->afq, frame);
55  if (ret < 0)
56  return ret;
57 
58  ret = WavpackPackSamples(s->wv, (int32_t*)frame->data[0], frame->nb_samples);
59  if (!ret) {
60  av_log(avctx, AV_LOG_ERROR, "Error encoding a frame: %s\n",
61  WavpackGetErrorMessage(s->wv));
62  return AVERROR_UNKNOWN;
63  }
64  }
65 
66  if (!s->got_output &&
67  (!frame || frame->nb_samples < avctx->frame_size)) {
68  ret = WavpackFlushSamples(s->wv);
69  if (!ret) {
70  av_log(avctx, AV_LOG_ERROR, "Error flushing the encoder: %s\n",
71  WavpackGetErrorMessage(s->wv));
72  return AVERROR_UNKNOWN;
73  }
74  }
75 
76  if (s->got_output) {
77  ff_af_queue_remove(&s->afq, avctx->frame_size, &pkt->pts, &pkt->duration);
78  *got_output = 1;
79  }
80 
81  return 0;
82 }
83 
84 static int encode_callback(void *id, void *data, int32_t count)
85 {
86  AVCodecContext *avctx = id;
87  LibWavpackContext *s = avctx->priv_data;
88  int ret, offset = s->pkt->size;
89 
90  if (s->user_size) {
91  if (s->user_size - count < s->pkt->size) {
92  av_log(avctx, AV_LOG_ERROR, "Provided packet too small.\n");
93  return 0;
94  }
95  s->pkt->size += count;
96  } else {
97  ret = av_grow_packet(s->pkt, count);
98  if (ret < 0) {
99  av_log(avctx, AV_LOG_ERROR, "Error allocating output packet.\n");
100  return 0;
101  }
102  }
103 
104  memcpy(s->pkt->data + offset, data, count);
105 
106  s->got_output = 1;
107 
108  return 1;
109 }
110 
112 {
113  LibWavpackContext *s = avctx->priv_data;
114  WavpackConfig config = { 0 };
115  int ret;
116 
117  s->wv = WavpackOpenFileOutput(encode_callback, avctx, NULL);
118  if (!s->wv) {
119  av_log(avctx, AV_LOG_ERROR, "Error allocating the encoder.\n");
120  return AVERROR(ENOMEM);
121  }
122 
123  if (!avctx->frame_size)
125 
126  config.bytes_per_sample = 4;
127  config.bits_per_sample = 32;
128  config.block_samples = avctx->frame_size;
129  config.channel_mask = avctx->channel_layout;
130  config.num_channels = avctx->channels;
131  config.sample_rate = avctx->sample_rate;
132 
134  if (avctx->compression_level >= 3) {
135  config.flags |= CONFIG_VERY_HIGH_FLAG;
136 
137  if (avctx->compression_level >= 8)
138  config.xmode = 6;
139  else if (avctx->compression_level >= 7)
140  config.xmode = 5;
141  else if (avctx->compression_level >= 6)
142  config.xmode = 4;
143  else if (avctx->compression_level >= 5)
144  config.xmode = 3;
145  else if (avctx->compression_level >= 4)
146  config.xmode = 2;
147  } else if (avctx->compression_level >= 2)
148  config.flags |= CONFIG_HIGH_FLAG;
149  else if (avctx->compression_level < 1)
150  config.flags |= CONFIG_FAST_FLAG;
151  }
152 
153  ret = WavpackSetConfiguration(s->wv, &config, -1);
154  if (!ret)
155  goto fail;
156 
157  ret = WavpackPackInit(s->wv);
158  if (!ret)
159  goto fail;
160 
161  ff_af_queue_init(avctx, &s->afq);
162 
163  return 0;
164 
165 fail:
166  av_log(avctx, AV_LOG_ERROR, "Error configuring the encoder: %s.\n",
167  WavpackGetErrorMessage(s->wv));
168  WavpackCloseFile(s->wv);
169  return AVERROR_UNKNOWN;
170 }
171 
173 {
174  LibWavpackContext *s = avctx->priv_data;
175 
176  WavpackCloseFile(s->wv);
177 
178  ff_af_queue_close(&s->afq);
179 
180  return 0;
181 }
182 
184  .name = "libwavpack",
185  .type = AVMEDIA_TYPE_AUDIO,
186  .id = AV_CODEC_ID_WAVPACK,
187  .priv_data_size = sizeof(LibWavpackContext),
189  .encode2 = wavpack_encode_frame,
190  .close = wavpack_encode_close,
192  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S32,
194  .wrapper_name = "libwavpack",
195 };
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2245
AVCodec
AVCodec.
Definition: avcodec.h:3481
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
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
opt.h
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2276
ff_af_queue_remove
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
Definition: audio_frame_queue.c:75
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:2225
ff_af_queue_close
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
Definition: audio_frame_queue.c:36
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:686
av_grow_packet
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: avpacket.c:109
ff_af_queue_init
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
Definition: audio_frame_queue.c:28
count
void INT64 INT64 count
Definition: avisynth_c.h:767
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
LibWavpackContext::afq
AudioFrameQueue afq
Definition: libwavpackenc.c:35
internal.h
data
const char data[16]
Definition: mxf.c:91
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1495
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
FF_COMPRESSION_DEFAULT
#define FF_COMPRESSION_DEFAULT
Definition: avcodec.h:1638
WavpackContext
Definition: wavpack.c:75
fail
#define fail()
Definition: checkasm.h:120
audio_frame_queue.h
samplefmt.h
ff_af_queue_add
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
Definition: audio_frame_queue.c:44
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_cold
#define av_cold
Definition: attributes.h:84
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
wavpack_encode_close
static av_cold int wavpack_encode_close(AVCodecContext *avctx)
Definition: libwavpackenc.c:172
AudioFrameQueue
Definition: audio_frame_queue.h:32
LibWavpackContext::got_output
int got_output
Definition: libwavpackenc.c:40
int32_t
int32_t
Definition: audio_convert.c:194
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
wavpack_encode_frame
static int wavpack_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_output)
Definition: libwavpackenc.c:43
WV_DEFAULT_BLOCK_SIZE
#define WV_DEFAULT_BLOCK_SIZE
Definition: libwavpackenc.c:30
LibWavpackContext
Definition: libwavpackenc.c:32
AVPacket::size
int size
Definition: avcodec.h:1478
id
enum AVCodecID id
Definition: extract_extradata_bsf.c:329
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
offset
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 offset
Definition: writing_filters.txt:86
attributes.h
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:2226
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1470
LibWavpackContext::pkt
AVPacket * pkt
Definition: libwavpackenc.c:37
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: avcodec.h:3488
avcodec.h
LibWavpackContext::wv
WavpackContext * wv
Definition: libwavpackenc.c:34
ret
ret
Definition: filter_design.txt:187
encode_callback
static int encode_callback(void *id, void *data, int32_t count)
Definition: libwavpackenc.c:84
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
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
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: avcodec.h:1006
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:1592
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
ff_libwavpack_encoder
AVCodec ff_libwavpack_encoder
Definition: libwavpackenc.c:183
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AV_CODEC_ID_WAVPACK
@ AV_CODEC_ID_WAVPACK
Definition: avcodec.h:589
AV_CODEC_CAP_SMALL_LAST_FRAME
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: avcodec.h:1011
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:62
wavpack_encode_init
static av_cold int wavpack_encode_init(AVCodecContext *avctx)
Definition: libwavpackenc.c:111
AVCodecContext::compression_level
int compression_level
Definition: avcodec.h:1637
LibWavpackContext::user_size
int user_size
Definition: libwavpackenc.c:38