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.
18 #define LOG_TAG "FFMPEG"
19 #include <utils/Log.h>
21 #include <utils/Errors.h>
33 #include <limits.h> /* INT_MAX */
35 #include "libavutil/avstring.h"
36 #include "libavutil/colorspace.h"
37 #include "libavutil/mathematics.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavutil/imgutils.h"
40 #include "libavutil/dict.h"
41 #include "libavutil/parseutils.h"
42 #include "libavutil/samplefmt.h"
43 #include "libavutil/avassert.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"
60 #include <cutils/properties.h>
62 #include "ffmpeg_utils.h"
63 #include "ffmpeg_source.h"
69 const char program_name[] = "dummy";
70 const int program_birth_year = 2012;
73 static pthread_mutex_t s_init_mutex = PTHREAD_MUTEX_INITIALIZER;
74 static int s_ref_count = 0;
78 //////////////////////////////////////////////////////////////////////////////////
80 //////////////////////////////////////////////////////////////////////////////////
85 void exit_program(int ret)
90 void show_help_default(const char *opt, const char *arg)
99 //////////////////////////////////////////////////////////////////////////////////
101 //////////////////////////////////////////////////////////////////////////////////
102 static void sanitize(uint8_t *line){
104 if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
110 // TODO, remove static variables to support multi-instances
111 void nam_av_log_callback(void* ptr, int level, const char* fmt, va_list vl)
113 static int print_prefix = 1;
115 static char prev[1024];
119 if (level > av_log_get_level())
121 av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix);
123 if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev)){
128 ALOGI("Last message repeated %d times\n", count);
132 sanitize((uint8_t *)line);
137 #define LOG_BUF_SIZE 1024
138 static char g_msg[LOG_BUF_SIZE];
139 static int g_msg_len = 0;
141 int saw_lf, check_len;
144 check_len = g_msg_len + strlen(line) + 1;
145 if (check_len <= LOG_BUF_SIZE) {
146 /* lf: Line feed ('\n') */
147 saw_lf = (strchr(line, '\n') != NULL) ? 1 : 0;
148 strncpy(g_msg + g_msg_len, line, strlen(line));
149 g_msg_len += strlen(line);
154 /* attach the line feed */
156 g_msg[g_msg_len] = '\n';
159 /* trace is fragmented */
161 g_msg[g_msg_len] = '\n';
164 /* reset g_msg and g_msg_len */
165 memset(g_msg, 0, LOG_BUF_SIZE);
167 } while (check_len > LOG_BUF_SIZE);
171 void nam_av_log_set_flags(int arg)
177 const struct { const char *name; int level; } log_levels[] = {
178 { "quiet" , AV_LOG_QUIET },
179 { "panic" , AV_LOG_PANIC },
180 { "fatal" , AV_LOG_FATAL },
181 { "error" , AV_LOG_ERROR },
182 { "warning", AV_LOG_WARNING },
183 { "info" , AV_LOG_INFO },
184 { "verbose", AV_LOG_VERBOSE },
185 { "debug" , AV_LOG_DEBUG },
188 #define AV_LOG_QUIET -8
189 #define AV_LOG_PANIC 0
190 #define AV_LOG_FATAL 8
191 #define AV_LOG_ERROR 16
192 #define AV_LOG_WARNING 24
193 #define AV_LOG_INFO 32
194 #define AV_LOG_VERBOSE 40
195 #define AV_LOG_DEBUG 48
198 //////////////////////////////////////////////////////////////////////////////////
199 // constructor and destructor
200 //////////////////////////////////////////////////////////////////////////////////
201 /* Mutex manager callback. */
202 static int lockmgr(void **mtx, enum AVLockOp op)
206 *mtx = (void *)av_malloc(sizeof(pthread_mutex_t));
209 return !!pthread_mutex_init((pthread_mutex_t *)(*mtx), NULL);
211 return !!pthread_mutex_lock((pthread_mutex_t *)(*mtx));
212 case AV_LOCK_RELEASE:
213 return !!pthread_mutex_unlock((pthread_mutex_t *)(*mtx));
214 case AV_LOCK_DESTROY:
215 pthread_mutex_destroy((pthread_mutex_t *)(*mtx));
223 * To debug ffmpeg", type this command on the console before starting playback:
224 * setprop debug.nam.ffmpeg 1
225 * To disable the debug, type:
226 * setprop debug.nam.ffmpge 0
228 status_t initFFmpeg()
231 bool debug_enabled = false;
232 char value[PROPERTY_VALUE_MAX];
234 pthread_mutex_lock(&s_init_mutex);
236 if (property_get("debug.nam.ffmpeg", value, NULL)
237 && (!strcmp(value, "1") || !av_strcasecmp(value, "true"))) {
238 ALOGI("set ffmpeg debug level to AV_LOG_DEBUG");
239 debug_enabled = true;
242 av_log_set_level(AV_LOG_DEBUG);
244 av_log_set_level(AV_LOG_INFO);
246 if(s_ref_count == 0) {
247 nam_av_log_set_flags(AV_LOG_SKIP_REPEATED);
248 av_log_set_callback(nam_av_log_callback);
250 /* register all codecs, demux and protocols */
251 avcodec_register_all();
253 avdevice_register_all();
256 avformat_network_init();
258 /* register android source */
259 ffmpeg_register_android_source();
261 if (av_lockmgr_register(lockmgr)) {
262 ALOGE("could not initialize lock manager!");
270 pthread_mutex_unlock(&s_init_mutex);
277 pthread_mutex_lock(&s_init_mutex);
282 if(s_ref_count == 0) {
283 av_lockmgr_register(NULL);
284 avformat_network_deinit();
287 pthread_mutex_unlock(&s_init_mutex);
290 //////////////////////////////////////////////////////////////////////////////////
292 //////////////////////////////////////////////////////////////////////////////////
293 /* H.264 bitstream with start codes, NOT AVC1! ref: libavcodec/h264_parser.c */
294 static int h264_split(AVCodecContext *avctx,
295 const uint8_t *buf, int buf_size, int check_compatible_only)
302 //av_hex_dump(stderr, buf, 100);
304 for(i=0; i<=buf_size; i++){
305 if((state&0xFFFFFF1F) == 0x107) {
306 ALOGI("found NAL_SPS");
309 if((state&0xFFFFFF1F) == 0x108) {
310 ALOGI("found NAL_PPS");
312 if (check_compatible_only)
313 return (has_sps & has_pps);
315 if((state&0xFFFFFF00) == 0x100
316 && ((state&0xFFFFFF1F) == 0x101
317 || (state&0xFFFFFF1F) == 0x102
318 || (state&0xFFFFFF1F) == 0x105)){
320 while(i>4 && buf[i-5]==0) i--;
325 state= (state<<8) | buf[i];
330 /* ref: libavcodec/mpegvideo_parser.c */
331 static int mpegvideo_split(AVCodecContext *avctx,
332 const uint8_t *buf, int buf_size, int check_compatible_only)
338 for(i=0; i<buf_size; i++){
339 state= (state<<8) | buf[i];
342 }else if(found && state != 0x1B5 && state < 0x200 && state >= 0x100)
348 /* split extradata from buf for Android OMXCodec */
349 int parser_split(AVCodecContext *avctx,
350 const uint8_t *buf, int buf_size)
352 if (!avctx || !buf || buf_size <= 0) {
353 ALOGE("parser split, valid params");
357 if (avctx->codec_id == AV_CODEC_ID_H264) {
358 return h264_split(avctx, buf, buf_size, 0);
359 } else if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
360 avctx->codec_id == AV_CODEC_ID_MPEG4) {
361 return mpegvideo_split(avctx, buf, buf_size, 0);
363 ALOGE("parser split, unsupport the codec, id: 0x%0x", avctx->codec_id);
369 int is_extradata_compatible_with_android(AVCodecContext *avctx)
371 if (avctx->extradata_size <= 0) {
372 ALOGI("extradata_size <= 0, extradata is not compatible with "
373 "android decoder, the codec id: 0x%0x", avctx->codec_id);
377 if (avctx->codec_id == AV_CODEC_ID_H264
378 && avctx->extradata[0] != 1 /* configurationVersion */) {
380 return !!(h264_split(avctx, avctx->extradata,
381 avctx->extradata_size, 1) > 0);
384 return !!(avctx->extradata_size > 0);
388 } // namespace android