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>
31 #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"
66 const char program_name[] = "dummy";
67 const int program_birth_year = 2012;
70 static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
71 static int ref_count = 0;
75 //////////////////////////////////////////////////////////////////////////////////
77 //////////////////////////////////////////////////////////////////////////////////
82 void av_noreturn exit_program(int ret)
87 void show_help_default(const char *opt, const char *arg)
96 //////////////////////////////////////////////////////////////////////////////////
98 //////////////////////////////////////////////////////////////////////////////////
99 static void sanitize(uint8_t *line){
101 if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
107 void nam_av_log_callback(void* ptr, int level, const char* fmt, va_list vl)
109 static int print_prefix = 1;
111 static char prev[1024];
115 if (level > av_log_get_level())
117 av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix);
119 if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev)){
124 LOGI("Last message repeated %d times\n", count);
128 sanitize((uint8_t *)line);
133 #define LOG_BUF_SIZE 1024
134 static char g_msg[LOG_BUF_SIZE];
135 static int g_msg_len = 0;
137 int saw_lf, check_len;
140 check_len = g_msg_len + strlen(line) + 1;
141 if (check_len <= LOG_BUF_SIZE) {
142 /* lf: Line feed ('\n') */
143 saw_lf = (strchr(line, '\n') != NULL) ? 1 : 0;
144 strncpy(g_msg + g_msg_len, line, strlen(line));
145 g_msg_len += strlen(line);
150 /* attach the line feed */
152 g_msg[g_msg_len] = '\n';
155 /* trace is fragmented */
157 g_msg[g_msg_len] = '\n';
160 /* reset g_msg and g_msg_len */
161 memset(g_msg, 0, LOG_BUF_SIZE);
163 } while (check_len > LOG_BUF_SIZE);
167 void nam_av_log_set_flags(int arg)
173 const struct { const char *name; int level; } log_levels[] = {
174 { "quiet" , AV_LOG_QUIET },
175 { "panic" , AV_LOG_PANIC },
176 { "fatal" , AV_LOG_FATAL },
177 { "error" , AV_LOG_ERROR },
178 { "warning", AV_LOG_WARNING },
179 { "info" , AV_LOG_INFO },
180 { "verbose", AV_LOG_VERBOSE },
181 { "debug" , AV_LOG_DEBUG },
184 #define AV_LOG_QUIET -8
185 #define AV_LOG_PANIC 0
186 #define AV_LOG_FATAL 8
187 #define AV_LOG_ERROR 16
188 #define AV_LOG_WARNING 24
189 #define AV_LOG_INFO 32
190 #define AV_LOG_VERBOSE 40
191 #define AV_LOG_DEBUG 48
194 //////////////////////////////////////////////////////////////////////////////////
195 // constructor and destructor
196 //////////////////////////////////////////////////////////////////////////////////
197 /* Mutex manager callback. */
198 static int lockmgr(void **mtx, enum AVLockOp op)
202 *mtx = (void *)av_malloc(sizeof(pthread_mutex_t));
205 return !!pthread_mutex_init((pthread_mutex_t *)(*mtx), NULL);
207 return !!pthread_mutex_lock((pthread_mutex_t *)(*mtx));
208 case AV_LOCK_RELEASE:
209 return !!pthread_mutex_unlock((pthread_mutex_t *)(*mtx));
210 case AV_LOCK_DESTROY:
211 pthread_mutex_destroy((pthread_mutex_t *)(*mtx));
218 status_t initFFmpeg()
222 pthread_mutex_lock(&init_mutex);
225 nam_av_log_set_flags(AV_LOG_SKIP_REPEATED);
226 av_log_set_level(AV_LOG_DEBUG);
227 av_log_set_callback(nam_av_log_callback);
229 /* register all codecs, demux and protocols */
230 avcodec_register_all();
232 avdevice_register_all();
235 avformat_network_init();
239 if (av_lockmgr_register(lockmgr)) {
240 LOGE("could not initialize lock manager!");
248 pthread_mutex_unlock(&init_mutex);
255 pthread_mutex_lock(&init_mutex);
261 av_lockmgr_register(NULL);
263 avformat_network_deinit();
266 pthread_mutex_unlock(&init_mutex);
269 //////////////////////////////////////////////////////////////////////////////////
271 //////////////////////////////////////////////////////////////////////////////////
272 /* H.264 bitstream with start codes, NOT AVC1! ref: libavcodec/h264_parser.c */
273 static int h264_split(AVCodecContext *avctx,
274 const uint8_t *buf, int buf_size, int check_compatible_only)
281 //av_hex_dump(stderr, buf, 100);
283 for(i=0; i<=buf_size; i++){
284 if((state&0xFFFFFF1F) == 0x107) {
285 LOGI("found NAL_SPS");
288 if((state&0xFFFFFF1F) == 0x108) {
289 LOGI("found NAL_PPS");
291 if (check_compatible_only)
292 return (has_sps & has_pps);
294 if((state&0xFFFFFF00) == 0x100 && ((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105)){
296 while(i>4 && buf[i-5]==0) i--;
301 state= (state<<8) | buf[i];
306 /* ref: libavcodec/mpegvideo_parser.c */
307 static int mpegvideo_split(AVCodecContext *avctx,
308 const uint8_t *buf, int buf_size, int check_compatible_only)
314 for(i=0; i<buf_size; i++){
315 state= (state<<8) | buf[i];
318 }else if(found && state != 0x1B5 && state < 0x200 && state >= 0x100)
324 /* split extradata from buf for Android OMXCodec */
325 int parser_split(AVCodecContext *avctx,
326 const uint8_t *buf, int buf_size)
328 if (!avctx || !buf || buf_size <= 0) {
329 LOGE("parser split, valid params");
333 if (avctx->codec_id == CODEC_ID_H264) {
334 return h264_split(avctx, buf, buf_size, 0);
335 } else if (avctx->codec_id == CODEC_ID_MPEG2VIDEO ||
336 avctx->codec_id == CODEC_ID_MPEG4) {
337 return mpegvideo_split(avctx, buf, buf_size, 0);
339 LOGE("parser split, unsupport the codec, id: 0x%0x", avctx->codec_id);
345 int is_extradata_compatible_with_android(AVCodecContext *avctx)
347 if (avctx->extradata_size <= 0) {
348 LOGI("extradata_size <= 0, extradata is not compatible with android decoder, the codec id: 0x%0x", avctx->codec_id);
352 if (avctx->codec_id == CODEC_ID_H264 && avctx->extradata[0] != 1 /* configurationVersion */) {
354 return !!(h264_split(avctx, avctx->extradata, avctx->extradata_size, 1) > 0);
357 return !!(avctx->extradata_size > 0);
361 } // namespace android