]> git.sesse.net Git - ffmpeg/blob - libavresample/utils.c
hls: Initialize stream_offset before find_stream_info.
[ffmpeg] / libavresample / utils.c
1 /*
2  * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav 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 GNU
14  * 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 Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavutil/dict.h"
22 // #include "libavutil/error.h"
23 #include "libavutil/log.h"
24 #include "libavutil/mem.h"
25 #include "libavutil/opt.h"
26
27 #include "avresample.h"
28 #include "audio_data.h"
29 #include "internal.h"
30
31 int avresample_open(AVAudioResampleContext *avr)
32 {
33     int ret;
34
35     /* set channel mixing parameters */
36     avr->in_channels = av_get_channel_layout_nb_channels(avr->in_channel_layout);
37     if (avr->in_channels <= 0 || avr->in_channels > AVRESAMPLE_MAX_CHANNELS) {
38         av_log(avr, AV_LOG_ERROR, "Invalid input channel layout: %"PRIu64"\n",
39                avr->in_channel_layout);
40         return AVERROR(EINVAL);
41     }
42     avr->out_channels = av_get_channel_layout_nb_channels(avr->out_channel_layout);
43     if (avr->out_channels <= 0 || avr->out_channels > AVRESAMPLE_MAX_CHANNELS) {
44         av_log(avr, AV_LOG_ERROR, "Invalid output channel layout: %"PRIu64"\n",
45                avr->out_channel_layout);
46         return AVERROR(EINVAL);
47     }
48     avr->resample_channels = FFMIN(avr->in_channels, avr->out_channels);
49     avr->downmix_needed    = avr->in_channels  > avr->out_channels;
50     avr->upmix_needed      = avr->out_channels > avr->in_channels ||
51                              (!avr->downmix_needed && (avr->am->matrix ||
52                               avr->in_channel_layout != avr->out_channel_layout));
53     avr->mixing_needed     = avr->downmix_needed || avr->upmix_needed;
54
55     /* set resampling parameters */
56     avr->resample_needed   = avr->in_sample_rate != avr->out_sample_rate ||
57                              avr->force_resampling;
58
59     /* select internal sample format if not specified by the user */
60     if (avr->internal_sample_fmt == AV_SAMPLE_FMT_NONE &&
61         (avr->mixing_needed || avr->resample_needed)) {
62         enum AVSampleFormat  in_fmt = av_get_planar_sample_fmt(avr->in_sample_fmt);
63         enum AVSampleFormat out_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
64         int max_bps = FFMAX(av_get_bytes_per_sample(in_fmt),
65                             av_get_bytes_per_sample(out_fmt));
66         if (max_bps <= 2) {
67             avr->internal_sample_fmt = AV_SAMPLE_FMT_S16P;
68         } else if (avr->mixing_needed) {
69             avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
70         } else {
71             if (max_bps <= 4) {
72                 if (in_fmt  == AV_SAMPLE_FMT_S32P ||
73                     out_fmt == AV_SAMPLE_FMT_S32P) {
74                     if (in_fmt  == AV_SAMPLE_FMT_FLTP ||
75                         out_fmt == AV_SAMPLE_FMT_FLTP) {
76                         /* if one is s32 and the other is flt, use dbl */
77                         avr->internal_sample_fmt = AV_SAMPLE_FMT_DBLP;
78                     } else {
79                         /* if one is s32 and the other is s32, s16, or u8, use s32 */
80                         avr->internal_sample_fmt = AV_SAMPLE_FMT_S32P;
81                     }
82                 } else {
83                     /* if one is flt and the other is flt, s16 or u8, use flt */
84                     avr->internal_sample_fmt = AV_SAMPLE_FMT_FLTP;
85                 }
86             } else {
87                 /* if either is dbl, use dbl */
88                 avr->internal_sample_fmt = AV_SAMPLE_FMT_DBLP;
89             }
90         }
91         av_log(avr, AV_LOG_DEBUG, "Using %s as internal sample format\n",
92                av_get_sample_fmt_name(avr->internal_sample_fmt));
93     }
94
95     /* set sample format conversion parameters */
96     if (avr->in_channels == 1)
97         avr->in_sample_fmt = av_get_planar_sample_fmt(avr->in_sample_fmt);
98     if (avr->out_channels == 1)
99         avr->out_sample_fmt = av_get_planar_sample_fmt(avr->out_sample_fmt);
100     avr->in_convert_needed = (avr->resample_needed || avr->mixing_needed) &&
101                               avr->in_sample_fmt != avr->internal_sample_fmt;
102     if (avr->resample_needed || avr->mixing_needed)
103         avr->out_convert_needed = avr->internal_sample_fmt != avr->out_sample_fmt;
104     else
105         avr->out_convert_needed = avr->in_sample_fmt != avr->out_sample_fmt;
106
107     /* allocate buffers */
108     if (avr->mixing_needed || avr->in_convert_needed) {
109         avr->in_buffer = ff_audio_data_alloc(FFMAX(avr->in_channels, avr->out_channels),
110                                              0, avr->internal_sample_fmt,
111                                              "in_buffer");
112         if (!avr->in_buffer) {
113             ret = AVERROR(EINVAL);
114             goto error;
115         }
116     }
117     if (avr->resample_needed) {
118         avr->resample_out_buffer = ff_audio_data_alloc(avr->out_channels,
119                                                        0, avr->internal_sample_fmt,
120                                                        "resample_out_buffer");
121         if (!avr->resample_out_buffer) {
122             ret = AVERROR(EINVAL);
123             goto error;
124         }
125     }
126     if (avr->out_convert_needed) {
127         avr->out_buffer = ff_audio_data_alloc(avr->out_channels, 0,
128                                               avr->out_sample_fmt, "out_buffer");
129         if (!avr->out_buffer) {
130             ret = AVERROR(EINVAL);
131             goto error;
132         }
133     }
134     avr->out_fifo = av_audio_fifo_alloc(avr->out_sample_fmt, avr->out_channels,
135                                         1024);
136     if (!avr->out_fifo) {
137         ret = AVERROR(ENOMEM);
138         goto error;
139     }
140
141     /* setup contexts */
142     if (avr->in_convert_needed) {
143         avr->ac_in = ff_audio_convert_alloc(avr, avr->internal_sample_fmt,
144                                             avr->in_sample_fmt, avr->in_channels);
145         if (!avr->ac_in) {
146             ret = AVERROR(ENOMEM);
147             goto error;
148         }
149     }
150     if (avr->out_convert_needed) {
151         enum AVSampleFormat src_fmt;
152         if (avr->in_convert_needed)
153             src_fmt = avr->internal_sample_fmt;
154         else
155             src_fmt = avr->in_sample_fmt;
156         avr->ac_out = ff_audio_convert_alloc(avr, avr->out_sample_fmt, src_fmt,
157                                              avr->out_channels);
158         if (!avr->ac_out) {
159             ret = AVERROR(ENOMEM);
160             goto error;
161         }
162     }
163     if (avr->resample_needed) {
164         avr->resample = ff_audio_resample_init(avr);
165         if (!avr->resample) {
166             ret = AVERROR(ENOMEM);
167             goto error;
168         }
169     }
170     if (avr->mixing_needed) {
171         ret = ff_audio_mix_init(avr);
172         if (ret < 0)
173             goto error;
174     }
175
176     return 0;
177
178 error:
179     avresample_close(avr);
180     return ret;
181 }
182
183 void avresample_close(AVAudioResampleContext *avr)
184 {
185     ff_audio_data_free(&avr->in_buffer);
186     ff_audio_data_free(&avr->resample_out_buffer);
187     ff_audio_data_free(&avr->out_buffer);
188     av_audio_fifo_free(avr->out_fifo);
189     avr->out_fifo = NULL;
190     av_freep(&avr->ac_in);
191     av_freep(&avr->ac_out);
192     ff_audio_resample_free(&avr->resample);
193     ff_audio_mix_close(avr->am);
194     return;
195 }
196
197 void avresample_free(AVAudioResampleContext **avr)
198 {
199     if (!*avr)
200         return;
201     avresample_close(*avr);
202     av_freep(&(*avr)->am);
203     av_opt_free(*avr);
204     av_freep(avr);
205 }
206
207 static int handle_buffered_output(AVAudioResampleContext *avr,
208                                   AudioData *output, AudioData *converted)
209 {
210     int ret;
211
212     if (!output || av_audio_fifo_size(avr->out_fifo) > 0 ||
213         (converted && output->allocated_samples < converted->nb_samples)) {
214         if (converted) {
215             /* if there are any samples in the output FIFO or if the
216                user-supplied output buffer is not large enough for all samples,
217                we add to the output FIFO */
218             av_dlog(avr, "[FIFO] add %s to out_fifo\n", converted->name);
219             ret = ff_audio_data_add_to_fifo(avr->out_fifo, converted, 0,
220                                             converted->nb_samples);
221             if (ret < 0)
222                 return ret;
223         }
224
225         /* if the user specified an output buffer, read samples from the output
226            FIFO to the user output */
227         if (output && output->allocated_samples > 0) {
228             av_dlog(avr, "[FIFO] read from out_fifo to output\n");
229             av_dlog(avr, "[end conversion]\n");
230             return ff_audio_data_read_from_fifo(avr->out_fifo, output,
231                                                 output->allocated_samples);
232         }
233     } else if (converted) {
234         /* copy directly to output if it is large enough or there is not any
235            data in the output FIFO */
236         av_dlog(avr, "[copy] %s to output\n", converted->name);
237         output->nb_samples = 0;
238         ret = ff_audio_data_copy(output, converted);
239         if (ret < 0)
240             return ret;
241         av_dlog(avr, "[end conversion]\n");
242         return output->nb_samples;
243     }
244     av_dlog(avr, "[end conversion]\n");
245     return 0;
246 }
247
248 int attribute_align_arg avresample_convert(AVAudioResampleContext *avr,
249                                            void **output, int out_plane_size,
250                                            int out_samples, void **input,
251                                            int in_plane_size, int in_samples)
252 {
253     AudioData input_buffer;
254     AudioData output_buffer;
255     AudioData *current_buffer;
256     int ret;
257
258     /* reset internal buffers */
259     if (avr->in_buffer) {
260         avr->in_buffer->nb_samples = 0;
261         ff_audio_data_set_channels(avr->in_buffer,
262                                    avr->in_buffer->allocated_channels);
263     }
264     if (avr->resample_out_buffer) {
265         avr->resample_out_buffer->nb_samples = 0;
266         ff_audio_data_set_channels(avr->resample_out_buffer,
267                                    avr->resample_out_buffer->allocated_channels);
268     }
269     if (avr->out_buffer) {
270         avr->out_buffer->nb_samples = 0;
271         ff_audio_data_set_channels(avr->out_buffer,
272                                    avr->out_buffer->allocated_channels);
273     }
274
275     av_dlog(avr, "[start conversion]\n");
276
277     /* initialize output_buffer with output data */
278     if (output) {
279         ret = ff_audio_data_init(&output_buffer, output, out_plane_size,
280                                  avr->out_channels, out_samples,
281                                  avr->out_sample_fmt, 0, "output");
282         if (ret < 0)
283             return ret;
284         output_buffer.nb_samples = 0;
285     }
286
287     if (input) {
288         /* initialize input_buffer with input data */
289         ret = ff_audio_data_init(&input_buffer, input, in_plane_size,
290                                  avr->in_channels, in_samples,
291                                  avr->in_sample_fmt, 1, "input");
292         if (ret < 0)
293             return ret;
294         current_buffer = &input_buffer;
295
296         if (avr->upmix_needed && !avr->in_convert_needed && !avr->resample_needed &&
297             !avr->out_convert_needed && output && out_samples >= in_samples) {
298             /* in some rare cases we can copy input to output and upmix
299                directly in the output buffer */
300             av_dlog(avr, "[copy] %s to output\n", current_buffer->name);
301             ret = ff_audio_data_copy(&output_buffer, current_buffer);
302             if (ret < 0)
303                 return ret;
304             current_buffer = &output_buffer;
305         } else if (avr->mixing_needed || avr->in_convert_needed) {
306             /* if needed, copy or convert input to in_buffer, and downmix if
307                applicable */
308             if (avr->in_convert_needed) {
309                 ret = ff_audio_data_realloc(avr->in_buffer,
310                                             current_buffer->nb_samples);
311                 if (ret < 0)
312                     return ret;
313                 av_dlog(avr, "[convert] %s to in_buffer\n", current_buffer->name);
314                 ret = ff_audio_convert(avr->ac_in, avr->in_buffer, current_buffer,
315                                        current_buffer->nb_samples);
316                 if (ret < 0)
317                     return ret;
318             } else {
319                 av_dlog(avr, "[copy] %s to in_buffer\n", current_buffer->name);
320                 ret = ff_audio_data_copy(avr->in_buffer, current_buffer);
321                 if (ret < 0)
322                     return ret;
323             }
324             ff_audio_data_set_channels(avr->in_buffer, avr->in_channels);
325             if (avr->downmix_needed) {
326                 av_dlog(avr, "[downmix] in_buffer\n");
327                 ret = ff_audio_mix(avr->am, avr->in_buffer);
328                 if (ret < 0)
329                     return ret;
330             }
331             current_buffer = avr->in_buffer;
332         }
333     } else {
334         /* flush resampling buffer and/or output FIFO if input is NULL */
335         if (!avr->resample_needed)
336             return handle_buffered_output(avr, output ? &output_buffer : NULL,
337                                           NULL);
338         current_buffer = NULL;
339     }
340
341     if (avr->resample_needed) {
342         AudioData *resample_out;
343         int consumed = 0;
344
345         if (!avr->out_convert_needed && output && out_samples > 0)
346             resample_out = &output_buffer;
347         else
348             resample_out = avr->resample_out_buffer;
349         av_dlog(avr, "[resample] %s to %s\n", current_buffer->name,
350                 resample_out->name);
351         ret = ff_audio_resample(avr->resample, resample_out,
352                                 current_buffer, &consumed);
353         if (ret < 0)
354             return ret;
355
356         /* if resampling did not produce any samples, just return 0 */
357         if (resample_out->nb_samples == 0) {
358             av_dlog(avr, "[end conversion]\n");
359             return 0;
360         }
361
362         current_buffer = resample_out;
363     }
364
365     if (avr->upmix_needed) {
366         av_dlog(avr, "[upmix] %s\n", current_buffer->name);
367         ret = ff_audio_mix(avr->am, current_buffer);
368         if (ret < 0)
369             return ret;
370     }
371
372     /* if we resampled or upmixed directly to output, return here */
373     if (current_buffer == &output_buffer) {
374         av_dlog(avr, "[end conversion]\n");
375         return current_buffer->nb_samples;
376     }
377
378     if (avr->out_convert_needed) {
379         if (output && out_samples >= current_buffer->nb_samples) {
380             /* convert directly to output */
381             av_dlog(avr, "[convert] %s to output\n", current_buffer->name);
382             ret = ff_audio_convert(avr->ac_out, &output_buffer, current_buffer,
383                                    current_buffer->nb_samples);
384             if (ret < 0)
385                 return ret;
386
387             av_dlog(avr, "[end conversion]\n");
388             return output_buffer.nb_samples;
389         } else {
390             ret = ff_audio_data_realloc(avr->out_buffer,
391                                         current_buffer->nb_samples);
392             if (ret < 0)
393                 return ret;
394             av_dlog(avr, "[convert] %s to out_buffer\n", current_buffer->name);
395             ret = ff_audio_convert(avr->ac_out, avr->out_buffer,
396                                    current_buffer, current_buffer->nb_samples);
397             if (ret < 0)
398                 return ret;
399             current_buffer = avr->out_buffer;
400         }
401     }
402
403     return handle_buffered_output(avr, output ? &output_buffer : NULL,
404                                   current_buffer);
405 }
406
407 int avresample_available(AVAudioResampleContext *avr)
408 {
409     return av_audio_fifo_size(avr->out_fifo);
410 }
411
412 int avresample_read(AVAudioResampleContext *avr, void **output, int nb_samples)
413 {
414     if (!output)
415         return av_audio_fifo_drain(avr->out_fifo, nb_samples);
416     return av_audio_fifo_read(avr->out_fifo, output, nb_samples);
417 }
418
419 unsigned avresample_version(void)
420 {
421     return LIBAVRESAMPLE_VERSION_INT;
422 }
423
424 const char *avresample_license(void)
425 {
426 #define LICENSE_PREFIX "libavresample license: "
427     return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
428 }
429
430 const char *avresample_configuration(void)
431 {
432     return FFMPEG_CONFIGURATION;
433 }