You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

592 lines
18KB

  1. /*
  2. * Interface to the Android Stagefright library for
  3. * H/W accelerated H.264 decoding
  4. *
  5. * Copyright (C) 2011 Mohamed Naufal
  6. * Copyright (C) 2011 Martin Storsjö
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include <binder/ProcessState.h>
  25. #include <media/stagefright/MetaData.h>
  26. #include <media/stagefright/MediaBufferGroup.h>
  27. #include <media/stagefright/MediaDebug.h>
  28. #include <media/stagefright/MediaDefs.h>
  29. #include <media/stagefright/OMXClient.h>
  30. #include <media/stagefright/OMXCodec.h>
  31. #include <utils/List.h>
  32. #include <new>
  33. #include <map>
  34. extern "C" {
  35. #include "avcodec.h"
  36. #include "libavutil/imgutils.h"
  37. #include "internal.h"
  38. }
  39. #define OMX_QCOM_COLOR_FormatYVU420SemiPlanar 0x7FA30C00
  40. using namespace android;
  41. struct Frame {
  42. status_t status;
  43. size_t size;
  44. int64_t time;
  45. int key;
  46. uint8_t *buffer;
  47. AVFrame *vframe;
  48. };
  49. struct TimeStamp {
  50. int64_t pts;
  51. int64_t reordered_opaque;
  52. };
  53. class CustomSource;
  54. struct StagefrightContext {
  55. AVCodecContext *avctx;
  56. AVBitStreamFilterContext *bsfc;
  57. uint8_t* orig_extradata;
  58. int orig_extradata_size;
  59. sp<MediaSource> *source;
  60. List<Frame*> *in_queue, *out_queue;
  61. pthread_mutex_t in_mutex, out_mutex;
  62. pthread_cond_t condition;
  63. pthread_t decode_thread_id;
  64. Frame *end_frame;
  65. bool source_done;
  66. volatile sig_atomic_t thread_started, thread_exited, stop_decode;
  67. AVFrame *prev_frame;
  68. std::map<int64_t, TimeStamp> *ts_map;
  69. int64_t frame_index;
  70. uint8_t *dummy_buf;
  71. int dummy_bufsize;
  72. OMXClient *client;
  73. sp<MediaSource> *decoder;
  74. const char *decoder_component;
  75. };
  76. class CustomSource : public MediaSource {
  77. public:
  78. CustomSource(AVCodecContext *avctx, sp<MetaData> meta) {
  79. s = (StagefrightContext*)avctx->priv_data;
  80. source_meta = meta;
  81. frame_size = (avctx->width * avctx->height * 3) / 2;
  82. buf_group.add_buffer(new MediaBuffer(frame_size));
  83. }
  84. virtual sp<MetaData> getFormat() {
  85. return source_meta;
  86. }
  87. virtual status_t start(MetaData *params) {
  88. return OK;
  89. }
  90. virtual status_t stop() {
  91. return OK;
  92. }
  93. virtual status_t read(MediaBuffer **buffer,
  94. const MediaSource::ReadOptions *options) {
  95. Frame *frame;
  96. status_t ret;
  97. if (s->thread_exited)
  98. return ERROR_END_OF_STREAM;
  99. pthread_mutex_lock(&s->in_mutex);
  100. while (s->in_queue->empty())
  101. pthread_cond_wait(&s->condition, &s->in_mutex);
  102. frame = *s->in_queue->begin();
  103. ret = frame->status;
  104. if (ret == OK) {
  105. ret = buf_group.acquire_buffer(buffer);
  106. if (ret == OK) {
  107. memcpy((*buffer)->data(), frame->buffer, frame->size);
  108. (*buffer)->set_range(0, frame->size);
  109. (*buffer)->meta_data()->clear();
  110. (*buffer)->meta_data()->setInt32(kKeyIsSyncFrame,frame->key);
  111. (*buffer)->meta_data()->setInt64(kKeyTime, frame->time);
  112. } else {
  113. av_log(s->avctx, AV_LOG_ERROR, "Failed to acquire MediaBuffer\n");
  114. }
  115. av_freep(&frame->buffer);
  116. }
  117. s->in_queue->erase(s->in_queue->begin());
  118. pthread_mutex_unlock(&s->in_mutex);
  119. av_freep(&frame);
  120. return ret;
  121. }
  122. private:
  123. MediaBufferGroup buf_group;
  124. sp<MetaData> source_meta;
  125. StagefrightContext *s;
  126. int frame_size;
  127. };
  128. void* decode_thread(void *arg)
  129. {
  130. AVCodecContext *avctx = (AVCodecContext*)arg;
  131. StagefrightContext *s = (StagefrightContext*)avctx->priv_data;
  132. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  133. Frame* frame;
  134. MediaBuffer *buffer;
  135. int32_t w, h;
  136. int decode_done = 0;
  137. int ret;
  138. int src_linesize[3];
  139. const uint8_t *src_data[3];
  140. int64_t out_frame_index = 0;
  141. do {
  142. buffer = NULL;
  143. frame = (Frame*)av_mallocz(sizeof(Frame));
  144. if (!frame) {
  145. frame = s->end_frame;
  146. frame->status = AVERROR(ENOMEM);
  147. decode_done = 1;
  148. s->end_frame = NULL;
  149. goto push_frame;
  150. }
  151. frame->status = (*s->decoder)->read(&buffer);
  152. if (frame->status == OK) {
  153. sp<MetaData> outFormat = (*s->decoder)->getFormat();
  154. outFormat->findInt32(kKeyWidth , &w);
  155. outFormat->findInt32(kKeyHeight, &h);
  156. frame->vframe = av_frame_alloc();
  157. if (!frame->vframe) {
  158. frame->status = AVERROR(ENOMEM);
  159. decode_done = 1;
  160. buffer->release();
  161. goto push_frame;
  162. }
  163. ret = ff_get_buffer(avctx, frame->vframe, AV_GET_BUFFER_FLAG_REF);
  164. if (ret < 0) {
  165. frame->status = ret;
  166. decode_done = 1;
  167. buffer->release();
  168. goto push_frame;
  169. }
  170. // The OMX.SEC decoder doesn't signal the modified width/height
  171. if (s->decoder_component && !strncmp(s->decoder_component, "OMX.SEC", 7) &&
  172. (w & 15 || h & 15)) {
  173. if (((w + 15)&~15) * ((h + 15)&~15) * 3/2 == buffer->range_length()) {
  174. w = (w + 15)&~15;
  175. h = (h + 15)&~15;
  176. }
  177. }
  178. if (!avctx->width || !avctx->height || avctx->width > w || avctx->height > h) {
  179. avctx->width = w;
  180. avctx->height = h;
  181. }
  182. src_linesize[0] = av_image_get_linesize(avctx->pix_fmt, w, 0);
  183. src_linesize[1] = av_image_get_linesize(avctx->pix_fmt, w, 1);
  184. src_linesize[2] = av_image_get_linesize(avctx->pix_fmt, w, 2);
  185. src_data[0] = (uint8_t*)buffer->data();
  186. src_data[1] = src_data[0] + src_linesize[0] * h;
  187. src_data[2] = src_data[1] + src_linesize[1] * -(-h>>pix_desc->log2_chroma_h);
  188. av_image_copy(frame->vframe->data, frame->vframe->linesize,
  189. src_data, src_linesize,
  190. avctx->pix_fmt, avctx->width, avctx->height);
  191. buffer->meta_data()->findInt64(kKeyTime, &out_frame_index);
  192. if (out_frame_index && s->ts_map->count(out_frame_index) > 0) {
  193. frame->vframe->pts = (*s->ts_map)[out_frame_index].pts;
  194. frame->vframe->reordered_opaque = (*s->ts_map)[out_frame_index].reordered_opaque;
  195. s->ts_map->erase(out_frame_index);
  196. }
  197. buffer->release();
  198. } else if (frame->status == INFO_FORMAT_CHANGED) {
  199. if (buffer)
  200. buffer->release();
  201. av_free(frame);
  202. continue;
  203. } else {
  204. decode_done = 1;
  205. }
  206. push_frame:
  207. while (true) {
  208. pthread_mutex_lock(&s->out_mutex);
  209. if (s->out_queue->size() >= 10) {
  210. pthread_mutex_unlock(&s->out_mutex);
  211. usleep(10000);
  212. continue;
  213. }
  214. break;
  215. }
  216. s->out_queue->push_back(frame);
  217. pthread_mutex_unlock(&s->out_mutex);
  218. } while (!decode_done && !s->stop_decode);
  219. s->thread_exited = true;
  220. return 0;
  221. }
  222. static av_cold int Stagefright_init(AVCodecContext *avctx)
  223. {
  224. StagefrightContext *s = (StagefrightContext*)avctx->priv_data;
  225. sp<MetaData> meta, outFormat;
  226. int32_t colorFormat = 0;
  227. int ret;
  228. if (!avctx->extradata || !avctx->extradata_size || avctx->extradata[0] != 1)
  229. return -1;
  230. s->avctx = avctx;
  231. s->bsfc = av_bitstream_filter_init("h264_mp4toannexb");
  232. if (!s->bsfc) {
  233. av_log(avctx, AV_LOG_ERROR, "Cannot open the h264_mp4toannexb BSF!\n");
  234. return -1;
  235. }
  236. s->orig_extradata_size = avctx->extradata_size;
  237. s->orig_extradata = (uint8_t*) av_mallocz(avctx->extradata_size +
  238. AV_INPUT_BUFFER_PADDING_SIZE);
  239. if (!s->orig_extradata) {
  240. ret = AVERROR(ENOMEM);
  241. goto fail;
  242. }
  243. memcpy(s->orig_extradata, avctx->extradata, avctx->extradata_size);
  244. meta = new MetaData;
  245. if (!meta) {
  246. ret = AVERROR(ENOMEM);
  247. goto fail;
  248. }
  249. meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
  250. meta->setInt32(kKeyWidth, avctx->width);
  251. meta->setInt32(kKeyHeight, avctx->height);
  252. meta->setData(kKeyAVCC, kTypeAVCC, avctx->extradata, avctx->extradata_size);
  253. android::ProcessState::self()->startThreadPool();
  254. s->source = new sp<MediaSource>();
  255. *s->source = new CustomSource(avctx, meta);
  256. s->in_queue = new List<Frame*>;
  257. s->out_queue = new List<Frame*>;
  258. s->ts_map = new std::map<int64_t, TimeStamp>;
  259. s->client = new OMXClient;
  260. s->end_frame = (Frame*)av_mallocz(sizeof(Frame));
  261. if (s->source == NULL || !s->in_queue || !s->out_queue || !s->client ||
  262. !s->ts_map || !s->end_frame) {
  263. ret = AVERROR(ENOMEM);
  264. goto fail;
  265. }
  266. if (s->client->connect() != OK) {
  267. av_log(avctx, AV_LOG_ERROR, "Cannot connect OMX client\n");
  268. ret = -1;
  269. goto fail;
  270. }
  271. s->decoder = new sp<MediaSource>();
  272. *s->decoder = OMXCodec::Create(s->client->interface(), meta,
  273. false, *s->source, NULL,
  274. OMXCodec::kClientNeedsFramebuffer);
  275. if ((*s->decoder)->start() != OK) {
  276. av_log(avctx, AV_LOG_ERROR, "Cannot start decoder\n");
  277. ret = -1;
  278. s->client->disconnect();
  279. goto fail;
  280. }
  281. outFormat = (*s->decoder)->getFormat();
  282. outFormat->findInt32(kKeyColorFormat, &colorFormat);
  283. if (colorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar ||
  284. colorFormat == OMX_COLOR_FormatYUV420SemiPlanar)
  285. avctx->pix_fmt = AV_PIX_FMT_NV21;
  286. else if (colorFormat == OMX_COLOR_FormatYCbYCr)
  287. avctx->pix_fmt = AV_PIX_FMT_YUYV422;
  288. else if (colorFormat == OMX_COLOR_FormatCbYCrY)
  289. avctx->pix_fmt = AV_PIX_FMT_UYVY422;
  290. else
  291. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  292. outFormat->findCString(kKeyDecoderComponent, &s->decoder_component);
  293. if (s->decoder_component)
  294. s->decoder_component = av_strdup(s->decoder_component);
  295. pthread_mutex_init(&s->in_mutex, NULL);
  296. pthread_mutex_init(&s->out_mutex, NULL);
  297. pthread_cond_init(&s->condition, NULL);
  298. return 0;
  299. fail:
  300. av_bitstream_filter_close(s->bsfc);
  301. av_freep(&s->orig_extradata);
  302. av_freep(&s->end_frame);
  303. delete s->in_queue;
  304. delete s->out_queue;
  305. delete s->ts_map;
  306. delete s->client;
  307. return ret;
  308. }
  309. static int Stagefright_decode_frame(AVCodecContext *avctx, void *data,
  310. int *got_frame, AVPacket *avpkt)
  311. {
  312. StagefrightContext *s = (StagefrightContext*)avctx->priv_data;
  313. Frame *frame;
  314. status_t status;
  315. int orig_size = avpkt->size;
  316. AVPacket pkt = *avpkt;
  317. AVFrame *ret_frame;
  318. if (!s->thread_started) {
  319. if(pthread_create(&s->decode_thread_id, NULL, &decode_thread, avctx))
  320. return AVERROR(ENOMEM);
  321. s->thread_started = true;
  322. }
  323. if (avpkt && avpkt->data) {
  324. av_bitstream_filter_filter(s->bsfc, avctx, NULL, &pkt.data, &pkt.size,
  325. avpkt->data, avpkt->size, avpkt->flags & AV_PKT_FLAG_KEY);
  326. avpkt = &pkt;
  327. }
  328. if (!s->source_done) {
  329. if(!s->dummy_buf) {
  330. s->dummy_buf = (uint8_t*)av_malloc(avpkt->size);
  331. if (!s->dummy_buf)
  332. return AVERROR(ENOMEM);
  333. s->dummy_bufsize = avpkt->size;
  334. memcpy(s->dummy_buf, avpkt->data, avpkt->size);
  335. }
  336. frame = (Frame*)av_mallocz(sizeof(Frame));
  337. if (avpkt->data) {
  338. frame->status = OK;
  339. frame->size = avpkt->size;
  340. frame->key = avpkt->flags & AV_PKT_FLAG_KEY ? 1 : 0;
  341. frame->buffer = (uint8_t*)av_malloc(avpkt->size);
  342. if (!frame->buffer) {
  343. av_freep(&frame);
  344. return AVERROR(ENOMEM);
  345. }
  346. uint8_t *ptr = avpkt->data;
  347. // The OMX.SEC decoder fails without this.
  348. if (avpkt->size == orig_size + avctx->extradata_size) {
  349. ptr += avctx->extradata_size;
  350. frame->size = orig_size;
  351. }
  352. memcpy(frame->buffer, ptr, orig_size);
  353. if (avpkt == &pkt)
  354. av_free(avpkt->data);
  355. frame->time = ++s->frame_index;
  356. (*s->ts_map)[s->frame_index].pts = avpkt->pts;
  357. (*s->ts_map)[s->frame_index].reordered_opaque = avctx->reordered_opaque;
  358. } else {
  359. frame->status = ERROR_END_OF_STREAM;
  360. s->source_done = true;
  361. }
  362. while (true) {
  363. if (s->thread_exited) {
  364. s->source_done = true;
  365. break;
  366. }
  367. pthread_mutex_lock(&s->in_mutex);
  368. if (s->in_queue->size() >= 10) {
  369. pthread_mutex_unlock(&s->in_mutex);
  370. usleep(10000);
  371. continue;
  372. }
  373. s->in_queue->push_back(frame);
  374. pthread_cond_signal(&s->condition);
  375. pthread_mutex_unlock(&s->in_mutex);
  376. break;
  377. }
  378. }
  379. while (true) {
  380. pthread_mutex_lock(&s->out_mutex);
  381. if (!s->out_queue->empty()) break;
  382. pthread_mutex_unlock(&s->out_mutex);
  383. if (s->source_done) {
  384. usleep(10000);
  385. continue;
  386. } else {
  387. return orig_size;
  388. }
  389. }
  390. frame = *s->out_queue->begin();
  391. s->out_queue->erase(s->out_queue->begin());
  392. pthread_mutex_unlock(&s->out_mutex);
  393. ret_frame = frame->vframe;
  394. status = frame->status;
  395. av_freep(&frame);
  396. if (status == ERROR_END_OF_STREAM)
  397. return 0;
  398. if (status != OK) {
  399. if (status == AVERROR(ENOMEM))
  400. return status;
  401. av_log(avctx, AV_LOG_ERROR, "Decode failed: %x\n", status);
  402. return -1;
  403. }
  404. if (s->prev_frame)
  405. av_frame_free(&s->prev_frame);
  406. s->prev_frame = ret_frame;
  407. *got_frame = 1;
  408. *(AVFrame*)data = *ret_frame;
  409. return orig_size;
  410. }
  411. static av_cold int Stagefright_close(AVCodecContext *avctx)
  412. {
  413. StagefrightContext *s = (StagefrightContext*)avctx->priv_data;
  414. Frame *frame;
  415. if (s->thread_started) {
  416. if (!s->thread_exited) {
  417. s->stop_decode = 1;
  418. // Make sure decode_thread() doesn't get stuck
  419. pthread_mutex_lock(&s->out_mutex);
  420. while (!s->out_queue->empty()) {
  421. frame = *s->out_queue->begin();
  422. s->out_queue->erase(s->out_queue->begin());
  423. if (frame->vframe)
  424. av_frame_free(&frame->vframe);
  425. av_freep(&frame);
  426. }
  427. pthread_mutex_unlock(&s->out_mutex);
  428. // Feed a dummy frame prior to signalling EOF.
  429. // This is required to terminate the decoder(OMX.SEC)
  430. // when only one frame is read during stream info detection.
  431. if (s->dummy_buf && (frame = (Frame*)av_mallocz(sizeof(Frame)))) {
  432. frame->status = OK;
  433. frame->size = s->dummy_bufsize;
  434. frame->key = 1;
  435. frame->buffer = s->dummy_buf;
  436. pthread_mutex_lock(&s->in_mutex);
  437. s->in_queue->push_back(frame);
  438. pthread_cond_signal(&s->condition);
  439. pthread_mutex_unlock(&s->in_mutex);
  440. s->dummy_buf = NULL;
  441. }
  442. pthread_mutex_lock(&s->in_mutex);
  443. s->end_frame->status = ERROR_END_OF_STREAM;
  444. s->in_queue->push_back(s->end_frame);
  445. pthread_cond_signal(&s->condition);
  446. pthread_mutex_unlock(&s->in_mutex);
  447. s->end_frame = NULL;
  448. }
  449. pthread_join(s->decode_thread_id, NULL);
  450. if (s->prev_frame)
  451. av_frame_free(&s->prev_frame);
  452. s->thread_started = false;
  453. }
  454. while (!s->in_queue->empty()) {
  455. frame = *s->in_queue->begin();
  456. s->in_queue->erase(s->in_queue->begin());
  457. if (frame->size)
  458. av_freep(&frame->buffer);
  459. av_freep(&frame);
  460. }
  461. while (!s->out_queue->empty()) {
  462. frame = *s->out_queue->begin();
  463. s->out_queue->erase(s->out_queue->begin());
  464. if (frame->vframe)
  465. av_frame_free(&frame->vframe);
  466. av_freep(&frame);
  467. }
  468. (*s->decoder)->stop();
  469. s->client->disconnect();
  470. if (s->decoder_component)
  471. av_freep(&s->decoder_component);
  472. av_freep(&s->dummy_buf);
  473. av_freep(&s->end_frame);
  474. // Reset the extradata back to the original mp4 format, so that
  475. // the next invocation (both when decoding and when called from
  476. // av_find_stream_info) get the original mp4 format extradata.
  477. av_freep(&avctx->extradata);
  478. avctx->extradata = s->orig_extradata;
  479. avctx->extradata_size = s->orig_extradata_size;
  480. delete s->in_queue;
  481. delete s->out_queue;
  482. delete s->ts_map;
  483. delete s->client;
  484. delete s->decoder;
  485. delete s->source;
  486. pthread_mutex_destroy(&s->in_mutex);
  487. pthread_mutex_destroy(&s->out_mutex);
  488. pthread_cond_destroy(&s->condition);
  489. av_bitstream_filter_close(s->bsfc);
  490. return 0;
  491. }
  492. AVCodec ff_libstagefright_h264_decoder = {
  493. "libstagefright_h264",
  494. NULL_IF_CONFIG_SMALL("libstagefright H.264"),
  495. AVMEDIA_TYPE_VIDEO,
  496. AV_CODEC_ID_H264,
  497. AV_CODEC_CAP_DELAY,
  498. NULL, //supported_framerates
  499. NULL, //pix_fmts
  500. NULL, //supported_samplerates
  501. NULL, //sample_fmts
  502. NULL, //channel_layouts
  503. 0, //max_lowres
  504. NULL, //priv_class
  505. NULL, //profiles
  506. sizeof(StagefrightContext),
  507. NULL, //next
  508. NULL, //init_thread_copy
  509. NULL, //update_thread_context
  510. NULL, //defaults
  511. NULL, //init_static_data
  512. Stagefright_init,
  513. NULL, //encode
  514. NULL, //encode2
  515. Stagefright_decode_frame,
  516. Stagefright_close,
  517. };