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.

521 lines
16KB

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