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.

620 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 "libavutil/internal.h"
  30. #include "avcodec.h"
  31. #include "decode.h"
  32. #include "h264_parse.h"
  33. #include "hevc_parse.h"
  34. #include "hwaccel.h"
  35. #include "internal.h"
  36. #include "mediacodec_wrapper.h"
  37. #include "mediacodecdec_common.h"
  38. typedef struct MediaCodecH264DecContext {
  39. MediaCodecDecContext *ctx;
  40. AVPacket buffered_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_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. ff_hevc_ps_uninit(&ps);
  211. av_freep(&vps_data);
  212. av_freep(&sps_data);
  213. av_freep(&pps_data);
  214. return ret;
  215. }
  216. #endif
  217. #if CONFIG_MPEG2_MEDIACODEC_DECODER
  218. static int mpeg2_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
  219. {
  220. int ret = 0;
  221. if (avctx->extradata) {
  222. ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
  223. }
  224. return ret;
  225. }
  226. #endif
  227. #if CONFIG_MPEG4_MEDIACODEC_DECODER
  228. static int mpeg4_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
  229. {
  230. int ret = 0;
  231. if (avctx->extradata) {
  232. ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
  233. }
  234. return ret;
  235. }
  236. #endif
  237. #if CONFIG_VP8_MEDIACODEC_DECODER || CONFIG_VP9_MEDIACODEC_DECODER
  238. static int vpx_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
  239. {
  240. int ret = 0;
  241. if (avctx->extradata) {
  242. ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
  243. }
  244. return ret;
  245. }
  246. #endif
  247. static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
  248. {
  249. int ret;
  250. const char *codec_mime = NULL;
  251. FFAMediaFormat *format = NULL;
  252. MediaCodecH264DecContext *s = avctx->priv_data;
  253. format = ff_AMediaFormat_new();
  254. if (!format) {
  255. av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
  256. ret = AVERROR_EXTERNAL;
  257. goto done;
  258. }
  259. switch (avctx->codec_id) {
  260. #if CONFIG_H264_MEDIACODEC_DECODER
  261. case AV_CODEC_ID_H264:
  262. codec_mime = "video/avc";
  263. ret = h264_set_extradata(avctx, format);
  264. if (ret < 0)
  265. goto done;
  266. break;
  267. #endif
  268. #if CONFIG_HEVC_MEDIACODEC_DECODER
  269. case AV_CODEC_ID_HEVC:
  270. codec_mime = "video/hevc";
  271. ret = hevc_set_extradata(avctx, format);
  272. if (ret < 0)
  273. goto done;
  274. break;
  275. #endif
  276. #if CONFIG_MPEG2_MEDIACODEC_DECODER
  277. case AV_CODEC_ID_MPEG2VIDEO:
  278. codec_mime = "video/mpeg2";
  279. ret = mpeg2_set_extradata(avctx, format);
  280. if (ret < 0)
  281. goto done;
  282. break;
  283. #endif
  284. #if CONFIG_MPEG4_MEDIACODEC_DECODER
  285. case AV_CODEC_ID_MPEG4:
  286. codec_mime = "video/mp4v-es",
  287. ret = mpeg4_set_extradata(avctx, format);
  288. if (ret < 0)
  289. goto done;
  290. break;
  291. #endif
  292. #if CONFIG_VP8_MEDIACODEC_DECODER
  293. case AV_CODEC_ID_VP8:
  294. codec_mime = "video/x-vnd.on2.vp8";
  295. ret = vpx_set_extradata(avctx, format);
  296. if (ret < 0)
  297. goto done;
  298. break;
  299. #endif
  300. #if CONFIG_VP9_MEDIACODEC_DECODER
  301. case AV_CODEC_ID_VP9:
  302. codec_mime = "video/x-vnd.on2.vp9";
  303. ret = vpx_set_extradata(avctx, format);
  304. if (ret < 0)
  305. goto done;
  306. break;
  307. #endif
  308. default:
  309. av_assert0(0);
  310. }
  311. ff_AMediaFormat_setString(format, "mime", codec_mime);
  312. ff_AMediaFormat_setInt32(format, "width", avctx->width);
  313. ff_AMediaFormat_setInt32(format, "height", avctx->height);
  314. s->ctx = av_mallocz(sizeof(*s->ctx));
  315. if (!s->ctx) {
  316. av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
  317. ret = AVERROR(ENOMEM);
  318. goto done;
  319. }
  320. if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
  321. s->ctx = NULL;
  322. goto done;
  323. }
  324. av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
  325. done:
  326. if (format) {
  327. ff_AMediaFormat_delete(format);
  328. }
  329. if (ret < 0) {
  330. mediacodec_decode_close(avctx);
  331. }
  332. return ret;
  333. }
  334. static int mediacodec_send_receive(AVCodecContext *avctx,
  335. MediaCodecH264DecContext *s,
  336. AVFrame *frame, bool wait)
  337. {
  338. int ret;
  339. /* send any pending data from buffered packet */
  340. while (s->buffered_pkt.size) {
  341. ret = ff_mediacodec_dec_send(avctx, s->ctx, &s->buffered_pkt);
  342. if (ret == AVERROR(EAGAIN))
  343. break;
  344. else if (ret < 0)
  345. return ret;
  346. s->buffered_pkt.size -= ret;
  347. s->buffered_pkt.data += ret;
  348. if (s->buffered_pkt.size <= 0)
  349. av_packet_unref(&s->buffered_pkt);
  350. }
  351. /* check for new frame */
  352. return ff_mediacodec_dec_receive(avctx, s->ctx, frame, wait);
  353. }
  354. static int mediacodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  355. {
  356. MediaCodecH264DecContext *s = avctx->priv_data;
  357. int ret;
  358. /*
  359. * MediaCodec.flush() discards both input and output buffers, thus we
  360. * need to delay the call to this function until the user has released or
  361. * renderered the frames he retains.
  362. *
  363. * After we have buffered an input packet, check if the codec is in the
  364. * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
  365. *
  366. * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
  367. * the codec (because the user retains frames). The codec stays in the
  368. * flushing state.
  369. *
  370. * ff_mediacodec_dec_flush returns 1 if the flush can actually be
  371. * performed on the codec. The codec leaves the flushing state and can
  372. * process again packets.
  373. *
  374. * ff_mediacodec_dec_flush returns a negative value if an error has
  375. * occurred.
  376. *
  377. */
  378. if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
  379. if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
  380. return AVERROR(EAGAIN);
  381. }
  382. }
  383. /* flush buffered packet and check for new frame */
  384. ret = mediacodec_send_receive(avctx, s, frame, false);
  385. if (ret != AVERROR(EAGAIN))
  386. return ret;
  387. /* skip fetching new packet if we still have one buffered */
  388. if (s->buffered_pkt.size > 0)
  389. return AVERROR(EAGAIN);
  390. /* fetch new packet or eof */
  391. ret = ff_decode_get_packet(avctx, &s->buffered_pkt);
  392. if (ret == AVERROR_EOF) {
  393. AVPacket null_pkt = { 0 };
  394. ret = ff_mediacodec_dec_send(avctx, s->ctx, &null_pkt);
  395. if (ret < 0)
  396. return ret;
  397. }
  398. else if (ret < 0)
  399. return ret;
  400. /* crank decoder with new packet */
  401. return mediacodec_send_receive(avctx, s, frame, true);
  402. }
  403. static void mediacodec_decode_flush(AVCodecContext *avctx)
  404. {
  405. MediaCodecH264DecContext *s = avctx->priv_data;
  406. av_packet_unref(&s->buffered_pkt);
  407. ff_mediacodec_dec_flush(avctx, s->ctx);
  408. }
  409. static const AVCodecHWConfigInternal *mediacodec_hw_configs[] = {
  410. &(const AVCodecHWConfigInternal) {
  411. .public = {
  412. .pix_fmt = AV_PIX_FMT_MEDIACODEC,
  413. .methods = AV_CODEC_HW_CONFIG_METHOD_AD_HOC |
  414. AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
  415. .device_type = AV_HWDEVICE_TYPE_MEDIACODEC,
  416. },
  417. .hwaccel = NULL,
  418. },
  419. NULL
  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. .receive_frame = mediacodec_receive_frame,
  430. .flush = mediacodec_decode_flush,
  431. .close = mediacodec_decode_close,
  432. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
  433. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  434. .bsfs = "h264_mp4toannexb",
  435. .hw_configs = mediacodec_hw_configs,
  436. .wrapper_name = "mediacodec",
  437. };
  438. #endif
  439. #if CONFIG_HEVC_MEDIACODEC_DECODER
  440. AVCodec ff_hevc_mediacodec_decoder = {
  441. .name = "hevc_mediacodec",
  442. .long_name = NULL_IF_CONFIG_SMALL("H.265 Android MediaCodec decoder"),
  443. .type = AVMEDIA_TYPE_VIDEO,
  444. .id = AV_CODEC_ID_HEVC,
  445. .priv_data_size = sizeof(MediaCodecH264DecContext),
  446. .init = mediacodec_decode_init,
  447. .receive_frame = mediacodec_receive_frame,
  448. .flush = mediacodec_decode_flush,
  449. .close = mediacodec_decode_close,
  450. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
  451. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  452. .bsfs = "hevc_mp4toannexb",
  453. .hw_configs = mediacodec_hw_configs,
  454. .wrapper_name = "mediacodec",
  455. };
  456. #endif
  457. #if CONFIG_MPEG2_MEDIACODEC_DECODER
  458. AVCodec ff_mpeg2_mediacodec_decoder = {
  459. .name = "mpeg2_mediacodec",
  460. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 Android MediaCodec decoder"),
  461. .type = AVMEDIA_TYPE_VIDEO,
  462. .id = AV_CODEC_ID_MPEG2VIDEO,
  463. .priv_data_size = sizeof(MediaCodecH264DecContext),
  464. .init = mediacodec_decode_init,
  465. .receive_frame = mediacodec_receive_frame,
  466. .flush = mediacodec_decode_flush,
  467. .close = mediacodec_decode_close,
  468. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
  469. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  470. .hw_configs = mediacodec_hw_configs,
  471. .wrapper_name = "mediacodec",
  472. };
  473. #endif
  474. #if CONFIG_MPEG4_MEDIACODEC_DECODER
  475. AVCodec ff_mpeg4_mediacodec_decoder = {
  476. .name = "mpeg4_mediacodec",
  477. .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Android MediaCodec decoder"),
  478. .type = AVMEDIA_TYPE_VIDEO,
  479. .id = AV_CODEC_ID_MPEG4,
  480. .priv_data_size = sizeof(MediaCodecH264DecContext),
  481. .init = mediacodec_decode_init,
  482. .receive_frame = mediacodec_receive_frame,
  483. .flush = mediacodec_decode_flush,
  484. .close = mediacodec_decode_close,
  485. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
  486. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  487. .hw_configs = mediacodec_hw_configs,
  488. .wrapper_name = "mediacodec",
  489. };
  490. #endif
  491. #if CONFIG_VP8_MEDIACODEC_DECODER
  492. AVCodec ff_vp8_mediacodec_decoder = {
  493. .name = "vp8_mediacodec",
  494. .long_name = NULL_IF_CONFIG_SMALL("VP8 Android MediaCodec decoder"),
  495. .type = AVMEDIA_TYPE_VIDEO,
  496. .id = AV_CODEC_ID_VP8,
  497. .priv_data_size = sizeof(MediaCodecH264DecContext),
  498. .init = mediacodec_decode_init,
  499. .receive_frame = mediacodec_receive_frame,
  500. .flush = mediacodec_decode_flush,
  501. .close = mediacodec_decode_close,
  502. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
  503. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  504. .hw_configs = mediacodec_hw_configs,
  505. .wrapper_name = "mediacodec",
  506. };
  507. #endif
  508. #if CONFIG_VP9_MEDIACODEC_DECODER
  509. AVCodec ff_vp9_mediacodec_decoder = {
  510. .name = "vp9_mediacodec",
  511. .long_name = NULL_IF_CONFIG_SMALL("VP9 Android MediaCodec decoder"),
  512. .type = AVMEDIA_TYPE_VIDEO,
  513. .id = AV_CODEC_ID_VP9,
  514. .priv_data_size = sizeof(MediaCodecH264DecContext),
  515. .init = mediacodec_decode_init,
  516. .receive_frame = mediacodec_receive_frame,
  517. .flush = mediacodec_decode_flush,
  518. .close = mediacodec_decode_close,
  519. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
  520. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
  521. .hw_configs = mediacodec_hw_configs,
  522. .wrapper_name = "mediacodec",
  523. };
  524. #endif