summaryrefslogtreecommitdiff
path: root/extra/xine-lib/xine-lib-1.2.2-ffmpeg_audio_decoder-support-new-av_decode_audio4.patch
blob: 255f56d0dad0009ed13f71b298f125f16ca90a0d (plain)
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
From 679582503c8ba2f91a82e44c4a2938550306fd07 Mon Sep 17 00:00:00 2001
From: Torsten Jager <t.jager@gmx.de>
Date: Thu, 21 Feb 2013 22:35:00 +0200
Subject: [PATCH 3/5] ffmpeg_audio_decoder: support new av_decode_audio4 ()

---
 src/combined/ffmpeg/ff_audio_decoder.c | 153 +++++++++++++++++++++++++++------
 src/combined/ffmpeg/ffmpeg_compat.h    |   4 +-
 2 files changed, 131 insertions(+), 26 deletions(-)

diff --git a/src/combined/ffmpeg/ff_audio_decoder.c b/src/combined/ffmpeg/ff_audio_decoder.c
index 4e64378..34a7a9e 100644
--- a/src/combined/ffmpeg/ff_audio_decoder.c
+++ b/src/combined/ffmpeg/ff_audio_decoder.c
@@ -71,7 +71,9 @@ typedef struct ff_audio_decoder_s {
   int               decoder_ok;
 
   AVCodecParserContext *parser_context;
-
+#if AVAUDIO > 3
+  AVFrame          *av_frame;
+#endif
 } ff_audio_decoder_t;
 
 
@@ -150,6 +152,9 @@ static void ff_audio_init_codec(ff_audio_decoder_t *this, unsigned int codec_typ
     return;
   }
 
+  /* Try to make the following true */
+  this->context->request_sample_fmt = AV_SAMPLE_FMT_S16;
+
   /* Current ffmpeg audio decoders usually use 16 bits/sample
    * buf->decoder_info[2] can't be used as it doesn't refer to the output
    * bits/sample for some codecs (e.g. MS ADPCM) */
@@ -358,25 +363,19 @@ static void ff_audio_output_close(ff_audio_decoder_t *this)
   this->audio_channels = 0;
 }
 
-static int ff_audio_decode(xine_t *xine,
-                           AVCodecContext *ctx,
-                           AVCodecParserContext *parser_ctx,
-                           int16_t *decode_buffer, int *decode_buffer_size,
-                           uint8_t *buf, int size)
-{
+static int ff_audio_decode (ff_audio_decoder_t *this,
+  int16_t *decode_buffer, int *decode_buffer_size, uint8_t *buf, int size) {
   int consumed;
   int parser_consumed = 0;
 
 #if AVPARSE > 1
-  if (parser_ctx) {
+  if (this->parser_context) {
     uint8_t *outbuf;
     int      outsize;
 
     do {
-      int ret = av_parser_parse2(parser_ctx, ctx,
-                                 &outbuf, &outsize,
-                                 buf, size,
-                                 0, 0, 0); 
+      int ret = av_parser_parse2 (this->parser_context, this->context,
+        &outbuf, &outsize, buf, size, 0, 0, 0);
       parser_consumed += ret;
       buf             += ret;
       size            -= ret;
@@ -385,7 +384,7 @@ static int ff_audio_decode(xine_t *xine,
     /* nothing to decode ? */
     if (outsize <= 0) {
       *decode_buffer_size = 0;
-      xprintf (xine, XINE_VERBOSITY_DEBUG,
+      xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
                "ffmpeg_audio_dec: not enough data to decode\n");
       return parser_consumed;
     }
@@ -402,21 +401,115 @@ static int ff_audio_decode(xine_t *xine,
   avpkt.data = buf;
   avpkt.size = size;
   avpkt.flags = AV_PKT_FLAG_KEY;
-  consumed = avcodec_decode_audio3 (ctx,
-                                    decode_buffer, decode_buffer_size,
-                                    &avpkt);
+#  if AVAUDIO > 3
+  int got_frame;
+  if (!this->av_frame)
+    this->av_frame = avcodec_alloc_frame ();
+
+  consumed = avcodec_decode_audio4 (this->context, this->av_frame, &got_frame, &avpkt);
+  if ((consumed >= 0) && got_frame) {
+    int16_t *q = decode_buffer;
+    int samples = this->av_frame->nb_samples;
+    int channels = this->context->channels;
+    int bytes, i, j;
+    /* limit buffer */
+    if (channels > 12)
+      channels = 12;
+    if (*decode_buffer_size < samples * channels * 2)
+      samples = *decode_buffer_size / (channels * 2);
+    bytes = samples * channels * 2;
+    *decode_buffer_size = bytes;
+    /* convert to packed int16_t. I guess there is something
+       in libavfilter but also another dependency... */
+    switch (this->context->sample_fmt) {
+      case AV_SAMPLE_FMT_U8P:
+        if (channels > 1) {
+          uint8_t *p[12];
+          for (i = 0; i < channels; i++)
+            p[i] = (uint8_t *)this->av_frame->extended_data[i];
+          for (i = samples; i; i--) {
+            for (j = 0; j < channels; j++)
+              *q++ = ((uint16_t)(*p[j]++) << 8) ^ 0x8000;
+          }
+          break;
+        }
+      case AV_SAMPLE_FMT_U8:
+        {
+          uint8_t *p = (uint8_t *)this->av_frame->extended_data[0];
+          for (i = samples * channels; i; i--)
+            *q++ = ((uint16_t)(*p++) << 8) ^ 0x8000;
+        }
+      break;
+      case AV_SAMPLE_FMT_S16P:
+        if (channels > 1) {
+          int16_t *p[12];
+          for (i = 0; i < channels; i++)
+            p[i] = (int16_t *)this->av_frame->extended_data[i];
+          for (i = samples; i; i--) {
+            for (j = 0; j < channels; j++)
+              *q++ = *p[j]++;
+          }
+          break;
+        }
+      case AV_SAMPLE_FMT_S16:
+        xine_fast_memcpy (q, this->av_frame->extended_data[0], bytes);
+      break;
+      case AV_SAMPLE_FMT_S32P:
+        if (channels > 1) {
+          int32_t *p[12];
+          for (i = 0; i < channels; i++)
+            p[i] = (int32_t *)this->av_frame->extended_data[i];
+          for (i = samples; i; i--) {
+            for (j = 0; j < channels; j++)
+              *q++ = *p[j]++ >> 16;
+          }
+          break;
+        }
+      case AV_SAMPLE_FMT_S32:
+        {
+          int32_t *p = (int32_t *)this->av_frame->extended_data[0];
+          for (i = samples * channels; i; i--)
+            *q++ = *p++ >> 16;
+        }
+      break;
+      case AV_SAMPLE_FMT_FLTP: /* the most popular one */
+        if (channels > 1) {
+          float *p[12];
+          for (i = 0; i < channels; i++)
+            p[i] = (float *)this->av_frame->extended_data[i];
+          for (i = samples; i; i--) {
+            for (j = 0; j < channels; j++) {
+              int v = *p[j]++ * (float)0x7fff;
+              *q++ = (v + 0x8000) & ~0xffff ? (v >> 31) ^ 0x7fff : v;
+            }
+          }
+          break;
+        }
+      case AV_SAMPLE_FMT_FLT:
+        {
+          float *p = (float *)this->av_frame->extended_data[0];
+          for (i = samples * channels; i; i--) {
+            int v = *p++ * (float)0x7fff;
+            *q++ = (v + 0x8000) & ~0xffff ? (v >> 31) ^ 0x7fff : v;
+          }
+        }
+      break;
+      default: ;
+    }
+  } else *decode_buffer_size = 0;
+#  else
+  consumed = avcodec_decode_audio3 (this->context, decode_buffer, decode_buffer_size, &avpkt);
+#  endif
 #else
-  consumed = avcodec_decode_audio2 (ctx,
-                                    decode_buffer, decode_buffer_size,
-                                    buf, size);
+  consumed = avcodec_decode_audio2 (this->context, decode_buffer, decode_buffer_size, buf, size);
 #endif
 
   if (consumed < 0) {
-    xprintf (xine, XINE_VERBOSITY_DEBUG,
+    xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
              "ffmpeg_audio_dec: error decompressing audio frame (%d)\n", consumed);
   } else if (parser_consumed && consumed != size) {
 
-    xprintf (xine, XINE_VERBOSITY_DEBUG,
+    xprintf (this->stream->xine, XINE_VERBOSITY_DEBUG,
              "ffmpeg_audio_dec: decoder didn't consume all data\n");
   }
 
@@ -469,8 +562,7 @@ static void ff_audio_decode_data (audio_decoder_t *this_gen, buf_element_t *buf)
         decode_buffer_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
 
 	bytes_consumed =
-          ff_audio_decode(this->stream->xine, this->context,
-                          this->parser_context,
+          ff_audio_decode(this,
                           (int16_t *)this->decode_buffer, &decode_buffer_size,
                           &this->buf[offset], this->size);
 
@@ -539,6 +631,7 @@ static void ff_audio_decode_data (audio_decoder_t *this_gen, buf_element_t *buf)
           }
 
           /* fill up this buffer */
+#if AVAUDIO < 4
           if (codec_type == BUF_AUDIO_WMAPRO) {
             /* the above codecs output float samples, not 16-bit integers */
             int bytes_per_sample = sizeof(float);
@@ -558,7 +651,9 @@ static void ff_audio_decode_data (audio_decoder_t *this_gen, buf_element_t *buf)
             bytes_to_send = bytes_to_send * 2 / bytes_per_sample;
             xine_fast_memcpy(audio_buffer->mem, int_buffer, bytes_to_send);
             free(int_buffer);
-          } else {
+          } else
+#endif
+          {
             if ((decode_buffer_size - out) > audio_buffer->mem_size)
               bytes_to_send = audio_buffer->mem_size;
             else
@@ -595,6 +690,9 @@ static void ff_audio_reset (audio_decoder_t *this_gen) {
 
   /* try to reset the wma decoder */
   if( this->decoder_ok ) {
+#if AVAUDIO > 3
+    avcodec_free_frame (&this->av_frame);
+#endif
     pthread_mutex_lock (&ffmpeg_lock);
     avcodec_close (this->context);
     if (avcodec_open (this->context, this->codec) < 0)
@@ -626,6 +724,9 @@ static void ff_audio_dispose (audio_decoder_t *this_gen) {
   }
 
   if( this->context && this->decoder_ok ) {
+#if AVAUDIO > 3
+    avcodec_free_frame (&this->av_frame);
+#endif
     pthread_mutex_lock (&ffmpeg_lock);
     avcodec_close (this->context);
     pthread_mutex_unlock (&ffmpeg_lock);
@@ -668,7 +769,9 @@ static audio_decoder_t *ff_audio_open_plugin (audio_decoder_class_t *class_gen,
 
   this->context = avcodec_alloc_context();
   this->decode_buffer = malloc16 (AVCODEC_MAX_AUDIO_FRAME_SIZE);
-
+#if AVAUDIO > 3
+  this->av_frame = NULL;
+#endif
   return &this->audio_decoder;
 }
 
diff --git a/src/combined/ffmpeg/ffmpeg_compat.h b/src/combined/ffmpeg/ffmpeg_compat.h
index 0acead8..b567dc1 100644
--- a/src/combined/ffmpeg/ffmpeg_compat.h
+++ b/src/combined/ffmpeg/ffmpeg_compat.h
@@ -94,7 +94,9 @@
 #endif
 
 /* avcodec_decode_audio() */
-#if LIBAVCODEC_VERSION_MAJOR >= 53 || (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR >= 32)
+#if LIBAVCODEC_VERSION_MAJOR >= 54
+#  define AVAUDIO 4
+#elif LIBAVCODEC_VERSION_MAJOR >= 53 || (LIBAVCODEC_VERSION_MAJOR == 52 && LIBAVCODEC_VERSION_MINOR >= 32)
 #  define AVAUDIO 3
 #else
 #  define AVAUDIO 2
-- 
1.8.1.5