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.

615 lines
17KB

  1. /*
  2. * Android MediaCodec MPEG-2 / 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. AVFifoBuffer *fifo;
  39. AVPacket buffered_pkt;
  40. } MediaCodecH264DecContext;
  41. static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
  42. {
  43. MediaCodecH264DecContext *s = avctx->priv_data;
  44. ff_mediacodec_dec_close(avctx, s->ctx);
  45. s->ctx = NULL;
  46. av_fifo_free(s->fifo);
  47. av_packet_unref(&s->buffered_pkt);
  48. return 0;
  49. }
  50. #if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
  51. static int h2645_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
  52. {
  53. int i;
  54. int ret = 0;
  55. uint8_t *p = NULL;
  56. static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
  57. if (!out || !out_size) {
  58. return AVERROR(EINVAL);
  59. }
  60. p = av_malloc(sizeof(nalu_header) + src_size);
  61. if (!p) {
  62. return AVERROR(ENOMEM);
  63. }
  64. *out = p;
  65. *out_size = sizeof(nalu_header) + src_size;
  66. memcpy(p, nalu_header, sizeof(nalu_header));
  67. memcpy(p + sizeof(nalu_header), src, src_size);
  68. /* Escape 0x00, 0x00, 0x0{0-3} pattern */
  69. for (i = 4; i < *out_size; i++) {
  70. if (i < *out_size - 3 &&
  71. p[i + 0] == 0 &&
  72. p[i + 1] == 0 &&
  73. p[i + 2] <= 3) {
  74. uint8_t *new;
  75. *out_size += 1;
  76. new = av_realloc(*out, *out_size);
  77. if (!new) {
  78. ret = AVERROR(ENOMEM);
  79. goto done;
  80. }
  81. *out = p = new;
  82. i = i + 2;
  83. memmove(p + i + 1, p + i, *out_size - (i + 1));
  84. p[i] = 0x03;
  85. }
  86. }
  87. done:
  88. if (ret < 0) {
  89. av_freep(out);
  90. *out_size = 0;
  91. }
  92. return ret;
  93. }
  94. #endif
  95. #if CONFIG_H264_MEDIACODEC_DECODER
  96. static int h264_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
  97. {
  98. int i;
  99. int ret;
  100. H264ParamSets ps;
  101. const PPS *pps = NULL;
  102. const SPS *sps = NULL;
  103. int is_avc = 0;
  104. int nal_length_size = 0;
  105. memset(&ps, 0, sizeof(ps));
  106. ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
  107. &ps, &is_avc, &nal_length_size, 0, avctx);
  108. if (ret < 0) {
  109. goto done;
  110. }
  111. for (i = 0; i < MAX_PPS_COUNT; i++) {
  112. if (ps.pps_list[i]) {
  113. pps = (const PPS*)ps.pps_list[i]->data;
  114. break;
  115. }
  116. }
  117. if (pps) {
  118. if (ps.sps_list[pps->sps_id]) {
  119. sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
  120. }
  121. }
  122. if (pps && sps) {
  123. uint8_t *data = NULL;
  124. int data_size = 0;
  125. if ((ret = h2645_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
  126. goto done;
  127. }
  128. ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
  129. av_freep(&data);
  130. if ((ret = h2645_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
  131. goto done;
  132. }
  133. ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
  134. av_freep(&data);
  135. } else {
  136. av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
  137. ret = AVERROR_INVALIDDATA;
  138. }
  139. done:
  140. ff_h264_ps_uninit(&ps);
  141. return ret;
  142. }
  143. #endif
  144. #if CONFIG_HEVC_MEDIACODEC_DECODER
  145. static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
  146. {
  147. int i;
  148. int ret;
  149. HEVCParamSets ps;
  150. HEVCSEI sei;
  151. const HEVCVPS *vps = NULL;
  152. const HEVCPPS *pps = NULL;
  153. const HEVCSPS *sps = NULL;
  154. int is_nalff = 0;
  155. int nal_length_size = 0;
  156. uint8_t *vps_data = NULL;
  157. uint8_t *sps_data = NULL;
  158. uint8_t *pps_data = NULL;
  159. int vps_data_size = 0;
  160. int sps_data_size = 0;
  161. int pps_data_size = 0;
  162. memset(&ps, 0, sizeof(ps));
  163. memset(&sei, 0, sizeof(sei));
  164. ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
  165. &ps, &sei, &is_nalff, &nal_length_size, 0, 1, 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_MPEG2_MEDIACODEC_DECODER
  217. static int mpeg2_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_MPEG4_MEDIACODEC_DECODER
  227. static int mpeg4_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. #if CONFIG_VP8_MEDIACODEC_DECODER || CONFIG_VP9_MEDIACODEC_DECODER
  237. static int vpx_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
  238. {
  239. int ret = 0;
  240. if (avctx->extradata) {
  241. ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
  242. }
  243. return ret;
  244. }
  245. #endif
  246. static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
  247. {
  248. int ret;
  249. const char *codec_mime = NULL;
  250. FFAMediaFormat *format = NULL;
  251. MediaCodecH264DecContext *s = avctx->priv_data;
  252. format = ff_AMediaFormat_new();
  253. if (!format) {
  254. av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
  255. ret = AVERROR_EXTERNAL;
  256. goto done;
  257. }
  258. switch (avctx->codec_id) {
  259. #if CONFIG_H264_MEDIACODEC_DECODER
  260. case AV_CODEC_ID_H264:
  261. codec_mime = "video/avc";
  262. ret = h264_set_extradata(avctx, format);
  263. if (ret < 0)
  264. goto done;
  265. break;
  266. #endif
  267. #if CONFIG_HEVC_MEDIACODEC_DECODER
  268. case AV_CODEC_ID_HEVC:
  269. codec_mime = "video/hevc";
  270. ret = hevc_set_extradata(avctx, format);
  271. if (ret < 0)
  272. goto done;
  273. break;
  274. #endif
  275. #if CONFIG_MPEG2_MEDIACODEC_DECODER
  276. case AV_CODEC_ID_MPEG2VIDEO:
  277. codec_mime = "video/mpeg2";
  278. ret = mpeg2_set_extradata(avctx, format);
  279. if (ret < 0)
  280. goto done;
  281. break;
  282. #endif
  283. #if CONFIG_MPEG4_MEDIACODEC_DECODER
  284. case AV_CODEC_ID_MPEG4:
  285. codec_mime = "video/mp4v-es",
  286. ret = mpeg4_set_extradata(avctx, format);
  287. if (ret < 0)
  288. goto done;
  289. break;
  290. #endif
  291. #if CONFIG_VP8_MEDIACODEC_DECODER
  292. case AV_CODEC_ID_VP8:
  293. codec_mime = "video/x-vnd.on2.vp8";
  294. ret = vpx_set_extradata(avctx, format);
  295. if (ret < 0)
  296. goto done;
  297. break;
  298. #endif
  299. #if CONFIG_VP9_MEDIACODEC_DECODER
  300. case AV_CODEC_ID_VP9:
  301. codec_mime = "video/x-vnd.on2.vp9";
  302. ret = vpx_set_extradata(avctx, format);
  303. if (ret < 0)
  304. goto done;
  305. break;
  306. #endif
  307. default:
  308. av_assert0(0);
  309. }
  310. ff_AMediaFormat_setString(format, "mime", codec_mime);
  311. ff_AMediaFormat_setInt32(format, "width", avctx->width);
  312. ff_AMediaFormat_setInt32(format, "height", avctx->height);
  313. s->ctx = av_mallocz(sizeof(*s->ctx));
  314. if (!s->ctx) {
  315. av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
  316. ret = AVERROR(ENOMEM);
  317. goto done;
  318. }
  319. if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
  320. s->ctx = NULL;
  321. goto done;
  322. }
  323. av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
  324. s->fifo = av_fifo_alloc(sizeof(AVPacket));
  325. if (!s->fifo) {
  326. ret = AVERROR(ENOMEM);
  327. goto done;
  328. }
  329. done:
  330. if (format) {
  331. ff_AMediaFormat_delete(format);
  332. }
  333. if (ret < 0) {
  334. mediacodec_decode_close(avctx);
  335. }
  336. return ret;
  337. }
  338. static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
  339. int *got_frame, AVPacket *pkt)
  340. {
  341. MediaCodecH264DecContext *s = avctx->priv_data;
  342. return ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, pkt);
  343. }
  344. static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
  345. int *got_frame, AVPacket *avpkt)
  346. {
  347. MediaCodecH264DecContext *s = avctx->priv_data;
  348. AVFrame *frame = data;
  349. int ret;
  350. /* buffer the input packet */
  351. if (avpkt->size) {
  352. AVPacket input_pkt = { 0 };
  353. if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
  354. ret = av_fifo_realloc2(s->fifo,
  355. av_fifo_size(s->fifo) + sizeof(input_pkt));
  356. if (ret < 0)
  357. return ret;
  358. }
  359. ret = av_packet_ref(&input_pkt, avpkt);
  360. if (ret < 0)
  361. return ret;
  362. av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
  363. }
  364. /*
  365. * MediaCodec.flush() discards both input and output buffers, thus we
  366. * need to delay the call to this function until the user has released or
  367. * renderered the frames he retains.
  368. *
  369. * After we have buffered an input packet, check if the codec is in the
  370. * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
  371. *
  372. * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
  373. * the codec (because the user retains frames). The codec stays in the
  374. * flushing state.
  375. *
  376. * ff_mediacodec_dec_flush returns 1 if the flush can actually be
  377. * performed on the codec. The codec leaves the flushing state and can
  378. * process again packets.
  379. *
  380. * ff_mediacodec_dec_flush returns a negative value if an error has
  381. * occurred.
  382. *
  383. */
  384. if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
  385. if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
  386. return avpkt->size;
  387. }
  388. }
  389. /* process buffered data */
  390. while (!*got_frame) {
  391. /* prepare the input data */
  392. if (s->buffered_pkt.size <= 0) {
  393. av_packet_unref(&s->buffered_pkt);
  394. /* no more data */
  395. if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
  396. return avpkt->size ? avpkt->size :
  397. ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, avpkt);
  398. }
  399. av_fifo_generic_read(s->fifo, &s->buffered_pkt, sizeof(s->buffered_pkt), NULL);
  400. }
  401. ret = mediacodec_process_data(avctx, frame, got_frame, &s->buffered_pkt);
  402. if (ret < 0)
  403. return ret;
  404. s->buffered_pkt.size -= ret;
  405. s->buffered_pkt.data += ret;
  406. }
  407. return avpkt->size;
  408. }
  409. static void mediacodec_decode_flush(AVCodecContext *avctx)
  410. {
  411. MediaCodecH264DecContext *s = avctx->priv_data;
  412. while (av_fifo_size(s->fifo)) {
  413. AVPacket pkt;
  414. av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
  415. av_packet_unref(&pkt);
  416. }
  417. av_fifo_reset(s->fifo);
  418. av_packet_unref(&s->buffered_pkt);
  419. ff_mediacodec_dec_flush(avctx, s->ctx);
  420. }
  421. #if CONFIG_H264_MEDIACODEC_DECODER
  422. AVCodec ff_h264_mediacodec_decoder = {
  423. .name = "h264_mediacodec",
  424. .long_name = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
  425. .type = AVMEDIA_TYPE_VIDEO,
  426. .id = AV_CODEC_ID_H264,
  427. .priv_data_size = sizeof(MediaCodecH264DecContext),
  428. .init = mediacodec_decode_init,
  429. .decode = mediacodec_decode_frame,
  430. .flush = mediacodec_decode_flush,
  431. .close = mediacodec_decode_close,
  432. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
  433. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  434. .bsfs = "h264_mp4toannexb",
  435. };
  436. #endif
  437. #if CONFIG_HEVC_MEDIACODEC_DECODER
  438. AVCodec ff_hevc_mediacodec_decoder = {
  439. .name = "hevc_mediacodec",
  440. .long_name = NULL_IF_CONFIG_SMALL("H.265 Android MediaCodec decoder"),
  441. .type = AVMEDIA_TYPE_VIDEO,
  442. .id = AV_CODEC_ID_HEVC,
  443. .priv_data_size = sizeof(MediaCodecH264DecContext),
  444. .init = mediacodec_decode_init,
  445. .decode = mediacodec_decode_frame,
  446. .flush = mediacodec_decode_flush,
  447. .close = mediacodec_decode_close,
  448. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
  449. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  450. .bsfs = "hevc_mp4toannexb",
  451. };
  452. #endif
  453. #if CONFIG_MPEG2_MEDIACODEC_DECODER
  454. AVCodec ff_mpeg2_mediacodec_decoder = {
  455. .name = "mpeg2_mediacodec",
  456. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 Android MediaCodec decoder"),
  457. .type = AVMEDIA_TYPE_VIDEO,
  458. .id = AV_CODEC_ID_MPEG2VIDEO,
  459. .priv_data_size = sizeof(MediaCodecH264DecContext),
  460. .init = mediacodec_decode_init,
  461. .decode = mediacodec_decode_frame,
  462. .flush = mediacodec_decode_flush,
  463. .close = mediacodec_decode_close,
  464. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
  465. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  466. };
  467. #endif
  468. #if CONFIG_MPEG4_MEDIACODEC_DECODER
  469. AVCodec ff_mpeg4_mediacodec_decoder = {
  470. .name = "mpeg4_mediacodec",
  471. .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Android MediaCodec decoder"),
  472. .type = AVMEDIA_TYPE_VIDEO,
  473. .id = AV_CODEC_ID_MPEG4,
  474. .priv_data_size = sizeof(MediaCodecH264DecContext),
  475. .init = mediacodec_decode_init,
  476. .decode = mediacodec_decode_frame,
  477. .flush = mediacodec_decode_flush,
  478. .close = mediacodec_decode_close,
  479. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
  480. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  481. };
  482. #endif
  483. #if CONFIG_VP8_MEDIACODEC_DECODER
  484. AVCodec ff_vp8_mediacodec_decoder = {
  485. .name = "vp8_mediacodec",
  486. .long_name = NULL_IF_CONFIG_SMALL("VP8 Android MediaCodec decoder"),
  487. .type = AVMEDIA_TYPE_VIDEO,
  488. .id = AV_CODEC_ID_VP8,
  489. .priv_data_size = sizeof(MediaCodecH264DecContext),
  490. .init = mediacodec_decode_init,
  491. .decode = mediacodec_decode_frame,
  492. .flush = mediacodec_decode_flush,
  493. .close = mediacodec_decode_close,
  494. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
  495. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  496. };
  497. #endif
  498. #if CONFIG_VP9_MEDIACODEC_DECODER
  499. AVCodec ff_vp9_mediacodec_decoder = {
  500. .name = "vp9_mediacodec",
  501. .long_name = NULL_IF_CONFIG_SMALL("VP9 Android MediaCodec decoder"),
  502. .type = AVMEDIA_TYPE_VIDEO,
  503. .id = AV_CODEC_ID_VP9,
  504. .priv_data_size = sizeof(MediaCodecH264DecContext),
  505. .init = mediacodec_decode_init,
  506. .decode = mediacodec_decode_frame,
  507. .flush = mediacodec_decode_flush,
  508. .close = mediacodec_decode_close,
  509. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
  510. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  511. };
  512. #endif