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.

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