00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <binder/ProcessState.h>
00026 #include <media/stagefright/MetaData.h>
00027 #include <media/stagefright/MediaBufferGroup.h>
00028 #include <media/stagefright/MediaDebug.h>
00029 #include <media/stagefright/MediaDefs.h>
00030 #include <media/stagefright/OMXClient.h>
00031 #include <media/stagefright/OMXCodec.h>
00032 #include <utils/List.h>
00033 #include <new>
00034 #include <map>
00035
00036 extern "C" {
00037 #include "avcodec.h"
00038 #include "libavutil/imgutils.h"
00039 }
00040
00041 #define OMX_QCOM_COLOR_FormatYVU420SemiPlanar 0x7FA30C00
00042
00043 using namespace android;
00044
00045 struct Frame {
00046 status_t status;
00047 size_t size;
00048 int64_t time;
00049 int key;
00050 uint8_t *buffer;
00051 AVFrame *vframe;
00052 };
00053
00054 struct TimeStamp {
00055 int64_t pts;
00056 int64_t reordered_opaque;
00057 };
00058
00059 class CustomSource;
00060
00061 struct StagefrightContext {
00062 AVCodecContext *avctx;
00063 AVBitStreamFilterContext *bsfc;
00064 uint8_t* orig_extradata;
00065 int orig_extradata_size;
00066 sp<MediaSource> *source;
00067 List<Frame*> *in_queue, *out_queue;
00068 pthread_mutex_t in_mutex, out_mutex;
00069 pthread_cond_t condition;
00070 pthread_t decode_thread_id;
00071
00072 Frame *end_frame;
00073 bool source_done;
00074 volatile sig_atomic_t thread_started, thread_exited, stop_decode;
00075
00076 AVFrame *prev_frame;
00077 std::map<int64_t, TimeStamp> *ts_map;
00078 int64_t frame_index;
00079
00080 uint8_t *dummy_buf;
00081 int dummy_bufsize;
00082
00083 OMXClient *client;
00084 sp<MediaSource> *decoder;
00085 const char *decoder_component;
00086 };
00087
00088 class CustomSource : public MediaSource {
00089 public:
00090 CustomSource(AVCodecContext *avctx, sp<MetaData> meta) {
00091 s = (StagefrightContext*)avctx->priv_data;
00092 source_meta = meta;
00093 frame_size = (avctx->width * avctx->height * 3) / 2;
00094 buf_group.add_buffer(new MediaBuffer(frame_size));
00095 }
00096
00097 virtual sp<MetaData> getFormat() {
00098 return source_meta;
00099 }
00100
00101 virtual status_t start(MetaData *params) {
00102 return OK;
00103 }
00104
00105 virtual status_t stop() {
00106 return OK;
00107 }
00108
00109 virtual status_t read(MediaBuffer **buffer,
00110 const MediaSource::ReadOptions *options) {
00111 Frame *frame;
00112 status_t ret;
00113
00114 if (s->thread_exited)
00115 return ERROR_END_OF_STREAM;
00116 pthread_mutex_lock(&s->in_mutex);
00117
00118 while (s->in_queue->empty())
00119 pthread_cond_wait(&s->condition, &s->in_mutex);
00120
00121 frame = *s->in_queue->begin();
00122 ret = frame->status;
00123
00124 if (ret == OK) {
00125 ret = buf_group.acquire_buffer(buffer);
00126 if (ret == OK) {
00127 memcpy((*buffer)->data(), frame->buffer, frame->size);
00128 (*buffer)->set_range(0, frame->size);
00129 (*buffer)->meta_data()->clear();
00130 (*buffer)->meta_data()->setInt32(kKeyIsSyncFrame,frame->key);
00131 (*buffer)->meta_data()->setInt64(kKeyTime, frame->time);
00132 } else {
00133 av_log(s->avctx, AV_LOG_ERROR, "Failed to acquire MediaBuffer\n");
00134 }
00135 av_freep(&frame->buffer);
00136 }
00137
00138 s->in_queue->erase(s->in_queue->begin());
00139 pthread_mutex_unlock(&s->in_mutex);
00140
00141 av_freep(&frame);
00142 return ret;
00143 }
00144
00145 private:
00146 MediaBufferGroup buf_group;
00147 sp<MetaData> source_meta;
00148 StagefrightContext *s;
00149 int frame_size;
00150 };
00151
00152 void* decode_thread(void *arg)
00153 {
00154 AVCodecContext *avctx = (AVCodecContext*)arg;
00155 StagefrightContext *s = (StagefrightContext*)avctx->priv_data;
00156 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[avctx->pix_fmt];
00157 Frame* frame;
00158 MediaBuffer *buffer;
00159 int32_t w, h;
00160 int decode_done = 0;
00161 int ret;
00162 int src_linesize[3];
00163 const uint8_t *src_data[3];
00164 int64_t out_frame_index = 0;
00165
00166 do {
00167 buffer = NULL;
00168 frame = (Frame*)av_mallocz(sizeof(Frame));
00169 if (!frame) {
00170 frame = s->end_frame;
00171 frame->status = AVERROR(ENOMEM);
00172 decode_done = 1;
00173 s->end_frame = NULL;
00174 goto push_frame;
00175 }
00176 frame->status = (*s->decoder)->read(&buffer);
00177 if (frame->status == OK) {
00178 sp<MetaData> outFormat = (*s->decoder)->getFormat();
00179 outFormat->findInt32(kKeyWidth , &w);
00180 outFormat->findInt32(kKeyHeight, &h);
00181 frame->vframe = (AVFrame*)av_mallocz(sizeof(AVFrame));
00182 if (!frame->vframe) {
00183 frame->status = AVERROR(ENOMEM);
00184 decode_done = 1;
00185 buffer->release();
00186 goto push_frame;
00187 }
00188 ret = avctx->get_buffer(avctx, frame->vframe);
00189 if (ret < 0) {
00190 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00191 frame->status = ret;
00192 decode_done = 1;
00193 buffer->release();
00194 goto push_frame;
00195 }
00196
00197
00198 if (s->decoder_component && !strncmp(s->decoder_component, "OMX.SEC", 7) &&
00199 (w & 15 || h & 15)) {
00200 if (((w + 15)&~15) * ((h + 15)&~15) * 3/2 == buffer->range_length()) {
00201 w = (w + 15)&~15;
00202 h = (h + 15)&~15;
00203 }
00204 }
00205
00206 if (!avctx->width || !avctx->height || avctx->width > w || avctx->height > h) {
00207 avctx->width = w;
00208 avctx->height = h;
00209 }
00210
00211 src_linesize[0] = av_image_get_linesize(avctx->pix_fmt, w, 0);
00212 src_linesize[1] = av_image_get_linesize(avctx->pix_fmt, w, 1);
00213 src_linesize[2] = av_image_get_linesize(avctx->pix_fmt, w, 2);
00214
00215 src_data[0] = (uint8_t*)buffer->data();
00216 src_data[1] = src_data[0] + src_linesize[0] * h;
00217 src_data[2] = src_data[1] + src_linesize[1] * -(-h>>pix_desc->log2_chroma_h);
00218 av_image_copy(frame->vframe->data, frame->vframe->linesize,
00219 src_data, src_linesize,
00220 avctx->pix_fmt, avctx->width, avctx->height);
00221
00222 buffer->meta_data()->findInt64(kKeyTime, &out_frame_index);
00223 if (out_frame_index && s->ts_map->count(out_frame_index) > 0) {
00224 frame->vframe->pts = (*s->ts_map)[out_frame_index].pts;
00225 frame->vframe->reordered_opaque = (*s->ts_map)[out_frame_index].reordered_opaque;
00226 s->ts_map->erase(out_frame_index);
00227 }
00228 buffer->release();
00229 } else if (frame->status == INFO_FORMAT_CHANGED) {
00230 if (buffer)
00231 buffer->release();
00232 av_free(frame);
00233 continue;
00234 } else {
00235 decode_done = 1;
00236 }
00237 push_frame:
00238 while (true) {
00239 pthread_mutex_lock(&s->out_mutex);
00240 if (s->out_queue->size() >= 10) {
00241 pthread_mutex_unlock(&s->out_mutex);
00242 usleep(10000);
00243 continue;
00244 }
00245 break;
00246 }
00247 s->out_queue->push_back(frame);
00248 pthread_mutex_unlock(&s->out_mutex);
00249 } while (!decode_done && !s->stop_decode);
00250
00251 s->thread_exited = true;
00252
00253 return 0;
00254 }
00255
00256 static av_cold int Stagefright_init(AVCodecContext *avctx)
00257 {
00258 StagefrightContext *s = (StagefrightContext*)avctx->priv_data;
00259 sp<MetaData> meta, outFormat;
00260 int32_t colorFormat = 0;
00261 int ret;
00262
00263 if (!avctx->extradata || !avctx->extradata_size || avctx->extradata[0] != 1)
00264 return -1;
00265
00266 s->avctx = avctx;
00267 s->bsfc = av_bitstream_filter_init("h264_mp4toannexb");
00268 if (!s->bsfc) {
00269 av_log(avctx, AV_LOG_ERROR, "Cannot open the h264_mp4toannexb BSF!\n");
00270 return -1;
00271 }
00272
00273 s->orig_extradata_size = avctx->extradata_size;
00274 s->orig_extradata = (uint8_t*) av_mallocz(avctx->extradata_size +
00275 FF_INPUT_BUFFER_PADDING_SIZE);
00276 if (!s->orig_extradata) {
00277 ret = AVERROR(ENOMEM);
00278 goto fail;
00279 }
00280 memcpy(s->orig_extradata, avctx->extradata, avctx->extradata_size);
00281
00282 meta = new MetaData;
00283 if (meta == NULL) {
00284 ret = AVERROR(ENOMEM);
00285 goto fail;
00286 }
00287 meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
00288 meta->setInt32(kKeyWidth, avctx->width);
00289 meta->setInt32(kKeyHeight, avctx->height);
00290 meta->setData(kKeyAVCC, kTypeAVCC, avctx->extradata, avctx->extradata_size);
00291
00292 android::ProcessState::self()->startThreadPool();
00293
00294 s->source = new sp<MediaSource>();
00295 *s->source = new CustomSource(avctx, meta);
00296 s->in_queue = new List<Frame*>;
00297 s->out_queue = new List<Frame*>;
00298 s->ts_map = new std::map<int64_t, TimeStamp>;
00299 s->client = new OMXClient;
00300 s->end_frame = (Frame*)av_mallocz(sizeof(Frame));
00301 if (s->source == NULL || !s->in_queue || !s->out_queue || !s->client ||
00302 !s->ts_map || !s->end_frame) {
00303 ret = AVERROR(ENOMEM);
00304 goto fail;
00305 }
00306
00307 if (s->client->connect() != OK) {
00308 av_log(avctx, AV_LOG_ERROR, "Cannot connect OMX client\n");
00309 ret = -1;
00310 goto fail;
00311 }
00312
00313 s->decoder = new sp<MediaSource>();
00314 *s->decoder = OMXCodec::Create(s->client->interface(), meta,
00315 false, *s->source, NULL,
00316 OMXCodec::kClientNeedsFramebuffer);
00317 if ((*s->decoder)->start() != OK) {
00318 av_log(avctx, AV_LOG_ERROR, "Cannot start decoder\n");
00319 ret = -1;
00320 s->client->disconnect();
00321 goto fail;
00322 }
00323
00324 outFormat = (*s->decoder)->getFormat();
00325 outFormat->findInt32(kKeyColorFormat, &colorFormat);
00326 if (colorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar ||
00327 colorFormat == OMX_COLOR_FormatYUV420SemiPlanar)
00328 avctx->pix_fmt = PIX_FMT_NV21;
00329 else if (colorFormat == OMX_COLOR_FormatYCbYCr)
00330 avctx->pix_fmt = PIX_FMT_YUYV422;
00331 else if (colorFormat == OMX_COLOR_FormatCbYCrY)
00332 avctx->pix_fmt = PIX_FMT_UYVY422;
00333 else
00334 avctx->pix_fmt = PIX_FMT_YUV420P;
00335
00336 outFormat->findCString(kKeyDecoderComponent, &s->decoder_component);
00337 if (s->decoder_component)
00338 s->decoder_component = av_strdup(s->decoder_component);
00339
00340 pthread_mutex_init(&s->in_mutex, NULL);
00341 pthread_mutex_init(&s->out_mutex, NULL);
00342 pthread_cond_init(&s->condition, NULL);
00343 return 0;
00344
00345 fail:
00346 av_bitstream_filter_close(s->bsfc);
00347 av_freep(&s->orig_extradata);
00348 av_freep(&s->end_frame);
00349 delete s->in_queue;
00350 delete s->out_queue;
00351 delete s->ts_map;
00352 delete s->client;
00353 return ret;
00354 }
00355
00356 static int Stagefright_decode_frame(AVCodecContext *avctx, void *data,
00357 int *data_size, AVPacket *avpkt)
00358 {
00359 StagefrightContext *s = (StagefrightContext*)avctx->priv_data;
00360 Frame *frame;
00361 status_t status;
00362 int orig_size = avpkt->size;
00363 AVPacket pkt = *avpkt;
00364 AVFrame *ret_frame;
00365
00366 if (!s->thread_started) {
00367 pthread_create(&s->decode_thread_id, NULL, &decode_thread, avctx);
00368 s->thread_started = true;
00369 }
00370
00371 if (avpkt && avpkt->data) {
00372 av_bitstream_filter_filter(s->bsfc, avctx, NULL, &pkt.data, &pkt.size,
00373 avpkt->data, avpkt->size, avpkt->flags & AV_PKT_FLAG_KEY);
00374 avpkt = &pkt;
00375 }
00376
00377 if (!s->source_done) {
00378 if(!s->dummy_buf) {
00379 s->dummy_buf = (uint8_t*)av_malloc(avpkt->size);
00380 if (!s->dummy_buf)
00381 return AVERROR(ENOMEM);
00382 s->dummy_bufsize = avpkt->size;
00383 memcpy(s->dummy_buf, avpkt->data, avpkt->size);
00384 }
00385
00386 frame = (Frame*)av_mallocz(sizeof(Frame));
00387 if (avpkt->data) {
00388 frame->status = OK;
00389 frame->size = avpkt->size;
00390 frame->key = avpkt->flags & AV_PKT_FLAG_KEY ? 1 : 0;
00391 frame->buffer = (uint8_t*)av_malloc(avpkt->size);
00392 if (!frame->buffer) {
00393 av_freep(&frame);
00394 return AVERROR(ENOMEM);
00395 }
00396 uint8_t *ptr = avpkt->data;
00397
00398 if (avpkt->size == orig_size + avctx->extradata_size) {
00399 ptr += avctx->extradata_size;
00400 frame->size = orig_size;
00401 }
00402 memcpy(frame->buffer, ptr, orig_size);
00403 if (avpkt == &pkt)
00404 av_free(avpkt->data);
00405
00406 frame->time = ++s->frame_index;
00407 (*s->ts_map)[s->frame_index].pts = avpkt->pts;
00408 (*s->ts_map)[s->frame_index].reordered_opaque = avctx->reordered_opaque;
00409 } else {
00410 frame->status = ERROR_END_OF_STREAM;
00411 s->source_done = true;
00412 }
00413
00414 while (true) {
00415 if (s->thread_exited) {
00416 s->source_done = true;
00417 break;
00418 }
00419 pthread_mutex_lock(&s->in_mutex);
00420 if (s->in_queue->size() >= 10) {
00421 pthread_mutex_unlock(&s->in_mutex);
00422 usleep(10000);
00423 continue;
00424 }
00425 s->in_queue->push_back(frame);
00426 pthread_cond_signal(&s->condition);
00427 pthread_mutex_unlock(&s->in_mutex);
00428 break;
00429 }
00430 }
00431 while (true) {
00432 pthread_mutex_lock(&s->out_mutex);
00433 if (!s->out_queue->empty()) break;
00434 pthread_mutex_unlock(&s->out_mutex);
00435 if (s->source_done) {
00436 usleep(10000);
00437 continue;
00438 } else {
00439 return orig_size;
00440 }
00441 }
00442
00443 frame = *s->out_queue->begin();
00444 s->out_queue->erase(s->out_queue->begin());
00445 pthread_mutex_unlock(&s->out_mutex);
00446
00447 ret_frame = frame->vframe;
00448 status = frame->status;
00449 av_freep(&frame);
00450
00451 if (status == ERROR_END_OF_STREAM)
00452 return 0;
00453 if (status != OK) {
00454 if (status == AVERROR(ENOMEM))
00455 return status;
00456 av_log(avctx, AV_LOG_ERROR, "Decode failed: %x\n", status);
00457 return -1;
00458 }
00459
00460 if (s->prev_frame) {
00461 avctx->release_buffer(avctx, s->prev_frame);
00462 av_freep(&s->prev_frame);
00463 }
00464 s->prev_frame = ret_frame;
00465
00466 *data_size = sizeof(AVFrame);
00467 *(AVFrame*)data = *ret_frame;
00468 return orig_size;
00469 }
00470
00471 static av_cold int Stagefright_close(AVCodecContext *avctx)
00472 {
00473 StagefrightContext *s = (StagefrightContext*)avctx->priv_data;
00474 Frame *frame;
00475
00476 if (s->thread_started) {
00477 if (!s->thread_exited) {
00478 s->stop_decode = 1;
00479
00480
00481 pthread_mutex_lock(&s->out_mutex);
00482 while (!s->out_queue->empty()) {
00483 frame = *s->out_queue->begin();
00484 s->out_queue->erase(s->out_queue->begin());
00485 if (frame->vframe) {
00486 avctx->release_buffer(avctx, frame->vframe);
00487 av_freep(&frame->vframe);
00488 }
00489 av_freep(&frame);
00490 }
00491 pthread_mutex_unlock(&s->out_mutex);
00492
00493
00494
00495
00496 if (s->dummy_buf && (frame = (Frame*)av_mallocz(sizeof(Frame)))) {
00497 frame->status = OK;
00498 frame->size = s->dummy_bufsize;
00499 frame->key = 1;
00500 frame->buffer = s->dummy_buf;
00501 pthread_mutex_lock(&s->in_mutex);
00502 s->in_queue->push_back(frame);
00503 pthread_cond_signal(&s->condition);
00504 pthread_mutex_unlock(&s->in_mutex);
00505 s->dummy_buf = NULL;
00506 }
00507
00508 pthread_mutex_lock(&s->in_mutex);
00509 s->end_frame->status = ERROR_END_OF_STREAM;
00510 s->in_queue->push_back(s->end_frame);
00511 pthread_cond_signal(&s->condition);
00512 pthread_mutex_unlock(&s->in_mutex);
00513 s->end_frame = NULL;
00514 }
00515
00516 pthread_join(s->decode_thread_id, NULL);
00517
00518 if (s->prev_frame) {
00519 avctx->release_buffer(avctx, s->prev_frame);
00520 av_freep(&s->prev_frame);
00521 }
00522
00523 s->thread_started = false;
00524 }
00525
00526 while (!s->in_queue->empty()) {
00527 frame = *s->in_queue->begin();
00528 s->in_queue->erase(s->in_queue->begin());
00529 if (frame->size)
00530 av_freep(&frame->buffer);
00531 av_freep(&frame);
00532 }
00533
00534 while (!s->out_queue->empty()) {
00535 frame = *s->out_queue->begin();
00536 s->out_queue->erase(s->out_queue->begin());
00537 if (frame->vframe) {
00538 avctx->release_buffer(avctx, frame->vframe);
00539 av_freep(&frame->vframe);
00540 }
00541 av_freep(&frame);
00542 }
00543
00544 (*s->decoder)->stop();
00545 s->client->disconnect();
00546
00547 if (s->decoder_component)
00548 av_freep(&s->decoder_component);
00549 av_freep(&s->dummy_buf);
00550 av_freep(&s->end_frame);
00551
00552
00553
00554
00555 av_freep(&avctx->extradata);
00556 avctx->extradata = s->orig_extradata;
00557 avctx->extradata_size = s->orig_extradata_size;
00558
00559 delete s->in_queue;
00560 delete s->out_queue;
00561 delete s->ts_map;
00562 delete s->client;
00563 delete s->decoder;
00564 delete s->source;
00565
00566 pthread_mutex_destroy(&s->in_mutex);
00567 pthread_mutex_destroy(&s->out_mutex);
00568 pthread_cond_destroy(&s->condition);
00569 av_bitstream_filter_close(s->bsfc);
00570 return 0;
00571 }
00572
00573 AVCodec ff_libstagefright_h264_decoder = {
00574 "libstagefright_h264",
00575 NULL_IF_CONFIG_SMALL("libstagefright H.264"),
00576 AVMEDIA_TYPE_VIDEO,
00577 CODEC_ID_H264,
00578 CODEC_CAP_DELAY,
00579 NULL,
00580 NULL,
00581 NULL,
00582 NULL,
00583 NULL,
00584 0,
00585 NULL,
00586 NULL,
00587 sizeof(StagefrightContext),
00588 NULL,
00589 NULL,
00590 NULL,
00591 NULL,
00592 NULL,
00593 Stagefright_init,
00594 NULL,
00595 NULL,
00596 Stagefright_decode_frame,
00597 Stagefright_close,
00598 };