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.

591 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 = (AVFrame*)av_mallocz(sizeof(AVFrame));
  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. FF_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 == NULL) {
  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. pthread_create(&s->decode_thread_id, NULL, &decode_thread, avctx);
  320. s->thread_started = true;
  321. }
  322. if (avpkt && avpkt->data) {
  323. av_bitstream_filter_filter(s->bsfc, avctx, NULL, &pkt.data, &pkt.size,
  324. avpkt->data, avpkt->size, avpkt->flags & AV_PKT_FLAG_KEY);
  325. avpkt = &pkt;
  326. }
  327. if (!s->source_done) {
  328. if(!s->dummy_buf) {
  329. s->dummy_buf = (uint8_t*)av_malloc(avpkt->size);
  330. if (!s->dummy_buf)
  331. return AVERROR(ENOMEM);
  332. s->dummy_bufsize = avpkt->size;
  333. memcpy(s->dummy_buf, avpkt->data, avpkt->size);
  334. }
  335. frame = (Frame*)av_mallocz(sizeof(Frame));
  336. if (avpkt->data) {
  337. frame->status = OK;
  338. frame->size = avpkt->size;
  339. frame->key = avpkt->flags & AV_PKT_FLAG_KEY ? 1 : 0;
  340. frame->buffer = (uint8_t*)av_malloc(avpkt->size);
  341. if (!frame->buffer) {
  342. av_freep(&frame);
  343. return AVERROR(ENOMEM);
  344. }
  345. uint8_t *ptr = avpkt->data;
  346. // The OMX.SEC decoder fails without this.
  347. if (avpkt->size == orig_size + avctx->extradata_size) {
  348. ptr += avctx->extradata_size;
  349. frame->size = orig_size;
  350. }
  351. memcpy(frame->buffer, ptr, orig_size);
  352. if (avpkt == &pkt)
  353. av_free(avpkt->data);
  354. frame->time = ++s->frame_index;
  355. (*s->ts_map)[s->frame_index].pts = avpkt->pts;
  356. (*s->ts_map)[s->frame_index].reordered_opaque = avctx->reordered_opaque;
  357. } else {
  358. frame->status = ERROR_END_OF_STREAM;
  359. s->source_done = true;
  360. }
  361. while (true) {
  362. if (s->thread_exited) {
  363. s->source_done = true;
  364. break;
  365. }
  366. pthread_mutex_lock(&s->in_mutex);
  367. if (s->in_queue->size() >= 10) {
  368. pthread_mutex_unlock(&s->in_mutex);
  369. usleep(10000);
  370. continue;
  371. }
  372. s->in_queue->push_back(frame);
  373. pthread_cond_signal(&s->condition);
  374. pthread_mutex_unlock(&s->in_mutex);
  375. break;
  376. }
  377. }
  378. while (true) {
  379. pthread_mutex_lock(&s->out_mutex);
  380. if (!s->out_queue->empty()) break;
  381. pthread_mutex_unlock(&s->out_mutex);
  382. if (s->source_done) {
  383. usleep(10000);
  384. continue;
  385. } else {
  386. return orig_size;
  387. }
  388. }
  389. frame = *s->out_queue->begin();
  390. s->out_queue->erase(s->out_queue->begin());
  391. pthread_mutex_unlock(&s->out_mutex);
  392. ret_frame = frame->vframe;
  393. status = frame->status;
  394. av_freep(&frame);
  395. if (status == ERROR_END_OF_STREAM)
  396. return 0;
  397. if (status != OK) {
  398. if (status == AVERROR(ENOMEM))
  399. return status;
  400. av_log(avctx, AV_LOG_ERROR, "Decode failed: %x\n", status);
  401. return -1;
  402. }
  403. if (s->prev_frame)
  404. av_frame_free(&s->prev_frame);
  405. s->prev_frame = ret_frame;
  406. *got_frame = 1;
  407. *(AVFrame*)data = *ret_frame;
  408. return orig_size;
  409. }
  410. static av_cold int Stagefright_close(AVCodecContext *avctx)
  411. {
  412. StagefrightContext *s = (StagefrightContext*)avctx->priv_data;
  413. Frame *frame;
  414. if (s->thread_started) {
  415. if (!s->thread_exited) {
  416. s->stop_decode = 1;
  417. // Make sure decode_thread() doesn't get stuck
  418. pthread_mutex_lock(&s->out_mutex);
  419. while (!s->out_queue->empty()) {
  420. frame = *s->out_queue->begin();
  421. s->out_queue->erase(s->out_queue->begin());
  422. if (frame->vframe)
  423. av_frame_free(&frame->vframe);
  424. av_freep(&frame);
  425. }
  426. pthread_mutex_unlock(&s->out_mutex);
  427. // Feed a dummy frame prior to signalling EOF.
  428. // This is required to terminate the decoder(OMX.SEC)
  429. // when only one frame is read during stream info detection.
  430. if (s->dummy_buf && (frame = (Frame*)av_mallocz(sizeof(Frame)))) {
  431. frame->status = OK;
  432. frame->size = s->dummy_bufsize;
  433. frame->key = 1;
  434. frame->buffer = s->dummy_buf;
  435. pthread_mutex_lock(&s->in_mutex);
  436. s->in_queue->push_back(frame);
  437. pthread_cond_signal(&s->condition);
  438. pthread_mutex_unlock(&s->in_mutex);
  439. s->dummy_buf = NULL;
  440. }
  441. pthread_mutex_lock(&s->in_mutex);
  442. s->end_frame->status = ERROR_END_OF_STREAM;
  443. s->in_queue->push_back(s->end_frame);
  444. pthread_cond_signal(&s->condition);
  445. pthread_mutex_unlock(&s->in_mutex);
  446. s->end_frame = NULL;
  447. }
  448. pthread_join(s->decode_thread_id, NULL);
  449. if (s->prev_frame)
  450. av_frame_free(&s->prev_frame);
  451. s->thread_started = false;
  452. }
  453. while (!s->in_queue->empty()) {
  454. frame = *s->in_queue->begin();
  455. s->in_queue->erase(s->in_queue->begin());
  456. if (frame->size)
  457. av_freep(&frame->buffer);
  458. av_freep(&frame);
  459. }
  460. while (!s->out_queue->empty()) {
  461. frame = *s->out_queue->begin();
  462. s->out_queue->erase(s->out_queue->begin());
  463. if (frame->vframe)
  464. av_frame_free(&frame->vframe);
  465. av_freep(&frame);
  466. }
  467. (*s->decoder)->stop();
  468. s->client->disconnect();
  469. if (s->decoder_component)
  470. av_freep(&s->decoder_component);
  471. av_freep(&s->dummy_buf);
  472. av_freep(&s->end_frame);
  473. // Reset the extradata back to the original mp4 format, so that
  474. // the next invocation (both when decoding and when called from
  475. // av_find_stream_info) get the original mp4 format extradata.
  476. av_freep(&avctx->extradata);
  477. avctx->extradata = s->orig_extradata;
  478. avctx->extradata_size = s->orig_extradata_size;
  479. delete s->in_queue;
  480. delete s->out_queue;
  481. delete s->ts_map;
  482. delete s->client;
  483. delete s->decoder;
  484. delete s->source;
  485. pthread_mutex_destroy(&s->in_mutex);
  486. pthread_mutex_destroy(&s->out_mutex);
  487. pthread_cond_destroy(&s->condition);
  488. av_bitstream_filter_close(s->bsfc);
  489. return 0;
  490. }
  491. AVCodec ff_libstagefright_h264_decoder = {
  492. "libstagefright_h264",
  493. NULL_IF_CONFIG_SMALL("libstagefright H.264"),
  494. AVMEDIA_TYPE_VIDEO,
  495. AV_CODEC_ID_H264,
  496. CODEC_CAP_DELAY,
  497. NULL, //supported_framerates
  498. NULL, //pix_fmts
  499. NULL, //supported_samplerates
  500. NULL, //sample_fmts
  501. NULL, //channel_layouts
  502. 0, //max_lowres
  503. NULL, //priv_class
  504. NULL, //profiles
  505. sizeof(StagefrightContext),
  506. NULL, //next
  507. NULL, //init_thread_copy
  508. NULL, //update_thread_context
  509. NULL, //defaults
  510. NULL, //init_static_data
  511. Stagefright_init,
  512. NULL, //encode
  513. NULL, //encode2
  514. Stagefright_decode_frame,
  515. Stagefright_close,
  516. };