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>
68 const char program_name[] = "dummy";
69 const int program_birth_year = 2012;
72 static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
73 static int ref_count = 0;
77 //////////////////////////////////////////////////////////////////////////////////
79 //////////////////////////////////////////////////////////////////////////////////
84 void av_noreturn exit_program(int ret)
89 void show_help_default(const char *opt, const char *arg)
98 //////////////////////////////////////////////////////////////////////////////////
100 //////////////////////////////////////////////////////////////////////////////////
101 static void sanitize(uint8_t *line){
103 if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
109 // TODO, remove static variables to support multi-instances
110 void nam_av_log_callback(void* ptr, int level, const char* fmt, va_list vl)
112 static int print_prefix = 1;
114 static char prev[1024];
118 if (level > av_log_get_level())
120 av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix);
122 if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev)){
127 LOGI("Last message repeated %d times\n", count);
131 sanitize((uint8_t *)line);
136 #define LOG_BUF_SIZE 1024
137 static char g_msg[LOG_BUF_SIZE];
138 static int g_msg_len = 0;
140 int saw_lf, check_len;
143 check_len = g_msg_len + strlen(line) + 1;
144 if (check_len <= LOG_BUF_SIZE) {
145 /* lf: Line feed ('\n') */
146 saw_lf = (strchr(line, '\n') != NULL) ? 1 : 0;
147 strncpy(g_msg + g_msg_len, line, strlen(line));
148 g_msg_len += strlen(line);
153 /* attach the line feed */
155 g_msg[g_msg_len] = '\n';
158 /* trace is fragmented */
160 g_msg[g_msg_len] = '\n';
163 /* reset g_msg and g_msg_len */
164 memset(g_msg, 0, LOG_BUF_SIZE);
166 } while (check_len > LOG_BUF_SIZE);
170 void nam_av_log_set_flags(int arg)
176 const struct { const char *name; int level; } log_levels[] = {
177 { "quiet" , AV_LOG_QUIET },
178 { "panic" , AV_LOG_PANIC },
179 { "fatal" , AV_LOG_FATAL },
180 { "error" , AV_LOG_ERROR },
181 { "warning", AV_LOG_WARNING },
182 { "info" , AV_LOG_INFO },
183 { "verbose", AV_LOG_VERBOSE },
184 { "debug" , AV_LOG_DEBUG },
187 #define AV_LOG_QUIET -8
188 #define AV_LOG_PANIC 0
189 #define AV_LOG_FATAL 8
190 #define AV_LOG_ERROR 16
191 #define AV_LOG_WARNING 24
192 #define AV_LOG_INFO 32
193 #define AV_LOG_VERBOSE 40
194 #define AV_LOG_DEBUG 48
197 //////////////////////////////////////////////////////////////////////////////////
198 // constructor and destructor
199 //////////////////////////////////////////////////////////////////////////////////
200 /* Mutex manager callback. */
201 static int lockmgr(void **mtx, enum AVLockOp op)
205 *mtx = (void *)av_malloc(sizeof(pthread_mutex_t));
208 return !!pthread_mutex_init((pthread_mutex_t *)(*mtx), NULL);
210 return !!pthread_mutex_lock((pthread_mutex_t *)(*mtx));
211 case AV_LOCK_RELEASE:
212 return !!pthread_mutex_unlock((pthread_mutex_t *)(*mtx));
213 case AV_LOCK_DESTROY:
214 pthread_mutex_destroy((pthread_mutex_t *)(*mtx));
222 * To debug ffmpeg", type this command on the console before starting playback:
223 * setprop debug.nam.ffmpeg 1
224 * To disable the debug, type:
225 * setprop debug.nam.ffmpge 0
227 status_t initFFmpeg()
230 bool debug_enabled = false;
231 char value[PROPERTY_VALUE_MAX];
233 pthread_mutex_lock(&init_mutex);
235 if (property_get("debug.nam.ffmpeg", value, NULL)
236 && (!strcmp(value, "1") || !av_strcasecmp(value, "true"))) {
237 LOGI("set ffmpeg debug level to AV_LOG_DEBUG");
238 debug_enabled = true;
241 av_log_set_level(AV_LOG_DEBUG);
243 av_log_set_level(AV_LOG_INFO);
246 nam_av_log_set_flags(AV_LOG_SKIP_REPEATED);
247 av_log_set_callback(nam_av_log_callback);
249 /* register all codecs, demux and protocols */
250 avcodec_register_all();
252 avdevice_register_all();
255 avformat_network_init();
259 if (av_lockmgr_register(lockmgr)) {
260 LOGE("could not initialize lock manager!");
268 pthread_mutex_unlock(&init_mutex);
275 pthread_mutex_lock(&init_mutex);
281 av_lockmgr_register(NULL);
283 avformat_network_deinit();
286 pthread_mutex_unlock(&init_mutex);
289 //////////////////////////////////////////////////////////////////////////////////
291 //////////////////////////////////////////////////////////////////////////////////
292 /* H.264 bitstream with start codes, NOT AVC1! ref: libavcodec/h264_parser.c */
293 static int h264_split(AVCodecContext *avctx,
294 const uint8_t *buf, int buf_size, int check_compatible_only)
301 //av_hex_dump(stderr, buf, 100);
303 for(i=0; i<=buf_size; i++){
304 if((state&0xFFFFFF1F) == 0x107) {
305 LOGI("found NAL_SPS");
308 if((state&0xFFFFFF1F) == 0x108) {
309 LOGI("found NAL_PPS");
311 if (check_compatible_only)
312 return (has_sps & has_pps);
314 if((state&0xFFFFFF00) == 0x100 && ((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105)){
316 while(i>4 && buf[i-5]==0) i--;
321 state= (state<<8) | buf[i];
326 /* ref: libavcodec/mpegvideo_parser.c */
327 static int mpegvideo_split(AVCodecContext *avctx,
328 const uint8_t *buf, int buf_size, int check_compatible_only)
334 for(i=0; i<buf_size; i++){
335 state= (state<<8) | buf[i];
338 }else if(found && state != 0x1B5 && state < 0x200 && state >= 0x100)
344 /* split extradata from buf for Android OMXCodec */
345 int parser_split(AVCodecContext *avctx,
346 const uint8_t *buf, int buf_size)
348 if (!avctx || !buf || buf_size <= 0) {
349 LOGE("parser split, valid params");
353 if (avctx->codec_id == CODEC_ID_H264) {
354 return h264_split(avctx, buf, buf_size, 0);
355 } else if (avctx->codec_id == CODEC_ID_MPEG2VIDEO ||
356 avctx->codec_id == CODEC_ID_MPEG4) {
357 return mpegvideo_split(avctx, buf, buf_size, 0);
359 LOGE("parser split, unsupport the codec, id: 0x%0x", avctx->codec_id);
365 int is_extradata_compatible_with_android(AVCodecContext *avctx)
367 if (avctx->extradata_size <= 0) {
368 LOGI("extradata_size <= 0, extradata is not compatible with android decoder, the codec id: 0x%0x", avctx->codec_id);
372 if (avctx->codec_id == CODEC_ID_H264 && avctx->extradata[0] != 1 /* configurationVersion */) {
374 return !!(h264_split(avctx, avctx->extradata, avctx->extradata_size, 1) > 0);
377 return !!(avctx->extradata_size > 0);
381 } // namespace android