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.

568 lines
19KB

  1. /*
  2. * Android MediaCodec decoder
  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 <string.h>
  23. #include <sys/types.h>
  24. #include "libavutil/common.h"
  25. #include "libavutil/mem.h"
  26. #include "libavutil/log.h"
  27. #include "libavutil/pixfmt.h"
  28. #include "libavutil/time.h"
  29. #include "libavutil/timestamp.h"
  30. #include "avcodec.h"
  31. #include "internal.h"
  32. #include "mediacodec_sw_buffer.h"
  33. #include "mediacodec_wrapper.h"
  34. #include "mediacodecdec.h"
  35. /**
  36. * OMX.k3.video.decoder.avc, OMX.NVIDIA.* OMX.SEC.avc.dec and OMX.google
  37. * codec workarounds used in various place are taken from the Gstreamer
  38. * project.
  39. *
  40. * Gstreamer references:
  41. * https://cgit.freedesktop.org/gstreamer/gst-plugins-bad/tree/sys/androidmedia/
  42. *
  43. * Gstreamer copyright notice:
  44. *
  45. * Copyright (C) 2012, Collabora Ltd.
  46. * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
  47. *
  48. * Copyright (C) 2012, Rafaël Carré <funman@videolanorg>
  49. *
  50. * Copyright (C) 2015, Sebastian Dröge <sebastian@centricular.com>
  51. *
  52. * Copyright (C) 2014-2015, Collabora Ltd.
  53. * Author: Matthieu Bouron <matthieu.bouron@gcollabora.com>
  54. *
  55. * Copyright (C) 2015, Edward Hervey
  56. * Author: Edward Hervey <bilboed@gmail.com>
  57. *
  58. * Copyright (C) 2015, Matthew Waters <matthew@centricular.com>
  59. *
  60. * This library is free software; you can redistribute it and/or
  61. * modify it under the terms of the GNU Lesser General Public
  62. * License as published by the Free Software Foundation
  63. * version 2.1 of the License.
  64. *
  65. * This library is distributed in the hope that it will be useful,
  66. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  67. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  68. * Lesser General Public License for more details.
  69. *
  70. * You should have received a copy of the GNU Lesser General Public
  71. * License along with this library; if not, write to the Free Software
  72. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  73. *
  74. */
  75. #define INPUT_DEQUEUE_TIMEOUT_US 8000
  76. #define OUTPUT_DEQUEUE_TIMEOUT_US 8000
  77. #define OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US 1000000
  78. enum {
  79. COLOR_FormatYUV420Planar = 0x13,
  80. COLOR_FormatYUV420SemiPlanar = 0x15,
  81. COLOR_FormatYCbYCr = 0x19,
  82. COLOR_FormatAndroidOpaque = 0x7F000789,
  83. COLOR_QCOM_FormatYUV420SemiPlanar = 0x7fa30c00,
  84. COLOR_QCOM_FormatYUV420SemiPlanar32m = 0x7fa30c04,
  85. COLOR_QCOM_FormatYUV420PackedSemiPlanar64x32Tile2m8ka = 0x7fa30c03,
  86. COLOR_TI_FormatYUV420PackedSemiPlanar = 0x7f000100,
  87. COLOR_TI_FormatYUV420PackedSemiPlanarInterlaced = 0x7f000001,
  88. };
  89. static const struct {
  90. int color_format;
  91. enum AVPixelFormat pix_fmt;
  92. } color_formats[] = {
  93. { COLOR_FormatYUV420Planar, AV_PIX_FMT_YUV420P },
  94. { COLOR_FormatYUV420SemiPlanar, AV_PIX_FMT_NV12 },
  95. { COLOR_QCOM_FormatYUV420SemiPlanar, AV_PIX_FMT_NV12 },
  96. { COLOR_QCOM_FormatYUV420SemiPlanar32m, AV_PIX_FMT_NV12 },
  97. { COLOR_QCOM_FormatYUV420PackedSemiPlanar64x32Tile2m8ka, AV_PIX_FMT_NV12 },
  98. { COLOR_TI_FormatYUV420PackedSemiPlanar, AV_PIX_FMT_NV12 },
  99. { COLOR_TI_FormatYUV420PackedSemiPlanarInterlaced, AV_PIX_FMT_NV12 },
  100. { 0 }
  101. };
  102. static enum AVPixelFormat mcdec_map_color_format(AVCodecContext *avctx,
  103. MediaCodecDecContext *s,
  104. int color_format)
  105. {
  106. int i;
  107. enum AVPixelFormat ret = AV_PIX_FMT_NONE;
  108. if (!strcmp(s->codec_name, "OMX.k3.video.decoder.avc") && color_format == COLOR_FormatYCbYCr) {
  109. s->color_format = color_format = COLOR_TI_FormatYUV420PackedSemiPlanar;
  110. }
  111. for (i = 0; i < FF_ARRAY_ELEMS(color_formats); i++) {
  112. if (color_formats[i].color_format == color_format) {
  113. return color_formats[i].pix_fmt;
  114. }
  115. }
  116. av_log(avctx, AV_LOG_ERROR, "Output color format 0x%x (value=%d) is not supported\n",
  117. color_format, color_format);
  118. return ret;
  119. }
  120. static int mediacodec_wrap_buffer(AVCodecContext *avctx,
  121. MediaCodecDecContext *s,
  122. uint8_t *data,
  123. size_t size,
  124. ssize_t index,
  125. FFAMediaCodecBufferInfo *info,
  126. AVFrame *frame)
  127. {
  128. int ret = 0;
  129. int status = 0;
  130. frame->width = avctx->width;
  131. frame->height = avctx->height;
  132. frame->format = avctx->pix_fmt;
  133. /* MediaCodec buffers needs to be copied to our own refcounted buffers
  134. * because the flush command invalidates all input and output buffers.
  135. */
  136. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  137. av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer\n");
  138. goto done;
  139. }
  140. /* Override frame->pkt_pts as ff_get_buffer will override its value based
  141. * on the last avpacket received which is not in sync with the frame:
  142. * * N avpackets can be pushed before 1 frame is actually returned
  143. * * 0-sized avpackets are pushed to flush remaining frames at EOS */
  144. frame->pkt_pts = info->presentationTimeUs;
  145. av_log(avctx, AV_LOG_DEBUG,
  146. "Frame: width=%d stride=%d height=%d slice-height=%d "
  147. "crop-top=%d crop-bottom=%d crop-left=%d crop-right=%d encoder=%s\n"
  148. "destination linesizes=%d,%d,%d\n" ,
  149. avctx->width, s->stride, avctx->height, s->slice_height,
  150. s->crop_top, s->crop_bottom, s->crop_left, s->crop_right, s->codec_name,
  151. frame->linesize[0], frame->linesize[1], frame->linesize[2]);
  152. switch (s->color_format) {
  153. case COLOR_FormatYUV420Planar:
  154. ff_mediacodec_sw_buffer_copy_yuv420_planar(avctx, s, data, size, info, frame);
  155. break;
  156. case COLOR_FormatYUV420SemiPlanar:
  157. case COLOR_QCOM_FormatYUV420SemiPlanar:
  158. case COLOR_QCOM_FormatYUV420SemiPlanar32m:
  159. ff_mediacodec_sw_buffer_copy_yuv420_semi_planar(avctx, s, data, size, info, frame);
  160. break;
  161. case COLOR_TI_FormatYUV420PackedSemiPlanar:
  162. case COLOR_TI_FormatYUV420PackedSemiPlanarInterlaced:
  163. ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar(avctx, s, data, size, info, frame);
  164. break;
  165. case COLOR_QCOM_FormatYUV420PackedSemiPlanar64x32Tile2m8ka:
  166. ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar_64x32Tile2m8ka(avctx, s, data, size, info, frame);
  167. break;
  168. default:
  169. av_log(avctx, AV_LOG_ERROR, "Unsupported color format 0x%x (value=%d)\n",
  170. s->color_format, s->color_format);
  171. ret = AVERROR(EINVAL);
  172. goto done;
  173. }
  174. ret = 0;
  175. done:
  176. status = ff_AMediaCodec_releaseOutputBuffer(s->codec, index, 0);
  177. if (status < 0) {
  178. av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
  179. ret = AVERROR_EXTERNAL;
  180. }
  181. return ret;
  182. }
  183. static int mediacodec_dec_parse_format(AVCodecContext *avctx, MediaCodecDecContext *s)
  184. {
  185. int width = 0;
  186. int height = 0;
  187. int32_t value = 0;
  188. char *format = NULL;
  189. if (!s->format) {
  190. av_log(avctx, AV_LOG_ERROR, "Output MediaFormat is not set\n");
  191. return AVERROR(EINVAL);
  192. }
  193. format = ff_AMediaFormat_toString(s->format);
  194. if (!format) {
  195. return AVERROR_EXTERNAL;
  196. }
  197. av_log(avctx, AV_LOG_DEBUG, "Parsing MediaFormat %s\n", format);
  198. av_freep(&format);
  199. /* Mandatory fields */
  200. if (!ff_AMediaFormat_getInt32(s->format, "width", &value)) {
  201. format = ff_AMediaFormat_toString(s->format);
  202. av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "width", format);
  203. av_freep(&format);
  204. return AVERROR_EXTERNAL;
  205. }
  206. s->width = value;
  207. if (!ff_AMediaFormat_getInt32(s->format, "height", &value)) {
  208. format = ff_AMediaFormat_toString(s->format);
  209. av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "height", format);
  210. av_freep(&format);
  211. return AVERROR_EXTERNAL;
  212. }
  213. s->height = value;
  214. if (!ff_AMediaFormat_getInt32(s->format, "stride", &value)) {
  215. format = ff_AMediaFormat_toString(s->format);
  216. av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "stride", format);
  217. av_freep(&format);
  218. return AVERROR_EXTERNAL;
  219. }
  220. s->stride = value > 0 ? value : s->width;
  221. if (!ff_AMediaFormat_getInt32(s->format, "slice-height", &value)) {
  222. format = ff_AMediaFormat_toString(s->format);
  223. av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "slice-height", format);
  224. av_freep(&format);
  225. return AVERROR_EXTERNAL;
  226. }
  227. s->slice_height = value > 0 ? value : s->height;
  228. if (strstr(s->codec_name, "OMX.Nvidia.")) {
  229. s->slice_height = FFALIGN(s->height, 16);
  230. } else if (strstr(s->codec_name, "OMX.SEC.avc.dec")) {
  231. s->slice_height = avctx->height;
  232. s->stride = avctx->width;
  233. }
  234. if (!ff_AMediaFormat_getInt32(s->format, "color-format", &value)) {
  235. format = ff_AMediaFormat_toString(s->format);
  236. av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "color-format", format);
  237. av_freep(&format);
  238. return AVERROR_EXTERNAL;
  239. }
  240. s->color_format = value;
  241. s->pix_fmt = avctx->pix_fmt = mcdec_map_color_format(avctx, s, value);
  242. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  243. av_log(avctx, AV_LOG_ERROR, "Output color format is not supported\n");
  244. return AVERROR(EINVAL);
  245. }
  246. /* Optional fields */
  247. if (ff_AMediaFormat_getInt32(s->format, "crop-top", &value))
  248. s->crop_top = value;
  249. if (ff_AMediaFormat_getInt32(s->format, "crop-bottom", &value))
  250. s->crop_bottom = value;
  251. if (ff_AMediaFormat_getInt32(s->format, "crop-left", &value))
  252. s->crop_left = value;
  253. if (ff_AMediaFormat_getInt32(s->format, "crop-right", &value))
  254. s->crop_right = value;
  255. width = s->crop_right + 1 - s->crop_left;
  256. height = s->crop_bottom + 1 - s->crop_top;
  257. av_log(avctx, AV_LOG_INFO,
  258. "Output crop parameters top=%d bottom=%d left=%d right=%d, "
  259. "resulting dimensions width=%d height=%d\n",
  260. s->crop_top, s->crop_bottom, s->crop_left, s->crop_right,
  261. width, height);
  262. return ff_set_dimensions(avctx, width, height);
  263. }
  264. int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s,
  265. const char *mime, FFAMediaFormat *format)
  266. {
  267. int ret = 0;
  268. int status;
  269. s->first_buffer_at = av_gettime();
  270. s->codec_name = ff_AMediaCodecList_getCodecNameByType(mime, avctx);
  271. if (!s->codec_name) {
  272. ret = AVERROR_EXTERNAL;
  273. goto fail;
  274. }
  275. av_log(avctx, AV_LOG_DEBUG, "Found decoder %s\n", s->codec_name);
  276. s->codec = ff_AMediaCodec_createCodecByName(s->codec_name);
  277. if (!s->codec) {
  278. av_log(avctx, AV_LOG_ERROR, "Failed to create media decoder for type %s and name %s\n", mime, s->codec_name);
  279. ret = AVERROR_EXTERNAL;
  280. goto fail;
  281. }
  282. status = ff_AMediaCodec_configure(s->codec, format, NULL, NULL, 0);
  283. if (status < 0) {
  284. char *desc = ff_AMediaFormat_toString(format);
  285. av_log(avctx, AV_LOG_ERROR,
  286. "Failed to configure codec (status = %d) with format %s\n",
  287. status, desc);
  288. av_freep(&desc);
  289. ret = AVERROR_EXTERNAL;
  290. goto fail;
  291. }
  292. status = ff_AMediaCodec_start(s->codec);
  293. if (status < 0) {
  294. char *desc = ff_AMediaFormat_toString(format);
  295. av_log(avctx, AV_LOG_ERROR,
  296. "Failed to start codec (status = %d) with format %s\n",
  297. status, desc);
  298. av_freep(&desc);
  299. ret = AVERROR_EXTERNAL;
  300. goto fail;
  301. }
  302. s->format = ff_AMediaCodec_getOutputFormat(s->codec);
  303. if (s->format) {
  304. if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
  305. av_log(avctx, AV_LOG_ERROR,
  306. "Failed to configure context\n");
  307. goto fail;
  308. }
  309. }
  310. av_log(avctx, AV_LOG_DEBUG, "MediaCodec %p started successfully\n", s->codec);
  311. return 0;
  312. fail:
  313. av_log(avctx, AV_LOG_ERROR, "MediaCodec %p failed to start\n", s->codec);
  314. ff_mediacodec_dec_close(avctx, s);
  315. return ret;
  316. }
  317. int ff_mediacodec_dec_decode(AVCodecContext *avctx, MediaCodecDecContext *s,
  318. AVFrame *frame, int *got_frame,
  319. AVPacket *pkt)
  320. {
  321. int ret;
  322. int offset = 0;
  323. int need_flushing = 0;
  324. uint8_t *data;
  325. ssize_t index;
  326. size_t size;
  327. FFAMediaCodec *codec = s->codec;
  328. FFAMediaCodecBufferInfo info = { 0 };
  329. int status;
  330. int64_t input_dequeue_timeout_us = INPUT_DEQUEUE_TIMEOUT_US;
  331. int64_t output_dequeue_timeout_us = OUTPUT_DEQUEUE_TIMEOUT_US;
  332. if (pkt->size == 0) {
  333. need_flushing = 1;
  334. }
  335. if (s->flushing && need_flushing && s->queued_buffer_nb <= 0) {
  336. return 0;
  337. }
  338. while (offset < pkt->size || (need_flushing && !s->flushing)) {
  339. int size;
  340. index = ff_AMediaCodec_dequeueInputBuffer(codec, input_dequeue_timeout_us);
  341. if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
  342. break;
  343. }
  344. if (index < 0) {
  345. av_log(avctx, AV_LOG_ERROR, "Failed to dequeue input buffer (status=%zd)\n", index);
  346. return AVERROR_EXTERNAL;
  347. }
  348. data = ff_AMediaCodec_getInputBuffer(codec, index, &size);
  349. if (!data) {
  350. av_log(avctx, AV_LOG_ERROR, "Failed to get input buffer\n");
  351. return AVERROR_EXTERNAL;
  352. }
  353. if (need_flushing) {
  354. uint32_t flags = ff_AMediaCodec_getBufferFlagEndOfStream(codec);
  355. av_log(avctx, AV_LOG_DEBUG, "Sending End Of Stream signal\n");
  356. status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, 0, pkt->pts, flags);
  357. if (status < 0) {
  358. av_log(avctx, AV_LOG_ERROR, "Failed to queue input empty buffer (status = %d)\n", status);
  359. return AVERROR_EXTERNAL;
  360. }
  361. s->flushing = 1;
  362. break;
  363. } else {
  364. size = FFMIN(pkt->size - offset, size);
  365. memcpy(data, pkt->data + offset, size);
  366. offset += size;
  367. status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, size, pkt->pts, 0);
  368. if (status < 0) {
  369. av_log(avctx, AV_LOG_ERROR, "Failed to queue input buffer (status = %d)\n", status);
  370. return AVERROR_EXTERNAL;
  371. }
  372. s->queued_buffer_nb++;
  373. if (s->queued_buffer_nb > s->queued_buffer_max)
  374. s->queued_buffer_max = s->queued_buffer_nb;
  375. }
  376. }
  377. if (s->flushing) {
  378. /* If the codec is flushing, block for a fair amount of time to
  379. * ensure we got a frame */
  380. output_dequeue_timeout_us = OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US;
  381. } else if (s->dequeued_buffer_nb == 0) {
  382. /* If the codec hasn't produced any frames, do not block so we
  383. * can push data to it as fast as possible, and get the first
  384. * frame */
  385. output_dequeue_timeout_us = 0;
  386. }
  387. index = ff_AMediaCodec_dequeueOutputBuffer(codec, &info, output_dequeue_timeout_us);
  388. if (index >= 0) {
  389. int ret;
  390. if (!s->first_buffer++) {
  391. av_log(avctx, AV_LOG_DEBUG, "Got first buffer after %fms\n", (av_gettime() - s->first_buffer_at) / 1000);
  392. }
  393. av_log(avctx, AV_LOG_DEBUG, "Got output buffer %zd"
  394. " offset=%" PRIi32 " size=%" PRIi32 " ts=%" PRIi64
  395. " flags=%" PRIu32 "\n", index, info.offset, info.size,
  396. info.presentationTimeUs, info.flags);
  397. data = ff_AMediaCodec_getOutputBuffer(codec, index, &size);
  398. if (!data) {
  399. av_log(avctx, AV_LOG_ERROR, "Failed to get output buffer\n");
  400. return AVERROR_EXTERNAL;
  401. }
  402. if ((ret = mediacodec_wrap_buffer(avctx, s, data, size, index, &info, frame)) < 0) {
  403. av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n");
  404. return ret;
  405. }
  406. *got_frame = 1;
  407. s->queued_buffer_nb--;
  408. s->dequeued_buffer_nb++;
  409. } else if (ff_AMediaCodec_infoOutputFormatChanged(codec, index)) {
  410. char *format = NULL;
  411. if (s->format) {
  412. status = ff_AMediaFormat_delete(s->format);
  413. if (status < 0) {
  414. av_log(avctx, AV_LOG_ERROR, "Failed to delete MediaFormat %p\n", s->format);
  415. }
  416. }
  417. s->format = ff_AMediaCodec_getOutputFormat(codec);
  418. if (!s->format) {
  419. av_log(avctx, AV_LOG_ERROR, "Failed to get output format\n");
  420. return AVERROR_EXTERNAL;
  421. }
  422. format = ff_AMediaFormat_toString(s->format);
  423. if (!format) {
  424. return AVERROR_EXTERNAL;
  425. }
  426. av_log(avctx, AV_LOG_INFO, "Output MediaFormat changed to %s\n", format);
  427. av_freep(&format);
  428. if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
  429. return ret;
  430. }
  431. } else if (ff_AMediaCodec_infoOutputBuffersChanged(codec, index)) {
  432. ff_AMediaCodec_cleanOutputBuffers(codec);
  433. } else if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
  434. if (s->flushing) {
  435. av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer within %" PRIi64 "ms "
  436. "while flushing remaining frames, output will probably lack last %d frames\n",
  437. output_dequeue_timeout_us / 1000, s->queued_buffer_nb);
  438. } else {
  439. av_log(avctx, AV_LOG_DEBUG, "No output buffer available, try again later\n");
  440. }
  441. } else {
  442. av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer (status=%zd)\n", index);
  443. return AVERROR_EXTERNAL;
  444. }
  445. return offset;
  446. }
  447. int ff_mediacodec_dec_flush(AVCodecContext *avctx, MediaCodecDecContext *s)
  448. {
  449. FFAMediaCodec *codec = s->codec;
  450. int status;
  451. s->queued_buffer_nb = 0;
  452. s->dequeued_buffer_nb = 0;
  453. s->flushing = 0;
  454. status = ff_AMediaCodec_flush(codec);
  455. if (status < 0) {
  456. av_log(avctx, AV_LOG_ERROR, "Failed to flush codec\n");
  457. return AVERROR_EXTERNAL;
  458. }
  459. s->first_buffer = 0;
  460. s->first_buffer_at = av_gettime();
  461. return 0;
  462. }
  463. int ff_mediacodec_dec_close(AVCodecContext *avctx, MediaCodecDecContext *s)
  464. {
  465. if (s->codec) {
  466. ff_AMediaCodec_delete(s->codec);
  467. s->codec = NULL;
  468. }
  469. if (s->format) {
  470. ff_AMediaFormat_delete(s->format);
  471. s->format = NULL;
  472. }
  473. av_freep(&s->codec_name);
  474. return 0;
  475. }