Debian Codegraph @

a52/pcm_a52.c

raw symbols SCIP
1 /*
2 * A52 Output Plugin
3 *
4 * Copyright (c) 2006 by Takashi Iwai <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <stdio.h>
22 #include <string.h>
23 #define __USE_XOPEN
24 #include <unistd.h>
25 #include <alsa/asoundlib.h>
26 #include <alsa/pcm_external.h>
27 #include <alsa/pcm_plugin.h>
28 #include <libavcodec/avcodec.h>
29 #include <libavutil/avutil.h>
30
31 #ifndef ESTRPIPE
32 #define ESTRPIPE ESPIPE
33 #endif
34
35 /* some compatibility wrappers */
36 #ifndef AV_VERSION_INT
37 #define AV_VERSION_INT(a, b, c) (((a) << 16) | ((b) << 8) | (c))
38 #endif
39 #ifndef LIBAVCODEC_VERSION_INT
40 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
41 LIBAVCODEC_VERSION_MINOR, \
42 LIBAVCODEC_VERSION_MICRO)
43 #endif
44
45 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(53, 34, 0)
46 #include <libavutil/channel_layout.h>
47 #include <libavutil/mem.h>
48 #define USE_AVCODEC_FRAME 1
49 #endif
50
51 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(58, 91, 0)
52 #include <libavcodec/packet.h>
53 #define USE_AVCODEC_PACKET_ALLOC
54 #endif
55
56 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 0, 0)
57 #ifndef AV_CH_LAYOUT_STEREO
58 #define AV_CH_LAYOUT_STEREO CH_LAYOUT_STEREO
59 #define AV_CH_LAYOUT_QUAD CH_LAYOUT_QUAD
60 #define AV_CH_LAYOUT_5POINT1 CH_LAYOUT_5POINT1
61 #endif
62 #endif
63
64 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(52, 95, 0)
65 #ifndef AV_SAMPLE_FMT_S16
66 #define AV_SAMPLE_FMT_S16 SAMPLE_FMT_S16
67 #endif
68 #endif
69
70 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 25, 0)
71 #define AV_CODEC_ID_AC3 CODEC_ID_AC3
72 #endif
73
74 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(56, 56, 0)
75 #ifndef AV_INPUT_BUFFER_PADDING_SIZE
76 #define AV_INPUT_BUFFER_PADDING_SIZE FF_INPUT_BUFFER_PADDING_SIZE
77 #endif
78 #endif
79
80 #if LIBAVCODEC_VERSION_INT < 0x371c01
81 #define av_frame_alloc avcodec_alloc_frame
82 #define av_frame_free avcodec_free_frame
83 #endif
84
85 #define HAVE_AVCODEC_FREE_CONTEXT (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(55, 69, 100))
86 #define HAVE_CH_LAYOUT (LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100))
87
88 struct a52_ctx {
89 snd_pcm_ioplug_t io;
90 snd_pcm_t *slave;
91 AVCodec *codec;
92 AVCodecContext *avctx;
93 snd_pcm_format_t src_format;
94 unsigned int src_sample_bits;
95 unsigned int src_sample_bytes;
96 snd_pcm_format_t format;
97 int av_format;
98 unsigned int channels;
99 unsigned int rate;
100 unsigned int bitrate;
101 void *inbuf;
102 unsigned char *outbuf;
103 unsigned char *outbuf1;
104 unsigned char *outbuf2;
105 int outbuf_size;
106 int remain;
107 int filled;
108 unsigned int slave_period_size;
109 unsigned int slave_buffer_size;
110 snd_pcm_uframes_t pointer;
111 snd_pcm_uframes_t boundary;
112 snd_pcm_hw_params_t *hw_params;
113 #ifdef USE_AVCODEC_PACKET_ALLOC
114 AVPacket *pkt;
115 #endif
116 #ifdef USE_AVCODEC_FRAME
117 AVFrame *frame;
118 int is_planar;
119 #endif
120 };
121
122 #ifdef USE_AVCODEC_FRAME
123 #define use_planar(rec) (rec)->is_planar
124 #else
125 #define use_planar(rec) 0
126 #endif
127
128 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(58, 91, 0)
129 static int do_encode(struct a52_ctx *rec)
130 {
131 AVPacket *pkt = rec->pkt;
132 int ret;
133
134 ret = avcodec_send_frame(rec->avctx, rec->frame);
135 if (ret < 0)
136 return -EINVAL;
137 ret = avcodec_receive_packet(rec->avctx, pkt);
138 if (ret < 0)
139 return -EINVAL;
140
141 if (pkt->size > rec->outbuf_size - 8)
142 return -EINVAL;
143 memcpy(rec->outbuf1 + 8, pkt->data, pkt->size);
144
145 return pkt->size;
146 }
147 #elif LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(53, 34, 0)
148 static int do_encode(struct a52_ctx *rec)
149 {
150 AVPacket pkt = {
151 .data = rec->outbuf1 + 8,
152 .size = rec->outbuf_size - 8
153 };
154 int ret, got_frame;
155
156 ret = avcodec_encode_audio2(rec->avctx, &pkt, rec->frame, &got_frame);
157 if (ret < 0)
158 return -EINVAL;
159
160 return pkt.size;
161 }
162 #else
163 static int do_encode(struct a52_ctx *rec)
164 {
165 int ret = avcodec_encode_audio(rec->avctx, rec->outbuf1 + 8,
166 rec->outbuf_size - 8,
167 rec->inbuf);
168 if (ret < 0)
169 return -EINVAL;
170
171 return ret;
172 }
173 #endif
174
175 /* convert the PCM data to A52 stream in IEC958 */
176 static int convert_data(struct a52_ctx *rec)
177 {
178 unsigned char *buf;
179 int out_bytes = do_encode(rec);
180
181 if (out_bytes < 0)
182 return out_bytes;
183
184 buf = rec->outbuf1;
185 buf[0] = 0xf8; /* sync words */
186 buf[1] = 0x72;
187 buf[2] = 0x4e;
188 buf[3] = 0x1f;
189 buf[4] = buf[13] & 7; /* bsmod */
190 buf[5] = 0x01; /* data type */
191 buf[6] = ((out_bytes * 8) >> 8) & 0xff;
192 buf[7] = (out_bytes * 8) & 0xff;
193 /* swap bytes for little-endian 16bit */
194 if (rec->format == SND_PCM_FORMAT_S16_LE) {
195 swab(rec->outbuf1, rec->outbuf2, out_bytes + 8);
196 buf = rec->outbuf2;
197 }
198 rec->outbuf = buf;
199 memset(buf + 8 + out_bytes, 0,
200 rec->outbuf_size - 8 - out_bytes);
201 rec->remain = rec->outbuf_size / 4;
202 rec->filled = 0;
203
204 return 0;
205 }
206
207 /* write pending encoded data to the slave pcm */
208 static int write_out_pending(snd_pcm_ioplug_t *io, struct a52_ctx *rec)
209 {
210 snd_pcm_sframes_t ret;
211 unsigned int ofs;
212
213 while (rec->remain) {
214 ofs = (rec->avctx->frame_size - rec->remain) * 4;
215 ret = snd_pcm_writei(rec->slave, rec->outbuf + ofs, rec->remain);
216 if (ret < 0) {
217 if (ret == -EPIPE)
218 io->state = SND_PCM_STATE_XRUN;
219 if (ret == -EAGAIN)
220 break;
221 return ret;
222 } else if (! ret)
223 break;
224 rec->remain -= ret;
225 }
226 return 0;
227 }
228
229 /*
230 * drain callback
231 */
232 #ifdef USE_AVCODEC_FRAME
233 static void clear_remaining_planar_data(snd_pcm_ioplug_t *io)
234 {
235 struct a52_ctx *rec = io->private_data;
236 unsigned int i;
237
238 for (i = 0; i < io->channels; i++)
239 memset(rec->frame->data[i] + rec->filled * rec->src_sample_bytes, 0,
240 (rec->avctx->frame_size - rec->filled) * rec->src_sample_bytes);
241 }
242 #else
243 #define clear_remaining_planar_data(io) /*NOP*/
244 #endif
245
246 static int a52_drain(snd_pcm_ioplug_t *io)
247 {
248 struct a52_ctx *rec = io->private_data;
249 int err;
250
251 if (rec->filled) {
252 if ((err = write_out_pending(io, rec)) < 0)
253 return err;
254 /* remaining data must be converted and sent out */
255 if (use_planar(rec))
256 clear_remaining_planar_data(io);
257 else {
258 memset(rec->inbuf + rec->filled * io->channels * rec->src_sample_bytes, 0,
259 (rec->avctx->frame_size - rec->filled) * io->channels * rec->src_sample_bytes);
260 }
261 err = convert_data(rec);
262 if (err < 0)
263 return err;
264 }
265 err = write_out_pending(io, rec);
266 if (err < 0)
267 return err;
268
269 return snd_pcm_drain(rec->slave);
270 }
271
272 /* check whether the areas consist of a continuous interleaved stream */
273 static int check_interleaved(struct a52_ctx *rec,
274 const snd_pcm_channel_area_t *areas,
275 unsigned int channels)
276 {
277 unsigned int ch;
278
279 if (channels > 4) /* we need re-routing for 6 channels */
280 return 0;
281
282 for (ch = 0; ch < channels; ch++) {
283 if (areas[ch].addr != areas[0].addr ||
284 areas[ch].first != ch * rec->src_sample_bits ||
285 areas[ch].step != channels * rec->src_sample_bits)
286 return 0;
287 }
288 return 1;
289 }
290
291 /* Fill the input PCM to the internal buffer until a52 frames,
292 * then covert and write it out.
293 *
294 * Returns the number of processed frames.
295 */
296 static int fill_data(snd_pcm_ioplug_t *io,
297 const snd_pcm_channel_area_t *areas,
298 unsigned int offset, unsigned int size,
299 int interleaved)
300 {
301 struct a52_ctx *rec = io->private_data;
302 unsigned int len = rec->avctx->frame_size - rec->filled;
303 void *_dst;
304 int err;
305 static unsigned int ch_index[3][6] = {
306 { 0, 1 },
307 { 0, 1, 2, 3 },
308 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 26, 0)
309 /* current libavcodec expects SMPTE order */
310 { 0, 1, 4, 5, 2, 3 },
311 #else
312 /* libavcodec older than r18540 expects A52 order */
313 { 0, 4, 1, 2, 3, 5 },
314 #endif
315 };
316
317 if ((err = write_out_pending(io, rec)) < 0)
318 return err;
319
320 /* If there are still frames left in outbuf, we can't
321 * accept a full a52 frame, because this would overwrite
322 * the frames in outbuf. This should not happen! The a52_pointer()
323 * callback should limit the transferred frames correctly. */
324 if (rec->remain && len) {
325 SNDERR("fill data issue (remain is %i)", rec->remain);
326 len--;
327 }
328
329 if (size > len)
330 size = len;
331
332 _dst = rec->inbuf + rec->filled * io->channels * rec->src_sample_bytes;
333 if (!use_planar(rec) && interleaved) {
334 memcpy(_dst, areas->addr + offset * io->channels * rec->src_sample_bytes,
335 size * io->channels * rec->src_sample_bytes);
336 } else if (rec->src_sample_bits == 16) {
337 unsigned int i, ch, src_step, dst_step;
338 short *src, *dst = _dst, *dst1;
339
340 /* flatten copy to n-channel interleaved */
341 dst_step = io->channels;
342 for (ch = 0; ch < io->channels; ch++, dst++) {
343 const snd_pcm_channel_area_t *ap;
344 ap = &areas[ch_index[io->channels / 2 - 1][ch]];
345 src = (short *)(ap->addr +
346 (ap->first + offset * ap->step) / 8);
347
348 #ifdef USE_AVCODEC_FRAME
349 if (use_planar(rec) && !interleaved) {
350 memcpy(rec->frame->data[ch] + rec->filled * 2, src, size * 2);
351 continue;
352 }
353 #endif
354 dst1 = dst;
355 src_step = ap->step / 16; /* in word */
356 for (i = 0; i < size; i++) {
357 *dst1 = *src;
358 src += src_step;
359 dst1 += dst_step;
360 }
361 }
362 } else if (rec->src_sample_bits == 32) {
363 unsigned int i, ch, src_step, dst_step;
364 int *src, *dst = _dst, *dst1;
365
366 /* flatten copy to n-channel interleaved */
367 dst_step = io->channels;
368 for (ch = 0; ch < io->channels; ch++, dst++) {
369 const snd_pcm_channel_area_t *ap;
370 ap = &areas[ch_index[io->channels / 2 - 1][ch]];
371 src = (int *)(ap->addr +
372 (ap->first + offset * ap->step) / 8);
373
374 #ifdef USE_AVCODEC_FRAME
375 if (use_planar(rec) && !interleaved) {
376 memcpy(rec->frame->data[ch] + rec->filled * 4, src, size * 4);
377 continue;
378 }
379 #endif
380 dst1 = dst;
381 src_step = ap->step / 32; /* in word */
382 for (i = 0; i < size; i++) {
383 *dst1 = *src;
384 src += src_step;
385 dst1 += dst_step;
386 }
387 }
388 } else {
389 return -EIO;
390 }
391 rec->filled += size;
392 if (rec->filled == rec->avctx->frame_size) {
393 err = convert_data(rec);
394 if (err < 0)
395 return err;
396 write_out_pending(io, rec);
397 }
398 return (int)size;
399 }
400
401 /*
402 * transfer callback
403 */
404 static snd_pcm_sframes_t a52_transfer(snd_pcm_ioplug_t *io,
405 const snd_pcm_channel_area_t *areas,
406 snd_pcm_uframes_t offset,
407 snd_pcm_uframes_t size)
408 {
409 struct a52_ctx *rec = io->private_data;
410 snd_pcm_sframes_t result = 0;
411 int err = 0;
412 int interleaved = check_interleaved(rec, areas, io->channels);
413
414 do {
415 err = fill_data(io, areas, offset, size, interleaved);
416 if (err <= 0)
417 break;
418 offset += (unsigned int)err;
419 size -= (unsigned int)err;
420 result += err;
421 rec->pointer += err;
422 rec->pointer %= rec->boundary;
423 } while (size);
424 return result > 0 ? result : err;
425 }
426
427 /*
428 * pointer callback
429 *
430 * Calculate the current position from the available frames of slave PCM
431 */
432 static snd_pcm_sframes_t a52_pointer(snd_pcm_ioplug_t *io)
433 {
434 struct a52_ctx *rec = io->private_data;
435 snd_pcm_sframes_t avail, delay;
436 snd_pcm_state_t state;
437
438 state = snd_pcm_state(rec->slave);
439 switch (state) {
440 case SND_PCM_STATE_RUNNING:
441 case SND_PCM_STATE_DRAINING:
442 break;
443 case SND_PCM_STATE_XRUN:
444 return -EPIPE;
445 case SND_PCM_STATE_SUSPENDED:
446 return -ESTRPIPE;
447 default:
448 return 0;
449 }
450
451 /* Write what we have from outbuf. */
452 write_out_pending(io, rec);
453
454 avail = snd_pcm_avail(rec->slave);
455 if (avail < 0)
456 return avail;
457
458 /* get buffer delay without additional FIFO information */
459 delay = rec->slave_buffer_size - avail;
460 while (delay < 0)
461 delay += rec->slave_buffer_size;
462
463 avail = rec->pointer - delay - rec->remain - rec->filled;
464 #ifdef SND_PCM_IOPLUG_FLAG_BOUNDARY_WA
465 return avail % rec->boundary;
466 #else
467 return avail % io->buffer_size;
468 #endif
469 }
470
471 /* set up the fixed parameters of slave PCM hw_parmas */
472 static int a52_slave_hw_params_half(struct a52_ctx *rec)
473 {
474 int err;
475
476 if ((err = snd_pcm_hw_params_malloc(&rec->hw_params)) < 0)
477 return err;
478
479 if ((err = snd_pcm_hw_params_any(rec->slave, rec->hw_params)) < 0) {
480 SNDERR("Cannot get slave hw_params");
481 goto out;
482 }
483 if ((err = snd_pcm_hw_params_set_access(rec->slave, rec->hw_params,
484 SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
485 SNDERR("Cannot set slave access RW_INTERLEAVED");
486 goto out;
487 }
488 if ((err = snd_pcm_hw_params_set_channels(rec->slave, rec->hw_params, 2)) < 0) {
489 SNDERR("Cannot set slave channels 2");
490 goto out;
491 }
492 if ((err = snd_pcm_hw_params_set_format(rec->slave, rec->hw_params,
493 rec->format)) < 0) {
494 SNDERR("Cannot set slave format");
495 goto out;
496 }
497 if ((err = snd_pcm_hw_params_set_rate(rec->slave, rec->hw_params, rec->rate, 0)) < 0) {
498 SNDERR("Cannot set slave rate %d", rec->rate);
499 goto out;
500 }
501 return 0;
502
503 out:
504 free(rec->hw_params);
505 rec->hw_params = NULL;
506 return err;
507 }
508
509 /*
510 * hw_params callback
511 *
512 * Set up slave PCM according to the current parameters
513 */
514 static int a52_hw_params(snd_pcm_ioplug_t *io,
515 snd_pcm_hw_params_t *params ATTRIBUTE_UNUSED)
516 {
517 struct a52_ctx *rec = io->private_data;
518 snd_pcm_uframes_t period_size;
519 snd_pcm_uframes_t buffer_size;
520 int err;
521
522 if (! rec->hw_params) {
523 err = a52_slave_hw_params_half(rec);
524 if (err < 0)
525 return err;
526 }
527 period_size = io->period_size;
528 if ((err = snd_pcm_hw_params_set_period_size_near(rec->slave, rec->hw_params,
529 &period_size, NULL)) < 0) {
530 SNDERR("Cannot set slave period size %ld", period_size);
531 return err;
532 }
533 buffer_size = io->buffer_size;
534 if ((err = snd_pcm_hw_params_set_buffer_size_near(rec->slave, rec->hw_params,
535 &buffer_size)) < 0) {
536 SNDERR("Cannot set slave buffer size %ld", buffer_size);
537 return err;
538 }
539 if ((err = snd_pcm_hw_params(rec->slave, rec->hw_params)) < 0) {
540 SNDERR("Cannot set slave hw_params");
541 return err;
542 }
543 rec->slave_period_size = period_size;
544 rec->slave_buffer_size = buffer_size;
545
546 return 0;
547 }
548
549 /*
550 * hw_free callback
551 */
552 static int a52_hw_free(snd_pcm_ioplug_t *io)
553 {
554 struct a52_ctx *rec = io->private_data;
555
556 free(rec->hw_params);
557 rec->hw_params = NULL;
558 return snd_pcm_hw_free(rec->slave);
559 }
560
561 /*
562 * dump callback
563 */
564 static void a52_dump(snd_pcm_ioplug_t *io, snd_output_t *out)
565 {
566 struct a52_ctx *rec = io->private_data;
567 snd_pcm_t *pcm = io->pcm;
568
569 snd_output_printf(out, "%s\n", io->name);
570 snd_output_printf(out, "Its setup is:\n");
571 snd_pcm_dump_setup(pcm, out);
572 snd_output_printf(out, " %-13s: %s\n", "av_format", av_get_sample_fmt_name(rec->av_format));
573 snd_output_printf(out, " %-13s: %i\n", "av_frame_size", rec->avctx ? rec->avctx->frame_size : -1);
574 snd_output_printf(out, " %-13s: %i\n", "remain", rec->remain);
575 snd_output_printf(out, " %-13s: %i\n", "filled", rec->filled);
576 snd_output_printf(out, "Slave: ");
577 snd_pcm_dump(rec->slave, out);
578 }
579
580 /*
581 * sw_params callback
582 *
583 * Set up slave PCM sw_params
584 */
585 static int a52_sw_params(snd_pcm_ioplug_t *io, snd_pcm_sw_params_t *params)
586 {
587 struct a52_ctx *rec = io->private_data;
588 snd_pcm_sw_params_t *sparams;
589 snd_pcm_uframes_t avail_min, start_threshold;
590 int len;
591
592 snd_pcm_sw_params_get_avail_min(params, &avail_min);
593 snd_pcm_sw_params_get_start_threshold(params, &start_threshold);
594 snd_pcm_sw_params_get_boundary(params, &rec->boundary);
595
596 len = avail_min;
597 len += (int)rec->slave_buffer_size - (int)io->buffer_size;
598 if (len < 0)
599 avail_min = 1;
600 else
601 avail_min = len;
602 snd_pcm_sw_params_alloca(&sparams);
603 snd_pcm_sw_params_current(rec->slave, sparams);
604 snd_pcm_sw_params_set_avail_min(rec->slave, sparams, avail_min);
605 snd_pcm_sw_params_set_start_threshold(rec->slave, sparams,
606 start_threshold);
607
608 return snd_pcm_sw_params(rec->slave, sparams);
609 }
610
611 /*
612 * start and stop callbacks - just trigger slave PCM
613 */
614 static int a52_start(snd_pcm_ioplug_t *io)
615 {
616 struct a52_ctx *rec = io->private_data;
617
618 /* When trying to start a PCM that's already running, the result is
619 EBADFD. We might have implicitly started the buffer by filling it
620 up, so just ignore this request if we're already running. */
621 if (snd_pcm_state(rec->slave) == SND_PCM_STATE_RUNNING)
622 return 0;
623
624 return snd_pcm_start(rec->slave);
625 }
626
627 static int a52_stop(snd_pcm_ioplug_t *io)
628 {
629 struct a52_ctx *rec = io->private_data;
630
631 return snd_pcm_drop(rec->slave);
632 }
633
634 /* release resources */
635 static void a52_free(struct a52_ctx *rec)
636 {
637 if (rec->avctx) {
638 #if HAVE_AVCODEC_FREE_CONTEXT
639 avcodec_free_context(&rec->avctx);
640 #else
641 avcodec_close(rec->avctx);
642 #endif
643 av_free(rec->avctx);
644 rec->avctx = NULL;
645 }
646
647 #ifdef USE_AVCODEC_FRAME
648 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 91, 0)
649 if (rec->frame)
650 av_freep(&rec->frame->data[0]);
651 #endif
652 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(54, 28, 0)
653 av_frame_free(&rec->frame);
654 #else
655 av_freep(&rec->frame);
656 #endif
657 #else /* USE_AVCODEC_FRAME */
658 free(rec->inbuf);
659 rec->inbuf = NULL;
660 #endif /* USE_AVCODEC_FRAME */
661
662 #ifdef USE_AVCODEC_PACKET_ALLOC
663 av_packet_free(&rec->pkt);
664 #endif
665 free(rec->outbuf2);
666 rec->outbuf2 = NULL;
667 free(rec->outbuf1);
668 rec->outbuf1 = NULL;
669 }
670
671 /*
672 * prepare callback
673 *
674 * Allocate internal buffers and set up libavcodec
675 */
676
677 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 3, 0)
678 static void set_channel_layout(snd_pcm_ioplug_t *io)
679 {
680 struct a52_ctx *rec = io->private_data;
681 #if HAVE_CH_LAYOUT
682 switch (io->channels) {
683 case 2:
684 rec->avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
685 break;
686 case 4:
687 rec->avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_QUAD;
688 break;
689 case 6:
690 rec->avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1;
691 break;
692 default:
693 break;
694 }
695 #else
696 switch (io->channels) {
697 case 2:
698 rec->avctx->channel_layout = AV_CH_LAYOUT_STEREO;
699 break;
700 case 4:
701 rec->avctx->channel_layout = AV_CH_LAYOUT_QUAD;
702 break;
703 case 6:
704 rec->avctx->channel_layout = AV_CH_LAYOUT_5POINT1;
705 break;
706 default:
707 break;
708 }
709 #endif
710 }
711 #else
712 #define set_channel_layout(io) /* NOP */
713 #endif
714
715 static int alloc_input_buffer(snd_pcm_ioplug_t *io)
716 {
717 struct a52_ctx *rec = io->private_data;
718 #ifdef USE_AVCODEC_FRAME
719 rec->frame = av_frame_alloc();
720 if (!rec->frame)
721 return -ENOMEM;
722 rec->frame->nb_samples = rec->avctx->frame_size;
723 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(58, 91, 0)
724 rec->frame->format = rec->avctx->sample_fmt;
725 #if HAVE_CH_LAYOUT
726 av_channel_layout_from_mask(&rec->frame->ch_layout, rec->avctx->ch_layout.u.mask);
727 #else
728 rec->frame->channels = rec->avctx->channels;
729 rec->frame->channel_layout = rec->avctx->channel_layout;
730 #endif
731 if (av_frame_get_buffer(rec->frame, 0))
732 return -ENOMEM;
733 #else
734 if (av_samples_alloc(rec->frame->data, rec->frame->linesize,
735 io->channels, rec->avctx->frame_size,
736 rec->avctx->sample_fmt, 0) < 0)
737 return -ENOMEM;
738 #endif
739 rec->inbuf = rec->frame->data[0];
740 #else
741 rec->inbuf = malloc(rec->avctx->frame_size * io->channels * rec->src_sample_bytes);
742 #endif
743 if (!rec->inbuf)
744 return -ENOMEM;
745 return 0;
746 }
747
748 static int a52_prepare(snd_pcm_ioplug_t *io)
749 {
750 struct a52_ctx *rec = io->private_data;
751 int err;
752
753 a52_free(rec);
754
755 #ifdef USE_AVCODEC_FRAME
756 rec->avctx = avcodec_alloc_context3(rec->codec);
757 #else
758 rec->avctx = avcodec_alloc_context();
759 #endif
760 if (!rec->avctx)
761 return -ENOMEM;
762
763 rec->avctx->bit_rate = rec->bitrate * 1000;
764 rec->avctx->sample_rate = io->rate;
765 #if HAVE_CH_LAYOUT
766 rec->avctx->ch_layout.nb_channels = io->channels;
767 #else
768 rec->avctx->channels = io->channels;
769 #endif
770 rec->avctx->sample_fmt = rec->av_format;
771
772 set_channel_layout(io);
773
774
775 #ifdef USE_AVCODEC_FRAME
776 err = avcodec_open2(rec->avctx, rec->codec, NULL);
777 #else
778 err = avcodec_open(rec->avctx, rec->codec);
779 #endif
780 if (err < 0)
781 return -EINVAL;
782
783 #ifdef USE_AVCODEC_PACKET_ALLOC
784 rec->pkt = av_packet_alloc();
785 if (!rec->pkt)
786 return -ENOMEM;
787 #endif
788
789 rec->outbuf_size = rec->avctx->frame_size * 4;
790 rec->outbuf1 = malloc(rec->outbuf_size + AV_INPUT_BUFFER_PADDING_SIZE);
791 if (! rec->outbuf1)
792 return -ENOMEM;
793 memset(rec->outbuf1 + rec->outbuf_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
794
795 if (rec->format == SND_PCM_FORMAT_S16_LE) {
796 rec->outbuf2 = malloc(rec->outbuf_size);
797 if ( !rec->outbuf2)
798 return -ENOMEM;
799 }
800
801 if (alloc_input_buffer(io))
802 return -ENOMEM;
803
804 rec->pointer = 0;
805 rec->remain = 0;
806 rec->filled = 0;
807
808 return snd_pcm_prepare(rec->slave);
809 }
810
811 /*
812 * poll-related callbacks - just pass to slave PCM
813 */
814 static int a52_poll_descriptors_count(snd_pcm_ioplug_t *io)
815 {
816 struct a52_ctx *rec = io->private_data;
817 return snd_pcm_poll_descriptors_count(rec->slave);
818 }
819
820 static int a52_poll_descriptors(snd_pcm_ioplug_t *io, struct pollfd *pfd,
821 unsigned int space)
822 {
823 struct a52_ctx *rec = io->private_data;
824 return snd_pcm_poll_descriptors(rec->slave, pfd, space);
825 }
826
827 static int a52_poll_revents(snd_pcm_ioplug_t *io, struct pollfd *pfd,
828 unsigned int nfds, unsigned short *revents)
829 {
830 struct a52_ctx *rec = io->private_data;
831 return snd_pcm_poll_descriptors_revents(rec->slave, pfd, nfds, revents);
832 }
833
834 /*
835 * close callback
836 */
837 static int a52_close(snd_pcm_ioplug_t *io)
838 {
839 struct a52_ctx *rec = io->private_data;
840 snd_pcm_t *slave = rec->slave;
841
842 a52_free(rec);
843 free(rec);
844 if (slave)
845 return snd_pcm_close(slave);
846 return 0;
847 }
848
849 #if SND_PCM_IOPLUG_VERSION >= 0x10002
850 static unsigned int chmap4[4] = {
851 SND_CHMAP_FL, SND_CHMAP_FR,
852 SND_CHMAP_RL, SND_CHMAP_RR,
853 };
854 static unsigned int chmap6[6] = {
855 SND_CHMAP_FL, SND_CHMAP_FR,
856 SND_CHMAP_RL, SND_CHMAP_RR,
857 SND_CHMAP_FC, SND_CHMAP_LFE,
858 };
859
860 static snd_pcm_chmap_query_t **a52_query_chmaps(snd_pcm_ioplug_t *io ATTRIBUTE_UNUSED)
861 {
862 snd_pcm_chmap_query_t **maps;
863 int i;
864
865 maps = calloc(4, sizeof(void *));
866 if (!maps)
867 return NULL;
868 for (i = 0; i < 3; i++) {
869 snd_pcm_chmap_query_t *p;
870 p = maps[i] = calloc((i + 1) * 2 + 2, sizeof(int));
871 if (!p) {
872 snd_pcm_free_chmaps(maps);
873 return NULL;
874 }
875 p->type = SND_CHMAP_TYPE_FIXED;
876 p->map.channels = (i + 1) * 2;
877 memcpy(p->map.pos, i < 2 ? chmap4 : chmap6,
878 (i + 1) * 2 * sizeof(int));
879 }
880 return maps;
881 }
882
883 static snd_pcm_chmap_t *a52_get_chmap(snd_pcm_ioplug_t *io)
884 {
885 snd_pcm_chmap_t *map;
886
887 if ((io->channels % 2) || io->channels < 2 || io->channels > 6)
888 return NULL;
889 map = malloc((io->channels + 1) * sizeof(int));
890 if (!map)
891 return NULL;
892 map->channels = io->channels;
893 memcpy(map->pos, io->channels < 6 ? chmap4 : chmap6,
894 io->channels * sizeof(int));
895 return map;
896 }
897 #endif /* SND_PCM_IOPLUG_VERSION >= 0x10002 */
898
899 /*
900 * callback table
901 */
902 static snd_pcm_ioplug_callback_t a52_ops = {
903 .start = a52_start,
904 .stop = a52_stop,
905 .pointer = a52_pointer,
906 .transfer = a52_transfer,
907 .close = a52_close,
908 .hw_params = a52_hw_params,
909 .hw_free = a52_hw_free,
910 .dump = a52_dump,
911 .sw_params = a52_sw_params,
912 .prepare = a52_prepare,
913 .drain = a52_drain,
914 .poll_descriptors_count = a52_poll_descriptors_count,
915 .poll_descriptors = a52_poll_descriptors,
916 .poll_revents = a52_poll_revents,
917 #if SND_PCM_IOPLUG_VERSION >= 0x10002
918 .query_chmaps = a52_query_chmaps,
919 .get_chmap = a52_get_chmap,
920 #endif /* SND_PCM_IOPLUG_VERSION >= 0x10002 */
921 };
922
923 /*
924 * set up h/w constraints
925 *
926 * set the period size identical with A52 frame size.
927 * the max buffer size is calculated from the max buffer size
928 * of the slave PCM
929 */
930
931 #define A52_FRAME_SIZE 1536
932
933 #define ARRAY_SIZE(ary) (sizeof(ary)/sizeof(ary[0]))
934
935 static int a52_set_hw_constraint(struct a52_ctx *rec)
936 {
937 static unsigned int accesses[] = {
938 SND_PCM_ACCESS_MMAP_INTERLEAVED,
939 SND_PCM_ACCESS_MMAP_NONINTERLEAVED,
940 SND_PCM_ACCESS_RW_INTERLEAVED,
941 SND_PCM_ACCESS_RW_NONINTERLEAVED
942 };
943 static unsigned int accesses_planar[] = {
944 SND_PCM_ACCESS_MMAP_NONINTERLEAVED,
945 SND_PCM_ACCESS_RW_NONINTERLEAVED
946 };
947 static struct format {
948 int av;
949 snd_pcm_format_t alib;
950 } formats[] = {
951 { .av = AV_SAMPLE_FMT_S16, .alib = SND_PCM_FORMAT_S16 },
952 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(52, 95, 0) && USE_AVCODEC_FRAME
953 { .av = AV_SAMPLE_FMT_S16P, .alib = SND_PCM_FORMAT_S16 },
954 { .av = AV_SAMPLE_FMT_S32, .alib = SND_PCM_FORMAT_S32 },
955 { .av = AV_SAMPLE_FMT_S32P, .alib = SND_PCM_FORMAT_S32 },
956 { .av = AV_SAMPLE_FMT_FLTP, .alib = SND_PCM_FORMAT_FLOAT }
957 #endif
958 };
959 int err, dir;
960 unsigned int i, fmt;
961 snd_pcm_uframes_t buffer_max;
962 unsigned int period_bytes, max_periods;
963
964 if (use_planar(rec))
965 err = snd_pcm_ioplug_set_param_list(&rec->io,
966 SND_PCM_IOPLUG_HW_ACCESS,
967 ARRAY_SIZE(accesses_planar),
968 accesses_planar);
969 else
970 err = snd_pcm_ioplug_set_param_list(&rec->io,
971 SND_PCM_IOPLUG_HW_ACCESS,
972 ARRAY_SIZE(accesses),
973 accesses);
974 if (err < 0)
975 return err;
976
977 rec->src_format = SND_PCM_FORMAT_UNKNOWN;
978 for (i = 0; i < ARRAY_SIZE(formats); i++)
979 if (formats[i].av == rec->av_format) {
980 rec->src_format = formats[i].alib;
981 break;
982 }
983 if (rec->src_format == SND_PCM_FORMAT_UNKNOWN) {
984 SNDERR("A/V format '%s' is not supported", av_get_sample_fmt_name(rec->av_format));
985 return -EINVAL;
986 }
987 fmt = rec->src_format;
988 rec->src_sample_bits = snd_pcm_format_physical_width(rec->src_format);
989 rec->src_sample_bytes = rec->src_sample_bits / 8;
990
991 if ((err = snd_pcm_ioplug_set_param_list(&rec->io, SND_PCM_IOPLUG_HW_FORMAT,
992 1, &fmt)) < 0 ||
993 (err = snd_pcm_ioplug_set_param_minmax(&rec->io, SND_PCM_IOPLUG_HW_CHANNELS,
994 rec->channels, rec->channels)) < 0 ||
995 (err = snd_pcm_ioplug_set_param_minmax(&rec->io, SND_PCM_IOPLUG_HW_RATE,
996 rec->rate, rec->rate)) < 0)
997 return err;
998
999 if ((err = a52_slave_hw_params_half(rec)) < 0)
1000 return err;
1001
1002 snd_pcm_hw_params_get_buffer_size_max(rec->hw_params, &buffer_max);
1003 dir = -1;
1004 snd_pcm_hw_params_get_periods_max(rec->hw_params, &max_periods, &dir);
1005 period_bytes = A52_FRAME_SIZE * 2 * rec->channels;
1006 if (buffer_max / A52_FRAME_SIZE < max_periods)
1007 max_periods = buffer_max / A52_FRAME_SIZE;
1008
1009 if ((err = snd_pcm_ioplug_set_param_minmax(&rec->io, SND_PCM_IOPLUG_HW_PERIOD_BYTES,
1010 period_bytes, period_bytes)) < 0 ||
1011 (err = snd_pcm_ioplug_set_param_minmax(&rec->io, SND_PCM_IOPLUG_HW_PERIODS,
1012 2, max_periods)) < 0)
1013 return err;
1014
1015 return 0;
1016 }
1017
1018 /*
1019 * Main entry point
1020 */
1021 SND_PCM_PLUGIN_DEFINE_FUNC(a52)
1022 {
1023 snd_config_iterator_t i, next;
1024 int err;
1025 const char *card = NULL;
1026 const char *pcm_string = NULL;
1027 const char *avcodec = NULL;
1028 unsigned int rate = 48000;
1029 unsigned int bitrate = 448;
1030 unsigned int channels = 6;
1031 snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
1032 char devstr[128], tmpcard[16];
1033 struct a52_ctx *rec;
1034
1035 if (stream != SND_PCM_STREAM_PLAYBACK) {
1036 SNDERR("a52 is only for playback");
1037 return -EINVAL;
1038 }
1039
1040 snd_config_for_each(i, next, conf) {
1041 snd_config_t *n = snd_config_iterator_entry(i);
1042 const char *id;
1043 if (snd_config_get_id(n, &id) < 0)
1044 continue;
1045 if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0 || strcmp(id, "hint") == 0)
1046 continue;
1047 if (strcmp(id, "card") == 0) {
1048 if (snd_config_get_string(n, &card) < 0) {
1049 long val;
1050 err = snd_config_get_integer(n, &val);
1051 if (err < 0) {
1052 SNDERR("Invalid type for %s", id);
1053 return -EINVAL;
1054 }
1055 snprintf(tmpcard, sizeof(tmpcard), "%ld", val);
1056 card = tmpcard;
1057 }
1058 continue;
1059 }
1060 if (strcmp(id, "slavepcm") == 0) {
1061 if (snd_config_get_string(n, &pcm_string) < 0) {
1062 SNDERR("a52 slavepcm must be a string");
1063 return -EINVAL;
1064 }
1065 continue;
1066 }
1067 if (strcmp(id, "rate") == 0) {
1068 long val;
1069 if (snd_config_get_integer(n, &val) < 0) {
1070 SNDERR("Invalid type for %s", id);
1071 return -EINVAL;
1072 }
1073 rate = val;
1074 if (rate != 44100 && rate != 48000) {
1075 SNDERR("rate must be 44100 or 48000");
1076 return -EINVAL;
1077 }
1078 continue;
1079 }
1080 if (strcmp(id, "bitrate") == 0) {
1081 long val;
1082 if (snd_config_get_integer(n, &val) < 0) {
1083 SNDERR("Invalid type for %s", id);
1084 return -EINVAL;
1085 }
1086 bitrate = val;
1087 if (bitrate < 128 || bitrate > 1000) {
1088 SNDERR("Invalid bitrate value %d", bitrate);
1089 return -EINVAL;
1090 }
1091 continue;
1092 }
1093 if (strcmp(id, "channels") == 0) {
1094 long val;
1095 if (snd_config_get_integer(n, &val) < 0) {
1096 SNDERR("Invalid type for %s", id);
1097 return -EINVAL;
1098 }
1099 channels = val;
1100 if (channels != 2 && channels != 4 && channels != 6) {
1101 SNDERR("channels must be 2, 4 or 6");
1102 return -EINVAL;
1103 }
1104 continue;
1105 }
1106 if (strcmp(id, "format") == 0) {
1107 const char *str;
1108 err = snd_config_get_string(n, &str);
1109 if (err < 0) {
1110 SNDERR("invalid type for %s", id);
1111 return -EINVAL;
1112 }
1113 format = snd_pcm_format_value(str);
1114 if (format == SND_PCM_FORMAT_UNKNOWN) {
1115 SNDERR("unknown format %s", str);
1116 return -EINVAL;
1117 }
1118 if (format != SND_PCM_FORMAT_S16_LE &&
1119 format != SND_PCM_FORMAT_S16_BE) {
1120 SNDERR("Only S16_LE/BE formats are allowed");
1121 return -EINVAL;
1122 }
1123 continue;
1124 }
1125 if (strcmp(id, "avcodec") == 0) {
1126 const char *str;
1127 err = snd_config_get_string(n, &str);
1128 if (err < 0) {
1129 SNDERR("invalid type for %s", id);
1130 return -EINVAL;
1131 }
1132 avcodec = str;
1133 continue;
1134 }
1135 SNDERR("Unknown field %s", id);
1136 return -EINVAL;
1137 }
1138
1139 rec = calloc(1, sizeof(*rec));
1140 if (! rec) {
1141 SNDERR("cannot allocate");
1142 return -ENOMEM;
1143 }
1144
1145 rec->src_format = SND_PCM_FORMAT_UNKNOWN;
1146 rec->rate = rate;
1147 rec->bitrate = bitrate;
1148 rec->channels = channels;
1149 rec->format = format;
1150
1151 #ifndef USE_AVCODEC_FRAME
1152 avcodec_init();
1153 #endif
1154 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 91, 0)
1155 avcodec_register_all();
1156 #endif
1157
1158 if (avcodec) {
1159 rec->codec = avcodec_find_encoder_by_name(avcodec);
1160 } else {
1161 rec->codec = avcodec_find_encoder_by_name("ac3_fixed");
1162 if (rec->codec == NULL)
1163 rec->codec = avcodec_find_encoder_by_name("ac3");
1164 }
1165 if (rec->codec == NULL)
1166 rec->codec = avcodec_find_encoder(AV_CODEC_ID_AC3);
1167 if (rec->codec == NULL) {
1168 SNDERR("Cannot find codec engine");
1169 err = -EINVAL;
1170 goto error;
1171 }
1172
1173 if (! pcm_string || pcm_string[0] == '\0') {
1174 snprintf(devstr, sizeof(devstr),
1175 "iec958:{AES0 0x%x AES1 0x%x AES2 0x%x AES3 0x%x%s%s}",
1176 IEC958_AES0_CON_EMPHASIS_NONE | IEC958_AES0_NONAUDIO |
1177 IEC958_AES0_CON_NOT_COPYRIGHT,
1178 IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER,
1179 0, rate == 48000 ? IEC958_AES3_CON_FS_48000 : IEC958_AES3_CON_FS_44100,
1180 card ? " CARD " : "",
1181 card ? card : "");
1182 err = snd_pcm_open(&rec->slave, devstr, stream, mode);
1183 if (err < 0)
1184 goto error;
1185 /* in case the slave doesn't support S16 format */
1186 err = snd_pcm_linear_open(&rec->slave, NULL, SND_PCM_FORMAT_S16,
1187 rec->slave, 1);
1188 if (err < 0)
1189 goto error;
1190 } else {
1191 err = snd_pcm_open(&rec->slave, pcm_string, stream, mode);
1192 if (err < 0)
1193 goto error;
1194 }
1195
1196 rec->io.version = SND_PCM_IOPLUG_VERSION;
1197 rec->io.name = "A52 Output Plugin";
1198 rec->io.mmap_rw = 0;
1199 rec->io.callback = &a52_ops;
1200 rec->io.private_data = rec;
1201 #ifdef SND_PCM_IOPLUG_FLAG_BOUNDARY_WA
1202 rec->io.flags = SND_PCM_IOPLUG_FLAG_BOUNDARY_WA;
1203 #endif
1204 #ifdef USE_AVCODEC_FRAME
1205 rec->av_format = rec->codec->sample_fmts[0];
1206 rec->is_planar = av_sample_fmt_is_planar(rec->av_format);
1207 #else
1208 rec->av_format = AV_SAMPLE_FMT_S16;
1209 #endif
1210
1211 err = snd_pcm_ioplug_create(&rec->io, name, stream, mode);
1212 if (err < 0)
1213 goto error;
1214
1215 if ((err = a52_set_hw_constraint(rec)) < 0) {
1216 snd_pcm_ioplug_delete(&rec->io);
1217 return err;
1218 }
1219
1220 *pcmp = rec->io.pcm;
1221 return 0;
1222
1223 error:
1224 if (rec->slave)
1225 snd_pcm_close(rec->slave);
1226 free(rec);
1227 return err;
1228 }
1229
1230 SND_PCM_PLUGIN_SYMBOL(a52);