27
27
#include <alsa/pcm_plugin.h>
28
28
#include AVCODEC_HEADER
29
29
#include <libavutil/avutil.h>
31
#if LIBAVCODEC_VERSION_MAJOR >= 53 && LIBAVCODEC_VERSION_MINOR >= 34
30
32
#include <libavutil/audioconvert.h>
31
33
#include <libavutil/mem.h>
34
#define USE_AVCODEC_FRAME
37
/* some compatibility wrappers */
38
#ifndef AV_VERSION_INT
39
#define AV_VERSION_INT(a, b, c) (((a) << 16) | ((b) << 8) | (c))
41
#ifndef LIBAVCODEC_VERSION_INT
42
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
43
LIBAVCODEC_VERSION_MINOR, \
44
LIBAVCODEC_VERSION_MICRO)
47
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 0, 0)
48
#ifndef AV_CH_LAYOUT_STEREO
49
#define AV_CH_LAYOUT_STEREO CH_LAYOUT_STEREO
50
#define AV_CH_LAYOUT_QUAD CH_LAYOUT_QUAD
51
#define AV_CH_LAYOUT_5POINT1 CH_LAYOUT_5POINT1
55
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(52, 95, 0)
56
#ifndef AV_SAMPLE_FMT_S16
57
#define AV_SAMPLE_FMT_S16 SAMPLE_FMT_S16
34
63
snd_pcm_ioplug_t io;
48
78
unsigned int slave_period_size;
49
79
unsigned int slave_buffer_size;
50
80
snd_pcm_hw_params_t *hw_params;
81
#ifdef USE_AVCODEC_FRAME
87
#ifdef USE_AVCODEC_FRAME
88
#define use_planar(rec) (rec)->is_planar
90
#define use_planar(rec) 0
93
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(53, 34, 0)
94
static int do_encode(struct a52_ctx *rec)
97
.data = rec->outbuf + 8,
98
.size = rec->outbuf_size - 8
102
avcodec_encode_audio2(rec->avctx, &pkt, rec->frame, &got_frame);
106
static int do_encode(struct a52_ctx *rec)
108
return avcodec_encode_audio(rec->avctx, rec->outbuf + 8,
109
rec->outbuf_size - 8,
53
114
/* convert the PCM data to A52 stream in IEC958 */
54
115
static void convert_data(struct a52_ctx *rec)
117
int out_bytes = do_encode(rec);
58
out_bytes = avcodec_encode_audio(rec->avctx, rec->outbuf + 8,
61
119
rec->outbuf[0] = 0xf8; /* sync words */
62
120
rec->outbuf[1] = 0x72;
63
121
rec->outbuf[2] = 0x4e;
164
#ifdef USE_AVCODEC_FRAME
165
static void clear_remaining_planar_data(snd_pcm_ioplug_t *io)
167
struct a52_ctx *rec = io->private_data;
170
for (i = 0; i < io->channels; i++)
171
memset(rec->frame->data[i] + rec->filled * 2, 0,
172
(rec->avctx->frame_size - rec->filled) * 2);
175
#define clear_remaining_planar_data(io) /*NOP*/
106
178
static int a52_drain(snd_pcm_ioplug_t *io)
108
180
struct a52_ctx *rec = io->private_data;
112
184
if ((err = write_out_pending(io, rec)) < 0)
114
186
/* remaining data must be converted and sent out */
115
memset(rec->inbuf + rec->filled * io->channels, 0,
116
(rec->avctx->frame_size - rec->filled) * io->channels * 2);
188
clear_remaining_planar_data(io);
190
memset(rec->inbuf + rec->filled * io->channels, 0,
191
(rec->avctx->frame_size - rec->filled) * io->channels * 2);
117
193
convert_data(rec);
119
195
err = write_out_pending(io, rec);
156
232
short *src, *dst;
157
233
unsigned int src_step;
235
static unsigned int ch_index[3][6] = {
238
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 26, 0)
239
/* current libavcodec expects SMPTE order */
240
{ 0, 1, 4, 5, 2, 3 },
242
/* libavcodec older than r18540 expects A52 order */
243
{ 0, 4, 1, 2, 3, 5 },
160
247
if ((err = write_out_pending(io, rec)) < 0)
166
253
dst = rec->inbuf + rec->filled * io->channels;
254
if (!use_planar(rec) && interleaved) {
168
255
memcpy(dst, areas->addr + offset * io->channels * 2,
169
256
size * io->channels * 2);
171
258
unsigned int i, ch, dst_step;
173
static unsigned int ch_index[3][6] = {
176
#if LIBAVCODEC_VERSION_MAJOR > 52 || (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR >= 26)
177
/* current libavcodec expects SMPTE order */
178
{ 0, 1, 4, 5, 2, 3 },
180
/* libavcodec older than r18540 expects A52 order */
181
{ 0, 4, 1, 2, 3, 5 },
184
261
/* flatten copy to n-channel interleaved */
185
262
dst_step = io->channels;
186
263
for (ch = 0; ch < io->channels; ch++, dst++) {
187
264
const snd_pcm_channel_area_t *ap;
188
265
ap = &areas[ch_index[io->channels / 2 - 1][ch]];
190
266
src = (short *)(ap->addr +
191
267
(ap->first + offset * ap->step) / 8);
269
#ifdef USE_AVCODEC_FRAME
270
if (use_planar(rec)) {
271
memcpy(rec->frame->data[ch], src, size * 2);
192
276
src_step = ap->step / 16; /* in word */
193
277
for (i = 0; i < size; i++) {
427
524
* Allocate internal buffers and set up libavcodec
429
static int a52_prepare(snd_pcm_ioplug_t *io)
527
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 3, 0)
528
static void set_channel_layout(snd_pcm_ioplug_t *io)
431
530
struct a52_ctx *rec = io->private_data;
435
rec->avctx = avcodec_alloc_context3(rec->codec);
439
rec->avctx->bit_rate = rec->bitrate * 1000;
440
rec->avctx->sample_rate = io->rate;
441
rec->avctx->channels = io->channels;
442
#if LIBAVCODEC_VERSION_MAJOR > 52 || (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR >= 95)
443
rec->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
445
rec->avctx->sample_fmt = SAMPLE_FMT_S16;
447
#if LIBAVCODEC_VERSION_MAJOR > 52 || (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR >= 3)
448
531
switch (io->channels) {
450
533
rec->avctx->channel_layout = AV_CH_LAYOUT_STEREO;
546
#define set_channel_layout(io) /* NOP */
463
if (avcodec_open2(rec->avctx, rec->codec, NULL) < 0)
549
static int alloc_input_buffer(snd_pcm_ioplug_t *io)
551
struct a52_ctx *rec = io->private_data;
552
#ifdef USE_AVCODEC_FRAME
553
rec->frame = avcodec_alloc_frame();
556
if (av_samples_alloc(rec->frame->data, rec->frame->linesize,
557
io->channels, rec->avctx->frame_size,
558
rec->avctx->sample_fmt, 0) < 0)
560
rec->frame->nb_samples = rec->avctx->frame_size;
561
rec->inbuf = (short *)rec->frame->data[0];
466
563
rec->inbuf = malloc(rec->avctx->frame_size * 2 * io->channels);
570
static int a52_prepare(snd_pcm_ioplug_t *io)
572
struct a52_ctx *rec = io->private_data;
577
#ifdef USE_AVCODEC_FRAME
578
rec->avctx = avcodec_alloc_context3(rec->codec);
580
rec->avctx = avcodec_alloc_context();
585
rec->avctx->bit_rate = rec->bitrate * 1000;
586
rec->avctx->sample_rate = io->rate;
587
rec->avctx->channels = io->channels;
588
rec->avctx->sample_fmt = rec->av_format;
590
set_channel_layout(io);
593
#ifdef USE_AVCODEC_FRAME
594
err = avcodec_open2(rec->avctx, rec->codec, NULL);
596
err = avcodec_open(rec->avctx, rec->codec);
469
601
rec->outbuf_size = rec->avctx->frame_size * 4;
470
602
rec->outbuf = malloc(rec->outbuf_size);
471
603
if (! rec->outbuf)
606
if (alloc_input_buffer(io))
474
609
rec->transfer = 0;
548
683
static int a52_set_hw_constraint(struct a52_ctx *rec)
550
unsigned int accesses[] = {
685
static unsigned int accesses[] = {
551
686
SND_PCM_ACCESS_MMAP_INTERLEAVED,
552
687
SND_PCM_ACCESS_MMAP_NONINTERLEAVED,
553
688
SND_PCM_ACCESS_RW_INTERLEAVED,
554
689
SND_PCM_ACCESS_RW_NONINTERLEAVED
691
static unsigned int accesses_planar[] = {
692
SND_PCM_ACCESS_MMAP_NONINTERLEAVED,
693
SND_PCM_ACCESS_RW_NONINTERLEAVED
556
695
unsigned int formats[] = { SND_PCM_FORMAT_S16 };
558
697
snd_pcm_uframes_t buffer_max;
559
698
unsigned int period_bytes, max_periods;
561
if ((err = snd_pcm_ioplug_set_param_list(&rec->io, SND_PCM_IOPLUG_HW_ACCESS,
562
ARRAY_SIZE(accesses), accesses)) < 0 ||
563
(err = snd_pcm_ioplug_set_param_list(&rec->io, SND_PCM_IOPLUG_HW_FORMAT,
701
err = snd_pcm_ioplug_set_param_list(&rec->io,
702
SND_PCM_IOPLUG_HW_ACCESS,
703
ARRAY_SIZE(accesses_planar),
706
err = snd_pcm_ioplug_set_param_list(&rec->io,
707
SND_PCM_IOPLUG_HW_ACCESS,
708
ARRAY_SIZE(accesses),
713
if ((err = snd_pcm_ioplug_set_param_list(&rec->io, SND_PCM_IOPLUG_HW_FORMAT,
564
714
ARRAY_SIZE(formats), formats)) < 0 ||
565
715
(err = snd_pcm_ioplug_set_param_minmax(&rec->io, SND_PCM_IOPLUG_HW_CHANNELS,
566
716
rec->channels, rec->channels)) < 0 ||
746
899
rec->io.mmap_rw = 0;
747
900
rec->io.callback = &a52_ops;
748
901
rec->io.private_data = rec;
902
#ifdef USE_AVCODEC_FRAME
903
rec->av_format = rec->codec->sample_fmts[0];
904
rec->is_planar = av_sample_fmt_is_planar(rec->av_format);
906
rec->av_format = AV_SAMPLE_FMT_S16;
750
909
err = snd_pcm_ioplug_create(&rec->io, name, stream, mode);