2 * Copyright 2012 Michael Chen <omxcodec@gmail.com>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #define LOG_TAG "FFMPEG"
18 #include <utils/Log.h>
20 #include <utils/Errors.h>
32 #include <limits.h> /* INT_MAX */
34 #include "libavutil/avstring.h"
35 #include "libavutil/colorspace.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/pixdesc.h"
38 #include "libavutil/imgutils.h"
39 #include "libavutil/dict.h"
40 #include "libavutil/parseutils.h"
41 #include "libavutil/samplefmt.h"
42 #include "libavutil/avassert.h"
43 #include "libavutil/intreadwrite.h"
44 #include "libavformat/avformat.h"
45 #include "libavdevice/avdevice.h"
46 #include "libswscale/swscale.h"
47 #include "libavcodec/audioconvert.h"
48 #include "libavutil/opt.h"
49 #include "libavutil/internal.h"
50 #include "libavcodec/avfft.h"
51 #include "libswresample/swresample.h"
62 #include <cutils/properties.h>
64 #include "ffmpeg_utils.h"
65 #include "ffmpeg_source.h"
71 const char program_name[] = "dummy";
72 const int program_birth_year = 2012;
75 static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
76 static int ref_count = 0;
80 //////////////////////////////////////////////////////////////////////////////////
82 //////////////////////////////////////////////////////////////////////////////////
87 void av_noreturn exit_program(int ret)
92 void show_help_default(const char *opt, const char *arg)
101 //////////////////////////////////////////////////////////////////////////////////
103 //////////////////////////////////////////////////////////////////////////////////
104 static void sanitize(uint8_t *line){
106 if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
112 // TODO, remove static variables to support multi-instances
113 void nam_av_log_callback(void* ptr, int level, const char* fmt, va_list vl)
115 static int print_prefix = 1;
117 static char prev[1024];
121 if (level > av_log_get_level())
123 av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix);
125 if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev)){
130 LOGI("Last message repeated %d times\n", count);
134 sanitize((uint8_t *)line);
139 #define LOG_BUF_SIZE 1024
140 static char g_msg[LOG_BUF_SIZE];
141 static int g_msg_len = 0;
143 int saw_lf, check_len;
146 check_len = g_msg_len + strlen(line) + 1;
147 if (check_len <= LOG_BUF_SIZE) {
148 /* lf: Line feed ('\n') */
149 saw_lf = (strchr(line, '\n') != NULL) ? 1 : 0;
150 strncpy(g_msg + g_msg_len, line, strlen(line));
151 g_msg_len += strlen(line);
156 /* attach the line feed */
158 g_msg[g_msg_len] = '\n';
161 /* trace is fragmented */
163 g_msg[g_msg_len] = '\n';
166 /* reset g_msg and g_msg_len */
167 memset(g_msg, 0, LOG_BUF_SIZE);
169 } while (check_len > LOG_BUF_SIZE);
173 void nam_av_log_set_flags(int arg)
179 const struct { const char *name; int level; } log_levels[] = {
180 { "quiet" , AV_LOG_QUIET },
181 { "panic" , AV_LOG_PANIC },
182 { "fatal" , AV_LOG_FATAL },
183 { "error" , AV_LOG_ERROR },
184 { "warning", AV_LOG_WARNING },
185 { "info" , AV_LOG_INFO },
186 { "verbose", AV_LOG_VERBOSE },
187 { "debug" , AV_LOG_DEBUG },
190 #define AV_LOG_QUIET -8
191 #define AV_LOG_PANIC 0
192 #define AV_LOG_FATAL 8
193 #define AV_LOG_ERROR 16
194 #define AV_LOG_WARNING 24
195 #define AV_LOG_INFO 32
196 #define AV_LOG_VERBOSE 40
197 #define AV_LOG_DEBUG 48
200 //////////////////////////////////////////////////////////////////////////////////
201 // constructor and destructor
202 //////////////////////////////////////////////////////////////////////////////////
203 /* Mutex manager callback. */
204 static int lockmgr(void **mtx, enum AVLockOp op)
208 *mtx = (void *)av_malloc(sizeof(pthread_mutex_t));
211 return !!pthread_mutex_init((pthread_mutex_t *)(*mtx), NULL);
213 return !!pthread_mutex_lock((pthread_mutex_t *)(*mtx));
214 case AV_LOCK_RELEASE:
215 return !!pthread_mutex_unlock((pthread_mutex_t *)(*mtx));
216 case AV_LOCK_DESTROY:
217 pthread_mutex_destroy((pthread_mutex_t *)(*mtx));
225 * To debug ffmpeg", type this command on the console before starting playback:
226 * setprop debug.nam.ffmpeg 1
227 * To disable the debug, type:
228 * setprop debug.nam.ffmpge 0
230 status_t initFFmpeg()
233 bool debug_enabled = false;
234 char value[PROPERTY_VALUE_MAX];
236 pthread_mutex_lock(&init_mutex);
238 if (property_get("debug.nam.ffmpeg", value, NULL)
239 && (!strcmp(value, "1") || !av_strcasecmp(value, "true"))) {
240 LOGI("set ffmpeg debug level to AV_LOG_DEBUG");
241 debug_enabled = true;
244 av_log_set_level(AV_LOG_DEBUG);
246 av_log_set_level(AV_LOG_INFO);
249 nam_av_log_set_flags(AV_LOG_SKIP_REPEATED);
250 av_log_set_callback(nam_av_log_callback);
252 /* register all codecs, demux and protocols */
253 avcodec_register_all();
255 avdevice_register_all();
258 avformat_network_init();
260 /* register android source */
261 ffmpeg_register_android_source();
265 if (av_lockmgr_register(lockmgr)) {
266 LOGE("could not initialize lock manager!");
274 pthread_mutex_unlock(&init_mutex);
281 pthread_mutex_lock(&init_mutex);
287 av_lockmgr_register(NULL);
289 avformat_network_deinit();
292 pthread_mutex_unlock(&init_mutex);
295 //////////////////////////////////////////////////////////////////////////////////
297 //////////////////////////////////////////////////////////////////////////////////
298 /* H.264 bitstream with start codes, NOT AVC1! ref: libavcodec/h264_parser.c */
299 static int h264_split(AVCodecContext *avctx,
300 const uint8_t *buf, int buf_size, int check_compatible_only)
307 //av_hex_dump(stderr, buf, 100);
309 for(i=0; i<=buf_size; i++){
310 if((state&0xFFFFFF1F) == 0x107) {
311 LOGI("found NAL_SPS");
314 if((state&0xFFFFFF1F) == 0x108) {
315 LOGI("found NAL_PPS");
317 if (check_compatible_only)
318 return (has_sps & has_pps);
320 if((state&0xFFFFFF00) == 0x100 && ((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105)){
322 while(i>4 && buf[i-5]==0) i--;
327 state= (state<<8) | buf[i];
332 /* ref: libavcodec/mpegvideo_parser.c */
333 static int mpegvideo_split(AVCodecContext *avctx,
334 const uint8_t *buf, int buf_size, int check_compatible_only)
340 for(i=0; i<buf_size; i++){
341 state= (state<<8) | buf[i];
344 }else if(found && state != 0x1B5 && state < 0x200 && state >= 0x100)
350 /* split extradata from buf for Android OMXCodec */
351 int parser_split(AVCodecContext *avctx,
352 const uint8_t *buf, int buf_size)
354 if (!avctx || !buf || buf_size <= 0) {
355 LOGE("parser split, valid params");
359 if (avctx->codec_id == CODEC_ID_H264) {
360 return h264_split(avctx, buf, buf_size, 0);
361 } else if (avctx->codec_id == CODEC_ID_MPEG2VIDEO ||
362 avctx->codec_id == CODEC_ID_MPEG4) {
363 return mpegvideo_split(avctx, buf, buf_size, 0);
365 LOGE("parser split, unsupport the codec, id: 0x%0x", avctx->codec_id);
371 int is_extradata_compatible_with_android(AVCodecContext *avctx)
373 if (avctx->extradata_size <= 0) {
374 LOGI("extradata_size <= 0, extradata is not compatible with android decoder, the codec id: 0x%0x", avctx->codec_id);
378 if (avctx->codec_id == CODEC_ID_H264 && avctx->extradata[0] != 1 /* configurationVersion */) {
380 return !!(h264_split(avctx, avctx->extradata, avctx->extradata_size, 1) > 0);
383 return !!(avctx->extradata_size > 0);
387 } // namespace android