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 */
41 #include <cutils/properties.h>
43 #include "ffmpeg_utils.h"
44 #include "ffmpeg_source.h"
50 const char program_name[] = "dummy";
51 const int program_birth_year = 2012;
54 static pthread_mutex_t s_init_mutex = PTHREAD_MUTEX_INITIALIZER;
55 static int s_ref_count = 0;
59 //////////////////////////////////////////////////////////////////////////////////
61 //////////////////////////////////////////////////////////////////////////////////
66 void exit_program(int ret)
71 void show_help_default(const char *opt, const char *arg)
80 //////////////////////////////////////////////////////////////////////////////////
82 //////////////////////////////////////////////////////////////////////////////////
83 static void sanitize(uint8_t *line){
85 if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
91 // TODO, remove static variables to support multi-instances
92 void nam_av_log_callback(void* ptr, int level, const char* fmt, va_list vl)
94 static int print_prefix = 1;
96 static char prev[1024];
100 if (level > av_log_get_level())
102 av_log_format_line(ptr, level, fmt, vl, line, sizeof(line), &print_prefix);
104 if (print_prefix && (flags & AV_LOG_SKIP_REPEATED) && !strcmp(line, prev)){
109 ALOGI("Last message repeated %d times\n", count);
113 sanitize((uint8_t *)line);
118 #define LOG_BUF_SIZE 1024
119 static char g_msg[LOG_BUF_SIZE];
120 static int g_msg_len = 0;
122 int saw_lf, check_len;
125 check_len = g_msg_len + strlen(line) + 1;
126 if (check_len <= LOG_BUF_SIZE) {
127 /* lf: Line feed ('\n') */
128 saw_lf = (strchr(line, '\n') != NULL) ? 1 : 0;
129 strncpy(g_msg + g_msg_len, line, strlen(line));
130 g_msg_len += strlen(line);
135 /* attach the line feed */
137 g_msg[g_msg_len] = '\n';
140 /* trace is fragmented */
142 g_msg[g_msg_len] = '\n';
145 /* reset g_msg and g_msg_len */
146 memset(g_msg, 0, LOG_BUF_SIZE);
148 } while (check_len > LOG_BUF_SIZE);
152 void nam_av_log_set_flags(int arg)
158 const struct { const char *name; int level; } log_levels[] = {
159 { "quiet" , AV_LOG_QUIET },
160 { "panic" , AV_LOG_PANIC },
161 { "fatal" , AV_LOG_FATAL },
162 { "error" , AV_LOG_ERROR },
163 { "warning", AV_LOG_WARNING },
164 { "info" , AV_LOG_INFO },
165 { "verbose", AV_LOG_VERBOSE },
166 { "debug" , AV_LOG_DEBUG },
169 #define AV_LOG_QUIET -8
170 #define AV_LOG_PANIC 0
171 #define AV_LOG_FATAL 8
172 #define AV_LOG_ERROR 16
173 #define AV_LOG_WARNING 24
174 #define AV_LOG_INFO 32
175 #define AV_LOG_VERBOSE 40
176 #define AV_LOG_DEBUG 48
179 //////////////////////////////////////////////////////////////////////////////////
180 // constructor and destructor
181 //////////////////////////////////////////////////////////////////////////////////
182 /* Mutex manager callback. */
183 static int lockmgr(void **mtx, enum AVLockOp op)
187 *mtx = (void *)av_malloc(sizeof(pthread_mutex_t));
190 return !!pthread_mutex_init((pthread_mutex_t *)(*mtx), NULL);
192 return !!pthread_mutex_lock((pthread_mutex_t *)(*mtx));
193 case AV_LOCK_RELEASE:
194 return !!pthread_mutex_unlock((pthread_mutex_t *)(*mtx));
195 case AV_LOCK_DESTROY:
196 pthread_mutex_destroy((pthread_mutex_t *)(*mtx));
204 * To debug ffmpeg", type this command on the console before starting playback:
205 * setprop debug.nam.ffmpeg 1
206 * To disable the debug, type:
207 * setprop debug.nam.ffmpge 0
209 status_t initFFmpeg()
212 bool debug_enabled = false;
213 char value[PROPERTY_VALUE_MAX];
215 pthread_mutex_lock(&s_init_mutex);
217 if (property_get("debug.nam.ffmpeg", value, NULL)
218 && (!strcmp(value, "1") || !av_strcasecmp(value, "true"))) {
219 ALOGI("set ffmpeg debug level to AV_LOG_DEBUG");
220 debug_enabled = true;
223 av_log_set_level(AV_LOG_DEBUG);
225 av_log_set_level(AV_LOG_INFO);
227 if(s_ref_count == 0) {
228 nam_av_log_set_flags(AV_LOG_SKIP_REPEATED);
229 av_log_set_callback(nam_av_log_callback);
231 /* register all codecs, demux and protocols */
232 avcodec_register_all();
234 avdevice_register_all();
237 avformat_network_init();
239 /* register android source */
240 ffmpeg_register_android_source();
242 if (av_lockmgr_register(lockmgr)) {
243 ALOGE("could not initialize lock manager!");
251 pthread_mutex_unlock(&s_init_mutex);
258 pthread_mutex_lock(&s_init_mutex);
263 if(s_ref_count == 0) {
264 av_lockmgr_register(NULL);
265 avformat_network_deinit();
268 pthread_mutex_unlock(&s_init_mutex);
271 //////////////////////////////////////////////////////////////////////////////////
273 //////////////////////////////////////////////////////////////////////////////////
274 /* H.264 bitstream with start codes, NOT AVC1! */
275 static int h264_split(AVCodecContext *avctx,
276 const uint8_t *buf, int buf_size, int check_compatible_only)
283 //av_hex_dump(stderr, buf, 100);
285 for(i=0; i<=buf_size; i++){
286 if((state&0xFFFFFF1F) == 0x107) {
287 ALOGI("found NAL_SPS");
290 if((state&0xFFFFFF1F) == 0x108) {
291 ALOGI("found NAL_PPS");
293 if (check_compatible_only)
294 return (has_sps & has_pps);
296 if((state&0xFFFFFF00) == 0x100
297 && ((state&0xFFFFFF1F) == 0x101
298 || (state&0xFFFFFF1F) == 0x102
299 || (state&0xFFFFFF1F) == 0x105)){
301 while(i>4 && buf[i-5]==0) i--;
306 state= (state<<8) | buf[i];
311 static int mpegvideo_split(AVCodecContext *avctx,
312 const uint8_t *buf, int buf_size, int check_compatible_only)
318 for(i=0; i<buf_size; i++){
319 state= (state<<8) | buf[i];
322 }else if(found && state != 0x1B5 && state < 0x200 && state >= 0x100)
328 /* split extradata from buf for Android OMXCodec */
329 int parser_split(AVCodecContext *avctx,
330 const uint8_t *buf, int buf_size)
332 if (!avctx || !buf || buf_size <= 0) {
333 ALOGE("parser split, valid params");
337 if (avctx->codec_id == AV_CODEC_ID_H264) {
338 return h264_split(avctx, buf, buf_size, 0);
339 } else if (avctx->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
340 avctx->codec_id == AV_CODEC_ID_MPEG4) {
341 return mpegvideo_split(avctx, buf, buf_size, 0);
343 ALOGE("parser split, unsupport the codec, id: 0x%0x", avctx->codec_id);
349 int is_extradata_compatible_with_android(AVCodecContext *avctx)
351 if (avctx->extradata_size <= 0) {
352 ALOGI("extradata_size <= 0, extradata is not compatible with "
353 "android decoder, the codec id: 0x%0x", avctx->codec_id);
357 if (avctx->codec_id == AV_CODEC_ID_H264
358 && avctx->extradata[0] != 1 /* configurationVersion */) {
360 return !!(h264_split(avctx, avctx->extradata,
361 avctx->extradata_size, 1) > 0);
364 return !!(avctx->extradata_size > 0);
368 //////////////////////////////////////////////////////////////////////////////////
370 //////////////////////////////////////////////////////////////////////////////////
371 void packet_queue_init(PacketQueue *q)
373 memset(q, 0, sizeof(PacketQueue));
374 pthread_mutex_init(&q->mutex, NULL);
375 pthread_cond_init(&q->cond, NULL);
377 av_init_packet(&q->flush_pkt);
378 q->flush_pkt.data = (uint8_t *)&q->flush_pkt;
379 q->flush_pkt.size = 0;
381 packet_queue_put(q, &q->flush_pkt);
384 void packet_queue_destroy(PacketQueue *q)
386 packet_queue_flush(q);
387 pthread_mutex_destroy(&q->mutex);
388 pthread_cond_destroy(&q->cond);
391 void packet_queue_flush(PacketQueue *q)
393 AVPacketList *pkt, *pkt1;
395 pthread_mutex_lock(&q->mutex);
396 for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
398 av_free_packet(&pkt->pkt);
405 pthread_mutex_unlock(&q->mutex);
408 void packet_queue_end(PacketQueue *q)
410 packet_queue_flush(q);
413 void packet_queue_abort(PacketQueue *q)
415 pthread_mutex_lock(&q->mutex);
417 q->abort_request = 1;
419 pthread_cond_signal(&q->cond);
421 pthread_mutex_unlock(&q->mutex);
424 int packet_queue_put(PacketQueue *q, AVPacket *pkt)
428 /* duplicate the packet */
429 if (pkt != &q->flush_pkt && av_dup_packet(pkt) < 0)
432 pkt1 = (AVPacketList *)av_malloc(sizeof(AVPacketList));
438 pthread_mutex_lock(&q->mutex);
444 q->last_pkt->next = pkt1;
447 //q->size += pkt1->pkt.size + sizeof(*pkt1);
448 q->size += pkt1->pkt.size;
449 pthread_cond_signal(&q->cond);
451 pthread_mutex_unlock(&q->mutex);
455 int packet_queue_put_nullpacket(PacketQueue *q, int stream_index)
457 AVPacket pkt1, *pkt = &pkt1;
461 pkt->stream_index = stream_index;
462 return packet_queue_put(q, pkt);
465 /* packet queue handling */
466 /* return < 0 if aborted, 0 if no packet and > 0 if packet. */
467 int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
472 pthread_mutex_lock(&q->mutex);
475 if (q->abort_request) {
482 q->first_pkt = pkt1->next;
486 //q->size -= pkt1->pkt.size + sizeof(*pkt1);
487 q->size -= pkt1->pkt.size;
496 pthread_cond_wait(&q->cond, &q->mutex);
499 pthread_mutex_unlock(&q->mutex);
503 //////////////////////////////////////////////////////////////////////////////////
505 //////////////////////////////////////////////////////////////////////////////////
506 bool setup_vorbis_extradata(uint8_t **extradata, int *extradata_size,
507 const uint8_t *header_start[3], const int header_len[3])
513 len = header_len[0] + header_len[1] + header_len[2];
514 p = *extradata = (uint8_t *)av_mallocz(64 + len + len/255);
516 ALOGE("oom for vorbis extradata");
521 p += av_xiphlacing(p, header_len[0]);
522 p += av_xiphlacing(p, header_len[1]);
523 for (i = 0; i < 3; i++) {
524 if (header_len[i] > 0) {
525 memcpy(p, header_start[i], header_len[i]);
529 *extradata_size = p - *extradata;
534 } // namespace android