27
27
#include <alsa/pcm_plugin.h>
28
28
#include AVCODEC_HEADER
30
#if LIBAVCODEC_VERSION_MAJOR >= 53 && LIBAVCODEC_VERSION_MINOR >= 34
31
#include <libavutil/audioconvert.h>
32
#include <libavutil/mem.h>
33
#define USE_AVCODEC_FRAME
36
/* some compatibility wrappers */
37
#ifndef AV_VERSION_INT
38
#define AV_VERSION_INT(a, b, c) (((a) << 16) | ((b) << 8) | (c))
40
#ifndef LIBAVCODEC_VERSION_INT
41
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
42
LIBAVCODEC_VERSION_MINOR, \
43
LIBAVCODEC_VERSION_MICRO)
46
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 0, 0)
47
#ifndef AV_CH_LAYOUT_STEREO
48
#define AV_CH_LAYOUT_STEREO CH_LAYOUT_STEREO
49
#define AV_CH_LAYOUT_QUAD CH_LAYOUT_QUAD
50
#define AV_CH_LAYOUT_5POINT1 CH_LAYOUT_5POINT1
54
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(52, 95, 0)
55
#ifndef AV_SAMPLE_FMT_S16
56
#define AV_SAMPLE_FMT_S16 SAMPLE_FMT_S16
31
62
snd_pcm_ioplug_t io;
34
65
AVCodecContext *avctx;
35
66
snd_pcm_format_t format;
36
68
unsigned int channels;
38
70
unsigned int bitrate;
45
77
unsigned int slave_period_size;
46
78
unsigned int slave_buffer_size;
47
79
snd_pcm_hw_params_t *hw_params;
80
#ifdef USE_AVCODEC_FRAME
86
#ifdef USE_AVCODEC_FRAME
87
#define use_planar(rec) (rec)->is_planar
89
#define use_planar(rec) 0
92
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(53, 34, 0)
93
static int do_encode(struct a52_ctx *rec)
96
.data = rec->outbuf + 8,
97
.size = rec->outbuf_size - 8
101
avcodec_encode_audio2(rec->avctx, &pkt, rec->frame, &got_frame);
105
static int do_encode(struct a52_ctx *rec)
107
return avcodec_encode_audio(rec->avctx, rec->outbuf + 8,
108
rec->outbuf_size - 8,
50
113
/* convert the PCM data to A52 stream in IEC958 */
51
114
static void convert_data(struct a52_ctx *rec)
116
int out_bytes = do_encode(rec);
55
out_bytes = avcodec_encode_audio(rec->avctx, rec->outbuf + 8,
58
118
rec->outbuf[0] = 0xf8; /* sync words */
59
119
rec->outbuf[1] = 0x72;
60
120
rec->outbuf[2] = 0x4e;
163
#ifdef USE_AVCODEC_FRAME
164
static void clear_remaining_planar_data(snd_pcm_ioplug_t *io)
166
struct a52_ctx *rec = io->private_data;
169
for (i = 0; i < io->channels; i++)
170
memset(rec->frame->data[i] + rec->filled * 2, 0,
171
(rec->avctx->frame_size - rec->filled) * 2);
174
#define clear_remaining_planar_data(io) /*NOP*/
103
177
static int a52_drain(snd_pcm_ioplug_t *io)
105
179
struct a52_ctx *rec = io->private_data;
109
183
if ((err = write_out_pending(io, rec)) < 0)
111
185
/* remaining data must be converted and sent out */
112
memset(rec->inbuf + rec->filled * io->channels, 0,
113
(rec->avctx->frame_size - rec->filled) * io->channels * 2);
187
clear_remaining_planar_data(io);
189
memset(rec->inbuf + rec->filled * io->channels, 0,
190
(rec->avctx->frame_size - rec->filled) * io->channels * 2);
114
192
convert_data(rec);
116
194
err = write_out_pending(io, rec);
153
231
short *src, *dst;
154
232
unsigned int src_step;
234
static unsigned int ch_index[3][6] = {
237
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 26, 0)
238
/* current libavcodec expects SMPTE order */
239
{ 0, 1, 4, 5, 2, 3 },
241
/* libavcodec older than r18540 expects A52 order */
242
{ 0, 4, 1, 2, 3, 5 },
157
246
if ((err = write_out_pending(io, rec)) < 0)
163
252
dst = rec->inbuf + rec->filled * io->channels;
253
if (!use_planar(rec) && interleaved) {
165
254
memcpy(dst, areas->addr + offset * io->channels * 2,
166
255
size * io->channels * 2);
168
257
unsigned int i, ch, dst_step;
170
static unsigned int ch_index[3][6] = {
173
#if LIBAVCODEC_VERSION_MAJOR > 52 || (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR >= 26)
174
/* current libavcodec expects SMPTE order */
175
{ 0, 1, 4, 5, 2, 3 },
177
/* libavcodec older than r18540 expects A52 order */
178
{ 0, 4, 1, 2, 3, 5 },
181
260
/* flatten copy to n-channel interleaved */
182
261
dst_step = io->channels;
183
262
for (ch = 0; ch < io->channels; ch++, dst++) {
184
263
const snd_pcm_channel_area_t *ap;
185
264
ap = &areas[ch_index[io->channels / 2 - 1][ch]];
187
265
src = (short *)(ap->addr +
188
266
(ap->first + offset * ap->step) / 8);
268
#ifdef USE_AVCODEC_FRAME
269
if (use_planar(rec)) {
270
memcpy(rec->frame->data[ch], src, size * 2);
189
275
src_step = ap->step / 16; /* in word */
190
276
for (i = 0; i < size; i++) {
424
523
* Allocate internal buffers and set up libavcodec
526
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 3, 0)
527
static void set_channel_layout(snd_pcm_ioplug_t *io)
529
struct a52_ctx *rec = io->private_data;
530
switch (io->channels) {
532
rec->avctx->channel_layout = AV_CH_LAYOUT_STEREO;
535
rec->avctx->channel_layout = AV_CH_LAYOUT_QUAD;
538
rec->avctx->channel_layout = AV_CH_LAYOUT_5POINT1;
545
#define set_channel_layout(io) /* NOP */
548
static int alloc_input_buffer(snd_pcm_ioplug_t *io)
550
struct a52_ctx *rec = io->private_data;
551
#ifdef USE_AVCODEC_FRAME
552
rec->frame = avcodec_alloc_frame();
555
if (av_samples_alloc(rec->frame->data, rec->frame->linesize,
556
io->channels, rec->avctx->frame_size,
557
rec->avctx->sample_fmt, 0) < 0)
559
rec->frame->nb_samples = rec->avctx->frame_size;
560
rec->inbuf = (short *)rec->frame->data[0];
562
rec->inbuf = malloc(rec->avctx->frame_size * 2 * io->channels);
426
569
static int a52_prepare(snd_pcm_ioplug_t *io)
428
571
struct a52_ctx *rec = io->private_data;
576
#ifdef USE_AVCODEC_FRAME
577
rec->avctx = avcodec_alloc_context3(rec->codec);
432
579
rec->avctx = avcodec_alloc_context();
436
584
rec->avctx->bit_rate = rec->bitrate * 1000;
437
585
rec->avctx->sample_rate = io->rate;
438
586
rec->avctx->channels = io->channels;
439
#if LIBAVCODEC_VERSION_MAJOR > 52 || (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR >= 95)
440
rec->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
587
rec->avctx->sample_fmt = rec->av_format;
589
set_channel_layout(io);
592
#ifdef USE_AVCODEC_FRAME
593
err = avcodec_open2(rec->avctx, rec->codec, NULL);
442
rec->avctx->sample_fmt = SAMPLE_FMT_S16;
444
#if LIBAVCODEC_VERSION_MAJOR > 52 || (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR >= 3)
445
switch (io->channels) {
447
rec->avctx->channel_layout = CH_LAYOUT_STEREO;
450
rec->avctx->channel_layout = CH_LAYOUT_QUAD;
453
rec->avctx->channel_layout = CH_LAYOUT_5POINT1;
460
if (avcodec_open(rec->avctx, rec->codec) < 0)
595
err = avcodec_open(rec->avctx, rec->codec);
463
rec->inbuf = malloc(rec->avctx->frame_size * 2 * io->channels);
466
600
rec->outbuf_size = rec->avctx->frame_size * 4;
467
601
rec->outbuf = malloc(rec->outbuf_size);
468
602
if (! rec->outbuf)
605
if (alloc_input_buffer(io))
471
608
rec->transfer = 0;
545
682
static int a52_set_hw_constraint(struct a52_ctx *rec)
547
unsigned int accesses[] = {
684
static unsigned int accesses[] = {
548
685
SND_PCM_ACCESS_MMAP_INTERLEAVED,
549
686
SND_PCM_ACCESS_MMAP_NONINTERLEAVED,
550
687
SND_PCM_ACCESS_RW_INTERLEAVED,
551
688
SND_PCM_ACCESS_RW_NONINTERLEAVED
690
static unsigned int accesses_planar[] = {
691
SND_PCM_ACCESS_MMAP_NONINTERLEAVED,
692
SND_PCM_ACCESS_RW_NONINTERLEAVED
553
694
unsigned int formats[] = { SND_PCM_FORMAT_S16 };
555
696
snd_pcm_uframes_t buffer_max;
556
697
unsigned int period_bytes, max_periods;
558
if ((err = snd_pcm_ioplug_set_param_list(&rec->io, SND_PCM_IOPLUG_HW_ACCESS,
559
ARRAY_SIZE(accesses), accesses)) < 0 ||
560
(err = snd_pcm_ioplug_set_param_list(&rec->io, SND_PCM_IOPLUG_HW_FORMAT,
700
err = snd_pcm_ioplug_set_param_list(&rec->io,
701
SND_PCM_IOPLUG_HW_ACCESS,
702
ARRAY_SIZE(accesses_planar),
705
err = snd_pcm_ioplug_set_param_list(&rec->io,
706
SND_PCM_IOPLUG_HW_ACCESS,
707
ARRAY_SIZE(accesses),
712
if ((err = snd_pcm_ioplug_set_param_list(&rec->io, SND_PCM_IOPLUG_HW_FORMAT,
561
713
ARRAY_SIZE(formats), formats)) < 0 ||
562
714
(err = snd_pcm_ioplug_set_param_minmax(&rec->io, SND_PCM_IOPLUG_HW_CHANNELS,
563
715
rec->channels, rec->channels)) < 0 ||
744
898
rec->io.mmap_rw = 0;
745
899
rec->io.callback = &a52_ops;
746
900
rec->io.private_data = rec;
901
#ifdef USE_AVCODEC_FRAME
902
rec->av_format = rec->codec->sample_fmts[0];
903
rec->is_planar = av_sample_fmt_is_planar(rec->av_format);
905
rec->av_format = AV_SAMPLE_FMT_S16;
748
908
err = snd_pcm_ioplug_create(&rec->io, name, stream, mode);