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.

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