1 /*****************************************************************************
2  * lwlibav_audio.c / lwlibav_audio.cpp
3  *****************************************************************************
4  * Copyright (C) 2012-2015 L-SMASH Works project
5  *
6  * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
7  *
8  * Permission to use, copy, modify, and/or distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  *****************************************************************************/
20 
21 /* This file is available under an ISC license. */
22 
23 #include "cpp_compat.h"
24 
25 #ifdef __cplusplus
26 extern "C"
27 {
28 #endif  /* __cplusplus */
29 #include <libavformat/avformat.h>       /* Demuxer */
30 #include <libavcodec/avcodec.h>         /* Decoder */
31 #include <libavresample/avresample.h>   /* Resampler/Buffer */
32 #include <libavutil/mem.h>
33 #include <libavutil/opt.h>
34 #ifdef __cplusplus
35 }
36 #endif  /* __cplusplus */
37 
38 #include "utils.h"
39 #include "audio_output.h"
40 #include "resample.h"
41 #include "decode.h"
42 
43 #include "lwlibav_dec.h"
44 #include "lwlibav_audio.h"
45 #include "lwlibav_audio_internal.h"
46 
47 /*****************************************************************************
48  * Allocators / Deallocators
49  *****************************************************************************/
lwlibav_audio_alloc_decode_handler(void)50 lwlibav_audio_decode_handler_t *lwlibav_audio_alloc_decode_handler
51 (
52     void
53 )
54 {
55     lwlibav_audio_decode_handler_t *adhp = (lwlibav_audio_decode_handler_t *)lw_malloc_zero( sizeof(lwlibav_audio_decode_handler_t) );
56     if( !adhp )
57         return NULL;
58     adhp->frame_buffer = av_frame_alloc();
59     if( !adhp->frame_buffer )
60     {
61         lwlibav_audio_free_decode_handler( adhp );
62         return NULL;
63     }
64     return adhp;
65 }
66 
lwlibav_audio_alloc_output_handler(void)67 lwlibav_audio_output_handler_t *lwlibav_audio_alloc_output_handler
68 (
69     void
70 )
71 {
72     return (lwlibav_audio_output_handler_t *)lw_malloc_zero( sizeof(lwlibav_audio_output_handler_t) );
73 }
74 
lwlibav_audio_free_decode_handler(lwlibav_audio_decode_handler_t * adhp)75 void lwlibav_audio_free_decode_handler
76 (
77     lwlibav_audio_decode_handler_t *adhp
78 )
79 {
80     if( !adhp )
81         return;
82     lwlibav_extradata_handler_t *exhp = &adhp->exh;
83     if( exhp->entries )
84     {
85         for( int i = 0; i < exhp->entry_count; i++ )
86             if( exhp->entries[i].extradata )
87                 av_free( exhp->entries[i].extradata );
88         lw_free( exhp->entries );
89     }
90     av_packet_unref( &adhp->packet );
91     lw_free( adhp->frame_list );
92     av_free( adhp->index_entries );
93     av_frame_free( &adhp->frame_buffer );
94     avcodec_free_context( &adhp->ctx );
95     if( adhp->format )
96         lavf_close_file( &adhp->format );
97     lw_free( adhp );
98 }
99 
lwlibav_audio_free_output_handler(lwlibav_audio_output_handler_t * aohp)100 void lwlibav_audio_free_output_handler
101 (
102     lwlibav_audio_output_handler_t *aohp
103 )
104 {
105     if( !aohp )
106         return;
107     lw_cleanup_audio_output_handler( aohp );
108     lw_free( aohp );
109 }
110 
lwlibav_audio_free_decode_handler_ptr(lwlibav_audio_decode_handler_t ** adhpp)111 void lwlibav_audio_free_decode_handler_ptr
112 (
113     lwlibav_audio_decode_handler_t **adhpp
114 )
115 {
116     if( !adhpp || !*adhpp )
117         return;
118     lwlibav_audio_free_decode_handler( *adhpp );
119     *adhpp = NULL;
120 }
121 
lwlibav_audio_free_output_handler_ptr(lwlibav_audio_output_handler_t ** aohpp)122 void lwlibav_audio_free_output_handler_ptr
123 (
124     lwlibav_audio_output_handler_t **aohpp
125 )
126 {
127     if( !aohpp || !*aohpp )
128         return;
129     lwlibav_audio_free_output_handler( *aohpp );
130     *aohpp = NULL;
131 }
132 
133 /*****************************************************************************
134  * Setters
135  *****************************************************************************/
lwlibav_audio_set_preferred_decoder_names(lwlibav_audio_decode_handler_t * adhp,const char ** preferred_decoder_names)136 void lwlibav_audio_set_preferred_decoder_names
137 (
138     lwlibav_audio_decode_handler_t *adhp,
139     const char                    **preferred_decoder_names
140 )
141 {
142     adhp->preferred_decoder_names = preferred_decoder_names;
143 }
144 
lwlibav_audio_set_log_handler(lwlibav_audio_decode_handler_t * adhp,lw_log_handler_t * lh)145 void lwlibav_audio_set_log_handler
146 (
147     lwlibav_audio_decode_handler_t *adhp,
148     lw_log_handler_t               *lh
149 )
150 {
151     adhp->lh = *lh;
152 }
153 
154 /*****************************************************************************
155  * Getters
156  *****************************************************************************/
lwlibav_audio_get_preferred_decoder_names(lwlibav_audio_decode_handler_t * adhp)157 const char **lwlibav_audio_get_preferred_decoder_names
158 (
159     lwlibav_audio_decode_handler_t *adhp
160 )
161 {
162     return adhp ? adhp->preferred_decoder_names : NULL;
163 }
164 
lwlibav_audio_get_log_handler(lwlibav_audio_decode_handler_t * adhp)165 lw_log_handler_t *lwlibav_audio_get_log_handler
166 (
167     lwlibav_audio_decode_handler_t *adhp
168 )
169 {
170     return adhp ? &adhp->lh : NULL;
171 }
172 
lwlibav_audio_get_codec_context(lwlibav_audio_decode_handler_t * adhp)173 AVCodecContext *lwlibav_audio_get_codec_context
174 (
175     lwlibav_audio_decode_handler_t *adhp
176 )
177 {
178     return adhp ? adhp->ctx : NULL;
179 }
180 
181 /*****************************************************************************
182  * Others
183  *****************************************************************************/
lwlibav_audio_force_seek(lwlibav_audio_decode_handler_t * adhp)184 void lwlibav_audio_force_seek
185 (
186     lwlibav_audio_decode_handler_t *adhp
187 )
188 {
189     /* Force seek before the next reading. */
190     adhp->next_pcm_sample_number = adhp->pcm_sample_count + 1;
191 }
192 
lwlibav_audio_get_desired_track(const char * file_path,lwlibav_audio_decode_handler_t * adhp,int threads)193 int lwlibav_audio_get_desired_track
194 (
195     const char                     *file_path,
196     lwlibav_audio_decode_handler_t *adhp,
197     int                             threads
198 )
199 {
200     AVCodecContext *ctx = NULL;
201     if( adhp->stream_index < 0
202      || adhp->frame_count == 0
203      || lavf_open_file( &adhp->format, file_path, &adhp->lh ) < 0
204      || find_and_open_decoder( &ctx, adhp->format->streams[ adhp->stream_index ]->codecpar,
205                                adhp->preferred_decoder_names, threads, 0 ) < 0 )
206     {
207         av_freep( &adhp->index_entries );
208         lw_freep( &adhp->frame_list );
209         if( adhp->format )
210             lavf_close_file( &adhp->format );
211         return -1;
212     }
213     adhp->ctx = ctx;
214     return 0;
215 }
216 
lwlibav_audio_count_overall_pcm_samples(lwlibav_audio_decode_handler_t * adhp,int output_sample_rate)217 uint64_t lwlibav_audio_count_overall_pcm_samples
218 (
219     lwlibav_audio_decode_handler_t *adhp,
220     int                             output_sample_rate
221 )
222 {
223     audio_frame_info_t *frame_list    = adhp->frame_list;
224     int      current_sample_rate      = frame_list[1].sample_rate > 0 ? frame_list[1].sample_rate : adhp->ctx->sample_rate;
225     uint32_t current_frame_length     = frame_list[1].length;
226     uint64_t pcm_sample_count         = 0;
227     uint64_t overall_pcm_sample_count = 0;
228     for( uint32_t i = 1; i <= adhp->frame_count; i++ )
229     {
230         if( (current_sample_rate != frame_list[i].sample_rate && frame_list[i].sample_rate > 0)
231          || current_frame_length != frame_list[i].length )
232         {
233             uint64_t resampled_sample_count = output_sample_rate == current_sample_rate || pcm_sample_count == 0
234                                             ? pcm_sample_count
235                                             : (pcm_sample_count * output_sample_rate - 1) / current_sample_rate + 1;
236             overall_pcm_sample_count += resampled_sample_count;
237             pcm_sample_count     = 0;
238             current_sample_rate  = frame_list[i].sample_rate > 0 ? frame_list[i].sample_rate : adhp->ctx->sample_rate;
239             current_frame_length = frame_list[i].length;
240         }
241         pcm_sample_count += frame_list[i].length;
242     }
243     current_sample_rate = frame_list[ adhp->frame_count ].sample_rate > 0
244                         ? frame_list[ adhp->frame_count ].sample_rate
245                         : adhp->ctx->sample_rate;
246     if( pcm_sample_count )
247         overall_pcm_sample_count += (pcm_sample_count * output_sample_rate - 1) / current_sample_rate + 1;
248     /* Return the number of output PCM audio samples. */
249     adhp->pcm_sample_count = overall_pcm_sample_count;
250     return overall_pcm_sample_count;
251 }
252 
find_start_audio_frame(lwlibav_audio_decode_handler_t * adhp,int output_sample_rate,uint64_t start_frame_pos,uint64_t * start_offset)253 static int find_start_audio_frame
254 (
255     lwlibav_audio_decode_handler_t *adhp,
256     int                             output_sample_rate,
257     uint64_t                        start_frame_pos,
258     uint64_t                       *start_offset
259 )
260 {
261     audio_frame_info_t *frame_list = adhp->frame_list;
262     uint32_t frame_number                    = 1;
263     uint64_t current_frame_pos               = 0;
264     uint64_t next_frame_pos                  = 0;
265     int      current_sample_rate             = frame_list[frame_number].sample_rate > 0 ? frame_list[frame_number].sample_rate : adhp->ctx->sample_rate;
266     int      current_frame_length            = frame_list[frame_number].length;
267     uint64_t resampled_sample_count          = 0;   /* the number of accumulated PCM samples after resampling per sequence */
268     uint64_t pcm_sample_count                = 0;   /* the number of accumulated PCM samples before resampling per sequence */
269     uint64_t prior_sequences_resampled_count = 0;   /* the number of accumulated PCM samples of all prior sequences */
270     do
271     {
272         current_frame_pos = next_frame_pos;
273         if( (current_sample_rate != frame_list[frame_number].sample_rate && frame_list[frame_number].sample_rate > 0)
274          || current_frame_length != frame_list[frame_number].length )
275         {
276             /* Encountered a new sequence. */
277             prior_sequences_resampled_count += resampled_sample_count;
278             pcm_sample_count = 0;
279             current_sample_rate  = frame_list[frame_number].sample_rate > 0 ? frame_list[frame_number].sample_rate : adhp->ctx->sample_rate;
280             current_frame_length = frame_list[frame_number].length;
281         }
282         pcm_sample_count += (uint64_t)current_frame_length;
283         resampled_sample_count = output_sample_rate == current_sample_rate || pcm_sample_count == 0
284                                ? pcm_sample_count
285                                : (pcm_sample_count * output_sample_rate - 1) / current_sample_rate + 1;
286         next_frame_pos = prior_sequences_resampled_count + resampled_sample_count;
287         if( start_frame_pos < next_frame_pos )
288             break;
289         ++frame_number;
290     } while( frame_number <= adhp->frame_count );
291     *start_offset = start_frame_pos - current_frame_pos;
292     if( *start_offset && current_sample_rate != output_sample_rate )
293         *start_offset = (*start_offset * current_sample_rate - 1) / output_sample_rate + 1;
294     if( frame_number > 1 )
295     {
296         /* Add pre-roll samples if needed.
297          * The condition is irresponsible. Patches welcome. */
298         enum AVCodecID codec_id = adhp->exh.entries[ frame_list[frame_number].extradata_index ].codec_id;
299         const AVCodecDescriptor *desc = avcodec_descriptor_get( codec_id );
300         if( (desc->props & AV_CODEC_PROP_LOSSY)
301          && frame_list[frame_number].extradata_index == frame_list[frame_number - 1].extradata_index )
302             *start_offset += (uint64_t)frame_list[--frame_number].length;
303     }
304     return frame_number;
305 }
306 
make_null_packet(AVPacket * pkt)307 static inline void make_null_packet
308 (
309     AVPacket *pkt
310 )
311 {
312     pkt->buf  = NULL;
313     pkt->data = NULL;
314     pkt->size = 0;
315 }
316 
make_decodable_packet(AVPacket * alt_pkt,AVPacket * pkt)317 static inline void make_decodable_packet
318 (
319     AVPacket *alt_pkt,
320     AVPacket *pkt
321 )
322 {
323     *alt_pkt = *pkt;
324 }
325 
get_audio_rap(lwlibav_audio_decode_handler_t * adhp,uint32_t frame_number)326 static uint32_t get_audio_rap
327 (
328     lwlibav_audio_decode_handler_t *adhp,
329     uint32_t                        frame_number
330 )
331 {
332     if( frame_number > adhp->frame_count )
333         return 0;
334     /* Get an unique value of the closest past audio keyframe. */
335     uint32_t rap_number = frame_number;
336     while( rap_number && !adhp->frame_list[rap_number].keyframe )
337         --rap_number;
338     if( rap_number == 0 )
339         rap_number = 1;
340     return rap_number;
341 }
342 
shift_current_frame_number_pos(audio_frame_info_t * info,AVPacket * pkt,uint32_t i,uint32_t goal)343 static uint32_t shift_current_frame_number_pos
344 (
345     audio_frame_info_t *info,
346     AVPacket           *pkt,
347     uint32_t            i,      /* frame_number */
348     uint32_t            goal
349 )
350 {
351     if( info[i].file_offset == pkt->pos )
352         return i;
353     if( pkt->pos > info[i].file_offset )
354     {
355         while( (pkt->pos != info[++i].file_offset) && i <= goal );
356         if( i > goal )
357             return 0;
358     }
359     else
360     {
361         while( (pkt->dts != info[--i].file_offset) && i );
362         if( i == 0 )
363             return 0;
364     }
365     return i;
366 }
367 
368 /* Note: for PTS based seek, there is no assumption that future prediction like B-picture is present. */
369 #define SHIFT_CURRENT_FRAME_NUMBER_TS( TS )                         \
370     static uint32_t shift_current_frame_number_##TS                 \
371     (                                                               \
372         audio_frame_info_t *info,                                   \
373         AVPacket           *pkt,                                    \
374         uint32_t            i,      /* frame_number */              \
375         uint32_t            goal                                    \
376     )                                                               \
377     {                                                               \
378         if( info[i].TS == AV_NOPTS_VALUE || info[i].TS == pkt->TS ) \
379             return i;                                               \
380         if( pkt->TS > info[i].TS )                                  \
381         {                                                           \
382             while( (pkt->TS != info[++i].TS) && i <= goal );        \
383             if( i > goal )                                          \
384                 return 0;                                           \
385         }                                                           \
386         else                                                        \
387         {                                                           \
388             while( (pkt->TS != info[--i].TS) && i );                \
389             if( i == 0 )                                            \
390                 return 0;                                           \
391         }                                                           \
392         return i;                                                   \
393     }
394 
395 SHIFT_CURRENT_FRAME_NUMBER_TS( pts )
SHIFT_CURRENT_FRAME_NUMBER_TS(dts)396 SHIFT_CURRENT_FRAME_NUMBER_TS( dts )
397 
398 #undef SHIFT_CURRENT_FRAME_NUMBER_TS
399 
400 static void no_output_audio_decoding
401 (
402     AVCodecContext *ctx,
403     AVPacket       *pkt,
404     AVFrame        *picture
405 )
406 {
407     do
408     {
409         int dummy;
410         int consumed_bytes = decode_audio_packet( ctx, picture, &dummy, pkt );
411         if( consumed_bytes < 0 )
412             return;
413         if( pkt->data )
414         {
415             pkt->size -= consumed_bytes;
416             pkt->data += consumed_bytes;
417         }
418     } while( pkt->size > 0 );
419 }
420 
421 /* This function seeks the requested frame and get it. */
seek_audio(lwlibav_audio_decode_handler_t * adhp,uint32_t frame_number,uint32_t past_rap_number,AVPacket * pkt,AVFrame * picture)422 static uint32_t seek_audio
423 (
424     lwlibav_audio_decode_handler_t *adhp,
425     uint32_t                        frame_number,
426     uint32_t                        past_rap_number,
427     AVPacket                       *pkt,
428     AVFrame                        *picture
429 )
430 {
431 #define MAX_ERROR_COUNT 3   /* arbitrary */
432     int error_count = 0;
433 retry_seek:;
434     uint32_t rap_number = past_rap_number == 0 ? get_audio_rap( adhp, frame_number ) : past_rap_number;
435     if( rap_number == 0 )
436         return 0;
437     int64_t rap_pos = (adhp->lw_seek_flags & SEEK_POS_BASED) ? adhp->frame_list[rap_number].file_offset
438                     : (adhp->lw_seek_flags & SEEK_PTS_BASED) ? adhp->frame_list[rap_number].pts
439                     : (adhp->lw_seek_flags & SEEK_DTS_BASED) ? adhp->frame_list[rap_number].dts
440                     :                                          adhp->frame_list[rap_number].sample_number;
441     /* Seek to audio keyframe.
442      * Note: av_seek_frame() for DV in AVI Type-1 requires stream_index = 0. */
443     int flags = (adhp->lw_seek_flags & SEEK_POS_BASED) ? AVSEEK_FLAG_BYTE : adhp->lw_seek_flags == 0 ? AVSEEK_FLAG_FRAME : 0;
444     int stream_index = adhp->dv_in_avi == 1 ? 0 : adhp->stream_index;
445     if( av_seek_frame( adhp->format, stream_index, rap_pos, flags | AVSEEK_FLAG_BACKWARD ) < 0 )
446         av_seek_frame( adhp->format, stream_index, rap_pos, flags | AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY );
447     /* Seek to the target audio frame and get it. */
448     int match = 0;
449     for( uint32_t i = rap_number; i <= frame_number; )
450     {
451         if( match && picture && adhp->exh.current_index == adhp->frame_list[i - 1].extradata_index )
452         {
453             /* Actual decoding to establish stability of subsequent decoding. */
454             AVPacket *alter_pkt = &adhp->alter_packet;
455             make_decodable_packet( alter_pkt, pkt );
456             no_output_audio_decoding( adhp->ctx, alter_pkt, picture );
457         }
458         if( lwlibav_get_av_frame( adhp->format, adhp->stream_index, i, pkt ) )
459             break;
460         if( !match && error_count <= MAX_ERROR_COUNT )
461         {
462             /* Shift the current frame number in order to match file offset, PTS or DTS
463              * since libavformat might have sought wrong position. */
464             if( adhp->lw_seek_flags & SEEK_POS_BASED )
465             {
466                 if( pkt->pos == -1 || adhp->frame_list[i].file_offset == -1 )
467                     continue;
468                 i = shift_current_frame_number_pos( adhp->frame_list, pkt, i, frame_number );
469             }
470             else if( adhp->lw_seek_flags & SEEK_PTS_BASED )
471             {
472                 if( pkt->pts == AV_NOPTS_VALUE )
473                     continue;
474                 i = shift_current_frame_number_pts( adhp->frame_list, pkt, i, frame_number );
475             }
476             else if( adhp->lw_seek_flags & SEEK_DTS_BASED )
477             {
478                 if( pkt->dts == AV_NOPTS_VALUE )
479                     continue;
480                 i = shift_current_frame_number_dts( adhp->frame_list, pkt, i, frame_number );
481             }
482             if( i == 0 )
483             {
484                 /* Retry to seek from more past audio keyframe. */
485                 past_rap_number = get_audio_rap( adhp, rap_number - 1 );
486                 ++error_count;
487                 goto retry_seek;
488             }
489             match = 1;
490         }
491         ++i;
492     }
493     return rap_number;
494 #undef MAX_ERROR_COUNT
495 }
496 
lwlibav_audio_get_pcm_samples(lwlibav_audio_decode_handler_t * adhp,lwlibav_audio_output_handler_t * aohp,void * buf,int64_t start,int64_t wanted_length)497 uint64_t lwlibav_audio_get_pcm_samples
498 (
499     lwlibav_audio_decode_handler_t *adhp,
500     lwlibav_audio_output_handler_t *aohp,
501     void                           *buf,
502     int64_t                         start,
503     int64_t                         wanted_length
504 )
505 {
506     if( adhp->error )
507         return 0;
508     uint32_t               frame_number;
509     uint32_t               rap_number      = 0;
510     uint32_t               past_rap_number = 0;
511     uint64_t               output_length   = 0;
512     enum audio_output_flag output_flags    = AUDIO_OUTPUT_NO_FLAGS;
513     AVPacket              *pkt       = &adhp->packet;
514     AVPacket              *alter_pkt = &adhp->alter_packet;
515     int                    already_gotten;
516     aohp->request_length = wanted_length;
517     if( start > 0 && start == adhp->next_pcm_sample_number )
518     {
519         frame_number   = adhp->last_frame_number;
520         output_length += output_pcm_samples_from_buffer( aohp, adhp->frame_buffer, (uint8_t **)&buf, &output_flags );
521         if( output_flags & AUDIO_OUTPUT_ENOUGH )
522             goto audio_out;
523         if( alter_pkt->size <= 0 )
524             ++frame_number;
525         aohp->output_sample_offset = 0;
526         already_gotten             = 0;
527     }
528     else
529     {
530         /* Seek audio stream. */
531         adhp->next_pcm_sample_number = 0;
532         adhp->last_frame_number      = 0;
533         /* Get frame_number. */
534         uint64_t start_frame_pos;
535         if( start >= 0 )
536             start_frame_pos = start;
537         else
538         {
539             uint64_t silence_length = -start;
540             put_silence_audio_samples( (int)(silence_length * aohp->output_block_align), aohp->output_bits_per_sample == 8, (uint8_t **)&buf );
541             output_length        += silence_length;
542             aohp->request_length -= silence_length;
543             start_frame_pos = 0;
544         }
545         frame_number = find_start_audio_frame( adhp, aohp->output_sample_rate, start_frame_pos, &aohp->output_sample_offset );
546 retry_seek:
547         av_packet_unref( pkt );
548         /* Flush audio resampler buffers. */
549         if( flush_resampler_buffers( aohp->avr_ctx ) < 0 )
550         {
551             adhp->error = 1;
552             lw_log_show( &adhp->lh, LW_LOG_FATAL,
553                          "Failed to flush resampler buffers.\n"
554                          "It is recommended you reopen the file." );
555             return 0;
556         }
557         /* Flush audio decoder buffers. */
558         lwlibav_extradata_handler_t *exhp = &adhp->exh;
559         int extradata_index = adhp->frame_list[frame_number].extradata_index;
560         if( extradata_index != exhp->current_index )
561         {
562             /* Update the extradata. */
563             rap_number = get_audio_rap( adhp, frame_number );
564             assert( rap_number != 0 );
565             lwlibav_update_configuration( (lwlibav_decode_handler_t *)adhp, rap_number, extradata_index, 0 );
566         }
567         else
568             lwlibav_flush_buffers( (lwlibav_decode_handler_t *)adhp );
569         if( adhp->error )
570             return 0;
571         /* Seek and get a audio packet. */
572         rap_number = seek_audio( adhp, frame_number, past_rap_number, pkt, output_flags != AUDIO_OUTPUT_NO_FLAGS ? adhp->frame_buffer : NULL );
573         already_gotten = 1;
574     }
575     do
576     {
577         if( already_gotten )
578         {
579             already_gotten = 0;
580             make_decodable_packet( alter_pkt, pkt );
581         }
582         else if( frame_number > adhp->frame_count )
583         {
584             av_packet_unref( pkt );
585             if( adhp->exh.delay_count || !(output_flags & AUDIO_OUTPUT_ENOUGH) )
586             {
587                 /* Null packet */
588                 av_init_packet( pkt );
589                 make_null_packet( pkt );
590                 *alter_pkt = *pkt;
591                 if( adhp->exh.delay_count )
592                     adhp->exh.delay_count -= 1;
593             }
594             else
595                 goto audio_out;
596         }
597         else if( alter_pkt->size <= 0 )
598         {
599             /* Getting an audio packet must be after flushing all remaining samples in resampler's FIFO buffer. */
600             lwlibav_get_av_frame( adhp->format, adhp->stream_index, frame_number, pkt );
601             make_decodable_packet( alter_pkt, pkt );
602         }
603         /* Decode and output from an audio packet. */
604         output_flags   = AUDIO_OUTPUT_NO_FLAGS;
605         output_length += output_pcm_samples_from_packet( aohp, adhp->ctx, alter_pkt, adhp->frame_buffer, (uint8_t **)&buf, &output_flags );
606         if( output_flags & AUDIO_DECODER_DELAY )
607         {
608             if( rap_number > 1 && (output_flags & AUDIO_DECODER_ERROR) )
609             {
610                 /* Retry to seek from more past audio keyframe because libavformat might have failed seek.
611                  * This operation occurs only at the first decoding time after seek. */
612                 past_rap_number = get_audio_rap( adhp, rap_number - 1 );
613                 if( past_rap_number
614                  && past_rap_number < rap_number )
615                     goto retry_seek;
616             }
617             ++ adhp->exh.delay_count;
618         }
619         else
620             /* Disable seek retry. */
621             rap_number = 0;
622         if( output_flags & AUDIO_RECONFIG_FAILURE )
623         {
624             adhp->error = 1;
625             lw_log_show( &adhp->lh, LW_LOG_FATAL,
626                          "Failed to reconfigure resampler.\n"
627                          "It is recommended you reopen the file." );
628             goto audio_out;
629         }
630         if( output_flags & AUDIO_OUTPUT_ENOUGH )
631             goto audio_out;
632         if( output_flags & (AUDIO_DECODER_ERROR | AUDIO_DECODER_RECEIVED_PACKET) )
633             ++frame_number;
634     } while( 1 );
635 audio_out:
636     adhp->next_pcm_sample_number = start + output_length;
637     adhp->last_frame_number      = frame_number;
638     return output_length;
639 }
640 
set_audio_basic_settings(lwlibav_decode_handler_t * dhp,const AVCodec * codec,uint32_t frame_number)641 void set_audio_basic_settings
642 (
643     lwlibav_decode_handler_t *dhp,
644     const AVCodec            *codec,
645     uint32_t                  frame_number
646 )
647 {
648     lwlibav_audio_decode_handler_t *adhp = (lwlibav_audio_decode_handler_t *)dhp;
649     AVCodecParameters   *codecpar = adhp->format->streams[ adhp->stream_index ]->codecpar;
650     lwlibav_extradata_t *entry    = &adhp->exh.entries[ adhp->frame_list[frame_number].extradata_index ];
651     codecpar->sample_rate           = entry->sample_rate;
652     codecpar->channel_layout        = entry->channel_layout;
653     codecpar->format                = (int)entry->sample_format;
654     codecpar->bits_per_coded_sample = entry->bits_per_sample;
655     codecpar->block_align           = entry->block_align;
656     codecpar->channels              = av_get_channel_layout_nb_channels( codecpar->channel_layout );
657 }
658 
try_decode_audio_frame(lwlibav_decode_handler_t * dhp,uint32_t frame_number,char * error_string)659 int try_decode_audio_frame
660 (
661     lwlibav_decode_handler_t *dhp,
662     uint32_t                  frame_number,
663     char                     *error_string
664 )
665 {
666     AVFrame *picture = av_frame_alloc();
667     if( !picture )
668     {
669         strcpy( error_string, "Failed to alloc AVFrame to set up a decoder configuration.\n" );
670         return -1;
671     }
672     lwlibav_audio_decode_handler_t *adhp = (lwlibav_audio_decode_handler_t *)dhp;
673     AVFormatContext *format_ctx   = adhp->format;
674     int              stream_index = adhp->stream_index;
675     AVPacket        *pkt          = &adhp->packet;
676     AVPacket        *alter_pkt    = &adhp->alter_packet;
677     AVCodecContext  *ctx          = adhp->ctx;
678     uint32_t         start_frame  = frame_number;
679     int              err          = 0;
680     do
681     {
682         if( frame_number > adhp->frame_count )
683             break;
684         /* Get a frame. */
685         int extradata_index = adhp->frame_list[frame_number].extradata_index;
686         if( extradata_index != adhp->exh.current_index )
687             break;
688         if( frame_number == start_frame )
689             seek_audio( adhp, frame_number, 0, pkt, NULL );
690         else
691         {
692             int ret = lwlibav_get_av_frame( format_ctx, stream_index, frame_number, pkt );
693             if( ret > 0 )
694                 break;
695             else if( ret < 0 )
696             {
697                 if( ctx->sample_rate == 0 )
698                     strcpy( error_string, "Failed to set up sample rate.\n" );
699                 else if( ctx->channel_layout == 0 && ctx->channels == 0 )
700                     strcpy( error_string, "Failed to set up channels.\n" );
701                 else
702                     strcpy( error_string, "Failed to set up sample format.\n" );
703                 err = -1;
704                 goto abort;
705             }
706         }
707         make_decodable_packet( alter_pkt, pkt );
708         /* Try decode a frame. */
709         no_output_audio_decoding( ctx, alter_pkt, picture );
710         ++frame_number;
711     } while( ctx->sample_rate == 0
712           || ctx->sample_fmt == AV_SAMPLE_FMT_NONE
713           || (ctx->channels == 0 && ctx->channel_layout == 0)
714           || (ctx->channels != av_get_channel_layout_nb_channels( ctx->channel_layout )) );
715 abort:
716     av_frame_free( &picture );
717     return err;
718 }
719