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.

553 lines
16KB

  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. AVClass *avclass;
  40. MediaCodecDecContext *ctx;
  41. AVPacket buffered_pkt;
  42. int delay_flush;
  43. } MediaCodecH264DecContext;
  44. static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
  45. {
  46. MediaCodecH264DecContext *s = avctx->priv_data;
  47. ff_mediacodec_dec_close(avctx, s->ctx);
  48. s->ctx = NULL;
  49. av_packet_unref(&s->buffered_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. HEVCSEI sei;
  153. const HEVCVPS *vps = NULL;
  154. const HEVCPPS *pps = NULL;
  155. const HEVCSPS *sps = NULL;
  156. int is_nalff = 0;
  157. int nal_length_size = 0;
  158. uint8_t *vps_data = NULL;
  159. uint8_t *sps_data = NULL;
  160. uint8_t *pps_data = NULL;
  161. int vps_data_size = 0;
  162. int sps_data_size = 0;
  163. int pps_data_size = 0;
  164. memset(&ps, 0, sizeof(ps));
  165. memset(&sei, 0, sizeof(sei));
  166. ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
  167. &ps, &sei, &is_nalff, &nal_length_size, 0, 1, avctx);
  168. if (ret < 0) {
  169. goto done;
  170. }
  171. for (i = 0; i < HEVC_MAX_VPS_COUNT; i++) {
  172. if (ps.vps_list[i]) {
  173. vps = (const HEVCVPS*)ps.vps_list[i]->data;
  174. break;
  175. }
  176. }
  177. for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
  178. if (ps.pps_list[i]) {
  179. pps = (const HEVCPPS*)ps.pps_list[i]->data;
  180. break;
  181. }
  182. }
  183. if (pps) {
  184. if (ps.sps_list[pps->sps_id]) {
  185. sps = (const HEVCSPS*)ps.sps_list[pps->sps_id]->data;
  186. }
  187. }
  188. if (vps && pps && sps) {
  189. uint8_t *data;
  190. int data_size;
  191. if ((ret = h2645_ps_to_nalu(vps->data, vps->data_size, &vps_data, &vps_data_size)) < 0 ||
  192. (ret = h2645_ps_to_nalu(sps->data, sps->data_size, &sps_data, &sps_data_size)) < 0 ||
  193. (ret = h2645_ps_to_nalu(pps->data, pps->data_size, &pps_data, &pps_data_size)) < 0) {
  194. goto done;
  195. }
  196. data_size = vps_data_size + sps_data_size + pps_data_size;
  197. data = av_mallocz(data_size);
  198. if (!data) {
  199. ret = AVERROR(ENOMEM);
  200. goto done;
  201. }
  202. memcpy(data , vps_data, vps_data_size);
  203. memcpy(data + vps_data_size , sps_data, sps_data_size);
  204. memcpy(data + vps_data_size + sps_data_size, pps_data, pps_data_size);
  205. ff_AMediaFormat_setBuffer(format, "csd-0", data, data_size);
  206. av_freep(&data);
  207. } else {
  208. av_log(avctx, AV_LOG_ERROR, "Could not extract VPS/PPS/SPS from extradata");
  209. ret = AVERROR_INVALIDDATA;
  210. }
  211. done:
  212. ff_hevc_ps_uninit(&ps);
  213. av_freep(&vps_data);
  214. av_freep(&sps_data);
  215. av_freep(&pps_data);
  216. return ret;
  217. }
  218. #endif
  219. #if CONFIG_MPEG2_MEDIACODEC_DECODER || \
  220. CONFIG_MPEG4_MEDIACODEC_DECODER || \
  221. CONFIG_VP8_MEDIACODEC_DECODER || \
  222. CONFIG_VP9_MEDIACODEC_DECODER
  223. static int common_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
  224. {
  225. int ret = 0;
  226. if (avctx->extradata) {
  227. ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
  228. }
  229. return ret;
  230. }
  231. #endif
  232. static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
  233. {
  234. int ret;
  235. const char *codec_mime = NULL;
  236. FFAMediaFormat *format = NULL;
  237. MediaCodecH264DecContext *s = avctx->priv_data;
  238. format = ff_AMediaFormat_new();
  239. if (!format) {
  240. av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
  241. ret = AVERROR_EXTERNAL;
  242. goto done;
  243. }
  244. switch (avctx->codec_id) {
  245. #if CONFIG_H264_MEDIACODEC_DECODER
  246. case AV_CODEC_ID_H264:
  247. codec_mime = "video/avc";
  248. ret = h264_set_extradata(avctx, format);
  249. if (ret < 0)
  250. goto done;
  251. break;
  252. #endif
  253. #if CONFIG_HEVC_MEDIACODEC_DECODER
  254. case AV_CODEC_ID_HEVC:
  255. codec_mime = "video/hevc";
  256. ret = hevc_set_extradata(avctx, format);
  257. if (ret < 0)
  258. goto done;
  259. break;
  260. #endif
  261. #if CONFIG_MPEG2_MEDIACODEC_DECODER
  262. case AV_CODEC_ID_MPEG2VIDEO:
  263. codec_mime = "video/mpeg2";
  264. ret = common_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 = common_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 = common_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 = common_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. s->ctx->delay_flush = s->delay_flush;
  306. if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
  307. s->ctx = NULL;
  308. goto done;
  309. }
  310. av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
  311. done:
  312. if (format) {
  313. ff_AMediaFormat_delete(format);
  314. }
  315. if (ret < 0) {
  316. mediacodec_decode_close(avctx);
  317. }
  318. return ret;
  319. }
  320. static int mediacodec_send_receive(AVCodecContext *avctx,
  321. MediaCodecH264DecContext *s,
  322. AVFrame *frame, bool wait)
  323. {
  324. int ret;
  325. /* send any pending data from buffered packet */
  326. while (s->buffered_pkt.size) {
  327. ret = ff_mediacodec_dec_send(avctx, s->ctx, &s->buffered_pkt);
  328. if (ret == AVERROR(EAGAIN))
  329. break;
  330. else if (ret < 0)
  331. return ret;
  332. s->buffered_pkt.size -= ret;
  333. s->buffered_pkt.data += ret;
  334. if (s->buffered_pkt.size <= 0)
  335. av_packet_unref(&s->buffered_pkt);
  336. }
  337. /* check for new frame */
  338. return ff_mediacodec_dec_receive(avctx, s->ctx, frame, wait);
  339. }
  340. static int mediacodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
  341. {
  342. MediaCodecH264DecContext *s = avctx->priv_data;
  343. int ret;
  344. /*
  345. * MediaCodec.flush() discards both input and output buffers, thus we
  346. * need to delay the call to this function until the user has released or
  347. * renderered the frames he retains.
  348. *
  349. * After we have buffered an input packet, check if the codec is in the
  350. * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
  351. *
  352. * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
  353. * the codec (because the user retains frames). The codec stays in the
  354. * flushing state.
  355. *
  356. * ff_mediacodec_dec_flush returns 1 if the flush can actually be
  357. * performed on the codec. The codec leaves the flushing state and can
  358. * process again packets.
  359. *
  360. * ff_mediacodec_dec_flush returns a negative value if an error has
  361. * occurred.
  362. *
  363. */
  364. if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
  365. if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
  366. return AVERROR(EAGAIN);
  367. }
  368. }
  369. /* flush buffered packet and check for new frame */
  370. ret = mediacodec_send_receive(avctx, s, frame, false);
  371. if (ret != AVERROR(EAGAIN))
  372. return ret;
  373. /* skip fetching new packet if we still have one buffered */
  374. if (s->buffered_pkt.size > 0)
  375. return AVERROR(EAGAIN);
  376. /* fetch new packet or eof */
  377. ret = ff_decode_get_packet(avctx, &s->buffered_pkt);
  378. if (ret == AVERROR_EOF) {
  379. AVPacket null_pkt = { 0 };
  380. ret = ff_mediacodec_dec_send(avctx, s->ctx, &null_pkt);
  381. if (ret < 0)
  382. return ret;
  383. }
  384. else if (ret < 0)
  385. return ret;
  386. /* crank decoder with new packet */
  387. return mediacodec_send_receive(avctx, s, frame, true);
  388. }
  389. static void mediacodec_decode_flush(AVCodecContext *avctx)
  390. {
  391. MediaCodecH264DecContext *s = avctx->priv_data;
  392. av_packet_unref(&s->buffered_pkt);
  393. ff_mediacodec_dec_flush(avctx, s->ctx);
  394. }
  395. static const AVCodecHWConfigInternal *mediacodec_hw_configs[] = {
  396. &(const AVCodecHWConfigInternal) {
  397. .public = {
  398. .pix_fmt = AV_PIX_FMT_MEDIACODEC,
  399. .methods = AV_CODEC_HW_CONFIG_METHOD_AD_HOC |
  400. AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
  401. .device_type = AV_HWDEVICE_TYPE_MEDIACODEC,
  402. },
  403. .hwaccel = NULL,
  404. },
  405. NULL
  406. };
  407. #define OFFSET(x) offsetof(MediaCodecH264DecContext, x)
  408. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  409. static const AVOption ff_mediacodec_vdec_options[] = {
  410. { "delay_flush", "Delay flush until hw output buffers are returned to the decoder",
  411. OFFSET(delay_flush), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VD },
  412. { NULL }
  413. };
  414. #define DECLARE_MEDIACODEC_VCLASS(short_name) \
  415. static const AVClass ff_##short_name##_mediacodec_dec_class = { \
  416. .class_name = #short_name "_mediacodec", \
  417. .item_name = av_default_item_name, \
  418. .option = ff_mediacodec_vdec_options, \
  419. .version = LIBAVUTIL_VERSION_INT, \
  420. };
  421. #define DECLARE_MEDIACODEC_VDEC(short_name, full_name, codec_id, bsf) \
  422. DECLARE_MEDIACODEC_VCLASS(short_name) \
  423. AVCodec ff_##short_name##_mediacodec_decoder = { \
  424. .name = #short_name "_mediacodec", \
  425. .long_name = NULL_IF_CONFIG_SMALL(full_name " Android MediaCodec decoder"), \
  426. .type = AVMEDIA_TYPE_VIDEO, \
  427. .id = codec_id, \
  428. .priv_class = &ff_##short_name##_mediacodec_dec_class, \
  429. .priv_data_size = sizeof(MediaCodecH264DecContext), \
  430. .init = mediacodec_decode_init, \
  431. .receive_frame = mediacodec_receive_frame, \
  432. .flush = mediacodec_decode_flush, \
  433. .close = mediacodec_decode_close, \
  434. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
  435. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS, \
  436. .bsfs = bsf, \
  437. .hw_configs = mediacodec_hw_configs, \
  438. .wrapper_name = "mediacodec", \
  439. }; \
  440. #if CONFIG_H264_MEDIACODEC_DECODER
  441. DECLARE_MEDIACODEC_VDEC(h264, "H.264", AV_CODEC_ID_H264, "h264_mp4toannexb")
  442. #endif
  443. #if CONFIG_HEVC_MEDIACODEC_DECODER
  444. DECLARE_MEDIACODEC_VDEC(hevc, "H.265", AV_CODEC_ID_HEVC, "hevc_mp4toannexb")
  445. #endif
  446. #if CONFIG_MPEG2_MEDIACODEC_DECODER
  447. DECLARE_MEDIACODEC_VDEC(mpeg2, "MPEG-2", AV_CODEC_ID_MPEG2VIDEO, NULL)
  448. #endif
  449. #if CONFIG_MPEG4_MEDIACODEC_DECODER
  450. DECLARE_MEDIACODEC_VDEC(mpeg4, "MPEG-4", AV_CODEC_ID_MPEG4, NULL)
  451. #endif
  452. #if CONFIG_VP8_MEDIACODEC_DECODER
  453. DECLARE_MEDIACODEC_VDEC(vp8, "VP8", AV_CODEC_ID_VP8, NULL)
  454. #endif
  455. #if CONFIG_VP9_MEDIACODEC_DECODER
  456. DECLARE_MEDIACODEC_VDEC(vp9, "VP9", AV_CODEC_ID_VP9, NULL)
  457. #endif