1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
/* encavcodecaudio.c
Copyright (c) 2003-2012 HandBrake Team
This file is part of the HandBrake source code
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License v2.
For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
*/
#include "hb.h"
#include "hbffmpeg.h"
#include "audio_remap.h"
struct hb_work_private_s
{
hb_job_t * job;
AVCodecContext * context;
int out_discrete_channels;
int samples_per_frame;
unsigned long input_samples;
unsigned long output_bytes;
hb_list_t * list;
uint8_t * buf;
AVAudioResampleContext *avresample;
int *remap_table;
};
static int encavcodecaInit( hb_work_object_t *, hb_job_t * );
static int encavcodecaWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
static void encavcodecaClose( hb_work_object_t * );
hb_work_object_t hb_encavcodeca =
{
WORK_ENCAVCODEC_AUDIO,
"AVCodec Audio encoder (libavcodec)",
encavcodecaInit,
encavcodecaWork,
encavcodecaClose
};
static int encavcodecaInit( hb_work_object_t * w, hb_job_t * job )
{
AVCodec * codec;
AVCodecContext * context;
hb_audio_t * audio = w->audio;
hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
w->private_data = pv;
pv->job = job;
codec = avcodec_find_encoder( w->codec_param );
if( !codec )
{
hb_log( "encavcodecaInit: avcodec_find_encoder "
"failed" );
return 1;
}
context = avcodec_alloc_context3(codec);
int mode;
context->channel_layout = hb_ff_mixdown_xlat(audio->config.out.mixdown, &mode);
pv->out_discrete_channels = hb_mixdown_get_discrete_channel_count(audio->config.out.mixdown);
if (pv->out_discrete_channels > 2 &&
audio->config.in.channel_map != &hb_libav_chan_map)
{
pv->remap_table = hb_audio_remap_build_table(context->channel_layout,
audio->config.in.channel_map,
&hb_libav_chan_map);
}
else
{
pv->remap_table = NULL;
}
AVDictionary *av_opts = NULL;
if (w->codec_param == CODEC_ID_AAC)
{
av_dict_set(&av_opts, "stereo_mode", "ms_off", 0);
}
else if (w->codec_param == CODEC_ID_AC3 && mode != AV_MATRIX_ENCODING_NONE)
{
av_dict_set(&av_opts, "dsur_mode", "on", 0);
}
if( audio->config.out.bitrate > 0 )
context->bit_rate = audio->config.out.bitrate * 1000;
else if( audio->config.out.quality >= 0 )
{
context->global_quality = audio->config.out.quality * FF_QP2LAMBDA;
context->flags |= CODEC_FLAG_QSCALE;
}
if( audio->config.out.compression_level >= 0 )
context->compression_level = audio->config.out.compression_level;
context->sample_rate = audio->config.out.samplerate;
context->channels = pv->out_discrete_channels;
// Try to set format to float. Fallback to whatever is supported.
hb_ff_set_sample_fmt( context, codec );
if( hb_avcodec_open( context, codec, &av_opts, 0 ) )
{
hb_log( "encavcodecaInit: avcodec_open failed" );
return 1;
}
// avcodec_open populates the opts dictionary with the
// things it didn't recognize.
AVDictionaryEntry *t = NULL;
while( ( t = av_dict_get( av_opts, "", t, AV_DICT_IGNORE_SUFFIX ) ) )
{
hb_log( "encavcodecaInit: Unknown avcodec option %s", t->key );
}
av_dict_free( &av_opts );
pv->context = context;
audio->config.out.samples_per_frame = pv->samples_per_frame = context->frame_size;
pv->input_samples = pv->samples_per_frame * pv->out_discrete_channels;
// Set a reasonable maximum output size
pv->output_bytes = context->frame_size *
av_get_bytes_per_sample(context->sample_fmt) *
context->channels;
pv->buf = malloc( pv->input_samples * sizeof( float ) );
pv->list = hb_list_init();
if ( context->extradata )
{
memcpy( w->config->extradata.bytes, context->extradata, context->extradata_size );
w->config->extradata.length = context->extradata_size;
}
// Check if sample format conversion is necessary
if (AV_SAMPLE_FMT_FLT != pv->context->sample_fmt)
{
// Set up avresample to do conversion
pv->avresample = avresample_alloc_context();
if (pv->avresample == NULL)
{
hb_error("Failed to initialize avresample");
return 1;
}
uint64_t layout;
layout = hb_ff_layout_xlat(context->channel_layout, context->channels);
av_opt_set_int(pv->avresample, "in_channel_layout", layout, 0);
av_opt_set_int(pv->avresample, "out_channel_layout", layout, 0);
av_opt_set_int(pv->avresample, "in_sample_fmt", AV_SAMPLE_FMT_FLT, 0);
av_opt_set_int(pv->avresample, "out_sample_fmt", context->sample_fmt, 0);
if (avresample_open(pv->avresample) < 0)
{
hb_error("Failed to open avresample");
avresample_free(&pv->avresample);
return 1;
}
}
return 0;
}
/***********************************************************************
* Close
***********************************************************************
*
**********************************************************************/
// Some encoders (e.g. flac) require a final NULL encode in order to
// finalize things.
static void Finalize( hb_work_object_t * w )
{
hb_work_private_t * pv = w->private_data;
hb_buffer_t * buf;
// Finalize with NULL input needed by FLAC to generate md5sum
// in context extradata
// Prepare output packet
AVPacket pkt;
int got_packet;
buf = hb_buffer_init( pv->output_bytes );
av_init_packet(&pkt);
pkt.data = buf->data;
pkt.size = buf->alloc;
avcodec_encode_audio2( pv->context, &pkt, NULL, &got_packet);
hb_buffer_close( &buf );
// Then we need to recopy the header since it was modified
if ( pv->context->extradata )
{
memcpy( w->config->extradata.bytes, pv->context->extradata, pv->context->extradata_size );
w->config->extradata.length = pv->context->extradata_size;
}
}
static void encavcodecaClose( hb_work_object_t * w )
{
hb_work_private_t * pv = w->private_data;
if ( pv )
{
if( pv->context )
{
Finalize( w );
hb_deep_log( 2, "encavcodeca: closing libavcodec" );
if ( pv->context->codec )
avcodec_flush_buffers( pv->context );
hb_avcodec_close( pv->context );
}
if ( pv->buf )
{
free( pv->buf );
pv->buf = NULL;
}
if ( pv->list )
hb_list_empty( &pv->list );
if (pv->avresample != NULL)
{
avresample_free(&pv->avresample);
}
free( pv );
w->private_data = NULL;
}
}
static void convertAudioFormat( hb_work_private_t *pv, AVFrame *frame )
{
if (pv->avresample != NULL)
{
int out_samples, out_linesize;
av_samples_get_buffer_size(&out_linesize, pv->context->channels,
frame->nb_samples, pv->context->sample_fmt, 0);
out_samples = avresample_convert(pv->avresample,
(void **)frame->data, out_linesize, frame->nb_samples,
(void **)frame->data, frame->linesize[0], frame->nb_samples);
if (out_samples < 0)
{
hb_error("avresample_convert() failed");
}
}
}
static hb_buffer_t * Encode( hb_work_object_t * w )
{
hb_work_private_t * pv = w->private_data;
uint64_t pts, pos;
hb_audio_t * audio = w->audio;
hb_buffer_t * buf;
if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
{
return NULL;
}
hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
&pts, &pos);
if (pv->remap_table != NULL)
{
hb_audio_remap(pv->out_discrete_channels, pv->samples_per_frame,
(hb_sample_t*)pv->buf, pv->remap_table);
}
// Prepare input frame
AVFrame frame;
frame.nb_samples= pv->samples_per_frame;
int size = av_samples_get_buffer_size(NULL, pv->context->channels,
frame.nb_samples, pv->context->sample_fmt, 1);
avcodec_fill_audio_frame(&frame, pv->context->channels,
pv->context->sample_fmt, pv->buf, size, 1);
frame.pts = pts + 90000 * pos / pv->out_discrete_channels / sizeof( float ) / audio->config.out.samplerate;
// libav requires that timebase of audio input frames to be
// in sample_rate units.
frame.pts = av_rescale( frame.pts, pv->context->sample_rate, 90000);
// Do we need to convert our internal float format?
convertAudioFormat(pv, &frame);
// Prepare output packet
AVPacket pkt;
int got_packet;
buf = hb_buffer_init( pv->output_bytes );
av_init_packet(&pkt);
pkt.data = buf->data;
pkt.size = buf->alloc;
// Encode
int ret = avcodec_encode_audio2( pv->context, &pkt, &frame, &got_packet);
if ( ret < 0 )
{
hb_log( "encavcodeca: avcodec_encode_audio failed" );
hb_buffer_close( &buf );
return NULL;
}
if ( got_packet && pkt.size )
{
buf->size = pkt.size;
// The output pts from libav is in context->time_base. Convert
// it back to our timebase.
//
// Also account for the "delay" factor that libav seems to arbitrarily
// subtract from the packet. Not sure WTH they think they are doing
// by offseting the value in a negative direction.
buf->s.start = av_rescale_q( pkt.pts + pv->context->delay,
pv->context->time_base, (AVRational){ 1, 90000 });
buf->s.stop = buf->s.start + 90000 * pv->samples_per_frame / audio->config.out.samplerate;
buf->s.type = AUDIO_BUF;
buf->s.frametype = HB_FRAME_AUDIO;
}
else
{
hb_buffer_close( &buf );
return Encode( w );
}
return buf;
}
static hb_buffer_t * Flush( hb_work_object_t * w )
{
hb_buffer_t *first, *buf, *last;
first = last = buf = Encode( w );
while( buf )
{
last = buf;
buf->next = Encode( w );
buf = buf->next;
}
if( last )
{
last->next = hb_buffer_init( 0 );
}
else
{
first = hb_buffer_init( 0 );
}
return first;
}
/***********************************************************************
* Work
***********************************************************************
*
**********************************************************************/
static int encavcodecaWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
hb_buffer_t ** buf_out )
{
hb_work_private_t * pv = w->private_data;
hb_buffer_t * in = *buf_in, * buf;
if ( in->size <= 0 )
{
/* EOF on input - send it downstream & say we're done */
*buf_out = Flush( w );
return HB_WORK_DONE;
}
if ( pv->context == NULL || pv->context->codec == NULL )
{
// No encoder context. Nothing we can do.
return HB_WORK_OK;
}
hb_list_add( pv->list, in );
*buf_in = NULL;
*buf_out = buf = Encode( w );
while ( buf )
{
buf->next = Encode( w );
buf = buf->next;
}
return HB_WORK_OK;
}
|