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.

619 lines
18KB

  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/opt.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/pixfmt.h"
  29. #include "avcodec.h"
  30. #include "decode.h"
  31. #include "h264_parse.h"
  32. #include "hevc_parse.h"
  33. #include "hwaccel.h"
  34. #include "internal.h"
  35. #include "mediacodec_wrapper.h"
  36. #include "mediacodecdec_common.h"
  37. typedef struct MediaCodecH264DecContext {
  38. MediaCodecDecContext *ctx;
  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_packet_unref(&s->buffered_pkt);
  47. return 0;
  48. }
  49. #if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
  50. static int h2645_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
  51. {
  52. int i;
  53. int ret = 0;
  54. uint8_t *p = NULL;
  55. static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
  56. if (!out || !out_size) {
  57. return AVERROR(EINVAL);
  58. }
  59. p = av_malloc(sizeof(nalu_header) + src_size);
  60. if (!p) {
  61. return AVERROR(ENOMEM);
  62. }
  63. *out = p;
  64. *out_size = sizeof(nalu_header) + src_size;
  65. memcpy(p, nalu_header, sizeof(nalu_header));
  66. memcpy(p + sizeof(nalu_header), src, src_size);
  67. /* Escape 0x00, 0x00, 0x0{0-3} pattern */
  68. for (i = 4; i < *out_size; i++) {
  69. if (i < *out_size - 3 &&
  70. p[i + 0] == 0 &&
  71. p[i + 1] == 0 &&
  72. p[i + 2] <= 3) {
  73. uint8_t *new;
  74. *out_size += 1;
  75. new = av_realloc(*out, *out_size);
  76. if (!new) {
  77. ret = AVERROR(ENOMEM);
  78. goto done;
  79. }
  80. *out = p = new;
  81. i = i + 2;
  82. memmove(p + i + 1, p + i, *out_size - (i + 1));
  83. p[i] = 0x03;
  84. }
  85. }
  86. done:
  87. if (ret < 0) {
  88. av_freep(out);
  89. *out_size = 0;
  90. }
  91. return ret;
  92. }
  93. #endif
  94. #if CONFIG_H264_MEDIACODEC_DECODER
  95. static int h264_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
  96. {
  97. int i;
  98. int ret;
  99. H264ParamSets ps;
  100. const PPS *pps = NULL;
  101. const SPS *sps = NULL;
  102. int is_avc = 0;
  103. int nal_length_size = 0;
  104. memset(&ps, 0, sizeof(ps));
  105. ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
  106. &ps, &is_avc, &nal_length_size, 0, avctx);
  107. if (ret < 0) {
  108. goto done;
  109. }
  110. for (i = 0; i < MAX_PPS_COUNT; i++) {
  111. if (ps.pps_list[i]) {
  112. pps = (const PPS*)ps.pps_list[i]->data;
  113. break;
  114. }
  115. }
  116. if (pps) {
  117. if (ps.sps_list[pps->sps_id]) {
  118. sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
  119. }
  120. }
  121. if (pps && sps) {
  122. uint8_t *data = NULL;
  123. int data_size = 0;
  124. if ((ret = h2645_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
  125. goto done;
  126. }
  127. ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
  128. av_freep(&data);
  129. if ((ret = h2645_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
  130. goto done;
  131. }
  132. ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
  133. av_freep(&data);
  134. } else {
  135. av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
  136. ret = AVERROR_INVALIDDATA;
  137. }
  138. done:
  139. ff_h264_ps_uninit(&ps);
  140. return ret;
  141. }
  142. #endif
  143. #if CONFIG_HEVC_MEDIACODEC_DECODER
  144. static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
  145. {
  146. int i;
  147. int ret;
  148. HEVCParamSets ps;
  149. HEVCSEI sei;
  150. const HEVCVPS *vps = NULL;
  151. const HEVCPPS *pps = NULL;
  152. const HEVCSPS *sps = NULL;
  153. int is_nalff = 0;
  154. int nal_length_size = 0;
  155. uint8_t *vps_data = NULL;
  156. uint8_t *sps_data = NULL;
  157. uint8_t *pps_data = NULL;
  158. int vps_data_size = 0;
  159. int sps_data_size = 0;
  160. int pps_data_size = 0;
  161. memset(&ps, 0, sizeof(ps));
  162. memset(&sei, 0, sizeof(sei));
  163. ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
  164. &ps, &sei, &is_nalff, &nal_length_size, 0, 1, avctx);
  165. if (ret < 0) {
  166. goto done;
  167. }
  168. for (i = 0; i < HEVC_MAX_VPS_COUNT; i++) {
  169. if (ps.vps_list[i]) {
  170. vps = (const HEVCVPS*)ps.vps_list[i]->data;
  171. break;
  172. }
  173. }
  174. for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
  175. if (ps.pps_list[i]) {
  176. pps = (const HEVCPPS*)ps.pps_list[i]->data;
  177. break;
  178. }
  179. }
  180. if (pps) {
  181. if (ps.sps_list[pps->sps_id]) {
  182. sps = (const HEVCSPS*)ps.sps_list[pps->sps_id]->data;
  183. }
  184. }
  185. if (vps && pps && sps) {
  186. uint8_t *data;
  187. int data_size;
  188. if ((ret = h2645_ps_to_nalu(vps->data, vps->data_size, &vps_data, &vps_data_size)) < 0 ||
  189. (ret = h2645_ps_to_nalu(sps->data, sps->data_size, &sps_data, &sps_data_size)) < 0 ||
  190. (ret = h2645_ps_to_nalu(pps->data, pps->data_size, &pps_data, &pps_data_size)) < 0) {
  191. goto done;
  192. }
  193. data_size = vps_data_size + sps_data_size + pps_data_size;
  194. data = av_mallocz(data_size);
  195. if (!data) {
  196. ret = AVERROR(ENOMEM);
  197. goto done;
  198. }
  199. memcpy(data , vps_data, vps_data_size);
  200. memcpy(data + vps_data_size , sps_data, sps_data_size);
  201. memcpy(data + vps_data_size + sps_data_size, pps_data, pps_data_size);
  202. ff_AMediaFormat_setBuffer(format, "csd-0", data, data_size);
  203. av_freep(&data);
  204. } else {
  205. av_log(avctx, AV_LOG_ERROR, "Could not extract VPS/PPS/SPS from extradata");
  206. ret = AVERROR_INVALIDDATA;
  207. }
  208. done:
  209. ff_hevc_ps_uninit(&ps);
  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. done:
  325. if (format) {
  326. ff_AMediaFormat_delete(format);
  327. }
  328. if (ret < 0) {
  329. mediacodec_decode_close(avctx);
  330. }
  331. return ret;
  332. }
  333. static int mediacodec_send_receive(AVCodecContext *avctx,
  334. MediaCodecH264DecContext *s,
  335. AVFrame *frame, bool wait)
  336. {
  337. int ret;
  338. /* send any pending data from buffered packet */
  339. while (s->buffered_pkt.size) {
  340. ret = ff_mediacodec_dec_send(avctx, s->ctx, &s->buffered_pkt);
  341. if (ret == AVERROR(EAGAIN))
  342. break;
  343. else if (ret < 0)
  344. return ret;
  345. s->buffered_pkt.size -= ret;
  346. s->buffered_pkt.data += ret;
  347. if (s->buffered_pkt.size <= 0)
  348. av_packet_unref(&s->buffered_pkt);
  349. }
  350. /* check for new frame */
  351. return ff_mediacodec_dec_receive(avctx, s->ctx, frame, wait);
  352. }
  353. static int mediacodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  354. {
  355. MediaCodecH264DecContext *s = avctx->priv_data;
  356. int ret;
  357. /*
  358. * MediaCodec.flush() discards both input and output buffers, thus we
  359. * need to delay the call to this function until the user has released or
  360. * renderered the frames he retains.
  361. *
  362. * After we have buffered an input packet, check if the codec is in the
  363. * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
  364. *
  365. * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
  366. * the codec (because the user retains frames). The codec stays in the
  367. * flushing state.
  368. *
  369. * ff_mediacodec_dec_flush returns 1 if the flush can actually be
  370. * performed on the codec. The codec leaves the flushing state and can
  371. * process again packets.
  372. *
  373. * ff_mediacodec_dec_flush returns a negative value if an error has
  374. * occurred.
  375. *
  376. */
  377. if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
  378. if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
  379. return AVERROR(EAGAIN);
  380. }
  381. }
  382. /* flush buffered packet and check for new frame */
  383. ret = mediacodec_send_receive(avctx, s, frame, false);
  384. if (ret != AVERROR(EAGAIN))
  385. return ret;
  386. /* skip fetching new packet if we still have one buffered */
  387. if (s->buffered_pkt.size > 0)
  388. return AVERROR(EAGAIN);
  389. /* fetch new packet or eof */
  390. ret = ff_decode_get_packet(avctx, &s->buffered_pkt);
  391. if (ret == AVERROR_EOF) {
  392. AVPacket null_pkt = { 0 };
  393. ret = ff_mediacodec_dec_send(avctx, s->ctx, &null_pkt);
  394. if (ret < 0)
  395. return ret;
  396. }
  397. else if (ret < 0)
  398. return ret;
  399. /* crank decoder with new packet */
  400. return mediacodec_send_receive(avctx, s, frame, true);
  401. }
  402. static void mediacodec_decode_flush(AVCodecContext *avctx)
  403. {
  404. MediaCodecH264DecContext *s = avctx->priv_data;
  405. av_packet_unref(&s->buffered_pkt);
  406. ff_mediacodec_dec_flush(avctx, s->ctx);
  407. }
  408. static const AVCodecHWConfigInternal *mediacodec_hw_configs[] = {
  409. &(const AVCodecHWConfigInternal) {
  410. .public = {
  411. .pix_fmt = AV_PIX_FMT_MEDIACODEC,
  412. .methods = AV_CODEC_HW_CONFIG_METHOD_AD_HOC |
  413. AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
  414. .device_type = AV_HWDEVICE_TYPE_MEDIACODEC,
  415. },
  416. .hwaccel = NULL,
  417. },
  418. NULL
  419. };
  420. #if CONFIG_H264_MEDIACODEC_DECODER
  421. AVCodec ff_h264_mediacodec_decoder = {
  422. .name = "h264_mediacodec",
  423. .long_name = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
  424. .type = AVMEDIA_TYPE_VIDEO,
  425. .id = AV_CODEC_ID_H264,
  426. .priv_data_size = sizeof(MediaCodecH264DecContext),
  427. .init = mediacodec_decode_init,
  428. .receive_frame = mediacodec_receive_frame,
  429. .flush = mediacodec_decode_flush,
  430. .close = mediacodec_decode_close,
  431. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
  432. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  433. .bsfs = "h264_mp4toannexb",
  434. .hw_configs = mediacodec_hw_configs,
  435. .wrapper_name = "mediacodec",
  436. };
  437. #endif
  438. #if CONFIG_HEVC_MEDIACODEC_DECODER
  439. AVCodec ff_hevc_mediacodec_decoder = {
  440. .name = "hevc_mediacodec",
  441. .long_name = NULL_IF_CONFIG_SMALL("H.265 Android MediaCodec decoder"),
  442. .type = AVMEDIA_TYPE_VIDEO,
  443. .id = AV_CODEC_ID_HEVC,
  444. .priv_data_size = sizeof(MediaCodecH264DecContext),
  445. .init = mediacodec_decode_init,
  446. .receive_frame = mediacodec_receive_frame,
  447. .flush = mediacodec_decode_flush,
  448. .close = mediacodec_decode_close,
  449. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
  450. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  451. .bsfs = "hevc_mp4toannexb",
  452. .hw_configs = mediacodec_hw_configs,
  453. .wrapper_name = "mediacodec",
  454. };
  455. #endif
  456. #if CONFIG_MPEG2_MEDIACODEC_DECODER
  457. AVCodec ff_mpeg2_mediacodec_decoder = {
  458. .name = "mpeg2_mediacodec",
  459. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 Android MediaCodec decoder"),
  460. .type = AVMEDIA_TYPE_VIDEO,
  461. .id = AV_CODEC_ID_MPEG2VIDEO,
  462. .priv_data_size = sizeof(MediaCodecH264DecContext),
  463. .init = mediacodec_decode_init,
  464. .receive_frame = mediacodec_receive_frame,
  465. .flush = mediacodec_decode_flush,
  466. .close = mediacodec_decode_close,
  467. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
  468. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  469. .hw_configs = mediacodec_hw_configs,
  470. .wrapper_name = "mediacodec",
  471. };
  472. #endif
  473. #if CONFIG_MPEG4_MEDIACODEC_DECODER
  474. AVCodec ff_mpeg4_mediacodec_decoder = {
  475. .name = "mpeg4_mediacodec",
  476. .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Android MediaCodec decoder"),
  477. .type = AVMEDIA_TYPE_VIDEO,
  478. .id = AV_CODEC_ID_MPEG4,
  479. .priv_data_size = sizeof(MediaCodecH264DecContext),
  480. .init = mediacodec_decode_init,
  481. .receive_frame = mediacodec_receive_frame,
  482. .flush = mediacodec_decode_flush,
  483. .close = mediacodec_decode_close,
  484. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
  485. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  486. .hw_configs = mediacodec_hw_configs,
  487. .wrapper_name = "mediacodec",
  488. };
  489. #endif
  490. #if CONFIG_VP8_MEDIACODEC_DECODER
  491. AVCodec ff_vp8_mediacodec_decoder = {
  492. .name = "vp8_mediacodec",
  493. .long_name = NULL_IF_CONFIG_SMALL("VP8 Android MediaCodec decoder"),
  494. .type = AVMEDIA_TYPE_VIDEO,
  495. .id = AV_CODEC_ID_VP8,
  496. .priv_data_size = sizeof(MediaCodecH264DecContext),
  497. .init = mediacodec_decode_init,
  498. .receive_frame = mediacodec_receive_frame,
  499. .flush = mediacodec_decode_flush,
  500. .close = mediacodec_decode_close,
  501. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
  502. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  503. .hw_configs = mediacodec_hw_configs,
  504. .wrapper_name = "mediacodec",
  505. };
  506. #endif
  507. #if CONFIG_VP9_MEDIACODEC_DECODER
  508. AVCodec ff_vp9_mediacodec_decoder = {
  509. .name = "vp9_mediacodec",
  510. .long_name = NULL_IF_CONFIG_SMALL("VP9 Android MediaCodec decoder"),
  511. .type = AVMEDIA_TYPE_VIDEO,
  512. .id = AV_CODEC_ID_VP9,
  513. .priv_data_size = sizeof(MediaCodecH264DecContext),
  514. .init = mediacodec_decode_init,
  515. .receive_frame = mediacodec_receive_frame,
  516. .flush = mediacodec_decode_flush,
  517. .close = mediacodec_decode_close,
  518. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
  519. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  520. .hw_configs = mediacodec_hw_configs,
  521. .wrapper_name = "mediacodec",
  522. };
  523. #endif