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.

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