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.

623 lines
17KB

  1. /*
  2. * Android MediaCodec H.264 / H.265 / MPEG-4 / VP8 / VP9 decoders
  3. *
  4. * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/fifo.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/pixfmt.h"
  30. #include "avcodec.h"
  31. #include "h264_parse.h"
  32. #include "hevc_parse.h"
  33. #include "internal.h"
  34. #include "mediacodec_wrapper.h"
  35. #include "mediacodecdec_common.h"
  36. typedef struct MediaCodecH264DecContext {
  37. MediaCodecDecContext *ctx;
  38. AVBSFContext *bsf;
  39. AVFifoBuffer *fifo;
  40. AVPacket filtered_pkt;
  41. } MediaCodecH264DecContext;
  42. static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
  43. {
  44. MediaCodecH264DecContext *s = avctx->priv_data;
  45. ff_mediacodec_dec_close(avctx, s->ctx);
  46. s->ctx = NULL;
  47. av_fifo_free(s->fifo);
  48. av_bsf_free(&s->bsf);
  49. av_packet_unref(&s->filtered_pkt);
  50. return 0;
  51. }
  52. #if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
  53. static int h2645_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
  54. {
  55. int i;
  56. int ret = 0;
  57. uint8_t *p = NULL;
  58. static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
  59. if (!out || !out_size) {
  60. return AVERROR(EINVAL);
  61. }
  62. p = av_malloc(sizeof(nalu_header) + src_size);
  63. if (!p) {
  64. return AVERROR(ENOMEM);
  65. }
  66. *out = p;
  67. *out_size = sizeof(nalu_header) + src_size;
  68. memcpy(p, nalu_header, sizeof(nalu_header));
  69. memcpy(p + sizeof(nalu_header), src, src_size);
  70. /* Escape 0x00, 0x00, 0x0{0-3} pattern */
  71. for (i = 4; i < *out_size; i++) {
  72. if (i < *out_size - 3 &&
  73. p[i + 0] == 0 &&
  74. p[i + 1] == 0 &&
  75. p[i + 2] <= 3) {
  76. uint8_t *new;
  77. *out_size += 1;
  78. new = av_realloc(*out, *out_size);
  79. if (!new) {
  80. ret = AVERROR(ENOMEM);
  81. goto done;
  82. }
  83. *out = p = new;
  84. i = i + 2;
  85. memmove(p + i + 1, p + i, *out_size - (i + 1));
  86. p[i] = 0x03;
  87. }
  88. }
  89. done:
  90. if (ret < 0) {
  91. av_freep(out);
  92. *out_size = 0;
  93. }
  94. return ret;
  95. }
  96. #endif
  97. #if CONFIG_H264_MEDIACODEC_DECODER
  98. static int h264_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
  99. {
  100. int i;
  101. int ret;
  102. H264ParamSets ps;
  103. const PPS *pps = NULL;
  104. const SPS *sps = NULL;
  105. int is_avc = 0;
  106. int nal_length_size = 0;
  107. memset(&ps, 0, sizeof(ps));
  108. ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
  109. &ps, &is_avc, &nal_length_size, 0, avctx);
  110. if (ret < 0) {
  111. goto done;
  112. }
  113. for (i = 0; i < MAX_PPS_COUNT; i++) {
  114. if (ps.pps_list[i]) {
  115. pps = (const PPS*)ps.pps_list[i]->data;
  116. break;
  117. }
  118. }
  119. if (pps) {
  120. if (ps.sps_list[pps->sps_id]) {
  121. sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
  122. }
  123. }
  124. if (pps && sps) {
  125. uint8_t *data = NULL;
  126. int data_size = 0;
  127. if ((ret = h2645_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
  128. goto done;
  129. }
  130. ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
  131. av_freep(&data);
  132. if ((ret = h2645_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
  133. goto done;
  134. }
  135. ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
  136. av_freep(&data);
  137. } else {
  138. av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
  139. ret = AVERROR_INVALIDDATA;
  140. }
  141. done:
  142. ff_h264_ps_uninit(&ps);
  143. return ret;
  144. }
  145. #endif
  146. #if CONFIG_HEVC_MEDIACODEC_DECODER
  147. static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
  148. {
  149. int i;
  150. int ret;
  151. HEVCParamSets ps;
  152. const HEVCVPS *vps = NULL;
  153. const HEVCPPS *pps = NULL;
  154. const HEVCSPS *sps = NULL;
  155. int is_nalff = 0;
  156. int nal_length_size = 0;
  157. uint8_t *vps_data = NULL;
  158. uint8_t *sps_data = NULL;
  159. uint8_t *pps_data = NULL;
  160. int vps_data_size = 0;
  161. int sps_data_size = 0;
  162. int pps_data_size = 0;
  163. memset(&ps, 0, sizeof(ps));
  164. ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
  165. &ps, &is_nalff, &nal_length_size, 0, avctx);
  166. if (ret < 0) {
  167. goto done;
  168. }
  169. for (i = 0; i < HEVC_MAX_VPS_COUNT; i++) {
  170. if (ps.vps_list[i]) {
  171. vps = (const HEVCVPS*)ps.vps_list[i]->data;
  172. break;
  173. }
  174. }
  175. for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
  176. if (ps.pps_list[i]) {
  177. pps = (const HEVCPPS*)ps.pps_list[i]->data;
  178. break;
  179. }
  180. }
  181. if (pps) {
  182. if (ps.sps_list[pps->sps_id]) {
  183. sps = (const HEVCSPS*)ps.sps_list[pps->sps_id]->data;
  184. }
  185. }
  186. if (vps && pps && sps) {
  187. uint8_t *data;
  188. int data_size;
  189. if ((ret = h2645_ps_to_nalu(vps->data, vps->data_size, &vps_data, &vps_data_size)) < 0 ||
  190. (ret = h2645_ps_to_nalu(sps->data, sps->data_size, &sps_data, &sps_data_size)) < 0 ||
  191. (ret = h2645_ps_to_nalu(pps->data, pps->data_size, &pps_data, &pps_data_size)) < 0) {
  192. goto done;
  193. }
  194. data_size = vps_data_size + sps_data_size + pps_data_size;
  195. data = av_mallocz(data_size);
  196. if (!data) {
  197. ret = AVERROR(ENOMEM);
  198. goto done;
  199. }
  200. memcpy(data , vps_data, vps_data_size);
  201. memcpy(data + vps_data_size , sps_data, sps_data_size);
  202. memcpy(data + vps_data_size + sps_data_size, pps_data, pps_data_size);
  203. ff_AMediaFormat_setBuffer(format, "csd-0", data, data_size);
  204. av_freep(&data);
  205. } else {
  206. av_log(avctx, AV_LOG_ERROR, "Could not extract VPS/PPS/SPS from extradata");
  207. ret = AVERROR_INVALIDDATA;
  208. }
  209. done:
  210. av_freep(&vps_data);
  211. av_freep(&sps_data);
  212. av_freep(&pps_data);
  213. return ret;
  214. }
  215. #endif
  216. #if CONFIG_MPEG4_MEDIACODEC_DECODER
  217. static int mpeg4_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
  218. {
  219. int ret = 0;
  220. if (avctx->extradata) {
  221. ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
  222. }
  223. return ret;
  224. }
  225. #endif
  226. #if CONFIG_VP8_MEDIACODEC_DECODER || CONFIG_VP9_MEDIACODEC_DECODER
  227. static int vpx_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
  228. {
  229. int ret = 0;
  230. if (avctx->extradata) {
  231. ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
  232. }
  233. return ret;
  234. }
  235. #endif
  236. static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
  237. {
  238. int ret;
  239. const char *codec_mime = NULL;
  240. const char *bsf_name = NULL;
  241. const AVBitStreamFilter *bsf = NULL;
  242. FFAMediaFormat *format = NULL;
  243. MediaCodecH264DecContext *s = avctx->priv_data;
  244. format = ff_AMediaFormat_new();
  245. if (!format) {
  246. av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
  247. ret = AVERROR_EXTERNAL;
  248. goto done;
  249. }
  250. switch (avctx->codec_id) {
  251. #if CONFIG_H264_MEDIACODEC_DECODER
  252. case AV_CODEC_ID_H264:
  253. codec_mime = "video/avc";
  254. bsf_name = "h264_mp4toannexb";
  255. ret = h264_set_extradata(avctx, format);
  256. if (ret < 0)
  257. goto done;
  258. break;
  259. #endif
  260. #if CONFIG_HEVC_MEDIACODEC_DECODER
  261. case AV_CODEC_ID_HEVC:
  262. codec_mime = "video/hevc";
  263. bsf_name = "hevc_mp4toannexb";
  264. ret = hevc_set_extradata(avctx, format);
  265. if (ret < 0)
  266. goto done;
  267. break;
  268. #endif
  269. #if CONFIG_MPEG4_MEDIACODEC_DECODER
  270. case AV_CODEC_ID_MPEG4:
  271. codec_mime = "video/mp4v-es",
  272. ret = mpeg4_set_extradata(avctx, format);
  273. if (ret < 0)
  274. goto done;
  275. break;
  276. #endif
  277. #if CONFIG_VP8_MEDIACODEC_DECODER
  278. case AV_CODEC_ID_VP8:
  279. codec_mime = "video/x-vnd.on2.vp8";
  280. ret = vpx_set_extradata(avctx, format);
  281. if (ret < 0)
  282. goto done;
  283. break;
  284. #endif
  285. #if CONFIG_VP9_MEDIACODEC_DECODER
  286. case AV_CODEC_ID_VP9:
  287. codec_mime = "video/x-vnd.on2.vp9";
  288. ret = vpx_set_extradata(avctx, format);
  289. if (ret < 0)
  290. goto done;
  291. break;
  292. #endif
  293. default:
  294. av_assert0(0);
  295. }
  296. ff_AMediaFormat_setString(format, "mime", codec_mime);
  297. ff_AMediaFormat_setInt32(format, "width", avctx->width);
  298. ff_AMediaFormat_setInt32(format, "height", avctx->height);
  299. s->ctx = av_mallocz(sizeof(*s->ctx));
  300. if (!s->ctx) {
  301. av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
  302. ret = AVERROR(ENOMEM);
  303. goto done;
  304. }
  305. if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
  306. s->ctx = NULL;
  307. goto done;
  308. }
  309. av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
  310. s->fifo = av_fifo_alloc(sizeof(AVPacket));
  311. if (!s->fifo) {
  312. ret = AVERROR(ENOMEM);
  313. goto done;
  314. }
  315. if (bsf_name) {
  316. bsf = av_bsf_get_by_name(bsf_name);
  317. if(!bsf) {
  318. ret = AVERROR_BSF_NOT_FOUND;
  319. goto done;
  320. }
  321. if ((ret = av_bsf_alloc(bsf, &s->bsf))) {
  322. goto done;
  323. }
  324. if (((ret = avcodec_parameters_from_context(s->bsf->par_in, avctx)) < 0) ||
  325. ((ret = av_bsf_init(s->bsf)) < 0)) {
  326. goto done;
  327. }
  328. }
  329. av_init_packet(&s->filtered_pkt);
  330. done:
  331. if (format) {
  332. ff_AMediaFormat_delete(format);
  333. }
  334. if (ret < 0) {
  335. mediacodec_decode_close(avctx);
  336. }
  337. return ret;
  338. }
  339. static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
  340. int *got_frame, AVPacket *pkt)
  341. {
  342. MediaCodecH264DecContext *s = avctx->priv_data;
  343. return ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, pkt);
  344. }
  345. static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
  346. int *got_frame, AVPacket *avpkt)
  347. {
  348. MediaCodecH264DecContext *s = avctx->priv_data;
  349. AVFrame *frame = data;
  350. int ret;
  351. /* buffer the input packet */
  352. if (avpkt->size) {
  353. AVPacket input_pkt = { 0 };
  354. if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
  355. ret = av_fifo_realloc2(s->fifo,
  356. av_fifo_size(s->fifo) + sizeof(input_pkt));
  357. if (ret < 0)
  358. return ret;
  359. }
  360. ret = av_packet_ref(&input_pkt, avpkt);
  361. if (ret < 0)
  362. return ret;
  363. av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
  364. }
  365. /*
  366. * MediaCodec.flush() discards both input and output buffers, thus we
  367. * need to delay the call to this function until the user has released or
  368. * renderered the frames he retains.
  369. *
  370. * After we have buffered an input packet, check if the codec is in the
  371. * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
  372. *
  373. * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
  374. * the codec (because the user retains frames). The codec stays in the
  375. * flushing state.
  376. *
  377. * ff_mediacodec_dec_flush returns 1 if the flush can actually be
  378. * performed on the codec. The codec leaves the flushing state and can
  379. * process again packets.
  380. *
  381. * ff_mediacodec_dec_flush returns a negative value if an error has
  382. * occurred.
  383. *
  384. */
  385. if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
  386. if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
  387. return avpkt->size;
  388. }
  389. }
  390. /* process buffered data */
  391. while (!*got_frame) {
  392. /* prepare the input data -- convert to Annex B if needed */
  393. if (s->filtered_pkt.size <= 0) {
  394. AVPacket input_pkt = { 0 };
  395. av_packet_unref(&s->filtered_pkt);
  396. /* no more data */
  397. if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
  398. return avpkt->size ? avpkt->size :
  399. ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, avpkt);
  400. }
  401. av_fifo_generic_read(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
  402. if (s->bsf) {
  403. ret = av_bsf_send_packet(s->bsf, &input_pkt);
  404. if (ret < 0) {
  405. return ret;
  406. }
  407. ret = av_bsf_receive_packet(s->bsf, &s->filtered_pkt);
  408. if (ret == AVERROR(EAGAIN)) {
  409. goto done;
  410. }
  411. } else {
  412. av_packet_move_ref(&s->filtered_pkt, &input_pkt);
  413. }
  414. /* {h264,hevc}_mp4toannexb are used here and do not require flushing */
  415. av_assert0(ret != AVERROR_EOF);
  416. if (ret < 0) {
  417. return ret;
  418. }
  419. }
  420. ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
  421. if (ret < 0)
  422. return ret;
  423. s->filtered_pkt.size -= ret;
  424. s->filtered_pkt.data += ret;
  425. }
  426. done:
  427. return avpkt->size;
  428. }
  429. static void mediacodec_decode_flush(AVCodecContext *avctx)
  430. {
  431. MediaCodecH264DecContext *s = avctx->priv_data;
  432. while (av_fifo_size(s->fifo)) {
  433. AVPacket pkt;
  434. av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
  435. av_packet_unref(&pkt);
  436. }
  437. av_fifo_reset(s->fifo);
  438. av_packet_unref(&s->filtered_pkt);
  439. ff_mediacodec_dec_flush(avctx, s->ctx);
  440. }
  441. #if CONFIG_H264_MEDIACODEC_DECODER
  442. AVCodec ff_h264_mediacodec_decoder = {
  443. .name = "h264_mediacodec",
  444. .long_name = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
  445. .type = AVMEDIA_TYPE_VIDEO,
  446. .id = AV_CODEC_ID_H264,
  447. .priv_data_size = sizeof(MediaCodecH264DecContext),
  448. .init = mediacodec_decode_init,
  449. .decode = mediacodec_decode_frame,
  450. .flush = mediacodec_decode_flush,
  451. .close = mediacodec_decode_close,
  452. .capabilities = AV_CODEC_CAP_DELAY,
  453. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  454. };
  455. #endif
  456. #if CONFIG_HEVC_MEDIACODEC_DECODER
  457. AVCodec ff_hevc_mediacodec_decoder = {
  458. .name = "hevc_mediacodec",
  459. .long_name = NULL_IF_CONFIG_SMALL("H.265 Android MediaCodec decoder"),
  460. .type = AVMEDIA_TYPE_VIDEO,
  461. .id = AV_CODEC_ID_HEVC,
  462. .priv_data_size = sizeof(MediaCodecH264DecContext),
  463. .init = mediacodec_decode_init,
  464. .decode = mediacodec_decode_frame,
  465. .flush = mediacodec_decode_flush,
  466. .close = mediacodec_decode_close,
  467. .capabilities = AV_CODEC_CAP_DELAY,
  468. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  469. };
  470. #endif
  471. #if CONFIG_MPEG4_MEDIACODEC_DECODER
  472. AVCodec ff_mpeg4_mediacodec_decoder = {
  473. .name = "mpeg4_mediacodec",
  474. .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Android MediaCodec decoder"),
  475. .type = AVMEDIA_TYPE_VIDEO,
  476. .id = AV_CODEC_ID_MPEG4,
  477. .priv_data_size = sizeof(MediaCodecH264DecContext),
  478. .init = mediacodec_decode_init,
  479. .decode = mediacodec_decode_frame,
  480. .flush = mediacodec_decode_flush,
  481. .close = mediacodec_decode_close,
  482. .capabilities = AV_CODEC_CAP_DELAY,
  483. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  484. };
  485. #endif
  486. #if CONFIG_VP8_MEDIACODEC_DECODER
  487. AVCodec ff_vp8_mediacodec_decoder = {
  488. .name = "vp8_mediacodec",
  489. .long_name = NULL_IF_CONFIG_SMALL("VP8 Android MediaCodec decoder"),
  490. .type = AVMEDIA_TYPE_VIDEO,
  491. .id = AV_CODEC_ID_VP8,
  492. .priv_data_size = sizeof(MediaCodecH264DecContext),
  493. .init = mediacodec_decode_init,
  494. .decode = mediacodec_decode_frame,
  495. .flush = mediacodec_decode_flush,
  496. .close = mediacodec_decode_close,
  497. .capabilities = AV_CODEC_CAP_DELAY,
  498. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  499. };
  500. #endif
  501. #if CONFIG_VP9_MEDIACODEC_DECODER
  502. AVCodec ff_vp9_mediacodec_decoder = {
  503. .name = "vp9_mediacodec",
  504. .long_name = NULL_IF_CONFIG_SMALL("VP9 Android MediaCodec decoder"),
  505. .type = AVMEDIA_TYPE_VIDEO,
  506. .id = AV_CODEC_ID_VP9,
  507. .priv_data_size = sizeof(MediaCodecH264DecContext),
  508. .init = mediacodec_decode_init,
  509. .decode = mediacodec_decode_frame,
  510. .flush = mediacodec_decode_flush,
  511. .close = mediacodec_decode_close,
  512. .capabilities = AV_CODEC_CAP_DELAY,
  513. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  514. };
  515. #endif