1 /*****************************************************************************
2  * libavsmash.c / libavsmash.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 #include <stdlib.h>
26 #include <string.h>
27 
28 #ifdef __cplusplus
29 extern "C"
30 {
31 #endif  /* __cplusplus */
32 #include <lsmash.h>
33 #include <libavformat/avformat.h>
34 #include <libavcodec/avcodec.h>
35 #include <libavutil/channel_layout.h>
36 #include <libavutil/mem.h>
37 #ifdef __cplusplus
38 }
39 #endif  /* __cplusplus */
40 
41 #include "utils.h"
42 #include "libavsmash.h"
43 #include "qsv.h"
44 #include "decode.h"
45 
46 #define BYTE_SWAP_16( x ) ((( x ) << 8 & 0xff00)  | (( x ) >> 8 & 0x00ff))
47 #define BYTE_SWAP_32( x ) (BYTE_SWAP_16( x ) << 16 | BYTE_SWAP_16(( x ) >> 16))
48 
libavsmash_open_file(AVFormatContext ** p_format_ctx,const char * file_name,lsmash_file_parameters_t * file_param,lsmash_movie_parameters_t * movie_param,lw_log_handler_t * lhp)49 lsmash_root_t *libavsmash_open_file
50 (
51     AVFormatContext          **p_format_ctx,
52     const char                *file_name,
53     lsmash_file_parameters_t  *file_param,
54     lsmash_movie_parameters_t *movie_param,
55     lw_log_handler_t          *lhp
56 )
57 {
58     /* L-SMASH */
59     lsmash_root_t *root = lsmash_create_root();
60     if( !root )
61         return NULL;
62     char error_string[96] = { 0 };
63     if( lsmash_open_file( file_name, 1, file_param ) < 0 )
64     {
65         strcpy( error_string, "Failed to open an input file.\n" );
66         goto open_fail;
67     }
68     lsmash_file_t *fh = lsmash_set_file( root, file_param );
69     if( !fh )
70     {
71         strcpy( error_string, "Failed to add an input file into a ROOT.\n" );
72         goto open_fail;
73     }
74     if( lsmash_read_file( fh, file_param ) < 0 )
75     {
76         strcpy( error_string, "Failed to read an input file\n" );
77         goto open_fail;
78     }
79     lsmash_initialize_movie_parameters( movie_param );
80     lsmash_get_movie_parameters( root, movie_param );
81     if( movie_param->number_of_tracks == 0 )
82     {
83         strcpy( error_string, "The number of tracks equals 0.\n" );
84         goto open_fail;
85     }
86     /* libavformat */
87     av_register_all();
88     avcodec_register_all();
89     if( avformat_open_input( p_format_ctx, file_name, NULL, NULL ) )
90     {
91         strcpy( error_string, "Failed to avformat_open_input.\n" );
92         goto open_fail;
93     }
94     if( avformat_find_stream_info( *p_format_ctx, NULL ) < 0 )
95     {
96         strcpy( error_string, "Failed to avformat_find_stream_info.\n" );
97         goto open_fail;
98     }
99     return root;
100 open_fail:
101     if( *p_format_ctx )
102         avformat_close_input( p_format_ctx );
103     lsmash_close_file( file_param );
104     lsmash_destroy_root( root );
105     lw_log_show( lhp, LW_LOG_FATAL, "%s", error_string );
106     return NULL;
107 }
108 
libavsmash_get_track_by_media_type(lsmash_root_t * root,uint32_t type,uint32_t track_number,lw_log_handler_t * lhp)109 uint32_t libavsmash_get_track_by_media_type
110 (
111     lsmash_root_t    *root,
112     uint32_t          type,
113     uint32_t          track_number,
114     lw_log_handler_t *lhp
115 )
116 {
117     char error_string[128] = { 0 };
118     char *media_type_str = type == ISOM_MEDIA_HANDLER_TYPE_VIDEO_TRACK ? "video" : "audio";
119     uint32_t track_id;
120     lsmash_media_parameters_t media_param;
121     if( track_number == 0 )
122     {
123         /* Get the first track. */
124         lsmash_movie_parameters_t movie_param;
125         if( lsmash_get_movie_parameters( root, &movie_param ) < 0 )
126         {
127             strcpy( error_string, "Failed to get movie paramters.\n" );
128             goto fail;
129         }
130         uint32_t i;
131         for( i = 1; i <= movie_param.number_of_tracks; i++ )
132         {
133             track_id = lsmash_get_track_ID( root, i );
134             if( track_id == 0 )
135             {
136                 sprintf( error_string, "Failed to find %s track.\n", media_type_str );
137                 goto fail;
138             }
139             lsmash_initialize_media_parameters( &media_param );
140             if( lsmash_get_media_parameters( root, track_id, &media_param ) < 0 )
141             {
142                 strcpy( error_string, "Failed to get media parameters.\n" );
143                 goto fail;
144             }
145             if( media_param.handler_type == type )
146                 break;
147         }
148         if( i > movie_param.number_of_tracks )
149         {
150             sprintf( error_string, "Failed to find the first %s track.\n", media_type_str );
151             goto fail;
152         }
153     }
154     else
155     {
156         /* Get the desired track. */
157         track_id = lsmash_get_track_ID( root, track_number );
158         if( track_id == 0 )
159         {
160             sprintf( error_string, "Failed to find %s track %" PRIu32 ".\n", media_type_str, track_number );
161             goto fail;
162         }
163         lsmash_initialize_media_parameters( &media_param );
164         if( lsmash_get_media_parameters( root, track_id, &media_param ) < 0 )
165         {
166             strcpy( error_string, "Failed to get media parameters.\n" );
167             goto fail;
168         }
169         if( media_param.handler_type != type )
170         {
171             sprintf( error_string, "the track you specified is not %s track.\n", media_type_str );
172             goto fail;
173         }
174     }
175     if( lsmash_construct_timeline( root, track_id ) < 0 )
176     {
177         sprintf( error_string, "Failed to get construct timeline of %s track.\n", media_type_str );
178         goto fail;
179     }
180     return track_id;
181 fail:
182     lw_log_show( lhp, LW_LOG_FATAL, "%s", error_string );
183     return 0;
184 }
185 
get_summaries(lsmash_root_t * root,uint32_t track_ID,codec_configuration_t * config)186 int get_summaries
187 (
188     lsmash_root_t         *root,
189     uint32_t               track_ID,
190     codec_configuration_t *config
191 )
192 {
193     char error_string[96] = { 0 };
194     uint32_t summary_count = lsmash_count_summary( root, track_ID );
195     if( summary_count == 0 )
196     {
197         strcpy( error_string, "Failed to find valid summaries.\n" );
198         goto fail;
199     }
200     libavsmash_summary_t *summaries = (libavsmash_summary_t *)lw_malloc_zero( summary_count * sizeof(libavsmash_summary_t) );
201     if( !summaries )
202     {
203         strcpy( error_string, "Failed to alloc input summaries.\n" );
204         goto fail;
205     }
206     for( uint32_t i = 0; i < summary_count; i++ )
207     {
208         lsmash_summary_t *summary = lsmash_get_summary( root, track_ID, i + 1 );
209         if( !summary )
210             continue;
211         summaries[i].summary = summary;
212     }
213     config->entries = summaries;
214     config->count   = summary_count;
215     return 0;
216 fail:
217     config->error = 1;
218     lw_log_show( &config->lh, LW_LOG_FATAL, "%s", error_string );
219     return -1;
220 }
221 
get_codec_id_from_description(lsmash_summary_t * summary)222 static enum AVCodecID get_codec_id_from_description
223 (
224     lsmash_summary_t *summary
225 )
226 {
227     lsmash_codec_type_t sample_type = summary->sample_type;
228 #define ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( codec_id, codec_type )        \
229     else if( lsmash_check_codec_type_identical( sample_type, codec_type ) ) \
230         return codec_id
231     if( summary->summary_type == LSMASH_SUMMARY_TYPE_VIDEO )
232     {
233         if( lsmash_check_codec_type_identical( sample_type, QT_CODEC_TYPE_RAW_VIDEO ) )
234         {
235             lsmash_video_summary_t *video = (lsmash_video_summary_t *)summary;
236             if( video->depth == QT_VIDEO_DEPTH_24RGB
237              || video->depth == QT_VIDEO_DEPTH_32ARGB
238              || video->depth == QT_VIDEO_DEPTH_GRAYSCALE_1 )
239                 return AV_CODEC_ID_RAWVIDEO;
240             return AV_CODEC_ID_NONE;
241         }
242         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_H264,     ISOM_CODEC_TYPE_AVC1_VIDEO );
243         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_H264,     ISOM_CODEC_TYPE_AVC3_VIDEO );
244         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_HEVC,     ISOM_CODEC_TYPE_HVC1_VIDEO );
245         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_HEVC,     ISOM_CODEC_TYPE_HEV1_VIDEO );
246         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_VC1,      ISOM_CODEC_TYPE_VC_1_VIDEO );
247         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_DIRAC,    ISOM_CODEC_TYPE_DRAC_VIDEO );
248         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_PRORES,     QT_CODEC_TYPE_APCH_VIDEO );
249         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_PRORES,     QT_CODEC_TYPE_APCN_VIDEO );
250         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_PRORES,     QT_CODEC_TYPE_APCS_VIDEO );
251         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_PRORES,     QT_CODEC_TYPE_APCO_VIDEO );
252         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_PRORES,     QT_CODEC_TYPE_AP4H_VIDEO );
253         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_PRORES,     QT_CODEC_TYPE_AP4X_VIDEO );
254         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_RAWVIDEO,   QT_CODEC_TYPE_DVOO_VIDEO );
255         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_DVVIDEO,    QT_CODEC_TYPE_DVC_VIDEO );
256         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_DVVIDEO,    QT_CODEC_TYPE_DVCP_VIDEO );
257         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_DVVIDEO,    QT_CODEC_TYPE_DVPP_VIDEO );
258         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_DVVIDEO,    QT_CODEC_TYPE_DV5N_VIDEO );
259         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_DVVIDEO,    QT_CODEC_TYPE_DV5P_VIDEO );
260         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_DVVIDEO,    QT_CODEC_TYPE_DVH2_VIDEO );
261         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_DVVIDEO,    QT_CODEC_TYPE_DVH3_VIDEO );
262         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_DVVIDEO,    QT_CODEC_TYPE_DVH5_VIDEO );
263         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_DVVIDEO,    QT_CODEC_TYPE_DVH6_VIDEO );
264         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_DVVIDEO,    QT_CODEC_TYPE_DVHP_VIDEO );
265         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_DVVIDEO,    QT_CODEC_TYPE_DVHQ_VIDEO );
266         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_FLIC,       QT_CODEC_TYPE_FLIC_VIDEO );
267         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_H261,       QT_CODEC_TYPE_H261_VIDEO );
268         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_H263,       QT_CODEC_TYPE_H263_VIDEO );
269         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_MJPEG,      QT_CODEC_TYPE_JPEG_VIDEO );
270         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_MJPEG,      QT_CODEC_TYPE_MJPA_VIDEO );
271         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_MJPEGB,     QT_CODEC_TYPE_MJPB_VIDEO );
272         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_PNG,        QT_CODEC_TYPE_PNG_VIDEO );
273         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_QTRLE,      QT_CODEC_TYPE_RLE_VIDEO );
274         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_RPZA,       QT_CODEC_TYPE_RPZA_VIDEO );
275         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_TARGA,      QT_CODEC_TYPE_TGA_VIDEO );
276         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_TIFF,       QT_CODEC_TYPE_TIFF_VIDEO );
277         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_UTVIDEO,    QT_CODEC_TYPE_ULRA_VIDEO );
278         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_UTVIDEO,    QT_CODEC_TYPE_ULRG_VIDEO );
279         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_UTVIDEO,    QT_CODEC_TYPE_ULY0_VIDEO );
280         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_UTVIDEO,    QT_CODEC_TYPE_ULY2_VIDEO );
281         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_UTVIDEO,    QT_CODEC_TYPE_ULH0_VIDEO );
282         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_UTVIDEO,    QT_CODEC_TYPE_ULH2_VIDEO );
283         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_UTVIDEO,    QT_CODEC_TYPE_UQY2_VIDEO );
284         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_V210,       QT_CODEC_TYPE_V210_VIDEO );
285         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_V410,       QT_CODEC_TYPE_V410_VIDEO );
286         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_RAWVIDEO,   QT_CODEC_TYPE_2VUY_VIDEO );
287         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_RAWVIDEO,   QT_CODEC_TYPE_YUV2_VIDEO );
288     }
289     else if( summary->summary_type == LSMASH_SUMMARY_TYPE_AUDIO )
290     {
291         if( lsmash_check_codec_type_identical( sample_type, ISOM_CODEC_TYPE_MP4A_AUDIO )
292          || lsmash_check_codec_type_identical( sample_type,   QT_CODEC_TYPE_MP4A_AUDIO ) )
293         {
294             uint32_t cs_count = lsmash_count_codec_specific_data( summary );
295             lsmash_codec_specific_t *orig_cs = NULL;
296             lsmash_codec_specific_t *cs      = NULL;
297             for( uint32_t i = 1; i <= cs_count; i++ )
298             {
299                 orig_cs = lsmash_get_codec_specific_data( summary, i );
300                 if( !orig_cs || orig_cs->type != LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG )
301                     continue;
302                 cs = orig_cs->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED
303                    ? orig_cs
304                    : lsmash_convert_codec_specific_format( orig_cs, LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED );
305                 break;
306             }
307             if( !cs )
308                 return AV_CODEC_ID_NONE;
309             lsmash_mp4sys_decoder_parameters_t *mdp = (lsmash_mp4sys_decoder_parameters_t *)cs->data.structured;
310             enum AVCodecID codec_id;
311             switch( mdp->objectTypeIndication )
312             {
313                 case MP4SYS_OBJECT_TYPE_Visual_ISO_14496_2 :
314                     codec_id = AV_CODEC_ID_MPEG4;
315                     break;
316                 case MP4SYS_OBJECT_TYPE_Audio_ISO_14496_3 :
317                     switch( ((lsmash_audio_summary_t *)summary)->aot )
318                     {
319                         case MP4A_AUDIO_OBJECT_TYPE_AAC_MAIN :
320                         case MP4A_AUDIO_OBJECT_TYPE_AAC_LC :
321                         case MP4A_AUDIO_OBJECT_TYPE_AAC_LTP :
322                             codec_id = AV_CODEC_ID_AAC;
323                             break;
324                         case MP4A_AUDIO_OBJECT_TYPE_ALS :
325                             codec_id = AV_CODEC_ID_MP4ALS;
326                             break;
327                         default :
328                             codec_id = AV_CODEC_ID_NONE;
329                             break;
330                     }
331                     break;
332                 case MP4SYS_OBJECT_TYPE_Audio_ISO_13818_7_Main_Profile :
333                 case MP4SYS_OBJECT_TYPE_Audio_ISO_13818_7_LC_Profile :
334                 case MP4SYS_OBJECT_TYPE_Audio_ISO_13818_7_SSR_Profile :
335                     codec_id = AV_CODEC_ID_AAC;
336                     break;
337                 case MP4SYS_OBJECT_TYPE_Audio_ISO_13818_3 :
338                 case MP4SYS_OBJECT_TYPE_Audio_ISO_11172_3 :
339                     /* Return temporal CODEC ID since we can't confirm what CODEC is used here. */
340                     codec_id = AV_CODEC_ID_MP3;
341                     break;
342                 case MP4SYS_OBJECT_TYPE_PNG :
343                     codec_id = AV_CODEC_ID_PNG;
344                     break;
345                 default :
346                     codec_id = AV_CODEC_ID_NONE;
347                     break;
348             }
349             if( orig_cs != cs )
350                 lsmash_destroy_codec_specific_data( cs );
351             return codec_id;
352         }
353         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_AC3,         ISOM_CODEC_TYPE_AC_3_AUDIO );
354         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_EAC3,        ISOM_CODEC_TYPE_EC_3_AUDIO );
355         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_DTS,         ISOM_CODEC_TYPE_DTSC_AUDIO );
356         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_DTS,         ISOM_CODEC_TYPE_DTSH_AUDIO );
357         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_DTS,         ISOM_CODEC_TYPE_DTSL_AUDIO );
358         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_AMR_NB,      ISOM_CODEC_TYPE_SAMR_AUDIO );
359         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_AMR_WB,      ISOM_CODEC_TYPE_SAWB_AUDIO );
360         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_ALAC,        ISOM_CODEC_TYPE_ALAC_AUDIO );
361         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_ALAC,          QT_CODEC_TYPE_ALAC_AUDIO );
362         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_MACE3,         QT_CODEC_TYPE_MAC3_AUDIO );
363         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_MACE6,         QT_CODEC_TYPE_MAC6_AUDIO );
364         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_QCELP,         QT_CODEC_TYPE_QCLP_AUDIO );
365         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_GSM,           QT_CODEC_TYPE_AGSM_AUDIO );
366         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_PCM_ALAW,      QT_CODEC_TYPE_ALAW_AUDIO );
367         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_ADPCM_IMA_QT,  QT_CODEC_TYPE_IMA4_AUDIO );
368         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_PCM_MULAW,     QT_CODEC_TYPE_ULAW_AUDIO );
369         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_MP3,           QT_CODEC_TYPE_FULLMP3_AUDIO );
370         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_ADPCM_MS,      QT_CODEC_TYPE_ADPCM2_AUDIO );
371         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_ADPCM_IMA_WAV, QT_CODEC_TYPE_ADPCM17_AUDIO );
372         ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE( AV_CODEC_ID_GSM_MS,        QT_CODEC_TYPE_GSM49_AUDIO );
373         else if( lsmash_check_codec_type_identical( sample_type, QT_CODEC_TYPE_LPCM_AUDIO )
374               || lsmash_check_codec_type_identical( sample_type, QT_CODEC_TYPE_FL32_AUDIO )
375               || lsmash_check_codec_type_identical( sample_type, QT_CODEC_TYPE_FL64_AUDIO )
376               || lsmash_check_codec_type_identical( sample_type, QT_CODEC_TYPE_23NI_AUDIO )
377               || lsmash_check_codec_type_identical( sample_type, QT_CODEC_TYPE_IN24_AUDIO )
378               || lsmash_check_codec_type_identical( sample_type, QT_CODEC_TYPE_IN32_AUDIO )
379               || lsmash_check_codec_type_identical( sample_type, QT_CODEC_TYPE_SOWT_AUDIO )
380               || lsmash_check_codec_type_identical( sample_type, QT_CODEC_TYPE_TWOS_AUDIO )
381               || lsmash_check_codec_type_identical( sample_type, QT_CODEC_TYPE_RAW_AUDIO )
382               || lsmash_check_codec_type_identical( sample_type, QT_CODEC_TYPE_NONE_AUDIO )
383               || lsmash_check_codec_type_identical( sample_type, QT_CODEC_TYPE_NOT_SPECIFIED ) )
384         {
385             uint32_t cs_count = lsmash_count_codec_specific_data( summary );
386             lsmash_codec_specific_t *cs = NULL;
387             for( uint32_t i = 1; i <= cs_count; i++ )
388             {
389                 cs = lsmash_get_codec_specific_data( summary, i );
390                 if( cs
391                  && cs->type == LSMASH_CODEC_SPECIFIC_DATA_TYPE_QT_AUDIO_FORMAT_SPECIFIC_FLAGS
392                  && cs->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED )
393                     break;
394             }
395             if( !cs )
396                 return AV_CODEC_ID_NONE;
397             lsmash_audio_summary_t *audio = (lsmash_audio_summary_t *)summary;
398             lsmash_qt_audio_format_specific_flags_t *data = (lsmash_qt_audio_format_specific_flags_t *)cs->data.structured;
399             int is_int    = !(data->format_flags & QT_LPCM_FORMAT_FLAG_FLOAT);
400             int is_signed = !!(data->format_flags & QT_LPCM_FORMAT_FLAG_SIGNED_INTEGER);
401             int is_be     = !!(data->format_flags & QT_LPCM_FORMAT_FLAG_BIG_ENDIAN);
402             switch( audio->sample_size )
403             {
404                 case  8 :
405                     return is_signed ? AV_CODEC_ID_PCM_S8 : AV_CODEC_ID_PCM_U8;
406                 case 16 :
407                     if( is_signed )
408                         return is_be ? AV_CODEC_ID_PCM_S16BE : AV_CODEC_ID_PCM_S16LE;
409                     else
410                         return is_be ? AV_CODEC_ID_PCM_U16BE : AV_CODEC_ID_PCM_U16LE;
411                 case 24 :
412                     if( is_signed )
413                         return is_be ? AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
414                     else
415                         return is_be ? AV_CODEC_ID_PCM_U24BE : AV_CODEC_ID_PCM_U24LE;
416                 case 32 :
417                     if( is_int )
418                     {
419                         if( is_signed )
420                             return is_be ? AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
421                         else
422                             return is_be ? AV_CODEC_ID_PCM_U32BE : AV_CODEC_ID_PCM_U32LE;
423                     }
424                     else
425                         return is_be ? AV_CODEC_ID_PCM_F32BE : AV_CODEC_ID_PCM_F32LE;
426                 case 64 :
427                     if( is_int )
428                         return AV_CODEC_ID_NONE;
429                     else
430                         return is_be ? AV_CODEC_ID_PCM_F64BE : AV_CODEC_ID_PCM_F64LE;
431                 default :
432                     return AV_CODEC_ID_NONE;
433             }
434         }
435     }
436     return AV_CODEC_ID_NONE;
437 #undef ELSE_IF_GET_CODEC_ID_FROM_CODEC_TYPE
438 }
439 
libavsmash_find_decoder(codec_configuration_t * config,enum AVCodecID codec_id)440 static const AVCodec *libavsmash_find_decoder
441 (
442     codec_configuration_t *config,
443     enum AVCodecID         codec_id
444 )
445 {
446     if( codec_id == AV_CODEC_ID_NONE )
447         /* Try to get any valid codec_id from summaries. */
448         for( uint32_t i = 0; i < config->count && codec_id == AV_CODEC_ID_NONE; i++ )
449             codec_id = get_codec_id_from_description( config->entries[i].summary );
450     return find_decoder( codec_id, config->preferred_decoder_names );
451 }
452 
libavsmash_find_and_open_decoder(codec_configuration_t * config,const AVCodecParameters * codecpar,const int thread_count,const int refcounted_frames)453 int libavsmash_find_and_open_decoder
454 (
455     codec_configuration_t   *config,
456     const AVCodecParameters *codecpar,
457     const int                thread_count,
458     const int                refcounted_frames
459 )
460 {
461     const AVCodec *codec = libavsmash_find_decoder( config, codecpar->codec_id );
462     if( !codec )
463         return -1;
464     return open_decoder( &config->ctx, codecpar, codec, thread_count, refcounted_frames );
465 }
466 
get_codec_specific_data_type(lsmash_codec_type_t codec_type,lsmash_codec_specific_format * format1,lsmash_codec_specific_format * format2)467 static lsmash_codec_specific_data_type get_codec_specific_data_type
468 (
469     lsmash_codec_type_t           codec_type,
470     lsmash_codec_specific_format *format1,
471     lsmash_codec_specific_format *format2
472 )
473 {
474     *format1 = LSMASH_CODEC_SPECIFIC_FORMAT_UNSPECIFIED;
475     *format2 = LSMASH_CODEC_SPECIFIC_FORMAT_UNSPECIFIED;
476     if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_AVC1_VIDEO )
477      || lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_AVC3_VIDEO ) )
478     {
479         *format1 = LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED;
480         return LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264;
481     }
482     else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_HVC1_VIDEO )
483           || lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_HEV1_VIDEO ) )
484     {
485         *format1 = LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED;
486         return LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_HEVC;
487     }
488     else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_MP4A_AUDIO )
489           || lsmash_check_codec_type_identical( codec_type,   QT_CODEC_TYPE_MP4A_AUDIO )
490           || lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_MP4V_VIDEO ) )
491     {
492         *format1 = LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED;
493         return LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG;
494     }
495     else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_VC_1_VIDEO ) )
496     {
497         *format1 = LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED;
498         return LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_VC_1;
499     }
500     else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_AC_3_AUDIO ) )
501     {
502         *format2 = LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED;
503         return LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_AC_3;
504     }
505     else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_ALAC_AUDIO )
506           || lsmash_check_codec_type_identical( codec_type,   QT_CODEC_TYPE_ALAC_AUDIO ) )
507     {
508         *format1 = LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED;
509         *format2 = LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED;
510         return LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_ALAC;
511     }
512     else if( lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_DTSC_AUDIO )
513           || lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_DTSH_AUDIO )
514           || lsmash_check_codec_type_identical( codec_type, ISOM_CODEC_TYPE_DTSL_AUDIO ) )
515     {
516         *format2 = LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED;
517         return LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_DTS;
518     }
519     else if( lsmash_check_codec_type_identical( codec_type, QT_CODEC_TYPE_ULRA_VIDEO )
520           || lsmash_check_codec_type_identical( codec_type, QT_CODEC_TYPE_ULRG_VIDEO )
521           || lsmash_check_codec_type_identical( codec_type, QT_CODEC_TYPE_ULY0_VIDEO )
522           || lsmash_check_codec_type_identical( codec_type, QT_CODEC_TYPE_ULY2_VIDEO )
523           || lsmash_check_codec_type_identical( codec_type, QT_CODEC_TYPE_ULH0_VIDEO )
524           || lsmash_check_codec_type_identical( codec_type, QT_CODEC_TYPE_ULH2_VIDEO )
525           || lsmash_check_codec_type_identical( codec_type, QT_CODEC_TYPE_UQY2_VIDEO ) )
526     {
527         *format1 = LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED;
528         return LSMASH_CODEC_SPECIFIC_DATA_TYPE_CODEC_GLOBAL_HEADER;
529     }
530     return LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNSPECIFIED;
531 }
532 
queue_extradata(codec_configuration_t * config,uint8_t * extradata,int extradata_size)533 static int queue_extradata
534 (
535     codec_configuration_t *config,
536     uint8_t               *extradata,
537     int                    extradata_size
538 )
539 {
540     if( extradata && extradata_size > 0 )
541     {
542         uint8_t *temp = (uint8_t *)av_mallocz( extradata_size + AV_INPUT_BUFFER_PADDING_SIZE );
543         if( !temp )
544         {
545             config->error = 1;
546             if( config->lh.show_log )
547                 config->lh.show_log( &config->lh, LW_LOG_FATAL,
548                                      "Failed to allocate memory for new extradata.\n"
549                                      "It is recommended you reopen the file." );
550             return -1;
551         }
552         memcpy( temp, extradata, extradata_size );
553         config->queue.extradata      = temp;
554         config->queue.extradata_size = extradata_size;
555     }
556     else
557     {
558         config->queue.extradata      = NULL;
559         config->queue.extradata_size = 0;
560     }
561     return 0;
562 }
563 
prepare_new_decoder_configuration(codec_configuration_t * config,uint32_t new_index)564 static int prepare_new_decoder_configuration
565 (
566     codec_configuration_t *config,
567     uint32_t               new_index
568 )
569 {
570     if( new_index == 0 )
571         new_index = 1;
572     lsmash_summary_t *summary = new_index <= config->count ? config->entries[new_index - 1].summary : NULL;
573     enum AVCodecID new_codec_id = summary ? get_codec_id_from_description( summary ) : AV_CODEC_ID_NONE;
574     config->queue.codec_id    = new_codec_id;
575     config->queue.delay_count = config->delay_count;
576     if( new_codec_id == AV_CODEC_ID_NONE )
577     {
578         /* Don't update the decoder configuration if L-SMASH cannot recognize CODEC or extract its specific info correctly. */
579         config->queue.index = new_index;
580         return 0;
581     }
582     config->queue.bits_per_sample = av_get_bits_per_sample( new_codec_id );
583     lsmash_codec_specific_format    cs_format1;
584     lsmash_codec_specific_format    cs_format2;
585     lsmash_codec_specific_data_type cs_type = get_codec_specific_data_type( summary->sample_type, &cs_format1, &cs_format2 );
586     if( cs_type == LSMASH_CODEC_SPECIFIC_DATA_TYPE_UNSPECIFIED
587      && cs_format1 == LSMASH_CODEC_SPECIFIC_FORMAT_UNSPECIFIED
588      && cs_format2 == LSMASH_CODEC_SPECIFIC_FORMAT_UNSPECIFIED )
589     {
590         if( new_codec_id == AV_CODEC_ID_AMR_NB )
591         {
592             config->queue.sample_rate = 8000;
593             config->queue.channels    = 1;
594         }
595         else if( new_codec_id == AV_CODEC_ID_AMR_WB )
596         {
597             config->queue.sample_rate = 16000;
598             config->queue.channels    = 1;
599         }
600         queue_extradata( config, NULL, 0 );
601         config->queue.index = new_index;
602         return 0;
603     }
604     uint32_t cs_count = lsmash_count_codec_specific_data( summary );
605     lsmash_codec_specific_t *orig_cs = NULL;
606     lsmash_codec_specific_t *cs1     = NULL;
607     lsmash_codec_specific_t *cs2     = NULL;
608     for( uint32_t i = 1; i <= cs_count; i++ )
609     {
610         orig_cs = lsmash_get_codec_specific_data( summary, i );
611         if( !orig_cs )
612             continue;
613         if( orig_cs->type == cs_type )
614         {
615             cs1 = orig_cs->format == cs_format1 ? orig_cs : lsmash_convert_codec_specific_format( orig_cs, cs_format1 );
616             cs2 = orig_cs->format == cs_format2 ? orig_cs : lsmash_convert_codec_specific_format( orig_cs, cs_format2 );
617             break;
618         }
619     }
620     if( (cs_format1 != LSMASH_CODEC_SPECIFIC_FORMAT_UNSPECIFIED && !cs1)
621      || (cs_format2 != LSMASH_CODEC_SPECIFIC_FORMAT_UNSPECIFIED && !cs2) )
622         return -1;
623     if( cs_format1 == LSMASH_CODEC_SPECIFIC_FORMAT_UNSTRUCTURED )
624     {
625         if( cs_type == LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_ALAC )
626         {
627             lsmash_alac_specific_parameters_t *alac = (lsmash_alac_specific_parameters_t *)cs2->data.structured;
628             config->queue.bits_per_sample = alac->bitDepth;
629             config->queue.channels        = alac->numChannels;
630             config->queue.sample_rate     = alac->sampleRate;
631         }
632         int offset = cs_type == LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_H264 ? 8
633                    : cs_type == LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_HEVC ? 8
634                    : cs_type == LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_VIDEO_VC_1 ? 15
635                    : 0;
636         if( queue_extradata( config, cs1->data.unstructured + offset, cs1->size - offset ) )
637             goto fail;
638     }
639     else if( cs_format1 == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED )
640     {
641         if( cs_type == LSMASH_CODEC_SPECIFIC_DATA_TYPE_MP4SYS_DECODER_CONFIG )
642         {
643             if( new_codec_id == AV_CODEC_ID_MP3 )
644             {
645                 /* Confirm MPEG-1/2 Audio by libavcodec's parser. */
646                 AVCodecParserContext *parser = av_parser_init( new_codec_id );
647                 if( !parser )
648                     goto fail;
649                 uint8_t *dummy_out;
650                 int      dummy_out_size;
651                 av_parser_parse2( parser, config->ctx, &dummy_out, &dummy_out_size,
652                                   config->queue.packet.data, config->queue.packet.size,
653                                   AV_NOPTS_VALUE, AV_NOPTS_VALUE, -1 );
654                 av_parser_close( parser );
655                 config->queue.codec_id = config->ctx->codec_id;
656             }
657             else
658             {
659                 lsmash_mp4sys_decoder_parameters_t *mdp = (lsmash_mp4sys_decoder_parameters_t *)cs1->data.structured;
660                 uint8_t *cs_data;
661                 uint32_t cs_data_size;
662                 if( lsmash_get_mp4sys_decoder_specific_info( mdp, &cs_data, &cs_data_size ) )
663                     goto fail;
664                 int ret = queue_extradata( config, cs_data, cs_data_size );
665                 lsmash_free( cs_data );
666                 if( ret )
667                     goto fail;
668             }
669         }
670         else    /* cs_type == LSMASH_CODEC_SPECIFIC_DATA_TYPE_CODEC_GLOBAL_HEADER */
671         {
672             lsmash_codec_global_header_t *gh = (lsmash_codec_global_header_t *)cs1->data.structured;
673             if( !gh )
674                 goto fail;
675             if( queue_extradata( config, gh->header_data, gh->header_size ) )
676                 goto fail;
677         }
678     }
679     else
680     {
681         /* For enhanced AC-3, we don't assume whether libavcodec's enhanced AC-3 decoder does support
682          * additional independent substreams and its associated dependent substreams or not.
683          * For DTS audio, we don't assume whether libavcodec's DTS decoder does support X96, XXCH, LBR and XLL extensions or not.
684          * Therefore for the above-mentioned audio formats, setup of 'sample_rate' and 'channels' is entrusted to actual decoding. */
685         if( cs_type == LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_AC_3 )
686         {
687             lsmash_ac3_specific_parameters_t *ac3 = (lsmash_ac3_specific_parameters_t *)cs2->data.structured;
688             if( ac3->acmod > 7 || ac3->fscod > 3 )
689                 goto fail;
690             int channels   [] = { 2, 1, 2, 3, 3, 4, 4, 5 };
691             int sample_rate[] = { 48000, 44100, 32000, 0 };
692             config->queue.bits_per_sample = 0;
693             config->queue.channels        = channels[ ac3->acmod ] + ac3->lfeon;
694             config->queue.sample_rate     = sample_rate[ ac3->fscod ];
695         }
696         else if( cs_type == LSMASH_CODEC_SPECIFIC_DATA_TYPE_ISOM_AUDIO_DTS )
697         {
698             /* Here, assume that libavcodec's DTS decoder doesn't support X96, XXCH, LBR and XLL extensions. */
699             lsmash_dts_specific_parameters_t *dts = (lsmash_dts_specific_parameters_t *)cs2->data.structured;
700             config->queue.bits_per_sample = dts->pcmSampleDepth;
701         }
702     }
703     config->queue.index = new_index;
704     if( orig_cs != cs1 )
705         lsmash_destroy_codec_specific_data( cs1 );
706     if( orig_cs != cs2 )
707         lsmash_destroy_codec_specific_data( cs2 );
708     return 0;
709 fail:
710     if( orig_cs != cs1 )
711         lsmash_destroy_codec_specific_data( cs1 );
712     if( orig_cs != cs2 )
713         lsmash_destroy_codec_specific_data( cs2 );
714     return -1;
715 }
716 
get_sample(lsmash_root_t * root,uint32_t track_ID,uint32_t sample_number,codec_configuration_t * config,AVPacket * pkt)717 int get_sample
718 (
719     lsmash_root_t         *root,
720     uint32_t               track_ID,
721     uint32_t               sample_number,
722     codec_configuration_t *config,
723     AVPacket              *pkt
724 )
725 {
726     if( !config->update_pending && config->dequeue_packet )
727     {
728         /* Dequeue the queued packet after the corresponding decoder configuration is activated. */
729         config->dequeue_packet = 0;
730         if( sample_number == config->queue.sample_number )
731         {
732             *pkt = config->queue.packet;
733             return 0;
734         }
735     }
736     av_init_packet( pkt );
737     if( config->update_pending || config->queue.delay_count )
738     {
739         /* Return NULL packet to flush data from the decoder until corresponding decoder configuration is activated. */
740         pkt->data = NULL;
741         pkt->size = 0;
742         if( config->queue.delay_count && (-- config->queue.delay_count == 0) )
743         {
744             config->update_pending = 1;
745             config->dequeue_packet = 1;
746         }
747         return 0;
748     }
749     lsmash_sample_t *sample = lsmash_get_sample_from_media_timeline( root, track_ID, sample_number );
750     if( !sample )
751     {
752         /* Reached the end of this media timeline. */
753         pkt->data = NULL;
754         pkt->size = 0;
755         return 1;
756     }
757     pkt->flags = sample->prop.ra_flags;     /* Set proper flags when feeding this packet into the decoder. */
758     pkt->size  = sample->length;
759     pkt->data  = config->input_buffer;
760     pkt->pts   = sample->cts;               /* Set composition timestamp to presentation timestamp field. */
761     pkt->dts   = sample->dts;
762     /* Copy sample data from L-SMASH.
763      * Set 0 to the end of the additional AV_INPUT_BUFFER_PADDING_SIZE bytes.
764      * Without this, some decoders could cause wrong results. */
765     memcpy( pkt->data, sample->data, sample->length );
766     memset( pkt->data + sample->length, 0, AV_INPUT_BUFFER_PADDING_SIZE );
767     /* TODO: add handling invalid indexes. */
768     if( sample->index != config->index )
769     {
770         if( prepare_new_decoder_configuration( config, sample->index ) )
771         {
772             lsmash_delete_sample( sample );
773             return -1;
774         }
775         /* Queue the current packet and, instead of this, return NULL packet.
776          * The current packet will be dequeued and returned after the corresponding decoder configuration is activated. */
777         config->queue.sample_number = sample_number;
778         config->queue.packet        = *pkt;
779         pkt->data = NULL;
780         pkt->size = 0;
781         if( config->queue.delay_count == 0 )
782         {
783             /* This NULL packet must not be sent to the decoder. */
784             config->update_pending = 1;
785             config->dequeue_packet = 1;
786             lsmash_delete_sample( sample );
787             return 2;
788         }
789         else
790             config->dequeue_packet = 0;
791     }
792     lsmash_delete_sample( sample );
793     return 0;
794 }
795 
796 /* Close and open the new decoder to flush buffers in the decoder even if the decoder implements avcodec_flush_buffers().
797  * It seems this brings about more stable composition when seeking.
798  * Note that this function could reallocate AVCodecContext. */
libavsmash_flush_buffers(codec_configuration_t * config)799 void libavsmash_flush_buffers
800 (
801     codec_configuration_t *config
802 )
803 {
804     AVCodecContext    *ctx          = NULL;
805     const AVCodec     *codec        = config->ctx->codec;
806     void              *app_specific = config->ctx->opaque;
807     AVCodecParameters *codecpar     = avcodec_parameters_alloc();
808     if( !codecpar
809      || avcodec_parameters_from_context( codecpar, config->ctx ) < 0
810      || open_decoder( &ctx, codecpar, codec, config->ctx->thread_count, config->ctx->refcounted_frames ) < 0 )
811     {
812         avcodec_flush_buffers( config->ctx );
813         config->error = 1;
814         if( config->lh.show_log )
815             config->lh.show_log( &config->lh, LW_LOG_FATAL,
816                                  "Failed to flush buffers by a reliable way.\n"
817                                  "It is recommended you reopen the file." );
818     }
819     else
820     {
821         config->ctx->opaque = NULL;
822         avcodec_free_context( &config->ctx );
823         config->ctx = ctx;
824         config->ctx->opaque = app_specific;
825     }
826     avcodec_parameters_free( &codecpar );
827     config->update_pending    = 0;
828     config->delay_count       = 0;
829     config->queue.delay_count = 0;
830     config->queue.index       = config->index;
831 }
832 
update_configuration(lsmash_root_t * root,uint32_t track_ID,codec_configuration_t * config)833 void update_configuration
834 (
835     lsmash_root_t         *root,
836     uint32_t               track_ID,
837     codec_configuration_t *config
838 )
839 {
840     uint32_t new_index = config->queue.index ? config->queue.index : 1;
841     if( !config->update_pending || config->queue.codec_id == AV_CODEC_ID_NONE )
842     {
843         /* Don't update the decoder configuration if L-SMASH cannot recognize CODEC or extract its specific info correctly. */
844         config->index = new_index;
845         libavsmash_flush_buffers( config );
846         /* Set up the maximum presentation width and height. */
847         libavsmash_summary_t *entry   = config->index <= config->count ? &config->entries[ config->index - 1 ] : NULL;
848         lsmash_summary_t     *summary = entry ? entry->summary : NULL;
849         if( summary && summary->summary_type == LSMASH_SUMMARY_TYPE_VIDEO )
850         {
851             lsmash_video_summary_t *video = (lsmash_video_summary_t *)summary;
852             entry->extended.width  = video->width;
853             entry->extended.height = video->height;
854         }
855         return;
856     }
857     char               error_string[96]  = { 0 };
858     void              *app_specific      = config->ctx->opaque;
859     const int          thread_count      = config->ctx->thread_count;
860     const int          refcounted_frames = config->ctx->refcounted_frames;
861     AVCodecParameters *codecpar          = avcodec_parameters_alloc();
862     if( !codecpar || avcodec_parameters_from_context( codecpar, config->ctx ) < 0 )
863     {
864         strcpy( error_string, "Failed to get the CODEC parameters.\n" );
865         goto fail;
866     }
867     /* Close the decoder here. */
868     config->ctx->opaque = NULL;
869     avcodec_free_context( &config->ctx );
870     /* Find an appropriate decoder. */
871     const AVCodec *codec = find_decoder( config->queue.codec_id, config->preferred_decoder_names );
872     if( !codec )
873     {
874         strcpy( error_string, "Failed to find the decoder.\n" );
875         goto fail;
876     }
877     /* Set up decoder basic settings. */
878     lsmash_summary_t *summary = config->entries[new_index - 1].summary;
879     if( codec->type == AVMEDIA_TYPE_VIDEO )
880     {
881         lsmash_video_summary_t *video = (lsmash_video_summary_t *)summary;
882         codecpar->width  = video->width;
883         codecpar->height = video->height;
884         /* Here, expect appropriate pixel format will be picked in avcodec_open2(). */
885         if( video->depth >= QT_VIDEO_DEPTH_GRAYSCALE_1 && video->depth <= QT_VIDEO_DEPTH_GRAYSCALE_8 )
886             config->queue.bits_per_sample = video->depth & 0x1f;
887         else
888             config->queue.bits_per_sample = video->depth;
889         if( config->queue.bits_per_sample > 0 )
890             codecpar->bits_per_coded_sample = config->queue.bits_per_sample;
891     }
892     else
893     {
894         if( codec->id != AV_CODEC_ID_AAC && codec->id != AV_CODEC_ID_DTS && codec->id != AV_CODEC_ID_EAC3 )
895         {
896             lsmash_audio_summary_t *audio = (lsmash_audio_summary_t *)summary;
897             codecpar->sample_rate           = config->queue.sample_rate     ? config->queue.sample_rate     : audio->frequency;
898             codecpar->bits_per_coded_sample = config->queue.bits_per_sample ? config->queue.bits_per_sample : audio->sample_size;
899             codecpar->channels              = config->queue.channels        ? config->queue.channels        : audio->channels;
900         }
901         if( codec->id == AV_CODEC_ID_DTS )
902         {
903             codecpar->bits_per_coded_sample = config->queue.bits_per_sample;
904             codecpar->format                = (int)(config->queue.bits_per_sample == 16 ? AV_SAMPLE_FMT_S16 : AV_SAMPLE_FMT_FLT);
905         }
906     }
907     /* This is needed by some CODECs such as UtVideo and raw video. */
908     codecpar->codec_tag = BYTE_SWAP_32( summary->sample_type.fourcc );
909     /* Update extradata. */
910     codecpar->extradata      = config->queue.extradata;
911     codecpar->extradata_size = config->queue.extradata_size;
912     config->queue.extradata      = NULL;
913     config->queue.extradata_size = 0;
914     /* Open an appropriate decoder.
915      * Here, we force single threaded decoding since some decoder doesn't do its proper initialization with multi-threaded decoding. */
916     AVCodecContext *ctx = NULL;
917     if( open_decoder( &ctx, codecpar, codec, 1, refcounted_frames ) < 0 )
918     {
919         strcpy( error_string, "Failed to open decoder.\n" );
920         goto fail;
921     }
922     avcodec_parameters_free( &codecpar );
923     config->ctx               = ctx;
924     config->index             = new_index;
925     config->update_pending    = 0;
926     config->delay_count       = 0;
927     config->queue.delay_count = 0;
928     /* Set up decoder basic settings by actual decoding. */
929     AVFrame *picture = av_frame_alloc();
930     if( !picture )
931     {
932         strcpy( error_string, "Failed to alloc AVFrame to set up a decoder configuration.\n" );
933         goto fail;
934     }
935     uint32_t current_sample_number = config->queue.sample_number;
936     extended_summary_t *extended = &config->entries[ config->index - 1 ].extended;
937     if( ctx->codec_type == AVMEDIA_TYPE_VIDEO )
938     {
939         /* Set the maximum width and height in this sequence. */
940         extended->width  = ctx->width;
941         extended->height = ctx->height;
942         /* Actual decoding */
943         uint32_t i = current_sample_number;
944         do
945         {
946             AVPacket pkt = { 0 };
947             int ret = get_sample( root, track_ID, i++, config, &pkt );
948             if( ret > 0 || config->index != config->queue.index )
949                 break;
950             else if( ret < 0 )
951             {
952                 if( ctx->pix_fmt == AV_PIX_FMT_NONE )
953                     strcpy( error_string, "Failed to set up pixel format.\n" );
954                 else
955                     strcpy( error_string, "Failed to set up resolution.\n" );
956                 av_frame_free( &picture );
957                 goto fail;
958             }
959             int dummy;
960             decode_video_packet( ctx, picture, &dummy, &pkt );
961         } while( ctx->width == 0 || ctx->height == 0 || ctx->pix_fmt == AV_PIX_FMT_NONE );
962     }
963     else
964     {
965         uint32_t i = current_sample_number;
966         do
967         {
968             AVPacket pkt = { 0 };
969             int ret = get_sample( root, track_ID, i++, config, &pkt );
970             if( ret > 0 || config->index != config->queue.index )
971                 break;
972             else if( ret < 0 )
973             {
974                 if( ctx->sample_rate == 0 )
975                     strcpy( error_string, "Failed to set up sample rate.\n" );
976                 else if( ctx->channel_layout == 0 && ctx->channels == 0 )
977                     strcpy( error_string, "Failed to set up channels.\n" );
978                 else
979                     strcpy( error_string, "Failed to set up sample format.\n" );
980                 av_frame_free( &picture );
981                 goto fail;
982             }
983             int dummy;
984             decode_audio_packet( ctx, picture, &dummy, &pkt );
985         } while( ctx->sample_rate == 0 || (ctx->channel_layout == 0 && ctx->channels == 0) || ctx->sample_fmt == AV_SAMPLE_FMT_NONE );
986         extended->channel_layout = ctx->channel_layout ? ctx->channel_layout : av_get_default_channel_layout( ctx->channels );
987         extended->sample_rate    = ctx->sample_rate;
988         extended->sample_format  = ctx->sample_fmt;
989         extended->frame_length   = ctx->frame_size;
990     }
991     av_frame_free( &picture );
992     /* Reopen/flush with the requested number of threads. */
993     ctx->thread_count = thread_count;
994     libavsmash_flush_buffers( config ); /* Note that config->ctx could change here. */
995     ctx = config->ctx;
996     if( current_sample_number == config->queue.sample_number )
997         config->dequeue_packet = 1;
998     ctx->get_buffer2 = config->get_buffer;
999     ctx->opaque      = app_specific;
1000     if( ctx->codec_type == AVMEDIA_TYPE_VIDEO )
1001     {
1002         /* avcodec_open2() may have changed resolution unexpectedly. */
1003         ctx->width  = extended->width;
1004         ctx->height = extended->height;
1005     }
1006     return;
1007 fail:
1008     avcodec_parameters_free( &codecpar );
1009     config->update_pending    = 0;
1010     config->delay_count       = 0;
1011     config->queue.delay_count = 0;
1012     config->error             = 1;
1013     lw_log_show( &config->lh, LW_LOG_FATAL, "%sIt is recommended you reopen the file.", error_string );
1014 }
1015 
initialize_decoder_configuration(lsmash_root_t * root,uint32_t track_ID,codec_configuration_t * config)1016 int initialize_decoder_configuration
1017 (
1018     lsmash_root_t         *root,
1019     uint32_t               track_ID,
1020     codec_configuration_t *config
1021 )
1022 {
1023     /* Note: the input buffer for libavcodec's decoders must be AV_INPUT_BUFFER_PADDING_SIZE larger than the actual read bytes. */
1024     uint32_t input_buffer_size = lsmash_get_max_sample_size_in_media_timeline( root, track_ID );
1025     if( input_buffer_size == 0 )
1026         return -1;
1027     config->input_buffer = (uint8_t *)av_mallocz( input_buffer_size + AV_INPUT_BUFFER_PADDING_SIZE );
1028     if( !config->input_buffer )
1029         return -1;
1030     config->get_buffer = avcodec_default_get_buffer2;
1031     /* Initialize decoder configuration at the first valid sample. */
1032     AVPacket dummy = { 0 };
1033     for( uint32_t i = 1; get_sample( root, track_ID, i, config, &dummy ) < 0; i++ );
1034     update_configuration( root, track_ID, config );
1035     /* Decide preferred settings. */
1036     config->prefer.width           = config->ctx->width;
1037     config->prefer.height          = config->ctx->height;
1038     config->prefer.sample_rate     = config->ctx->sample_rate;
1039     config->prefer.sample_format   = config->ctx->sample_fmt;
1040     config->prefer.bits_per_sample = config->ctx->bits_per_raw_sample   > 0 ? config->ctx->bits_per_raw_sample
1041                                    : config->ctx->bits_per_coded_sample > 0 ? config->ctx->bits_per_coded_sample
1042                                    : av_get_bytes_per_sample( config->ctx->sample_fmt ) << 3;
1043     config->prefer.channel_layout  = config->ctx->channel_layout
1044                                    ? config->ctx->channel_layout
1045                                    : av_get_default_channel_layout( config->ctx->channels );
1046     if( config->count <= 1 )
1047         return config->error ? -1 : 0;
1048     /* Investigate other decoder configurations and pick preferred settings from them. */
1049     uint8_t *index_list = (uint8_t *)lw_malloc_zero( config->count );
1050     if( !index_list )
1051     {
1052         config->error = 1;
1053         return -1;
1054     }
1055     uint32_t valid_index_count = (config->index && config->index <= config->count);
1056     if( valid_index_count )
1057         index_list[ config->index - 1 ] = 1;
1058     uint32_t sample_count = lsmash_get_sample_count_in_media_timeline( root, track_ID );
1059     for( uint32_t i = 2; i <= sample_count && valid_index_count < config->count; i++ )
1060     {
1061         lsmash_sample_t sample;
1062         if( lsmash_get_sample_info_from_media_timeline( root, track_ID, i, &sample ) )
1063             continue;
1064         if( sample.index == config->index || sample.index == 0 )
1065             continue;
1066         if( sample.index <= config->count && !index_list[ sample.index - 1 ] )
1067         {
1068             for( uint32_t j = i; get_sample( root, track_ID, j, config, &dummy ) < 0; j++ );
1069             update_configuration( root, track_ID, config );
1070             index_list[ sample.index - 1 ] = 1;
1071             if( config->ctx->width > config->prefer.width )
1072                 config->prefer.width = config->ctx->width;
1073             if( config->ctx->height > config->prefer.height )
1074                 config->prefer.height = config->ctx->height;
1075             if( av_get_channel_layout_nb_channels( config->ctx->channel_layout )
1076               > av_get_channel_layout_nb_channels( config->prefer.channel_layout ) )
1077                 config->prefer.channel_layout = config->ctx->channel_layout;
1078             if( config->ctx->sample_rate > config->prefer.sample_rate )
1079                 config->prefer.sample_rate = config->ctx->sample_rate;
1080             switch( config->prefer.sample_format )
1081             {
1082                 case AV_SAMPLE_FMT_NONE :
1083                     if( config->ctx->sample_fmt != AV_SAMPLE_FMT_NONE )
1084                         config->prefer.sample_format = config->ctx->sample_fmt;
1085                     break;
1086                 case AV_SAMPLE_FMT_U8 :
1087                 case AV_SAMPLE_FMT_U8P :
1088                     if( config->ctx->sample_fmt != AV_SAMPLE_FMT_U8 && config->ctx->sample_fmt != AV_SAMPLE_FMT_U8P )
1089                         config->prefer.sample_format = config->ctx->sample_fmt;
1090                     break;
1091                 case AV_SAMPLE_FMT_S16 :
1092                 case AV_SAMPLE_FMT_S16P :
1093                     if( config->ctx->sample_fmt != AV_SAMPLE_FMT_U8  && config->ctx->sample_fmt != AV_SAMPLE_FMT_U8P
1094                      && config->ctx->sample_fmt != AV_SAMPLE_FMT_S16 && config->ctx->sample_fmt != AV_SAMPLE_FMT_S16P )
1095                         config->prefer.sample_format = config->ctx->sample_fmt;
1096                     break;
1097                 case AV_SAMPLE_FMT_S32 :
1098                 case AV_SAMPLE_FMT_S32P :
1099                     if( config->ctx->sample_fmt != AV_SAMPLE_FMT_U8  && config->ctx->sample_fmt != AV_SAMPLE_FMT_U8P
1100                      && config->ctx->sample_fmt != AV_SAMPLE_FMT_S16 && config->ctx->sample_fmt != AV_SAMPLE_FMT_S16P
1101                      && config->ctx->sample_fmt != AV_SAMPLE_FMT_S32 && config->ctx->sample_fmt != AV_SAMPLE_FMT_S32P )
1102                         config->prefer.sample_format = config->ctx->sample_fmt;
1103                     break;
1104                 case AV_SAMPLE_FMT_FLT :
1105                 case AV_SAMPLE_FMT_FLTP :
1106                     if( config->ctx->sample_fmt == AV_SAMPLE_FMT_DBL || config->ctx->sample_fmt == AV_SAMPLE_FMT_DBLP )
1107                         config->prefer.sample_format = config->ctx->sample_fmt;
1108                     break;
1109                 default :
1110                     break;
1111             }
1112             int bits_per_sample = config->ctx->bits_per_raw_sample   > 0 ? config->ctx->bits_per_raw_sample
1113                                 : config->ctx->bits_per_coded_sample > 0 ? config->ctx->bits_per_coded_sample
1114                                 : av_get_bytes_per_sample( config->ctx->sample_fmt ) << 3;
1115             if( bits_per_sample > config->prefer.bits_per_sample )
1116                 config->prefer.bits_per_sample = bits_per_sample;
1117             ++valid_index_count;
1118         }
1119     }
1120     lw_free( index_list );
1121     /* Reinitialize decoder configuration at the first valid sample. */
1122     for( uint32_t i = 1; get_sample( root, track_ID, i, config, &dummy ) < 0; i++ );
1123     update_configuration( root, track_ID, config );
1124     return config->error ? -1 : 0;
1125 }
1126 
cleanup_configuration(codec_configuration_t * config)1127 void cleanup_configuration
1128 (
1129     codec_configuration_t *config
1130 )
1131 {
1132     if( config->entries )
1133     {
1134         for( uint32_t i = 0; i < config->count; i++ )
1135             lsmash_cleanup_summary( config->entries[i].summary );
1136         free( config->entries );
1137     }
1138     av_freep( &config->queue.extradata );
1139     av_freep( &config->input_buffer );
1140     avcodec_free_context( &config->ctx );
1141 }
1142