1. Fix a bug when media has only audio track
[platform/core/multimedia/libmm-fileinfo.git] / formats / ffmpeg / mm_file_format_ffmpeg.c
1 /*
2  * libmm-fileinfo
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Haejeong Kim <backto.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21  
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include <libavformat/avformat.h>
26 #include <libavcodec/avcodec.h>
27 #ifdef __MMFILE_FFMPEG_V085__
28 #include <libswscale/swscale.h>
29 #endif
30 #include <mm_error.h>
31 #include <mm_types.h>
32 #include "mm_debug.h"
33 #include "mm_file_formats.h"
34 #include "mm_file_utils.h"
35 #include "mm_file_format_ffmpeg.h"
36
37 #include "mm_file_format_ffmpeg_mem.h"
38 #include <sys/time.h>
39
40 #define _SHORT_MEDIA_LIMIT              2000    /* under X seconds duration*/
41
42 extern int      img_convert (AVPicture *dst, int dst_pix_fmt, const AVPicture *src, int src_pix_fmt,int src_width, int src_height);
43
44 /* internal functions */
45 static int _is_good_pgm (unsigned char *buf, int wrap, int xsize, int ysize);
46 #ifdef MMFILE_FORMAT_DEBUG_DUMP
47 static void _save_pgm (unsigned char *buf, int wrap, int xsize, int ysize, char *filename);
48 #endif
49 #ifdef __MMFILE_TEST_MODE__
50 static void _dump_av_packet (AVPacket *pkt);
51 #endif
52
53 static int      _get_video_fps (int frame_cnt, int duration, AVRational r_frame_rate, int is_roundup);
54 static int      _get_first_good_video_frame (AVFormatContext *pFormatCtx, AVCodecContext *pCodecCtx, int videoStream, AVFrame **pFrame);
55
56 static int      ConvertVideoCodecEnum (int AVVideoCodecID);
57 static int      ConvertAudioCodecEnum (int AVAudioCodecID);
58
59 /* plugin manadatory API */
60 int mmfile_format_read_stream_ffmpg (MMFileFormatContext * formatContext);
61 int mmfile_format_read_frame_ffmpg  (MMFileFormatContext *formatContext, unsigned int timestamp, MMFileFormatFrame *frame);
62 int mmfile_format_read_tag_ffmpg    (MMFileFormatContext *formatContext);
63 int mmfile_format_close_ffmpg       (MMFileFormatContext *formatContext);
64 static int getMimeType(int formatId, char *mimeType); 
65
66
67
68 EXPORT_API
69 int mmfile_format_open_ffmpg (MMFileFormatContext *formatContext)
70 {
71         AVFormatContext     *pFormatCtx = NULL;
72         AVInputFormat       *grab_iformat = NULL;
73         int ret = 0;
74         int i;
75         char ffmpegFormatName[MMFILE_FILE_FMT_MAX_LEN] = {0,};
76         char mimeType[MMFILE_MIMETYPE_MAX_LEN] = {0,};
77
78         formatContext->ReadStream   = mmfile_format_read_stream_ffmpg;
79         formatContext->ReadFrame    = mmfile_format_read_frame_ffmpg;
80         formatContext->ReadTag      = mmfile_format_read_tag_ffmpg;
81         formatContext->Close        = mmfile_format_close_ffmpg;
82
83 #ifdef __MMFILE_TEST_MODE__
84         debug_msg ("ffmpeg version: %d\n", avformat_version ());
85         /**
86          * FFMPEG DEBUG LEVEL
87          *  AV_LOG_QUIET -1
88          *  AV_LOG_FATAL 0
89          *  AV_LOG_ERROR 0
90          *  AV_LOG_WARNING 1
91          *  AV_LOG_INFO 1
92          *  AV_LOG_VERBOSE 1
93          *  AV_LOG_DEBUG 2
94      */
95         av_log_set_level (AV_LOG_DEBUG);
96 #else
97         av_log_set_level (AV_LOG_QUIET);
98 #endif
99
100         av_register_all();
101
102         if (formatContext->filesrc->type  == MM_FILE_SRC_TYPE_MEMORY) {
103
104 #ifdef __MMFILE_FFMPEG_V085__ 
105                 ffurl_register_protocol(&MMFileMEMProtocol, sizeof (URLProtocol));
106 #else
107                 register_protocol (&MMFileMEMProtocol);
108 #endif  
109                 if(getMimeType(formatContext->filesrc->memory.format,mimeType)< 0) {
110                         debug_error ("error: Error in MIME Type finding\n");
111                         return MMFILE_FORMAT_FAIL;
112                 }
113
114                 memset (ffmpegFormatName, 0x00, MMFILE_FILE_FMT_MAX_LEN);
115                 
116                 ret = mmfile_util_get_ffmpeg_format (mimeType,ffmpegFormatName);
117
118                 if (MMFILE_UTIL_SUCCESS != ret) {
119                         debug_error ("error: mmfile_util_get_ffmpeg_format\n");
120                         return MMFILE_FORMAT_FAIL;
121                 }
122
123                 grab_iformat = av_find_input_format (ffmpegFormatName);
124
125                 if (NULL == grab_iformat) {
126                         debug_error ("error: cannot find format\n");
127                         goto exception;
128                 }
129
130 #ifdef __MMFILE_FFMPEG_V085__
131                 ret = avformat_open_input (&pFormatCtx, formatContext->uriFileName, grab_iformat, NULL);
132 #else
133                 ret = av_open_input_file (&pFormatCtx, formatContext->uriFileName, grab_iformat, 0, NULL);
134 #endif
135                 if (ret < 0) {
136                         debug_error("error: cannot open %s %d\n", formatContext->uriFileName, ret);
137                         goto exception;
138                 }
139                 formatContext->privateFormatData = pFormatCtx;
140         }
141         
142         if (formatContext->filesrc->type  == MM_FILE_SRC_TYPE_FILE) {
143
144                 if (formatContext->isdrm == MM_FILE_DRM_OMA) {
145                         debug_error ("error: drm content\n");
146                         goto exception;
147                 } else {
148 HANDLING_DRM_DIVX:
149 #ifdef __MMFILE_FFMPEG_V085__
150                         ret = avformat_open_input(&pFormatCtx, formatContext->filesrc->file.path, NULL, NULL);
151 #else
152                         ret = av_open_input_file(&pFormatCtx, formatContext->filesrc->file.path, NULL, 0, NULL);
153 #endif
154                         if (ret < 0) {
155                                 debug_error("error: cannot open %s %d\n", formatContext->filesrc->file.path, ret);
156                                 goto exception;
157                         }
158                         formatContext->privateFormatData = pFormatCtx;
159                 }
160         }
161
162         if (!pFormatCtx || !(pFormatCtx->nb_streams > 0)) {
163                 debug_warning ("failed to find av stream. maybe corrupted data.\n");
164                 goto exception;
165         }
166
167         #ifdef __MMFILE_TEST_MODE__
168         debug_msg ("number of stream: %d\n", pFormatCtx->nb_streams);
169         #endif
170
171         formatContext->videoTotalTrackNum = 0;
172         formatContext->audioTotalTrackNum = 0;
173
174         for(i = 0; i < pFormatCtx->nb_streams; i++) {
175 #ifdef __MMFILE_FFMPEG_V085__           
176                 if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
177                         #ifdef __MMFILE_TEST_MODE__
178                         debug_msg ("FFMPEG video codec id: 0x%08X\n", pFormatCtx->streams[i]->codec->codec_id);
179                         #endif
180                         if (ConvertVideoCodecEnum(pFormatCtx->streams[i]->codec->codec_id) != MM_VIDEO_CODEC_NONE)
181                                 formatContext->videoTotalTrackNum += 1;
182                 }
183                 if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
184                         #ifdef __MMFILE_TEST_MODE__
185                         debug_msg ("FFMPEG audio codec id: 0x%08X\n", pFormatCtx->streams[i]->codec->codec_id);
186                         #endif
187                         formatContext->audioTotalTrackNum += 1;
188                 }
189 #else   
190                 if (pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
191                         #ifdef __MMFILE_TEST_MODE__
192                         debug_msg ("FFMPEG video codec id: 0x%08X\n", pFormatCtx->streams[i]->codec->codec_id);
193                         #endif
194                         formatContext->videoTotalTrackNum += 1;
195                 }
196                 if (pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
197                         #ifdef __MMFILE_TEST_MODE__
198                         debug_msg ("FFMPEG audio codec id: 0x%08X\n", pFormatCtx->streams[i]->codec->codec_id);
199                         #endif
200                         formatContext->audioTotalTrackNum += 1;
201                 }
202 #endif
203         }
204
205         #ifdef __MMFILE_TEST_MODE__
206         debug_msg ("format: %s (%s)\n", pFormatCtx->iformat->name, pFormatCtx->iformat->long_name);
207         #ifdef __MMFILE_FFMPEG_V085__
208         av_dump_format (pFormatCtx, 0, formatContext->filesrc->file.path, 0);
209         #else
210         dump_format (pFormatCtx, 0, formatContext->filesrc->file.path, 0);
211         #endif
212         #endif
213
214         return MMFILE_FORMAT_SUCCESS;
215
216 exception: /* fail to get content information */
217
218         mmfile_format_close_ffmpg (formatContext);
219         formatContext->privateFormatData = NULL;
220
221         return MMFILE_FORMAT_FAIL;
222 }
223
224
225 EXPORT_API
226 int mmfile_format_read_stream_ffmpg (MMFileFormatContext * formatContext)
227 {
228         AVFormatContext     *pFormatCtx = NULL;
229         AVCodecContext      *pAudioCodecCtx = NULL;
230         AVCodecContext      *pVideoCodecCtx = NULL;
231
232         MMFileFormatStream  *videoStream = NULL;
233         MMFileFormatStream  *audioStream = NULL;
234         int ret = 0;
235
236         if (NULL == formatContext || NULL == formatContext->privateFormatData) {
237                 debug_error ("invalid param\n");
238                 return MMFILE_FORMAT_FAIL;
239         }
240
241         pFormatCtx = formatContext->privateFormatData;
242
243         /**
244          *@important if data is corrupted, occur segment fault by av_find_stream_info().
245          *                      - fixed 2009-06-25.
246          */
247 #ifdef __MMFILE_FFMPEG_V100__
248         ret = avformat_find_stream_info (pFormatCtx, NULL);
249 #else
250         ret = av_find_stream_info (pFormatCtx);
251 #endif
252         if ( ret < 0 ) {
253                 debug_warning ("failed to find stream info. errcode = %d\n", ret);
254                 goto exception;
255         }
256
257         #ifdef __MMFILE_TEST_MODE__
258         debug_msg ("FFMPEG: dur %lld, start %lld\n", pFormatCtx->duration, pFormatCtx->start_time);
259         #endif
260
261         /**
262          *@note asf has long duration bug. and Some content's start time is wrong(negative number).
263          */
264         if(pFormatCtx->start_time < 0) {
265                 debug_warning ("Wrong Start time = %lld\n", pFormatCtx->start_time);
266                 formatContext->duration = (long long)(pFormatCtx->duration) * 1000 / AV_TIME_BASE;
267         }
268         else {
269                 formatContext->duration = (long long)(pFormatCtx->duration + pFormatCtx->start_time) * 1000 / AV_TIME_BASE;
270         }
271
272         formatContext->videoStreamId = -1;
273         formatContext->audioStreamId = -1;
274         formatContext->nbStreams = 0;
275
276         int i = 0;
277         for ( i = 0; i < pFormatCtx->nb_streams; i++ ) {
278 #ifdef __MMFILE_FFMPEG_V085__           
279                 if ( pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
280 #else
281                 if ( pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
282 #endif
283                         if (formatContext->videoStreamId == -1) {
284                                 videoStream = mmfile_malloc (sizeof(MMFileFormatStream));
285                                 if (NULL == videoStream) {
286                                         debug_error ("mmfile_malloc error\n");
287                                         goto exception;
288                                 }
289
290                                 videoStream->streamType = MMFILE_VIDEO_STREAM;
291                                 formatContext->streams[MMFILE_VIDEO_STREAM] = videoStream;
292                                 formatContext->nbStreams += 1;
293                                 formatContext->videoStreamId = i;
294
295                                 pVideoCodecCtx = pFormatCtx->streams[i]->codec;
296                                 if (pVideoCodecCtx) {
297                                         videoStream->codecId            = ConvertVideoCodecEnum (pVideoCodecCtx->codec_id);
298                                         if (videoStream->codecId == MM_VIDEO_CODEC_NONE) {
299                                                 debug_error("Proper codec is not found in [%d] stream", i);
300                                                 formatContext->videoStreamId = -1;
301                                                 mmfile_free(videoStream);
302                                                 formatContext->streams[MMFILE_VIDEO_STREAM] = NULL;
303                                                 videoStream = NULL;
304                                                 continue;
305                                         }
306
307                                         /**
308                                          * Get FPS
309                                          * 1. try to get average fps of video stream.
310                                          * 2. if (1) failed, try to get fps of media container.
311                                          */
312                                         videoStream->framePerSec        = _get_video_fps (pFormatCtx->streams[i]->nb_frames,
313                                                                                                                                         pFormatCtx->streams[i]->duration,
314                                                                                                                                         pFormatCtx->streams[i]->time_base,
315                                                                                                                                         1);
316
317                                         if (videoStream->framePerSec == 0)
318                                                 videoStream->framePerSec = av_q2d (pFormatCtx->streams[i]->r_frame_rate);
319
320                                         videoStream->width                      = pVideoCodecCtx->width;
321                                         videoStream->height                     = pVideoCodecCtx->height;
322                                         videoStream->bitRate            = pVideoCodecCtx->bit_rate;
323                                 }
324                         }
325                 } 
326 #ifdef __MMFILE_FFMPEG_V085__
327                 else if ( pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO ) {
328 #else
329                 else if ( pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO ) {
330 #endif
331                         if (formatContext->audioStreamId == -1) {
332                                 audioStream = mmfile_malloc (sizeof(MMFileFormatStream));
333                                 if (NULL == audioStream) {
334                                         debug_error ("mmfile_malloc error\n");
335                                         goto exception;
336                                 }
337
338                                 audioStream->streamType = MMFILE_AUDIO_STREAM;
339                                 formatContext->streams[MMFILE_AUDIO_STREAM] = audioStream;
340                                 formatContext->nbStreams += 1;
341                                 formatContext->audioStreamId = i;
342
343                                 pAudioCodecCtx = pFormatCtx->streams[i]->codec;
344                                 if (pAudioCodecCtx) {
345                                         audioStream->codecId            = ConvertAudioCodecEnum (pAudioCodecCtx->codec_id);
346                                         audioStream->bitRate            = pAudioCodecCtx->bit_rate;
347                                         audioStream->nbChannel          = pAudioCodecCtx->channels;
348                                         audioStream->samplePerSec       = pAudioCodecCtx->sample_rate;
349                                 }
350                         }
351                 }
352         }
353
354         if ( formatContext->nbStreams == 0 ) {
355                 debug_error("error: there is no audio and video track\n");
356                 goto exception;
357         }
358
359         #ifdef __MMFILE_TEST_MODE__
360         mmfile_format_print_contents (formatContext);
361         #endif
362
363         return MMFILE_FORMAT_SUCCESS;
364
365 exception:
366         if (videoStream) {
367                 mmfile_free (videoStream);
368                 formatContext->streams[MMFILE_VIDEO_STREAM] = NULL;
369         }
370
371         if (audioStream) {
372                 mmfile_free (audioStream);
373                 formatContext->streams[MMFILE_AUDIO_STREAM] = NULL;
374         }
375
376         if (pFormatCtx) {
377 #ifdef __MMFILE_FFMPEG_V100__
378                 avformat_close_input (&pFormatCtx);
379 #else
380                 av_close_input_file (pFormatCtx);
381 #endif
382                 formatContext->privateFormatData = NULL;
383         }
384
385         formatContext->audioTotalTrackNum = 0;
386         formatContext->videoTotalTrackNum = 0;
387         formatContext->nbStreams = 0;
388
389         return MMFILE_FORMAT_FAIL;
390 }
391
392 #define DATA_LENGTH 4
393 #define POS_OF_MIME_LEN DATA_LENGTH
394 #define CONVERT_TO_INT(dest, src) {dest = 0; dest |= (0 |src[0] << 24) | (0 | src[1] << 16) | (0 | src[2] << 8) | (0 | src[3]);}
395
396 EXPORT_API
397 int mmfile_format_read_tag_ffmpg (MMFileFormatContext *formatContext)
398 {
399         AVFormatContext     *pFormatCtx = NULL;
400
401         if (NULL == formatContext || NULL == formatContext->privateFormatData) {
402                 debug_error ("invalid param\n");
403                 return MMFILE_FORMAT_FAIL;
404         }
405
406         pFormatCtx = formatContext->privateFormatData;
407
408         if (formatContext->formatType == MM_FILE_FORMAT_3GP ||formatContext->formatType == MM_FILE_FORMAT_MP4) {
409                 MMFileUtilGetMetaDataFromMP4 (formatContext);
410         }
411
412 #ifdef __MMFILE_FFMPEG_V085__
413 /*metadata extracted by ffmpeg*/
414         int idx = 0;
415
416         if(pFormatCtx != NULL) {
417                 for(idx = 0; idx < pFormatCtx->nb_streams + 1; idx++) {
418                         AVDictionary *metainfo = NULL;
419                         AVStream *st = NULL;
420
421                         if(idx < pFormatCtx->nb_streams) {      //Check metadata of normal stream like audio, video, video cover art(cover art saved in new stream). refer to mov_read_covr() in ffmpeg.
422                                 st = pFormatCtx->streams[idx];
423                                         if(st != NULL)
424                                                 metainfo = st->metadata;
425                         } else {        //Check metadata of Content
426                                 if(pFormatCtx->metadata != NULL) {
427                                         metainfo = pFormatCtx->metadata;
428                                 } else {
429                                         continue;
430                                 }
431                         }
432
433                         /*refer to mov_read_covr() in ffmpeg.*/
434                         if(st != NULL) {
435                                 AVPacket pkt = st->attached_pic;
436                                 int codec_id = st->codec->codec_id;
437
438                                 if((pkt.data != NULL) && (pkt.size > 0)) {
439                                         /*Set mime type*/
440                                         if (formatContext->artworkMime) mmfile_free (formatContext->artworkMime);
441
442                                         if(codec_id == AV_CODEC_ID_MJPEG)
443                                                 formatContext->artworkMime = mmfile_strdup("image/jpeg");
444                                         else if(codec_id == AV_CODEC_ID_PNG)
445                                                 formatContext->artworkMime = mmfile_strdup("image/png");
446                                         else if(codec_id == AV_CODEC_ID_BMP)
447                                                 formatContext->artworkMime = mmfile_strdup("image/bmp");
448                                         else
449                                                 debug_error ("Unknown cover type: 0x%x\n", codec_id);
450
451                                         /*Copy artwork*/
452                                         if (formatContext->artwork)     mmfile_free (formatContext->artwork);
453
454                                         formatContext->artworkSize = pkt.size;
455                                         formatContext->artwork = mmfile_malloc (pkt.size);
456                                         memcpy (formatContext->artwork, pkt.data, pkt.size);
457                                 }
458                         }
459
460                         if(metainfo != NULL) {
461                                 AVDictionaryEntry *tag = NULL;
462                                 while((tag=av_dict_get(metainfo, "", tag, AV_DICT_IGNORE_SUFFIX))) {
463                                         if(tag->key != NULL) {
464                                                 if(!strcasecmp(tag->key, "title")) {
465                                                         if (formatContext->title)       free (formatContext->title);
466                                                         formatContext->title = mmfile_strdup (tag->value);
467                                                 } else if(!strcasecmp(tag->key, "artist")) {
468                                                         if (formatContext->artist)      free (formatContext->artist);
469                                                         formatContext->artist = mmfile_strdup (tag->value);
470                                                 } else if(!strcasecmp(tag->key, "composer")) {
471                                                         if (formatContext->composer)    free (formatContext->composer);
472                                                         formatContext->composer = mmfile_strdup (tag->value);
473                                                 } else if(!strcasecmp(tag->key, "album")) {
474                                                         if (formatContext->album)       free (formatContext->album);
475                                                         formatContext->album = mmfile_strdup (tag->value);
476                                                 } else if(!strcasecmp(tag->key, "copyright")) {
477                                                         if (formatContext->copyright)   free (formatContext->copyright);
478                                                         formatContext->copyright = mmfile_strdup (tag->value);
479                                                 } else if(!strcasecmp(tag->key, "comment")) {
480                                                         if (formatContext->comment)     free (formatContext->comment);
481                                                         formatContext->comment = mmfile_strdup (tag->value);
482                                                 } else if(!strcasecmp(tag->key, "description")) {
483                                                         if (formatContext->description) free (formatContext->description);
484                                                         formatContext->description = mmfile_strdup (tag->value);
485                                                 } else if(!strcasecmp(tag->key, "genre")) {
486                                                         if (formatContext->genre)       free (formatContext->genre);
487                                                         formatContext->genre = mmfile_strdup (tag->value);
488                                                 } else if(!strcasecmp(tag->key, "date")) {
489                                                         if (formatContext->year)        free (formatContext->year);
490                                                         formatContext->year = mmfile_strdup (tag->value);
491                                                 } else if((!strcasecmp(tag->key, "track")) || (!strcasecmp(tag->key, "tracknumber"))) {
492                                                         if (formatContext->tagTrackNum) free (formatContext->tagTrackNum);
493                                                         formatContext->tagTrackNum = mmfile_strdup (tag->value);
494                                                 } else if(!strcasecmp(tag->key, "lyrics")) {
495                                                         if (formatContext->unsyncLyrics)        free (formatContext->unsyncLyrics);
496                                                         formatContext->unsyncLyrics= mmfile_strdup (tag->value);
497                                                 } else if(!strcasecmp(tag->key, "rotate")) {    //can be "90", "180", "270"
498                                                         if (formatContext->rotate)      free (formatContext->rotate);
499                                                         formatContext->rotate= mmfile_strdup (tag->value);
500                                                 } else if(!strcasecmp(tag->key, "metadata_block_picture")) {
501                                                         gsize len = 0;
502                                                         guchar *meta_data = NULL;
503
504                                                         meta_data = g_base64_decode(tag->value, &len);
505                                                         if (meta_data != NULL) {
506                                                                 /* in METADATA_BLOCK_PICTURE,
507                                                                 the length of mime type and  the length of description are flexible,
508                                                                 so, we have to get the length of their for getting correct postion of picture data. */
509                                                                 int mime_len = 0;
510                                                                 int description_len = 0;
511                                                                 int data_len = 0;
512                                                                 int current_pos = 0;
513                                                                 unsigned char current_data[DATA_LENGTH] = {0};
514
515                                                                 /* get length of mime_type */
516                                                                 memcpy(current_data, meta_data + POS_OF_MIME_LEN, DATA_LENGTH);
517                                                                 CONVERT_TO_INT(mime_len, current_data);
518
519                                                                 /* get length of description */
520                                                                 current_pos =  mime_len + (DATA_LENGTH * 2); /*current position is length of description */
521                                                                 memcpy(current_data, meta_data + current_pos, DATA_LENGTH);
522                                                                 CONVERT_TO_INT(description_len, current_data);
523
524                                                                 /* get length of picture data */
525                                                                 current_pos = mime_len  + description_len + (DATA_LENGTH * 7); /*current position is length of picture data */
526                                                                 memcpy(current_data, meta_data + current_pos, DATA_LENGTH);
527                                                                 CONVERT_TO_INT(data_len, current_data);
528
529                                                                 /* set the size of art work */
530                                                                 formatContext->artworkSize = data_len;
531
532                                                                 /* set mime type */
533                                                                 current_pos = POS_OF_MIME_LEN + DATA_LENGTH; /*current position is mime type */
534                                                                 if (formatContext->artworkMime) mmfile_free (formatContext->artworkMime);
535                                                                 formatContext->artworkMime = strndup((const char*)meta_data + current_pos, mime_len);
536
537                                                                 /* set art work data */
538                                                                 current_pos = mime_len  + description_len + (DATA_LENGTH * 8); /*current position is picture data */
539                                                                 if (formatContext->artwork) mmfile_free (formatContext->artwork);
540                                                                 formatContext->artwork = mmfile_malloc (data_len);
541                                                                 memcpy(formatContext->artwork, meta_data + current_pos, data_len);
542
543                                                                 g_free(meta_data);
544                                                         }
545                                                 } else {
546                                                         debug_log("Not support metadata. [%s:%s]", tag->key, tag->value);
547                                                 }
548                                         }
549                                 }
550                         }
551 #ifdef  __MMFILE_TEST_MODE__
552                         mmfile_format_print_tags (formatContext);
553 #endif
554                 }
555         }
556 #else
557         if (pFormatCtx->title[0])               {
558                 if (formatContext->title)
559                         free (formatContext->title);
560                 formatContext->title = mmfile_strdup (pFormatCtx->title);
561         }
562         if (pFormatCtx->author[0]){
563                 if (formatContext->author)
564                         free (formatContext->author);
565                 formatContext->author = mmfile_strdup (pFormatCtx->author);
566         }
567         if (pFormatCtx->copyright[0])   {
568                 if (formatContext->copyright)
569                         free (formatContext->copyright);
570                 formatContext->copyright = mmfile_strdup (pFormatCtx->copyright);
571         }
572         if (pFormatCtx->comment[0])             {
573                 if (formatContext->comment)
574                         free (formatContext->comment);
575                 formatContext->comment = mmfile_strdup (pFormatCtx->comment);
576         }
577         if (pFormatCtx->album[0])               {
578                 if (formatContext->album)
579                         free (formatContext->album);
580                 formatContext->album = mmfile_strdup (pFormatCtx->album);
581         }
582         if (pFormatCtx->genre[0])               {
583                 if (formatContext->genre)
584                         free (formatContext->genre);
585                 formatContext->genre = mmfile_strdup (pFormatCtx->genre);
586         }
587
588         if (pFormatCtx->year) {
589                 char year[10] = {0,};
590                 snprintf (year, 10, "%d", pFormatCtx->year);
591                 year[9] = '\0';
592                 if (formatContext->year)
593                         free (formatContext->year);
594                 formatContext->year = mmfile_strdup (year);
595         }
596
597         if (pFormatCtx->track) {
598                 char tracknum[10] = {0,};
599                 snprintf (tracknum, 10, "%d", pFormatCtx->track);
600                 tracknum[9] = '\0';
601                 if (formatContext->tagTrackNum)
602                         free (formatContext->tagTrackNum);
603                 formatContext->tagTrackNum = mmfile_strdup (tracknum);
604         }
605 #endif
606
607         return MMFILE_FORMAT_SUCCESS;
608 }
609
610
611 EXPORT_API
612 int mmfile_format_read_frame_ffmpg  (MMFileFormatContext *formatContext, unsigned int timestamp, MMFileFormatFrame *frame)
613 {
614         AVFormatContext *pFormatCtx = NULL;
615         AVCodecContext  *pVideoCodecCtx = NULL;
616         AVCodec                 *pVideoCodec = NULL;
617         AVFrame                 *pFrame = NULL;
618         AVFrame                 *pFrameRGB = NULL;
619
620         int width;
621         int height;
622         int numBytes = 0;
623         int ret = 0;
624
625         if (NULL == formatContext ||
626                 NULL == frame ||
627                 NULL == formatContext->privateFormatData ||
628                 formatContext->videoTotalTrackNum <= 0) {
629
630                 debug_error ("invalid param\n");
631                 return MMFILE_FORMAT_FAIL;
632         }
633
634         pFormatCtx = formatContext->privateFormatData;
635
636         if (formatContext->videoStreamId != -1) {
637                 pVideoCodecCtx = pFormatCtx->streams[formatContext->videoStreamId]->codec;
638                 if (NULL == pVideoCodecCtx) {
639                         debug_error ("invalid param\n");
640                         return MMFILE_FORMAT_FAIL;
641                 }
642
643                 pVideoCodec = avcodec_find_decoder (pVideoCodecCtx->codec_id);
644                 if ( NULL == pVideoCodec ) {
645                         debug_error ("error: avcodec_find_decoder failed\n");
646                         return MMFILE_FORMAT_FAIL;
647                 }
648
649                 #ifdef __MMFILE_TEST_MODE__
650                 debug_msg ("flag: 0x%08X\n", pVideoCodec->capabilities);
651                 // debug_msg ("  DRAW_HORIZ_BAND : %d\n", pVideoCodec->capabilities & CODEC_CAP_DRAW_HORIZ_BAND ? 1 : 0);
652                 // debug_msg ("  DR1             : %d\n", pVideoCodec->capabilities & CODEC_CAP_DR1 ? 1 : 0);
653                 // debug_msg ("  PARSE_ONLY      : %d\n", pVideoCodec->capabilities & CODEC_CAP_PARSE_ONLY ? 1 : 0);
654                 // debug_msg ("  TRUNCATED       : %d\n", pVideoCodec->capabilities & CODEC_CAP_TRUNCATED ? 1 : 0);
655                 // debug_msg ("  HWACCEL         : %d\n", pVideoCodec->capabilities & CODEC_CAP_HWACCEL ? 1 : 0);
656                 // debug_msg ("  DELAY           : %d\n", pVideoCodec->capabilities & CODEC_CAP_DELAY ? 1 : 0);
657                 // debug_msg ("  SMALL_LAST_FRAME: %d\n", pVideoCodec->capabilities & CODEC_CAP_SMALL_LAST_FRAME ? 1 : 0);
658                 // debug_msg ("  HWACCEL_VDPAU   : %d\n", pVideoCodec->capabilities & CODEC_CAP_HWACCEL_VDPAU ? 1 : 0);
659                 #endif
660
661                 if (pVideoCodec->capabilities & CODEC_CAP_TRUNCATED) {
662                         pVideoCodecCtx->flags |= CODEC_FLAG_TRUNCATED;
663                 }
664
665                 /*set workaround bug flag*/
666                 pVideoCodecCtx->workaround_bugs = FF_BUG_AUTODETECT;
667 #ifdef __MMFILE_FFMPEG_V100__
668                 ret = avcodec_open2 (pVideoCodecCtx, pVideoCodec, NULL);
669 #else
670                 ret = avcodec_open (pVideoCodecCtx, pVideoCodec);
671 #endif
672                 if (ret < 0) {
673                         debug_error ("error: avcodec_open fail.\n");
674                         return MMFILE_FORMAT_FAIL;
675                 }
676
677                 pFrameRGB = avcodec_alloc_frame ();
678
679                 if (!pFrameRGB) {
680                         debug_error ("error: pFrame or pFrameRGB is NULL\n");
681                         ret = MMFILE_FORMAT_FAIL;
682                         goto exception;
683                 }
684
685                 /* search & decode */
686                 // seek_ts = formatContext->duration > _SHORT_MEDIA_LIMIT ? seek_ts : 0;        /*if short media, seek first key frame*/
687                 ret = _get_first_good_video_frame (pFormatCtx, pVideoCodecCtx, formatContext->videoStreamId, &pFrame);
688                 if ( ret != MMFILE_FORMAT_SUCCESS ) {
689                         debug_error ("error: get key frame\n");
690                         ret = MMFILE_FORMAT_FAIL;
691                         goto exception;
692                 }
693
694                 #ifdef __MMFILE_TEST_MODE__
695                 debug_msg ("Video default resolution = [%dx%d]\n", pVideoCodecCtx->coded_width, pVideoCodecCtx->coded_height);
696                 debug_msg ("Video coded resolution = [%dx%d]\n", pVideoCodecCtx->width, pVideoCodecCtx->height);
697                 #endif
698
699                 /*sometimes, ffmpeg's width/height is wrong*/
700                 #if 0   /*coded_width/height sometimes wrong. so use width/height*/
701                 width = pVideoCodecCtx->coded_width == 0 ? pVideoCodecCtx->width : pVideoCodecCtx->coded_width;
702                 height = pVideoCodecCtx->coded_height == 0 ? pVideoCodecCtx->height : pVideoCodecCtx->coded_height;
703                 #endif
704                 if((pVideoCodecCtx->width == 0) || (pVideoCodecCtx->height == 0)) {
705                         width = pVideoCodecCtx->coded_width;
706                         height = pVideoCodecCtx->coded_height;
707                 } else {
708                         width = pVideoCodecCtx->width;
709                         height = pVideoCodecCtx->height;
710                 }
711
712                 numBytes = avpicture_get_size(PIX_FMT_RGB24, width, height);
713                 if (numBytes < 0) {
714                         debug_error ("error: avpicture_get_size. [%d x %d]\n", width, height);
715                         ret = MMFILE_FORMAT_FAIL;
716                         goto exception;
717                 }
718
719                 frame->frameData = mmfile_malloc (numBytes);
720                 if (NULL == frame->frameData) {
721                         debug_error ("error: avpicture_get_size. [%d]\n", numBytes);
722                         ret = MMFILE_FORMAT_FAIL;
723                         goto exception;
724                 }
725
726                 ret = avpicture_fill ((AVPicture *)pFrameRGB, frame->frameData, PIX_FMT_RGB24, width, height);
727                 if (ret < 0) {
728                         debug_error ("error: avpicture_fill fail. errcode = 0x%08X\n", ret);
729                         ret = MMFILE_FORMAT_FAIL;
730                         goto exception;
731                 }
732
733 #ifdef __MMFILE_FFMPEG_V085__
734                 struct SwsContext *img_convert_ctx = NULL;
735
736                 img_convert_ctx = sws_getContext (width, height, pVideoCodecCtx->pix_fmt,
737                                           width, height, PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
738
739                 if (NULL == img_convert_ctx) {
740                         debug_error ("failed to get img convet ctx\n");
741                         ret = MMFILE_FORMAT_FAIL;
742                         goto exception;
743                 }
744
745                 ret = sws_scale (img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize,
746                      0, height, pFrameRGB->data, pFrameRGB->linesize);
747                 if ( ret < 0 ) {
748                         debug_error ("failed to convet image\n");
749                         ret = MMFILE_FORMAT_FAIL;
750                         sws_freeContext(img_convert_ctx);
751                         img_convert_ctx = NULL;
752                         goto exception;
753                 }
754
755                 sws_freeContext(img_convert_ctx);
756                 img_convert_ctx = NULL;
757 #else
758                 ret = img_convert ((AVPicture *)pFrameRGB, PIX_FMT_RGB24, (AVPicture*)pFrame, pVideoCodecCtx->pix_fmt, width, height);
759                 if ( ret < 0 ) {
760                         debug_error ("failed to convet image\n");
761                         ret = MMFILE_FORMAT_FAIL;
762                         goto exception;
763                 }
764 #endif
765                 frame->frameSize = numBytes;
766                 frame->frameWidth = width;
767                 frame->frameHeight = height;
768                 frame->configLenth = 0;
769                 frame->bCompressed = 0; /* false */
770
771                 if (pFrame)                     av_free (pFrame);
772                 if (pFrameRGB)          av_free (pFrameRGB);
773
774                 avcodec_close(pVideoCodecCtx);
775
776                 return MMFILE_FORMAT_SUCCESS;
777         }
778
779
780 exception:
781         if (pVideoCodecCtx)             avcodec_close (pVideoCodecCtx);
782         if (frame->frameData)   { mmfile_free (frame->frameData); frame->frameData = NULL; }
783         if (pFrame)                             av_free (pFrame);
784         if (pFrameRGB)                  av_free (pFrameRGB);
785         return ret;
786 }
787
788
789 EXPORT_API
790 int mmfile_format_close_ffmpg(MMFileFormatContext *formatContext)
791 {
792         if (formatContext) {
793                 AVFormatContext *pFormatCtx = formatContext->privateFormatData;
794
795                 if (pFormatCtx) {
796 #ifdef __MMFILE_FFMPEG_V100__
797                         avformat_close_input(&pFormatCtx);
798 #else
799                         av_close_input_file (pFormatCtx);
800 #endif
801                         formatContext->privateFormatData = NULL;
802                 }
803         }
804
805         return MMFILE_FORMAT_SUCCESS;
806 }
807
808 /**
809  * return average of difference
810  */
811 static unsigned int _diff_memory (const void *s1, const void *s2, unsigned int n)
812 {
813         char *s = (char *)s1;
814         char *d = (char *)s2;
815         int i;
816         int ret;
817         int tmp;
818
819         for (i = 0, ret = 0; i < n; i++) {
820                 if (*s++ != *d++) {
821                         tmp = (*s - *d);
822                         ret += (tmp < 0 ? -tmp : tmp);
823                 }
824         }
825         ret /= n;
826         return ret;
827 }
828
829 int64_t gettime(void)
830 {
831         struct timeval tv;
832         gettimeofday(&tv,NULL);
833         return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
834 }
835
836
837 #define IS_GOOD_OLD_METHOD
838 #ifdef IS_GOOD_OLD_METHOD
839 /**
840  * compare with center line.
841  */
842 static int _is_good_pgm (unsigned char *buf, int wrap, int xsize, int ysize)
843 {
844 #define _MM_CHUNK_NUM                   8                                               /*FIXME*/
845 #define _MM_CHUNK_LIMIT                 (_MM_CHUNK_NUM / 2)
846 #define _MM_CHUNK_DIFF_LIMIT    (_MM_CHUNK_LIMIT * 2 + 1)       /*FIXME*/
847
848         int i;
849         int step;
850         int point;
851         unsigned char *cnt;             /*center line of image*/
852         int is_different;
853         unsigned int sum_diff;
854         int cnt_offset;
855
856         /*set center line*/
857         step = ysize / _MM_CHUNK_NUM;
858         cnt_offset = (ysize / 2);
859         cnt = buf + cnt_offset * wrap;
860
861         #ifdef __MMFILE_TEST_MODE__
862         debug_msg ("checking frame. %p, %d, %d, %d\n", buf, wrap, xsize, ysize);
863         #endif
864
865         /*if too small, always ok return.*/
866         if (ysize < _MM_CHUNK_NUM)
867                 return 1;
868
869         for (point = 0, sum_diff = 0, i = step; i < ysize; i += step) {
870                 if (i != cnt_offset) {
871                 
872                         /*binary compare*/
873                         is_different = _diff_memory (cnt, buf + i * wrap, xsize);
874                         point += (is_different == 0 ? 0 : 1);
875                         sum_diff += is_different;
876
877                         #ifdef __MMFILE_TEST_MODE__
878                         debug_msg ("check %04d line. %s [%d]\n", i, (is_different == 0 ? "same" : "different"), is_different);
879                         #endif
880
881                         if (point >= _MM_CHUNK_LIMIT) {
882                                 if (sum_diff > _MM_CHUNK_DIFF_LIMIT) {
883                                         #ifdef __MMFILE_TEST_MODE__
884                                         debug_msg ("Good :-)\n");
885                                         #endif
886                                         return 1;
887                                 } else {
888                                         #ifdef __MMFILE_TEST_MODE__
889                                         debug_msg ("Bad :-(\n");
890                                         #endif
891                                         return 0;
892                                 }
893                         }
894                 }
895
896         }
897         return 0;
898 }
899 #else // IS_GOOD_OLD_METHOD 
900 /* ToDo : for enhancement */
901 #endif // IS_GOOD_OLD_METHOD
902
903
904
905 static int
906 _get_video_fps (int frame_cnt, int duration, AVRational r_frame_rate, int is_roundup)
907 {
908         double fps, round;
909
910         #ifdef __MMFILE_TEST_MODE__
911         debug_msg ("frame count: %d, dur: %d, num: %d, den: %d\n", frame_cnt, duration, r_frame_rate.num, r_frame_rate.den)
912         #endif
913
914         if (duration <= 0 || r_frame_rate.num <= 0 || r_frame_rate.den <= 0)
915                 return 0;
916
917         round = (is_roundup != 0 ? 0.50f : 0.00f);
918
919         fps = (double)frame_cnt / ((double)(duration * r_frame_rate.num) / r_frame_rate.den) + round;
920
921         return (int)fps;
922 }
923
924 #ifdef MMFILE_FORMAT_DEBUG_DUMP
925 static void _save_pgm (unsigned char *buf,int wrap, int xsize,int ysize,char *filename)
926 {
927         FILE *f;
928         int i;
929
930         f = fopen(filename,"w");
931         if (f) {
932                 fprintf (f,"P5\n%d %d\n%d\n",xsize,ysize,255);
933                 for (i = 0; i < ysize; i++)
934                         fwrite (buf + i * wrap, 1, xsize, f);
935                 fclose (f);
936         }
937 }
938 #endif
939
940 #ifdef __MMFILE_TEST_MODE__
941 static void _dump_av_packet (AVPacket *pkt)
942 {
943         debug_msg ("--------- AV Packet -----------\n");
944         debug_msg (" pts: %lld\n", pkt->pts);
945         debug_msg (" dts: %lld\n", pkt->dts);
946         debug_msg (" data: %p\n", pkt->data);
947         debug_msg (" size: %d\n", pkt->size);
948         debug_msg (" stream_index: %d\n", pkt->stream_index);
949 #ifdef __MMFILE_FFMPEG_V085__   
950         debug_msg (" flags: 0x%08X, %s\n", pkt->flags, (pkt->flags & AV_PKT_FLAG_KEY) ? "Keyframe" : "_");
951 #else
952         debug_msg (" flags: 0x%08X, %s\n", pkt->flags, (pkt->flags & PKT_FLAG_KEY) ? "Keyframe" : "_");
953 #endif
954         debug_msg (" duration: %d\n", pkt->duration);
955         debug_msg (" destruct: %p\n", pkt->destruct);
956         debug_msg (" priv: %p\n", pkt->priv);
957         debug_msg (" pos: %lld\n", pkt->pos);
958         debug_msg (" convergence_duration: %lld\n", pkt->convergence_duration);
959         debug_msg ("-------------------------------\n");
960 }
961 #endif
962
963 static int _get_first_good_video_frame (AVFormatContext *pFormatCtx, AVCodecContext *pCodecCtx, int videoStream, AVFrame **pFrame)
964 {
965         // AVStream *st = NULL;
966         AVPacket pkt;
967
968         AVFrame *frame = NULL;
969         AVFrame *tmp_frame = NULL;
970         AVFrame *first_frame = NULL;
971
972         // long long timestamp;
973         int stream_id = videoStream;
974         int ret;
975         int found = 0;
976         int i,v, len, got_picture;
977         int retry = 0;
978         int key_detected;
979 #ifdef MMFILE_FORMAT_DEBUG_DUMP
980         char pgm_name[256] = {0,};
981 #endif
982
983 #define _RETRY_SEARCH_LIMIT             150
984 #define _KEY_SEARCH_LIMIT               (_RETRY_SEARCH_LIMIT*2)         /*2 = 1 read. some frame need to read one more*/
985 #define _FRAME_SEARCH_LIMIT             1000
986
987         first_frame = avcodec_alloc_frame ();
988         tmp_frame = avcodec_alloc_frame ();
989
990         if (!first_frame || !tmp_frame) {
991                 debug_error ("failed to alloc frame.\n");
992                 if (first_frame) av_free (first_frame);
993                 if (tmp_frame) av_free (tmp_frame);
994                 return MMFILE_FORMAT_FAIL;
995         }
996
997         #ifdef __MMFILE_TEST_MODE__
998         debug_msg ("frame: 1. %p, 2. %p\n", first_frame, tmp_frame);
999         #endif
1000
1001 #ifdef __MMFILE_FFMPEG_V085__
1002         pCodecCtx->skip_frame = AVDISCARD_BIDIR;
1003 #else
1004         pCodecCtx->hurry_up = 1;
1005 #endif
1006
1007         for(i = 0, v = 0, key_detected = 0, frame = first_frame; i < _KEY_SEARCH_LIMIT && v < _FRAME_SEARCH_LIMIT;) {
1008                 av_init_packet (&pkt);
1009                 got_picture = 0;
1010
1011                 ret = av_read_frame (pFormatCtx, &pkt);
1012                 if (ret < 0) {
1013                         debug_error ("read failed. (maybe EOF or broken)\n");
1014                         break;
1015                 } else {
1016                         if (pkt.stream_index == stream_id) {
1017                                 v++;
1018 #ifdef __MMFILE_FFMPEG_V085__                           
1019                                 if ((pkt.flags & AV_PKT_FLAG_KEY ) || (key_detected == 1)) 
1020 #else
1021                                 if ((pkt.flags & PKT_FLAG_KEY ) || (key_detected == 1)) 
1022 #endif
1023                                 {
1024                                         #ifdef __MMFILE_TEST_MODE__
1025                                         debug_msg ("video frame: %d, %d, %d\n", retry, i, v);
1026                                         _dump_av_packet (&pkt);
1027                                         #endif
1028
1029                                         i++;
1030                                         key_detected = 0;
1031 #ifdef __MMFILE_FFMPEG_V085__
1032                                         len = avcodec_decode_video2 (pCodecCtx, frame, &got_picture, &pkt);
1033 #else
1034                                         len = avcodec_decode_video (pCodecCtx, frame, &got_picture, pkt.data, pkt.size);
1035 #endif
1036                                         if (len < 0) {
1037                                                 debug_warning ("Error while decoding frame %dth\n", i);
1038                                         } else if (got_picture) {
1039                                                 if (frame->key_frame) {
1040                                                         #ifdef __MMFILE_TEST_MODE__
1041                                                         debug_msg ("key frame!\n");
1042                                                         #endif
1043                                                         #ifdef MMFILE_FORMAT_DEBUG_DUMP
1044                                                         sprintf (pgm_name, "./key_%d_%d_%d.pgm", retry, i, v);
1045                                                         _save_pgm (frame->data[0], frame->linesize[0], pCodecCtx->width, pCodecCtx->height, pgm_name);
1046                                                         #endif
1047
1048                                                         found++;
1049
1050                                                         #ifdef __MMFILE_TEST_MODE__
1051                                                         int64_t ti;
1052                                                         ti = gettime();
1053                                                         #endif
1054                                                         ret = _is_good_pgm (frame->data[0], frame->linesize[0], pCodecCtx->width, pCodecCtx->height);
1055                                                         #ifdef __MMFILE_TEST_MODE__
1056                                                         ti = gettime() - ti;
1057                                                         debug_msg ("Elapsed time = %lld\n", ti);
1058                                                         #endif
1059                                                         if (ret != 0) {
1060                                                                 #ifdef __MMFILE_TEST_MODE__
1061                                                                 debug_msg ("is good frame.\n");
1062                                                                 #endif
1063                                                                 break;
1064                                                         } else {
1065                                                                 /*reset video frame count & retry searching*/
1066                                                                 debug_warning ("not good fame. retry scanning.\n");
1067                                                                 i = 0;
1068                                                                 v = 0;
1069                                                                 retry++;
1070                                                         }
1071
1072                                                         /*set buffer frame*/
1073                                                         frame = tmp_frame;
1074
1075                                                         /*limit of retry.*/
1076                                                         if (retry > _RETRY_SEARCH_LIMIT)        break;
1077
1078                                                 } else {
1079                                                         #ifdef __MMFILE_TEST_MODE__
1080                                                         debug_msg ("skip (not key frame).\n");
1081                                                         #endif
1082                                                         #ifdef MMFILE_FORMAT_DEBUG_DUMP
1083                                                         sprintf (pgm_name, "./not_key_%d_%d_%d.pgm", retry, i, v);
1084                                                         _save_pgm (frame->data[0], frame->linesize[0], pCodecCtx->width, pCodecCtx->height, pgm_name);
1085                                                         #endif
1086                                                 }
1087                                         } else {
1088                                                 #ifdef __MMFILE_TEST_MODE__
1089                                                 debug_msg ("decode not completed.\n");
1090                                                 #endif
1091                                                 key_detected = 1;
1092                                         }
1093                                 }
1094                         }
1095                 }
1096                 av_free_packet (&pkt);
1097         }
1098
1099         /*free pkt after loop breaking*/
1100         if (pkt.data) av_free_packet (&pkt);
1101
1102         #ifdef __MMFILE_TEST_MODE__
1103         debug_msg ("found: %d, retry: %d\n", found, retry);
1104         #endif
1105
1106         /*set decode frame to output*/
1107         if (found > 0) {
1108                 ret = MMFILE_FORMAT_SUCCESS;
1109                 if (retry == 0 || found == retry) {
1110                         *pFrame = first_frame;
1111                         if (tmp_frame) av_free (tmp_frame);
1112                 } else {
1113                         *pFrame = tmp_frame;
1114                         if (first_frame) av_free (first_frame);
1115                 }
1116         } else {
1117                 ret = MMFILE_FORMAT_FAIL;
1118                 if (first_frame) av_free (first_frame);
1119                 if (tmp_frame) av_free (tmp_frame);
1120         }
1121
1122         #ifdef __MMFILE_TEST_MODE__
1123         debug_msg ("out frame: %p\n", *pFrame);
1124         #endif
1125
1126 #ifdef __MMFILE_FFMPEG_V085__
1127         pCodecCtx->skip_frame = AVDISCARD_NONE;
1128 #else
1129         pCodecCtx->hurry_up = 0;
1130 #endif
1131
1132         return ret;
1133 }
1134
1135 static int ConvertVideoCodecEnum (int AVVideoCodecID)
1136 {
1137         int ret_codecid = 0;
1138
1139         switch (AVVideoCodecID)
1140         {
1141                 case AV_CODEC_ID_NONE:
1142                         ret_codecid = MM_VIDEO_CODEC_NONE;
1143                         break;
1144                 case AV_CODEC_ID_MPEG1VIDEO:
1145                         ret_codecid = MM_VIDEO_CODEC_MPEG1;
1146                         break;
1147                 case AV_CODEC_ID_MPEG2VIDEO:  ///< preferred ID for MPEG-1/2 video decoding
1148                         ret_codecid = MM_VIDEO_CODEC_MPEG2;
1149                         break;
1150                 case AV_CODEC_ID_MPEG2VIDEO_XVMC:
1151                         ret_codecid = MM_VIDEO_CODEC_MPEG2;
1152                         break;
1153                 case AV_CODEC_ID_H261:
1154                         ret_codecid = MM_VIDEO_CODEC_H261;
1155                         break;
1156                 case AV_CODEC_ID_H263:
1157                         ret_codecid = MM_VIDEO_CODEC_H263;
1158                         break;
1159                 case AV_CODEC_ID_MPEG4:
1160                         ret_codecid = MM_VIDEO_CODEC_MPEG4;
1161                         break;
1162                 case AV_CODEC_ID_MSMPEG4V1:
1163                         ret_codecid = MM_VIDEO_CODEC_MPEG4;
1164                         break;
1165                 case AV_CODEC_ID_MSMPEG4V2:
1166                         ret_codecid = MM_VIDEO_CODEC_MPEG4;
1167                         break;
1168                 case AV_CODEC_ID_MSMPEG4V3:
1169                         ret_codecid = MM_VIDEO_CODEC_MPEG4;
1170                         break;
1171                 case AV_CODEC_ID_WMV1:
1172                         ret_codecid = MM_VIDEO_CODEC_WMV;
1173                         break;
1174                 case AV_CODEC_ID_WMV2:
1175                         ret_codecid = MM_VIDEO_CODEC_WMV;
1176                         break;
1177                 case AV_CODEC_ID_H263P:
1178                         ret_codecid = MM_VIDEO_CODEC_H263;
1179                         break;
1180                 case AV_CODEC_ID_H263I:
1181                         ret_codecid = MM_VIDEO_CODEC_H263;
1182                         break;
1183                 case AV_CODEC_ID_FLV1:
1184                         ret_codecid = MM_VIDEO_CODEC_FLV;
1185                         break;
1186                 case AV_CODEC_ID_H264:
1187                         ret_codecid = MM_VIDEO_CODEC_H264;
1188                         break;
1189                 case AV_CODEC_ID_INDEO2:
1190                 case AV_CODEC_ID_INDEO3:
1191                 case AV_CODEC_ID_INDEO4:
1192                 case AV_CODEC_ID_INDEO5:
1193                         ret_codecid = MM_VIDEO_CODEC_INDEO;
1194                         break;
1195                 case AV_CODEC_ID_THEORA:
1196                         ret_codecid = MM_VIDEO_CODEC_THEORA;
1197                         break;
1198                 case AV_CODEC_ID_CINEPAK:
1199                         ret_codecid = MM_VIDEO_CODEC_CINEPAK;
1200                         break;
1201 #ifndef __MMFILE_FFMPEG_V085__
1202                 case CODEC_ID_XVID:
1203                         ret_codecid = MM_VIDEO_CODEC_XVID;
1204                         break;
1205 #endif
1206                 case AV_CODEC_ID_VC1:
1207                         ret_codecid = MM_VIDEO_CODEC_VC1;
1208                         break;
1209                 case AV_CODEC_ID_WMV3:
1210                         ret_codecid = MM_VIDEO_CODEC_WMV;
1211                         break;
1212                 case AV_CODEC_ID_AVS:
1213                         ret_codecid = MM_VIDEO_CODEC_AVS;
1214                         break;
1215                 case AV_CODEC_ID_RL2:
1216                         ret_codecid = MM_VIDEO_CODEC_REAL;
1217                         break;
1218                 default:
1219                         ret_codecid = MM_VIDEO_CODEC_NONE;
1220                         break;
1221         }
1222
1223         return ret_codecid;
1224 }
1225
1226
1227 static int ConvertAudioCodecEnum (int AVAudioCodecID)
1228 {
1229         int ret_codecid = 0;
1230
1231         switch (AVAudioCodecID)
1232         {
1233                 case AV_CODEC_ID_AMR_NB:
1234                 case AV_CODEC_ID_AMR_WB:
1235                         ret_codecid = MM_AUDIO_CODEC_AMR;
1236                         break;
1237                 /* RealAudio codecs*/
1238                 case AV_CODEC_ID_RA_144:
1239                 case AV_CODEC_ID_RA_288:
1240                         ret_codecid = MM_AUDIO_CODEC_REAL;
1241                         break;
1242                 case AV_CODEC_ID_MP2:
1243                         ret_codecid = MM_AUDIO_CODEC_MP2;
1244                         break;
1245                 case AV_CODEC_ID_MP3:
1246                 case AV_CODEC_ID_MP3ADU:
1247                 case AV_CODEC_ID_MP3ON4:
1248                         ret_codecid = MM_AUDIO_CODEC_MP3;
1249                         break;
1250                 case AV_CODEC_ID_AAC:
1251                         ret_codecid = MM_AUDIO_CODEC_AAC;
1252                         break;
1253                 case AV_CODEC_ID_AC3:
1254                         ret_codecid = MM_AUDIO_CODEC_AC3;
1255                         break;
1256                 case AV_CODEC_ID_VORBIS:
1257                         ret_codecid = MM_AUDIO_CODEC_VORBIS;
1258                         break;
1259                 case AV_CODEC_ID_WMAV1:
1260                 case AV_CODEC_ID_WMAV2:
1261                 case AV_CODEC_ID_WMAVOICE:
1262                 case AV_CODEC_ID_WMAPRO:
1263                 case AV_CODEC_ID_WMALOSSLESS:
1264                         ret_codecid = MM_AUDIO_CODEC_WMA;
1265                         break;
1266                 case AV_CODEC_ID_FLAC:
1267                         ret_codecid = MM_AUDIO_CODEC_FLAC;
1268                         break;
1269                 case AV_CODEC_ID_ALAC:
1270                         ret_codecid = MM_AUDIO_CODEC_ALAC;
1271                         break;
1272                 case AV_CODEC_ID_WAVPACK:
1273                         ret_codecid = MM_AUDIO_CODEC_WAVE;
1274                         break;
1275                 case AV_CODEC_ID_ATRAC3:
1276                 case AV_CODEC_ID_ATRAC3P:
1277                 case AV_CODEC_ID_EAC3:
1278                         ret_codecid = MM_AUDIO_CODEC_AC3;
1279                         break;
1280                 default:
1281                         ret_codecid = MM_AUDIO_CODEC_NONE;
1282                         break;
1283         }
1284
1285         return ret_codecid;
1286 }
1287
1288
1289
1290 static int getMimeType(int formatId, char *mimeType)
1291 {
1292         int ret = 0;    /*default: success*/
1293
1294         switch(formatId) {
1295                 case MM_FILE_FORMAT_3GP:
1296                 case MM_FILE_FORMAT_MP4:
1297                         sprintf(mimeType,"video/3gpp");
1298                         break;
1299                 case MM_FILE_FORMAT_ASF:
1300                 case MM_FILE_FORMAT_WMA:
1301                 case MM_FILE_FORMAT_WMV:
1302                         sprintf(mimeType,"video/x-ms-asf");
1303                         break;
1304                 case  MM_FILE_FORMAT_AVI:
1305                         sprintf(mimeType,"video/avi");
1306                         break;
1307                 case MM_FILE_FORMAT_OGG:
1308                         sprintf(mimeType,"video/ogg");
1309                         break;
1310                 case MM_FILE_FORMAT_REAL:
1311                         sprintf(mimeType,"video/vnd.rn-realvideo");
1312                         break;
1313                 case MM_FILE_FORMAT_AMR:
1314                         sprintf(mimeType,"audio/AMR");
1315                         break;
1316                 case MM_FILE_FORMAT_AAC:
1317                         sprintf(mimeType,"audio/aac");
1318                         break;
1319                 case MM_FILE_FORMAT_MP3:
1320                         sprintf(mimeType,"audio/mp3");
1321                         break;
1322                 case MM_FILE_FORMAT_AIFF:
1323                 case MM_FILE_FORMAT_WAV:
1324                         sprintf(mimeType,"audio/wave");
1325                         break;
1326                 case MM_FILE_FORMAT_MID:
1327                         sprintf(mimeType,"audio/midi");
1328                         break;
1329                 case MM_FILE_FORMAT_MMF:
1330                         sprintf(mimeType,"audio/mmf");
1331                         break;
1332                 case MM_FILE_FORMAT_DIVX:
1333                         sprintf(mimeType,"video/divx");
1334                         break;
1335                 case MM_FILE_FORMAT_IMELODY:
1336                         sprintf(mimeType,"audio/iMelody");
1337                         break;
1338                 case MM_FILE_FORMAT_JPG:
1339                         sprintf(mimeType,"image/jpeg");
1340                         break;
1341                 case MM_FILE_FORMAT_AU:
1342                         sprintf(mimeType,"audio/basic");
1343                         break;
1344                 case MM_FILE_FORMAT_VOB:
1345                         sprintf(mimeType,"video/mpeg");
1346                         break;
1347                 case MM_FILE_FORMAT_FLV:
1348                         sprintf(mimeType,"video/x-flv");
1349                         break;
1350                 case MM_FILE_FORMAT_QT:
1351                         sprintf(mimeType,"video/quicktime");
1352                         break;
1353                 case MM_FILE_FORMAT_MATROSKA:
1354                         sprintf(mimeType,"video/x-matroska");
1355                         break;
1356                 case MM_FILE_FORMAT_FLAC:
1357                         sprintf(mimeType,"audio/x-flac");
1358                         break;
1359                 default:
1360                         ret = -1;
1361         }
1362
1363         debug_msg ("id: %d, mimetype: %s\n", formatId, mimeType);
1364
1365         return ret;
1366 }
1367
1368