root/trunk/showtime/src/audio/audio_decoder.c @ 4208

Revision 4208, 20.4 KB (checked in by andoma, 7 months ago)

Add displaying of some stats when playing video

Line 
1/*
2 *  Audio decoder and features
3 *  Copyright (C) 2007 Andreas Öman
4 *
5 *  This program is free software: you can redistribute it and/or modify
6 *  it under the terms of the GNU General Public License as published by
7 *  the Free Software Foundation, either version 3 of the License, or
8 *  (at your option) any later version.
9 *
10 *  This program is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 *  GNU General Public License for more details.
14 *
15 *  You should have received a copy of the GNU General Public License
16 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <unistd.h>
22#include <string.h>
23#include <assert.h>
24#include <math.h>
25
26#include "showtime.h"
27#include "audio_decoder.h"
28#include "audio.h"
29#include "event.h"
30
31extern audio_fifo_t *thefifo;
32
33static void audio_mix1(audio_decoder_t *ad, audio_mode_t *am,
34                       int channels, int rate, int64_t chlayout,
35                       enum CodecID codec_id,
36                       int16_t *data0, int frames, int64_t pts, int epoch,
37                       media_pipe_t *mp);
38
39static void audio_mix2(audio_decoder_t *ad, audio_mode_t *am,
40                       int channels, int rate,
41                       int16_t *data0, int frames, int64_t pts, int epoch,
42                       media_pipe_t *mp);
43
44static void close_resampler(audio_decoder_t *ad);
45
46static int resample(audio_decoder_t *ad, int16_t *dstmix, int dstavail,
47                    int *writtenp, int16_t *srcmix, int srcframes,
48                    int channels);
49
50static void ad_decode_buf(audio_decoder_t *ad, media_pipe_t *mp,
51                          media_buf_t *mb);
52
53static void audio_deliver(audio_decoder_t *ad, audio_mode_t *am, int16_t *src,
54                          int channels, int frames, int rate, int64_t pts,
55                          int epoch, media_pipe_t *mp);
56
57static void *ad_thread(void *aux);
58
59
60/**
61 * Create an audio decoder pipeline.
62 *
63 * Called from media.c
64 */
65audio_decoder_t *
66audio_decoder_create(media_pipe_t *mp)
67{
68  audio_decoder_t *ad;
69
70  ad = calloc(1, sizeof(audio_decoder_t));
71  ad->ad_mp = mp;
72  ad->ad_outbuf = av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE * 2);
73
74  TAILQ_INIT(&ad->ad_hold_queue);
75
76  hts_thread_create_joinable("audio decoder", &ad->ad_tid, ad_thread, ad);
77  return ad;
78}
79
80
81/**
82 * Audio decoder flush
83 *
84 * Remove all audio data from decoder pipeline
85 */
86static void
87audio_decoder_flush(audio_decoder_t *ad)
88{
89  audio_fifo_clear_queue(&ad->ad_hold_queue);
90
91  close_resampler(ad);
92
93  if(ad->ad_buf != NULL) {
94    ab_free(ad->ad_buf);
95    ad->ad_buf = NULL;
96  }
97}
98
99
100
101
102/**
103 * Destroy an audio decoder pipeline.
104 *
105 * Called from media.c
106 */
107void
108audio_decoder_destroy(audio_decoder_t *ad)
109{
110  mp_send_cmd_head(ad->ad_mp, &ad->ad_mp->mp_audio, MB_CTRL_EXIT);
111
112  hts_thread_join(&ad->ad_tid);
113
114  audio_decoder_flush(ad);
115
116  av_free(ad->ad_outbuf);
117 
118  free(ad);
119}
120
121
122/**
123 *
124 */
125static void *
126ad_thread(void *aux)
127{
128  audio_decoder_t *ad = aux;
129  media_pipe_t *mp = ad->ad_mp;
130  media_queue_t *mq = &mp->mp_audio;
131  media_buf_t *mb;
132  int hold = 0;
133  int run = 1;
134
135  hts_mutex_lock(&mp->mp_mutex);
136
137  while(run) {
138
139    if((mb = TAILQ_FIRST(&mq->mq_q)) == NULL) {
140      hts_cond_wait(&mq->mq_avail, &mp->mp_mutex);
141      continue;
142    }
143
144    if(mb->mb_data_type == MB_AUDIO && hold && mb->mb_skip == 0) {
145      hts_cond_wait(&mq->mq_avail, &mp->mp_mutex);
146      continue;
147    }
148
149    TAILQ_REMOVE(&mq->mq_q, mb, mb_link);
150    mq->mq_len--;
151    if(mp->mp_stats)
152      prop_set_int(mq->mq_prop_qlen_cur, mq->mq_len);
153    hts_cond_signal(&mp->mp_backpressure);
154    hts_mutex_unlock(&mp->mp_mutex);
155
156    switch(mb->mb_data_type) {
157    case MB_CTRL_EXIT:
158      run = 0;
159      break;
160
161    case MB_CTRL_PAUSE:
162      /* Copy back any pending audio in the output fifo */
163      audio_fifo_purge(thefifo, ad, &ad->ad_hold_queue);
164      hold = 1;
165      break;
166
167    case MB_CTRL_PLAY:
168      hold = 0;
169      break;
170
171    case MB_FLUSH:
172      ad->ad_do_flush = 1;
173      /* Flush any pending audio in the output fifo */
174      audio_fifo_purge(thefifo, ad, NULL);
175      audio_decoder_flush(ad);
176      break;
177
178    case MB_AUDIO:
179      if(mb->mb_skip == 0)
180        ad_decode_buf(ad, mp, mb);
181      break;
182
183    case MB_END:
184      mp_set_current_time(mp, AV_NOPTS_VALUE);
185      break;
186
187    default:
188      abort();
189    }
190    media_buf_free(mb);
191    hts_mutex_lock(&mp->mp_mutex);
192  }
193  hts_mutex_unlock(&mp->mp_mutex);
194  audio_fifo_purge(thefifo, ad, NULL);
195  return NULL;
196}
197
198/**
199 *
200 */
201static void
202audio_deliver_passthru(media_buf_t *mb, audio_decoder_t *ad, int format,
203                       media_pipe_t *mp)
204{
205  audio_fifo_t *af = thefifo;
206  audio_buf_t *ab;
207
208  ab = af_alloc(mb->mb_size, mp);
209  ab->ab_channels = 2;
210  ab->ab_format   = format;
211  ab->ab_rate     = AM_SR_48000;
212  ab->ab_frames   = mb->mb_size;
213  ab->ab_pts      = mb->mb_pts;
214  ab->ab_epoch    = mb->mb_epoch;
215
216  memcpy(ab->ab_data, mb->mb_data, mb->mb_size);
217 
218  ab->ab_ref = ad; /* A reference to our decoder. This is used
219                      to revert out packets in the play queue during
220                      a pause event */
221 
222  af_enq(af, ab);
223}
224
225static const size_t sample_fmt_to_size[] = {
226  [SAMPLE_FMT_U8]  = sizeof(uint8_t),
227  [SAMPLE_FMT_S16] = sizeof(int16_t),
228  [SAMPLE_FMT_S32] = sizeof(int32_t),
229  [SAMPLE_FMT_FLT] = sizeof(float),
230  [SAMPLE_FMT_DBL] = sizeof(double),
231};
232
233/**
234 *
235 */
236static void
237ad_decode_buf(audio_decoder_t *ad, media_pipe_t *mp, media_buf_t *mb)
238{
239  audio_mode_t *am = audio_mode_current;
240  uint8_t *buf;
241  int size, r, data_size, channels, rate, frames, delay, i;
242  codecwrap_t *cw = mb->mb_cw;
243  AVCodecContext *ctx;
244  enum CodecID codec_id;
245  int64_t pts, chlayout;
246 
247  if(cw == NULL) {
248    /* Raw native endian PCM */
249
250
251    if(ad->ad_do_flush) {
252      ad->ad_do_flush = 0;
253      if(mp_is_primary(mp))
254        ad->ad_send_flush = 1;
255    } else if(mb->mb_time != AV_NOPTS_VALUE)
256      mp_set_current_time(mp, mb->mb_time);
257
258    frames = mb->mb_size / sizeof(int16_t) / mb->mb_channels;
259
260
261    if(mp_is_primary(mp)) {
262
263      /* Must copy if auto pipeline does multichannel upmixing */
264      memcpy(ad->ad_outbuf, mb->mb_data, mb->mb_size);
265
266      audio_mix1(ad, am, mb->mb_channels, mb->mb_rate, 0,
267                 CODEC_ID_NONE, ad->ad_outbuf, frames,
268                 mb->mb_pts, mb->mb_epoch, mp);
269     
270    } else {
271
272      /* We are just suppoed to be silent, emulate some kind of
273         delay, this is not accurate, so we also set the clock epoch
274         to zero to avoid AV sync */
275
276      mp->mp_audio_clock_epoch = 0;
277
278      delay = (int64_t)frames * 1000000LL / mb->mb_rate;
279      usleep(delay); /* XXX: Must be better */
280       
281      /* Flush any packets in the pause pending queue */
282     
283      audio_fifo_clear_queue(&ad->ad_hold_queue);
284    }
285    return;
286  }
287
288  ctx = cw->codec_ctx;
289
290
291  if(mp_is_primary(mp)) {
292    switch(ctx->codec_id) {
293    case CODEC_ID_AC3:
294      if(am->am_formats & AM_FORMAT_AC3) {
295        audio_deliver_passthru(mb, ad, AM_FORMAT_AC3, mp);
296        return;
297      }
298      break;
299
300    case CODEC_ID_DTS:
301      if(am->am_formats & AM_FORMAT_DTS) {
302        audio_deliver_passthru(mb, ad, AM_FORMAT_DTS, mp);
303        return;
304      }
305      break;
306
307    default:
308      break;
309    }
310  }
311  buf = mb->mb_data;
312  size = mb->mb_size;
313  pts = mb->mb_pts;
314 
315  while(size > 0) {
316
317    if(ad->ad_do_flush) {
318      avcodec_flush_buffers(cw->codec_ctx);
319      ad->ad_do_flush = 0;
320      if(mp_is_primary(mp))
321        ad->ad_send_flush = 1;
322    } else if(mb->mb_time != AV_NOPTS_VALUE)
323      mp_set_current_time(mp, mb->mb_time);
324
325    if(audio_mode_stereo_only(am))
326      ctx->request_channels = 2; /* We can only output stereo.
327                                    Ask codecs to do downmixing for us. */
328    else
329      ctx->request_channels = 0;
330
331    data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
332    r = avcodec_decode_audio2(ctx, ad->ad_outbuf, &data_size, buf, size);
333
334    if(r == -1)
335      break;
336
337    channels = ctx->channels;
338    rate     = ctx->sample_rate;
339    codec_id = ctx->codec_id;
340    chlayout = ctx->channel_layout;
341
342    /* Convert to signed 16bit */
343
344    frames = data_size / sample_fmt_to_size[ctx->sample_fmt];
345
346    switch(ctx->sample_fmt) {
347    case SAMPLE_FMT_NONE:
348    case SAMPLE_FMT_NB:
349      return;
350
351    case SAMPLE_FMT_U8:
352      for(i = frames - 1; i >= 0; i--)
353        ad->ad_outbuf[i] = (((uint8_t *)ad->ad_outbuf)[i] - 0x80) << 8;
354          break;
355    case SAMPLE_FMT_S16:
356      break;
357    case SAMPLE_FMT_S32:
358      for(i = 0; i < frames; i++)
359        ad->ad_outbuf[i] = ((int32_t *)ad->ad_outbuf)[i] >> 16;
360      break;
361    case SAMPLE_FMT_FLT:
362      for(i = 0; i < frames; i++)
363        ad->ad_outbuf[i] = rintf(((float *)ad->ad_outbuf)[i]) * (1 << 15);
364      break;
365    case SAMPLE_FMT_DBL:
366      for(i = 0; i < frames; i++)
367        ad->ad_outbuf[i] = rint(((float *)ad->ad_outbuf)[i]) * (1 << 15);
368      break;
369    }
370
371    frames /= channels;
372
373    if(mp_is_primary(mp)) {
374
375      /* We are the primary audio decoder == we may play, forward
376         to the mixer stages */
377
378      /* But first, if we have any pending packets (due to a previous pause),
379         release them */
380     
381      audio_fifo_reinsert(thefifo, &ad->ad_hold_queue);
382
383      if(data_size > 0)
384        audio_mix1(ad, am, channels, rate, chlayout, codec_id, ad->ad_outbuf,
385                   frames, pts, mb->mb_epoch, mp);
386
387    } else {
388
389      /* We are just suppoed to be silent, emulate some kind of
390         delay, this is not accurate, so we also set the clock epoch
391         to zero to avoid AV sync */
392
393      mp->mp_audio_clock_epoch = 0;
394
395      delay = (int64_t)frames * 1000000LL / rate;
396      usleep(delay); /* XXX: Must be better */
397       
398      /* Flush any packets in the pause pending queue */
399     
400      audio_fifo_clear_queue(&ad->ad_hold_queue);
401    }
402    pts = AV_NOPTS_VALUE;
403    buf += r;
404    size -= r;
405  }
406}
407
408
409
410/**
411 * Audio mixing stage 1
412 *
413 * All stages that reduces the number of channels is performed here.
414 * This reduces the CPU load required during the (optional) resampling.
415 */
416static void
417audio_mix1(audio_decoder_t *ad, audio_mode_t *am,
418           int channels, int rate, int64_t chlayout, enum CodecID codec_id,
419           int16_t *data0, int frames, int64_t pts, int epoch,
420           media_pipe_t *mp)
421{
422  int16_t tmp[AUDIO_CHAN_MAX];
423  int x, y, z, i, c;
424  int16_t *data, *src, *dst;
425  int rf = audio_rateflag_from_rate(rate);
426
427  /**
428   * Channel swizzling
429   */
430
431  if(chlayout != 0 && channels > 2) {
432
433    if(chlayout == 0x3f)
434      chlayout = 0x60f;
435
436    uint8_t s[AUDIO_CHAN_MAX], d[AUDIO_CHAN_MAX];
437    int ochan = 0;
438
439    x = 0;
440    i = 0;
441
442    if(chlayout & CH_FRONT_LEFT)            {s[x] = i++; d[x++] = 0;}
443    if(chlayout & CH_FRONT_RIGHT)           {s[x] = i++; d[x++] = 1;}
444    if(chlayout & CH_FRONT_CENTER)          {s[x] = i++; d[x++] = 4;}
445    if(chlayout & CH_LOW_FREQUENCY)         {s[x] = i++; d[x++] = 5;}
446    if(chlayout & CH_BACK_LEFT)             {s[x] = i++; d[x++] = 6;}
447    if(chlayout & CH_BACK_RIGHT)            {s[x] = i++; d[x++] = 7;}
448    if(chlayout & CH_FRONT_LEFT_OF_CENTER)          i++;
449    if(chlayout & CH_FRONT_RIGHT_OF_CENTER)         i++;
450    if(chlayout & CH_BACK_CENTER)                   i++;
451    if(chlayout & CH_SIDE_LEFT)             {s[x] = i++; d[x++] = 2;}
452    if(chlayout & CH_SIDE_RIGHT)            {s[x] = i++; d[x++] = 3;}
453 
454    ochan = 0;
455    for(i = 0; i < x; i++) if(d[i] > ochan) ochan = d[i];
456    ochan++;
457
458    memset(tmp, 0, sizeof(tmp));
459
460    if(ochan > channels) {
461
462      src = data0 + frames * channels;
463      dst = data0 + frames * ochan;
464
465      for(i = 0; i < frames; i++) {
466
467        src -= channels;
468        dst -= ochan;
469
470        for(c = 0; c < x; c++)
471          tmp[d[c]] = src[s[c]];
472     
473        for(c = 0; c < ochan; c++)
474          dst[c] = tmp[c];
475      }
476
477    } else {
478
479      src = data0;
480      dst = data0;
481
482      for(i = 0; i < frames; i++) {
483
484        for(c = 0; c < x; c++)
485          tmp[d[c]] = src[s[c]];
486     
487        for(c = 0; c < ochan; c++)
488          dst[c] = tmp[c];
489
490        src += channels;
491        dst += ochan;
492      }
493    }
494
495    channels = ochan;
496
497  }
498
499
500  /**
501   * 5.1 to stereo downmixing, coeffs are stolen from AAC spec
502   */
503  if(channels == 6 && audio_mode_stereo_only(am)) {
504
505    src = data0;
506    dst = data0;
507
508    for(i = 0; i < frames; i++) {
509
510      x = (src[0] * 26869) >> 16;
511      y = (src[1] * 26869) >> 16;
512
513      z = (src[4] * 19196) >> 16;
514      x += z;
515      y += z;
516
517      z = (src[5] * 13571) >> 16;
518      x += z;
519      y += z;
520
521      z = (src[2] * 13571) >> 16;
522      x -= z;
523      y += z;
524
525      z = (src[3] * 19196) >> 16;
526      x -= z;
527      y += z;
528
529      src += 6;
530
531      *dst++ = CLIP16(x);
532      *dst++ = CLIP16(y);
533    }
534    channels = 2;
535  }
536
537  /**
538   * Phantom LFE, mix it into front speakers
539   */
540  if(am->am_phantom_lfe && channels > 5) {
541    data = data0;
542    for(i = 0; i < frames; i++) {
543      x = data[0];
544      y = data[1];
545
546      z = (data[5] * 46334) >> 16;
547      x += z;
548      y += z;
549
550      data[0] = CLIP16(x);
551      data[1] = CLIP16(y);
552      data[5] = 0;
553      data += channels;
554    }
555  }
556
557
558  /**
559   * Phantom center, mix it into front speakers
560   */
561  if(am->am_phantom_center && channels > 4) {
562    data = data0;
563    for(i = 0; i < frames; i++) {
564      x = data[0];
565      y = data[1];
566
567      z = (data[4] * 46334) >> 16;
568      x += z;
569      y += z;
570
571      data[0] = CLIP16(x);
572      data[1] = CLIP16(y);
573      data[4] = 0;
574      data += channels;
575    }
576  }
577
578  /**
579   * Resampling
580   */
581  if(!(rf & am->am_sample_rates)) {
582
583    int dstrate = 48000;
584    int consumed;
585    int written;
586    int resbufsize = 4096;
587
588    if(ad->ad_resampler_srcrate  != rate    ||
589       ad->ad_resampler_dstrate  != dstrate ||
590       ad->ad_resampler_channels != channels) {
591
592      /* Must reconfigure, close */
593      close_resampler(ad);
594
595      ad->ad_resampler_srcrate  = rate;
596      ad->ad_resampler_dstrate  = dstrate;
597      ad->ad_resampler_channels = channels;
598    }
599
600    if(ad->ad_resampler == NULL) {
601      ad->ad_resbuf = malloc(resbufsize * sizeof(int16_t) * 6);
602      ad->ad_resampler = av_resample_init(dstrate, rate, 16, 10, 0, 1.0);
603    }
604
605    src = data0;
606    rate= dstrate;
607
608    /* If we have something in spill buffer, adjust PTS */
609    /* XXX: need this ?, it's very small */
610    if(pts != AV_NOPTS_VALUE)
611      pts -= 1000000LL * ad->ad_resampler_spill_size / rate;
612
613    while(frames > 0) {
614      consumed =
615        resample(ad, ad->ad_resbuf, resbufsize,
616                 &written, src, frames, channels);
617      src += consumed * channels;
618      frames -= consumed;
619
620      audio_mix2(ad, am, channels, rate, ad->ad_resbuf, written,
621                 pts, epoch, mp);
622      pts = AV_NOPTS_VALUE;
623    }
624  } else {
625    close_resampler(ad);
626    audio_mix2(ad, am, channels, rate, data0, frames, pts, epoch, mp);
627  }
628}
629
630
631 /**
632  * Audio mixing stage 2
633  *
634  * All stages that increases the number of channels is performed here now
635  * after resampling is done
636  */
637static void
638audio_mix2(audio_decoder_t *ad, audio_mode_t *am,
639           int channels, int rate, int16_t *data0, int frames, int64_t pts,
640           int epoch, media_pipe_t *mp)
641{
642  int x, y, i, c;
643  int16_t *data, *src, *dst;
644
645  /**
646   * Mono expansion (ethier to center speaker or to L + R)
647   * We also mix to LFE if possible
648   */
649  if(channels == 1) {
650    src = data0 + frames;
651
652    if(am->am_formats & AM_FORMAT_PCM_5DOT1 && !am->am_phantom_center &&
653       !am->am_force_downmix) {
654     
655      /* Mix mono to center and LFE */
656
657      dst = data0 + frames * 6;
658      for(i = 0; i < frames; i++) {
659        src--;
660        dst -= 6;
661
662        x = *src;
663
664        dst[0] = 0;
665        dst[1] = 0;
666        dst[2] = 0;
667        dst[3] = 0;
668        dst[4] = x;
669        dst[5] = x;
670      }
671      channels = 6;
672    } else if(am->am_formats & AM_FORMAT_PCM_5DOT1 && !am->am_force_downmix) {
673
674      /* Mix mono to L + R and LFE */
675
676      dst = data0 + frames * 6;
677      for(i = 0; i < frames; i++) {
678        src--;
679        dst -= 6;
680
681        x = *src;
682
683        dst[5] = x;
684        x = (x * 46334) >> 16;
685        dst[0] = x;
686        dst[1] = x;
687        dst[2] = 0;
688        dst[3] = 0;
689        dst[4] = 0;
690      }
691      channels = 6;
692    } else {
693      /* Mix mono to L + R  */
694
695      dst = data0 + frames * 2;
696      for(i = 0; i < frames; i++) {
697        src--;
698        dst -= 2;
699
700        x = *src;
701
702        x = (x * 46334) >> 16;
703
704        dst[0] = x;
705        dst[1] = x;
706      }
707      channels = 2;
708    }
709  } else /* 'Small front' already dealt with */ {
710
711    /**
712     * Small front speakers (need to mix front audio to LFE)
713     */
714    if(am->am_formats & AM_FORMAT_PCM_5DOT1 && am->am_small_front) {
715      if(channels >= 6) {
716        data = data0;
717        for(i = 0; i < frames; i++) {
718          x = data[5] + (data[0] + data[1]) / 2;
719          data[5] = CLIP16(x);
720          data += channels;
721        }
722      } else {
723        src = data0 + frames * channels;
724        dst = data0 + frames * 6;
725
726        for(i = 0; i < frames; i++) {
727          src -= channels;
728          dst -= 6;
729
730          x = (src[0] + src[1]) / 2;
731       
732          for(c = 0; c < channels; c++)
733            dst[c] = src[c];
734
735          for(; c < 5; c++)
736            dst[c] = 0;
737
738          dst[5] = x;
739        }
740        channels = 6;
741      }
742    }
743  }
744
745  /**
746   * Swap Center + LFE with Surround
747   */
748  if(am->am_swap_surround && channels > 5) {
749    data = data0;
750    for(i = 0; i < frames; i++) {
751      x = data[4];
752      y = data[5];
753      data[4] = data[2];
754      data[5] = data[3];
755      data[2] = x;
756      data[3] = y;
757     
758      data += channels;
759    }
760  }
761
762  audio_deliver(ad, am, data0, channels, frames, rate, pts, epoch, mp);
763}
764
765
766/**
767 * Enqueue audio into fifo.
768 * We slice the audio into fixed size blocks, if 'am_preferred_size' is
769 * set by the audio output module, we use that size, otherwise 1024 frames.
770 */
771static void
772audio_deliver(audio_decoder_t *ad, audio_mode_t *am, int16_t *src,
773              int channels, int frames, int rate, int64_t pts, int epoch,
774              media_pipe_t *mp)
775{
776  audio_buf_t *ab = ad->ad_buf;
777  audio_fifo_t *af = thefifo;
778  int outsize = am->am_preferred_size ?: 1024;
779  int c, r;
780
781  int format;
782  int rf = audio_rateflag_from_rate(rate);
783
784  switch(channels) {
785  case 2: format = AM_FORMAT_PCM_STEREO; break;
786  case 6: format = AM_FORMAT_PCM_5DOT1;  break;
787  case 8: format = AM_FORMAT_PCM_7DOT1;  break;
788  default:
789    return;
790  }
791
792  while(frames > 0) {
793
794    if(ab != NULL && ab->ab_channels != channels) {
795      /* Channels have changed, flush buffer */
796      ab_free(ab);
797      ab = NULL;
798    }
799
800    if(ab == NULL) {
801      ab = af_alloc(sizeof(int16_t) * channels * outsize, mp);
802      ab->ab_channels = channels;
803      ab->ab_alloced = outsize;
804      ab->ab_format = format;
805      ab->ab_rate = rf;
806      ab->ab_frames = 0;
807      ab->ab_pts = AV_NOPTS_VALUE;
808    }
809
810    if(ab->ab_pts == AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE) {
811      pts -= 1000000LL * ab->ab_frames / rate;
812      ab->ab_pts = pts;
813      ab->ab_epoch = epoch;
814      pts = AV_NOPTS_VALUE;
815    }
816
817
818    r = ab->ab_alloced - ab->ab_frames;
819    c = r < frames ? r : frames;
820
821    memcpy(ab->ab_data + sizeof(int16_t) * channels * ab->ab_frames,
822           src,          sizeof(int16_t) * channels * c);
823
824    src           += c * channels;
825    ab->ab_frames += c;
826    frames        -= c;
827
828    if(ab->ab_frames == ab->ab_alloced) {
829      ab->ab_ref = ad; /* A reference to our decoder. This is used
830                          to revert out packets in the play queue during
831                          a pause event */
832      if(ad->ad_send_flush) {
833        ab->ab_flush = 1;
834        ad->ad_send_flush = 0;
835      }
836
837      af_enq(af, ab);
838      ab = NULL;
839    }
840  }
841  ad->ad_buf = ab;
842}
843
844
845
846
847
848
849
850/**
851 *
852 */
853static void
854close_resampler(audio_decoder_t *ad)
855{
856  int c;
857
858  if(ad->ad_resampler == NULL)
859    return;
860
861  free(ad->ad_resbuf);
862  ad->ad_resbuf = NULL;
863
864  av_resample_close(ad->ad_resampler);
865 
866  for(c = 0; c < AUDIO_CHAN_MAX; c++) {
867    free(ad->ad_resampler_spill[c]);
868    ad->ad_resampler_spill[c] = NULL;
869  }
870
871  ad->ad_resampler_spill_size = 0;
872  ad->ad_resampler_channels = 0;
873  ad->ad_resampler = NULL;
874}
875
876
877/**
878 *
879 */
880static int
881resample(audio_decoder_t *ad, int16_t *dstmix, int dstavail,
882         int *writtenp, int16_t *srcmix, int srcframes, int channels)
883{
884  int c, i, j;
885  int16_t *src;
886  int16_t *dst;
887  int written = 0;
888  int consumed;
889  int srcsize;
890  int spill = ad->ad_resampler_spill_size;
891 
892   if(spill > srcframes)
893     srcframes = 0;
894
895   dst = malloc(dstavail * sizeof(uint16_t));
896
897   for(c = 0; c < channels; c++) {
898
899     if(ad->ad_resampler_spill[c] != NULL) {
900
901       srcsize = spill + srcframes;
902
903       src = malloc(srcsize * sizeof(uint16_t));
904
905       j = 0;
906
907       for(i = 0; i < spill; i++)
908         src[j++] = ad->ad_resampler_spill[c][i];
909
910       for(i = 0; i < srcframes; i++)
911         src[j++] = srcmix[i * channels + c];
912
913       free(ad->ad_resampler_spill[c]);
914       ad->ad_resampler_spill[c] = NULL;
915
916     } else {
917
918       srcsize = srcframes;
919
920       src = malloc(srcsize * sizeof(uint16_t));
921
922       for(i = 0; i < srcframes; i++)
923         src[i] = srcmix[i * channels + c];
924
925     }
926
927     written = av_resample(ad->ad_resampler, dst, src, &consumed,
928                           srcsize, dstavail, c == channels - 1);
929
930     if(consumed != srcsize) {
931       ad->ad_resampler_spill_size = srcsize - consumed;
932
933       ad->ad_resampler_spill[c] =
934         malloc(ad->ad_resampler_spill_size * sizeof(uint16_t));
935
936       memcpy(ad->ad_resampler_spill[c], src + consumed,
937              ad->ad_resampler_spill_size * sizeof(uint16_t));
938     }
939
940     for(i = 0; i < written; i++)
941       dstmix[i * channels + c] = dst[i];
942
943     free(src);
944   }
945
946   *writtenp = written;
947
948   free(dst);
949
950   return srcframes;
951 }
952
953
954
Note: See TracBrowser for help on using the browser.