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_NDEBUG 0
19 #define LOG_TAG "FFMPEG"
20 #include <utils/Log.h>
22 #include <utils/Errors.h>
34 #include <limits.h> /* INT_MAX */
44 #include <cutils/properties.h>
46 #include "ffmpeg_utils.h"
47 #include "ffmpeg_source.h"
53 const char program_name[] = "dummy";
54 const int program_birth_year = 2012;
57 static pthread_mutex_t s_init_mutex = PTHREAD_MUTEX_INITIALIZER;
58 static int s_ref_count = 0;
62 //////////////////////////////////////////////////////////////////////////////////
64 //////////////////////////////////////////////////////////////////////////////////
65 static void sanitize(uint8_t *line){
67 if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
73 // TODO, remove static variables to support multi-instances
74 void nam_av_log_callback(void* ptr, int level, const char* fmt, va_list vl)
76 static int print_prefix = 1;
78 static char prev[1024];
82 if (level > av_log_get_level())
84 av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix);
86 if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev)){
91 ALOGI("Last message repeated %d times\n", count);
95 sanitize((uint8_t *)line);
100 #define LOG_BUF_SIZE 1024
101 static char g_msg[LOG_BUF_SIZE];
102 static int g_msg_len = 0;
104 int saw_lf, check_len;
107 check_len = g_msg_len + strlen(line) + 1;
108 if (check_len <= LOG_BUF_SIZE) {
109 /* lf: Line feed ('\n') */
110 saw_lf = (strchr(line, '\n') != NULL) ? 1 : 0;
111 strncpy(g_msg + g_msg_len, line, strlen(line));
112 g_msg_len += strlen(line);
117 /* attach the line feed */
119 g_msg[g_msg_len] = '\n';
122 /* trace is fragmented */
124 g_msg[g_msg_len] = '\n';
127 /* reset g_msg and g_msg_len */
128 memset(g_msg, 0, LOG_BUF_SIZE);
130 } while (check_len > LOG_BUF_SIZE);
134 void nam_av_log_set_flags(int arg)
140 const struct { const char *name; int level; } log_levels[] = {
141 { "quiet" , AV_LOG_QUIET },
142 { "panic" , AV_LOG_PANIC },
143 { "fatal" , AV_LOG_FATAL },
144 { "error" , AV_LOG_ERROR },
145 { "warning", AV_LOG_WARNING },
146 { "info" , AV_LOG_INFO },
147 { "verbose", AV_LOG_VERBOSE },
148 { "debug" , AV_LOG_DEBUG },
151 #define AV_LOG_QUIET -8
152 #define AV_LOG_PANIC 0
153 #define AV_LOG_FATAL 8
154 #define AV_LOG_ERROR 16
155 #define AV_LOG_WARNING 24
156 #define AV_LOG_INFO 32
157 #define AV_LOG_VERBOSE 40
158 #define AV_LOG_DEBUG 48
161 //////////////////////////////////////////////////////////////////////////////////
162 // constructor and destructor
163 //////////////////////////////////////////////////////////////////////////////////
164 /* Mutex manager callback. */
165 static int lockmgr(void **mtx, enum AVLockOp op)
169 *mtx = (void *)av_malloc(sizeof(pthread_mutex_t));
172 return !!pthread_mutex_init((pthread_mutex_t *)(*mtx), NULL);
174 return !!pthread_mutex_lock((pthread_mutex_t *)(*mtx));
175 case AV_LOCK_RELEASE:
176 return !!pthread_mutex_unlock((pthread_mutex_t *)(*mtx));
177 case AV_LOCK_DESTROY:
178 pthread_mutex_destroy((pthread_mutex_t *)(*mtx));
186 * To debug ffmpeg", type this command on the console before starting playback:
187 * setprop debug.nam.ffmpeg 1
188 * To disable the debug, type:
189 * setprop debug.nam.ffmpge 0
191 status_t initFFmpeg()
194 bool debug_enabled = false;
195 char value[PROPERTY_VALUE_MAX];
197 pthread_mutex_lock(&s_init_mutex);
199 if (property_get("debug.nam.ffmpeg", value, NULL)
200 && (!strcmp(value, "1") || !av_strcasecmp(value, "true"))) {
201 ALOGI("set ffmpeg debug level to AV_LOG_DEBUG");
202 debug_enabled = true;
205 av_log_set_level(AV_LOG_DEBUG);
207 av_log_set_level(AV_LOG_INFO);
209 if(s_ref_count == 0) {
210 nam_av_log_set_flags(AV_LOG_SKIP_REPEATED);
211 av_log_set_callback(nam_av_log_callback);
213 /* register all codecs, demux and protocols */
214 avcodec_register_all();
216 avdevice_register_all();
219 avformat_network_init();
221 /* register android source */
222 ffmpeg_register_android_source();
224 if (av_lockmgr_register(lockmgr)) {
225 ALOGE("could not initialize lock manager!");
233 pthread_mutex_unlock(&s_init_mutex);
240 pthread_mutex_lock(&s_init_mutex);
245 if(s_ref_count == 0) {
246 av_lockmgr_register(NULL);
247 avformat_network_deinit();
250 pthread_mutex_unlock(&s_init_mutex);
253 //////////////////////////////////////////////////////////////////////////////////
255 //////////////////////////////////////////////////////////////////////////////////
256 /* H.264 bitstream with start codes, NOT AVC1! */
257 static int h264_split(AVCodecContext *avctx __unused,
258 const uint8_t *buf, int buf_size, int check_compatible_only)
265 //av_hex_dump(stderr, buf, 100);
267 for(i=0; i<=buf_size; i++){
268 if((state&0xFFFFFF1F) == 0x107) {
269 ALOGI("found NAL_SPS");
272 if((state&0xFFFFFF1F) == 0x108) {
273 ALOGI("found NAL_PPS");
275 if (check_compatible_only)
276 return (has_sps & has_pps);
278 if((state&0xFFFFFF00) == 0x100
279 && ((state&0xFFFFFF1F) == 0x101
280 || (state&0xFFFFFF1F) == 0x102
281 || (state&0xFFFFFF1F) == 0x105)){
283 while(i>4 && buf[i-5]==0) i--;
288 state= (state<<8) | buf[i];
293 static int mpegvideo_split(AVCodecContext *avctx __unused,
294 const uint8_t *buf, int buf_size, int check_compatible_only __unused)
300 for(i=0; i<buf_size; i++){
301 state= (state<<8) | buf[i];
304 }else if(found && state != 0x1B5 && state < 0x200 && state >= 0x100)
310 /* split extradata from buf for Android OMXCodec */
311 int parser_split(AVCodecContext *avctx,
312 const uint8_t *buf, int buf_size)
314 if (!avctx || !buf || buf_size <= 0) {
315 ALOGE("parser split, valid params");
319 if (avctx->codec_id == AV_CODEC_ID_H264) {
320 return h264_split(avctx, buf, buf_size, 0);
321 } else if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
322 avctx->codec_id == AV_CODEC_ID_MPEG4) {
323 return mpegvideo_split(avctx, buf, buf_size, 0);
325 ALOGE("parser split, unsupport the codec, id: 0x%0x", avctx->codec_id);
331 int is_extradata_compatible_with_android(AVCodecContext *avctx)
333 if (avctx->extradata_size <= 0) {
334 ALOGI("extradata_size <= 0, extradata is not compatible with "
335 "android decoder, the codec id: 0x%0x", avctx->codec_id);
339 if (avctx->codec_id == AV_CODEC_ID_H264
340 && avctx->extradata[0] != 1 /* configurationVersion */) {
342 return !!(h264_split(avctx, avctx->extradata,
343 avctx->extradata_size, 1) > 0);
346 return !!(avctx->extradata_size > 0);
350 //////////////////////////////////////////////////////////////////////////////////
352 //////////////////////////////////////////////////////////////////////////////////
353 void packet_queue_init(PacketQueue *q)
355 memset(q, 0, sizeof(PacketQueue));
356 q->abort_request = 1;
359 void packet_queue_destroy(PacketQueue *q)
361 packet_queue_abort(q);
362 packet_queue_flush(q);
365 void packet_queue_flush(PacketQueue *q)
367 AVPacketList *pkt, *pkt1;
369 Mutex::Autolock autoLock(q->lock);
370 for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
372 av_free_packet(&pkt->pkt);
381 void packet_queue_abort(PacketQueue *q)
383 q->abort_request = 1;
384 Mutex::Autolock autoLock(q->lock);
388 static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
392 if (q->abort_request)
395 pkt1 = (AVPacketList *)av_malloc(sizeof(AVPacketList));
404 q->last_pkt->next = pkt1;
407 //q->size += pkt1->pkt.size + sizeof(*pkt1);
408 q->size += pkt1->pkt.size;
413 int packet_queue_put(PacketQueue *q, AVPacket *pkt)
417 /* duplicate the packet */
418 if (pkt != &q->flush_pkt && av_dup_packet(pkt) < 0)
422 ret = packet_queue_put_private(q, pkt);
425 if (pkt != &q->flush_pkt && ret < 0)
431 int packet_queue_is_wait_for_data(PacketQueue *q)
433 Mutex::Autolock autoLock(q->lock);
434 return q->wait_for_data;
437 int packet_queue_put_nullpacket(PacketQueue *q, int stream_index)
439 AVPacket pkt1, *pkt = &pkt1;
443 pkt->stream_index = stream_index;
444 return packet_queue_put(q, pkt);
447 /* packet queue handling */
448 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
449 int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
454 Mutex::Autolock autoLock(q->lock);
456 while (!q->abort_request) {
459 q->first_pkt = pkt1->next;
463 //q->size -= pkt1->pkt.size + sizeof(*pkt1);
464 q->size -= pkt1->pkt.size;
473 q->wait_for_data = 1;
474 q->cond.waitRelative(q->lock, 10000000LL);
477 q->wait_for_data = 0;
481 void packet_queue_start(PacketQueue *q)
483 Mutex::Autolock autoLock(q->lock);
484 av_init_packet(&q->flush_pkt);
485 q->flush_pkt.data = (uint8_t *)&q->flush_pkt;
486 q->flush_pkt.size = 0;
487 q->abort_request = 0;
488 packet_queue_put_private(q, &q->flush_pkt);
491 //////////////////////////////////////////////////////////////////////////////////
493 //////////////////////////////////////////////////////////////////////////////////
494 bool setup_vorbis_extradata(uint8_t **extradata, int *extradata_size,
495 const uint8_t *header_start[3], const int header_len[3])
501 len = header_len[0] + header_len[1] + header_len[2];
502 p = *extradata = (uint8_t *)av_mallocz(64 + len + len/255);
504 ALOGE("oom for vorbis extradata");
509 p += av_xiphlacing(p, header_len[0]);
510 p += av_xiphlacing(p, header_len[1]);
511 for (i = 0; i < 3; i++) {
512 if (header_len[i] > 0) {
513 memcpy(p, header_start[i], header_len[i]);
517 *extradata_size = p - *extradata;
522 int64_t get_timestamp() {
524 gettimeofday(&tv, NULL);
525 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
528 audio_format_t to_android_audio_format(enum AVSampleFormat fmt) {
529 AVSampleFormat packed = av_get_packed_sample_fmt(fmt);
530 if (packed == AV_SAMPLE_FMT_U8)
531 return AUDIO_FORMAT_PCM_8_BIT;
532 if (packed == AV_SAMPLE_FMT_S16)
533 return AUDIO_FORMAT_PCM_16_BIT;
534 if (packed == AV_SAMPLE_FMT_S32)
535 return AUDIO_FORMAT_PCM_32_BIT;
536 if (packed == AV_SAMPLE_FMT_FLT)
537 return AUDIO_FORMAT_PCM_FLOAT;
538 return AUDIO_FORMAT_DEFAULT;
541 } // namespace android