2 * Copyright 2012 Michael Chen <omxcodec@gmail.com>
3 * Copyright 2015 The CyanogenMod Project
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #define LOG_TAG "FFMPEG"
19 #include <utils/Log.h>
21 #include <utils/Errors.h>
31 #include <limits.h> /* INT_MAX */
39 #include <cutils/properties.h>
41 #include "ffmpeg_utils.h"
42 #include "ffmpeg_source.h"
48 static pthread_mutex_t s_init_mutex = PTHREAD_MUTEX_INITIALIZER;
49 static int s_ref_count = 0;
53 //////////////////////////////////////////////////////////////////////////////////
55 //////////////////////////////////////////////////////////////////////////////////
56 static void sanitize(uint8_t *line){
58 if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
64 // TODO, remove static variables to support multi-instances
65 void nam_av_log_callback(void* ptr, int level, const char* fmt, va_list vl)
67 static int print_prefix = 1;
69 static char prev[1024];
72 if (level > av_log_get_level())
74 av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix);
76 if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev)){
81 ALOGI("Last message repeated %d times\n", count);
85 sanitize((uint8_t *)line);
90 #define LOG_BUF_SIZE 1024
91 static char g_msg[LOG_BUF_SIZE];
92 static int g_msg_len = 0;
94 int saw_lf, check_len;
97 check_len = g_msg_len + strlen(line) + 1;
98 if (check_len <= LOG_BUF_SIZE) {
99 /* lf: Line feed ('\n') */
100 saw_lf = (strchr(line, '\n') != NULL) ? 1 : 0;
101 strncpy(g_msg + g_msg_len, line, strlen(line));
102 g_msg_len += strlen(line);
107 /* attach the line feed */
109 g_msg[g_msg_len] = '\n';
112 /* trace is fragmented */
114 g_msg[g_msg_len] = '\n';
117 /* reset g_msg and g_msg_len */
118 memset(g_msg, 0, LOG_BUF_SIZE);
120 } while (check_len > LOG_BUF_SIZE);
124 void nam_av_log_set_flags(int arg)
130 const struct { const char *name; int level; } log_levels[] = {
131 { "quiet" , AV_LOG_QUIET },
132 { "panic" , AV_LOG_PANIC },
133 { "fatal" , AV_LOG_FATAL },
134 { "error" , AV_LOG_ERROR },
135 { "warning", AV_LOG_WARNING },
136 { "info" , AV_LOG_INFO },
137 { "verbose", AV_LOG_VERBOSE },
138 { "debug" , AV_LOG_DEBUG },
141 #define AV_LOG_QUIET -8
142 #define AV_LOG_PANIC 0
143 #define AV_LOG_FATAL 8
144 #define AV_LOG_ERROR 16
145 #define AV_LOG_WARNING 24
146 #define AV_LOG_INFO 32
147 #define AV_LOG_VERBOSE 40
148 #define AV_LOG_DEBUG 48
151 //////////////////////////////////////////////////////////////////////////////////
152 // constructor and destructor
153 //////////////////////////////////////////////////////////////////////////////////
154 /* Mutex manager callback. */
155 static int lockmgr(void **mtx, enum AVLockOp op)
159 *mtx = (void *)av_malloc(sizeof(pthread_mutex_t));
162 return !!pthread_mutex_init((pthread_mutex_t *)(*mtx), NULL);
164 return !!pthread_mutex_lock((pthread_mutex_t *)(*mtx));
165 case AV_LOCK_RELEASE:
166 return !!pthread_mutex_unlock((pthread_mutex_t *)(*mtx));
167 case AV_LOCK_DESTROY:
168 pthread_mutex_destroy((pthread_mutex_t *)(*mtx));
176 * To debug ffmpeg", type this command on the console before starting playback:
177 * setprop debug.nam.ffmpeg 1
178 * To disable the debug, type:
179 * setprop debug.nam.ffmpge 0
181 status_t initFFmpeg()
185 pthread_mutex_lock(&s_init_mutex);
187 if (property_get_bool("debug.nam.ffmpeg", 0))
188 av_log_set_level(AV_LOG_DEBUG);
190 av_log_set_level(AV_LOG_INFO);
192 if(s_ref_count == 0) {
193 nam_av_log_set_flags(AV_LOG_SKIP_REPEATED);
194 av_log_set_callback(nam_av_log_callback);
196 /* register all codecs, demux and protocols */
197 avcodec_register_all();
199 avdevice_register_all();
202 avformat_network_init();
204 /* register android source */
205 ffmpeg_register_android_source();
207 if (av_lockmgr_register(lockmgr)) {
208 ALOGE("could not initialize lock manager!");
216 pthread_mutex_unlock(&s_init_mutex);
223 pthread_mutex_lock(&s_init_mutex);
228 if(s_ref_count == 0) {
229 av_lockmgr_register(NULL);
230 avformat_network_deinit();
233 pthread_mutex_unlock(&s_init_mutex);
236 //////////////////////////////////////////////////////////////////////////////////
238 //////////////////////////////////////////////////////////////////////////////////
239 /* H.264 bitstream with start codes, NOT AVC1! */
240 static int h264_split(AVCodecContext *avctx __unused,
241 const uint8_t *buf, int buf_size, int check_compatible_only)
248 //av_hex_dump(stderr, buf, 100);
250 for(i=0; i<=buf_size; i++){
251 if((state&0xFFFFFF1F) == 0x107) {
252 ALOGI("found NAL_SPS");
255 if((state&0xFFFFFF1F) == 0x108) {
256 ALOGI("found NAL_PPS");
258 if (check_compatible_only)
259 return (has_sps & has_pps);
261 if((state&0xFFFFFF00) == 0x100
262 && ((state&0xFFFFFF1F) == 0x101
263 || (state&0xFFFFFF1F) == 0x102
264 || (state&0xFFFFFF1F) == 0x105)){
266 while(i>4 && buf[i-5]==0) i--;
271 state= (state<<8) | buf[i];
276 static int mpegvideo_split(AVCodecContext *avctx __unused,
277 const uint8_t *buf, int buf_size, int check_compatible_only __unused)
283 for(i=0; i<buf_size; i++){
284 state= (state<<8) | buf[i];
287 }else if(found && state != 0x1B5 && state < 0x200 && state >= 0x100)
293 /* split extradata from buf for Android OMXCodec */
294 int parser_split(AVCodecContext *avctx,
295 const uint8_t *buf, int buf_size)
297 if (!avctx || !buf || buf_size <= 0) {
298 ALOGE("parser split, valid params");
302 if (avctx->codec_id == AV_CODEC_ID_H264) {
303 return h264_split(avctx, buf, buf_size, 0);
304 } else if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
305 avctx->codec_id == AV_CODEC_ID_MPEG4) {
306 return mpegvideo_split(avctx, buf, buf_size, 0);
308 ALOGE("parser split, unsupport the codec, id: 0x%0x", avctx->codec_id);
314 int is_extradata_compatible_with_android(AVCodecContext *avctx)
316 if (avctx->extradata_size <= 0) {
317 ALOGI("extradata_size <= 0, extradata is not compatible with "
318 "android decoder, the codec id: 0x%0x", avctx->codec_id);
322 if (avctx->codec_id == AV_CODEC_ID_H264
323 && avctx->extradata[0] != 1 /* configurationVersion */) {
325 return !!(h264_split(avctx, avctx->extradata,
326 avctx->extradata_size, 1) > 0);
329 return !!(avctx->extradata_size > 0);
333 //////////////////////////////////////////////////////////////////////////////////
335 //////////////////////////////////////////////////////////////////////////////////
336 void packet_queue_init(PacketQueue *q)
338 memset(q, 0, sizeof(PacketQueue));
339 q->abort_request = 1;
342 void packet_queue_destroy(PacketQueue *q)
344 packet_queue_abort(q);
345 packet_queue_flush(q);
348 void packet_queue_flush(PacketQueue *q)
350 AVPacketList *pkt, *pkt1;
352 Mutex::Autolock autoLock(q->lock);
353 for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
355 av_packet_unref(&pkt->pkt);
364 void packet_queue_abort(PacketQueue *q)
366 q->abort_request = 1;
367 Mutex::Autolock autoLock(q->lock);
371 static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
375 if (q->abort_request)
378 pkt1 = (AVPacketList *)av_malloc(sizeof(AVPacketList));
387 q->last_pkt->next = pkt1;
390 //q->size += pkt1->pkt.size + sizeof(*pkt1);
391 q->size += pkt1->pkt.size;
396 int packet_queue_put(PacketQueue *q, AVPacket *pkt)
400 /* duplicate the packet */
401 if (pkt != &q->flush_pkt && av_dup_packet(pkt) < 0)
405 ret = packet_queue_put_private(q, pkt);
408 if (pkt != &q->flush_pkt && ret < 0)
409 av_packet_unref(pkt);
414 int packet_queue_is_wait_for_data(PacketQueue *q)
416 Mutex::Autolock autoLock(q->lock);
417 return q->wait_for_data;
420 int packet_queue_put_nullpacket(PacketQueue *q, int stream_index)
422 AVPacket pkt1, *pkt = &pkt1;
426 pkt->stream_index = stream_index;
427 return packet_queue_put(q, pkt);
430 /* packet queue handling */
431 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
432 int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
437 Mutex::Autolock autoLock(q->lock);
439 while (!q->abort_request) {
442 q->first_pkt = pkt1->next;
446 //q->size -= pkt1->pkt.size + sizeof(*pkt1);
447 q->size -= pkt1->pkt.size;
456 q->wait_for_data = 1;
457 q->cond.waitRelative(q->lock, 10000000LL);
460 q->wait_for_data = 0;
464 void packet_queue_start(PacketQueue *q)
466 Mutex::Autolock autoLock(q->lock);
467 av_init_packet(&q->flush_pkt);
468 q->flush_pkt.data = (uint8_t *)&q->flush_pkt;
469 q->flush_pkt.size = 0;
470 q->abort_request = 0;
471 packet_queue_put_private(q, &q->flush_pkt);
474 //////////////////////////////////////////////////////////////////////////////////
476 //////////////////////////////////////////////////////////////////////////////////
477 bool setup_vorbis_extradata(uint8_t **extradata, int *extradata_size,
478 const uint8_t *header_start[3], const int header_len[3])
484 len = header_len[0] + header_len[1] + header_len[2];
485 p = *extradata = (uint8_t *)av_mallocz(64 + len + len/255);
487 ALOGE("oom for vorbis extradata");
492 p += av_xiphlacing(p, header_len[0]);
493 p += av_xiphlacing(p, header_len[1]);
494 for (i = 0; i < 3; i++) {
495 if (header_len[i] > 0) {
496 memcpy(p, header_start[i], header_len[i]);
500 *extradata_size = p - *extradata;
505 int64_t get_timestamp() {
507 gettimeofday(&tv, NULL);
508 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
511 audio_format_t to_android_audio_format(enum AVSampleFormat fmt) {
512 AVSampleFormat packed = av_get_packed_sample_fmt(fmt);
513 if (packed == AV_SAMPLE_FMT_U8)
514 return AUDIO_FORMAT_PCM_8_BIT;
515 if (packed == AV_SAMPLE_FMT_S16)
516 return AUDIO_FORMAT_PCM_16_BIT;
517 if (packed == AV_SAMPLE_FMT_S32)
518 return AUDIO_FORMAT_PCM_32_BIT;
519 if (packed == AV_SAMPLE_FMT_FLT)
520 return AUDIO_FORMAT_PCM_FLOAT;
521 return AUDIO_FORMAT_DEFAULT;
524 } // namespace android