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.

517 lines
15KB

  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 Libav.
  9. *
  10. * Libav 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. * Libav 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 Libav; 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 CustomSource(avctx, meta);
  191. s->in_queue = new List<Frame*>;
  192. s->out_queue = new List<Frame*>;
  193. s->client = new OMXClient;
  194. s->end_frame = (Frame*)av_mallocz(sizeof(Frame));
  195. if (s->source == NULL || !s->in_queue || !s->out_queue || !s->client ||
  196. !s->end_frame) {
  197. ret = AVERROR(ENOMEM);
  198. goto fail;
  199. }
  200. if (s->client->connect() != OK) {
  201. av_log(avctx, AV_LOG_ERROR, "Cannot connect OMX client\n");
  202. ret = -1;
  203. goto fail;
  204. }
  205. s->decoder = OMXCodec::Create(s->client->interface(), meta,
  206. false, s->source, NULL,
  207. OMXCodec::kClientNeedsFramebuffer);
  208. if (s->decoder->start() != OK) {
  209. av_log(avctx, AV_LOG_ERROR, "Cannot start decoder\n");
  210. ret = -1;
  211. s->client->disconnect();
  212. goto fail;
  213. }
  214. outFormat = s->decoder->getFormat();
  215. outFormat->findInt32(kKeyColorFormat, &colorFormat);
  216. if (colorFormat == OMX_QCOM_COLOR_FormatYVU420SemiPlanar ||
  217. colorFormat == OMX_COLOR_FormatYUV420SemiPlanar)
  218. avctx->pix_fmt = PIX_FMT_NV21;
  219. else
  220. avctx->pix_fmt = PIX_FMT_YUV420P;
  221. outFormat->findCString(kKeyDecoderComponent, &s->decoder_component);
  222. if (s->decoder_component)
  223. s->decoder_component = av_strdup(s->decoder_component);
  224. pthread_mutex_init(&s->in_mutex, NULL);
  225. pthread_mutex_init(&s->out_mutex, NULL);
  226. pthread_cond_init(&s->condition, NULL);
  227. pthread_create(&s->decode_thread_id, NULL, &decode_thread, avctx);
  228. return 0;
  229. fail:
  230. av_bitstream_filter_close(s->bsfc);
  231. av_freep(&s->orig_extradata);
  232. av_freep(&s->end_frame);
  233. delete s->in_queue;
  234. delete s->out_queue;
  235. delete s->client;
  236. return ret;
  237. }
  238. static int Stagefright_decode_frame(AVCodecContext *avctx, void *data,
  239. int *data_size, AVPacket *avpkt)
  240. {
  241. StagefrightContext *s = (StagefrightContext*)avctx->priv_data;
  242. Frame *frame;
  243. MediaBuffer *mbuffer;
  244. status_t status;
  245. size_t size;
  246. uint8_t *buf;
  247. const uint8_t *src_data[3];
  248. int w, h;
  249. int src_linesize[3];
  250. int orig_size = avpkt->size;
  251. AVPacket pkt = *avpkt;
  252. int ret;
  253. if (avpkt && avpkt->data) {
  254. av_bitstream_filter_filter(s->bsfc, avctx, NULL, &pkt.data, &pkt.size,
  255. avpkt->data, avpkt->size, avpkt->flags & AV_PKT_FLAG_KEY);
  256. avpkt = &pkt;
  257. }
  258. if (!s->source_done) {
  259. if(!s->dummy_buf) {
  260. s->dummy_buf = (uint8_t*)av_malloc(avpkt->size);
  261. if (!s->dummy_buf)
  262. return AVERROR(ENOMEM);
  263. s->dummy_bufsize = avpkt->size;
  264. memcpy(s->dummy_buf, avpkt->data, avpkt->size);
  265. }
  266. frame = (Frame*)av_mallocz(sizeof(Frame));
  267. if (avpkt->data) {
  268. frame->status = OK;
  269. frame->size = orig_size;
  270. // Stagefright can't handle negative timestamps -
  271. // if needed, work around this by offsetting them manually?
  272. if (avpkt->pts >= 0)
  273. frame->time = avpkt->pts;
  274. frame->key = avpkt->flags & AV_PKT_FLAG_KEY ? 1 : 0;
  275. frame->buffer = (uint8_t*)av_malloc(avpkt->size);
  276. if (!frame->buffer) {
  277. av_freep(&frame);
  278. return AVERROR(ENOMEM);
  279. }
  280. uint8_t *ptr = avpkt->data;
  281. // The OMX.SEC decoder fails without this.
  282. if (avpkt->size == orig_size + avctx->extradata_size)
  283. ptr += avctx->extradata_size;
  284. memcpy(frame->buffer, ptr, orig_size);
  285. } else {
  286. frame->status = ERROR_END_OF_STREAM;
  287. s->source_done = true;
  288. }
  289. while (true) {
  290. if (s->thread_exited) {
  291. s->source_done = true;
  292. break;
  293. }
  294. pthread_mutex_lock(&s->in_mutex);
  295. if (s->in_queue->size() >= 10) {
  296. pthread_mutex_unlock(&s->in_mutex);
  297. usleep(10000);
  298. continue;
  299. }
  300. s->in_queue->push_back(frame);
  301. pthread_cond_signal(&s->condition);
  302. pthread_mutex_unlock(&s->in_mutex);
  303. break;
  304. }
  305. }
  306. while (true) {
  307. pthread_mutex_lock(&s->out_mutex);
  308. if (!s->out_queue->empty()) break;
  309. pthread_mutex_unlock(&s->out_mutex);
  310. if (s->source_done) {
  311. usleep(10000);
  312. continue;
  313. } else {
  314. return orig_size;
  315. }
  316. }
  317. frame = *s->out_queue->begin();
  318. s->out_queue->erase(s->out_queue->begin());
  319. pthread_mutex_unlock(&s->out_mutex);
  320. mbuffer = frame->mbuffer;
  321. status = frame->status;
  322. size = frame->size;
  323. w = frame->w;
  324. h = frame->h;
  325. av_freep(&frame);
  326. if (status == ERROR_END_OF_STREAM)
  327. return 0;
  328. if (status != OK) {
  329. if (status == AVERROR(ENOMEM))
  330. return status;
  331. av_log(avctx, AV_LOG_ERROR, "Decode failed: %x\n", status);
  332. return -1;
  333. }
  334. // The OMX.SEC decoder doesn't signal the modified width/height
  335. if (s->decoder_component && !strncmp(s->decoder_component, "OMX.SEC", 7) &&
  336. (w & 15 || h & 15)) {
  337. if (((w + 15)&~15) * ((h + 15)&~15) * 3/2 == size) {
  338. w = (w + 15)&~15;
  339. h = (h + 15)&~15;
  340. }
  341. }
  342. if (!avctx->width || !avctx->height || avctx->width > w || avctx->height > h) {
  343. avctx->width = w;
  344. avctx->height = h;
  345. }
  346. ret = avctx->reget_buffer(avctx, &s->ret_frame);
  347. if (ret < 0) {
  348. av_log(avctx, AV_LOG_ERROR, "reget buffer() failed\n");
  349. goto end;
  350. }
  351. src_linesize[0] = w;
  352. if (avctx->pix_fmt == PIX_FMT_YUV420P)
  353. src_linesize[1] = src_linesize[2] = w/2;
  354. else if (avctx->pix_fmt == PIX_FMT_NV21)
  355. src_linesize[1] = w;
  356. buf = (uint8_t*)mbuffer->data();
  357. src_data[0] = buf;
  358. src_data[1] = buf + src_linesize[0] * h;
  359. src_data[2] = src_data[1] + src_linesize[1] * h/2;
  360. av_image_copy(s->ret_frame.data, s->ret_frame.linesize,
  361. src_data, src_linesize,
  362. avctx->pix_fmt, avctx->width, avctx->height);
  363. *data_size = sizeof(AVFrame);
  364. *(AVFrame*)data = s->ret_frame;
  365. ret = orig_size;
  366. end:
  367. mbuffer->release();
  368. return ret;
  369. }
  370. static av_cold int Stagefright_close(AVCodecContext *avctx)
  371. {
  372. StagefrightContext *s = (StagefrightContext*)avctx->priv_data;
  373. Frame *frame;
  374. if (!s->thread_exited) {
  375. s->stop_decode = 1;
  376. // Feed a dummy frame prior to signalling EOF.
  377. // This is required to terminate the decoder(OMX.SEC)
  378. // when only one frame is read during stream info detection.
  379. if (s->dummy_buf && (frame = (Frame*)av_mallocz(sizeof(Frame)))) {
  380. frame->status = OK;
  381. frame->size = s->dummy_bufsize;
  382. frame->buffer = s->dummy_buf;
  383. pthread_mutex_lock(&s->in_mutex);
  384. s->in_queue->push_back(frame);
  385. pthread_cond_signal(&s->condition);
  386. pthread_mutex_unlock(&s->in_mutex);
  387. s->dummy_buf = NULL;
  388. }
  389. pthread_mutex_lock(&s->in_mutex);
  390. s->end_frame->status = ERROR_END_OF_STREAM;
  391. s->in_queue->push_back(s->end_frame);
  392. pthread_cond_signal(&s->condition);
  393. pthread_mutex_unlock(&s->in_mutex);
  394. s->end_frame = NULL;
  395. }
  396. pthread_join(s->decode_thread_id, NULL);
  397. if (s->ret_frame.data[0])
  398. avctx->release_buffer(avctx, &s->ret_frame);
  399. while (!s->in_queue->empty()) {
  400. frame = *s->in_queue->begin();
  401. s->in_queue->erase(s->in_queue->begin());
  402. if (frame->size)
  403. av_freep(&frame->buffer);
  404. av_freep(&frame);
  405. }
  406. while (!s->out_queue->empty()) {
  407. frame = *s->out_queue->begin();
  408. s->out_queue->erase(s->out_queue->begin());
  409. if (frame->size)
  410. frame->mbuffer->release();
  411. av_freep(&frame);
  412. }
  413. s->decoder->stop();
  414. s->client->disconnect();
  415. if (s->decoder_component)
  416. av_freep(&s->decoder_component);
  417. av_freep(&s->dummy_buf);
  418. av_freep(&s->end_frame);
  419. // Reset the extradata back to the original mp4 format, so that
  420. // the next invocation (both when decoding and when called from
  421. // av_find_stream_info) get the original mp4 format extradata.
  422. av_freep(&avctx->extradata);
  423. avctx->extradata = s->orig_extradata;
  424. avctx->extradata_size = s->orig_extradata_size;
  425. delete s->in_queue;
  426. delete s->out_queue;
  427. delete s->client;
  428. pthread_mutex_destroy(&s->in_mutex);
  429. pthread_mutex_destroy(&s->out_mutex);
  430. pthread_cond_destroy(&s->condition);
  431. av_bitstream_filter_close(s->bsfc);
  432. return 0;
  433. }
  434. AVCodec ff_libstagefright_h264_decoder = {
  435. "libstagefright_h264",
  436. AVMEDIA_TYPE_VIDEO,
  437. CODEC_ID_H264,
  438. sizeof(StagefrightContext),
  439. Stagefright_init,
  440. NULL, //encode
  441. Stagefright_close,
  442. Stagefright_decode_frame,
  443. CODEC_CAP_DELAY,
  444. NULL, //next
  445. NULL, //flush
  446. NULL, //supported_framerates
  447. NULL, //pixel_formats
  448. NULL_IF_CONFIG_SMALL("libstagefright H.264"),
  449. };