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.

574 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. int profile;
  270. s->first_buffer_at = av_gettime();
  271. profile = ff_AMediaCodecProfile_getProfileFromAVCodecContext(avctx);
  272. if (profile < 0) {
  273. av_log(avctx, AV_LOG_WARNING, "Unsupported or unknown profile");
  274. }
  275. s->codec_name = ff_AMediaCodecList_getCodecNameByType(mime, profile, 0, avctx);
  276. if (!s->codec_name) {
  277. ret = AVERROR_EXTERNAL;
  278. goto fail;
  279. }
  280. av_log(avctx, AV_LOG_DEBUG, "Found decoder %s\n", s->codec_name);
  281. s->codec = ff_AMediaCodec_createCodecByName(s->codec_name);
  282. if (!s->codec) {
  283. av_log(avctx, AV_LOG_ERROR, "Failed to create media decoder for type %s and name %s\n", mime, s->codec_name);
  284. ret = AVERROR_EXTERNAL;
  285. goto fail;
  286. }
  287. status = ff_AMediaCodec_configure(s->codec, format, NULL, NULL, 0);
  288. if (status < 0) {
  289. char *desc = ff_AMediaFormat_toString(format);
  290. av_log(avctx, AV_LOG_ERROR,
  291. "Failed to configure codec (status = %d) with format %s\n",
  292. status, desc);
  293. av_freep(&desc);
  294. ret = AVERROR_EXTERNAL;
  295. goto fail;
  296. }
  297. status = ff_AMediaCodec_start(s->codec);
  298. if (status < 0) {
  299. char *desc = ff_AMediaFormat_toString(format);
  300. av_log(avctx, AV_LOG_ERROR,
  301. "Failed to start codec (status = %d) with format %s\n",
  302. status, desc);
  303. av_freep(&desc);
  304. ret = AVERROR_EXTERNAL;
  305. goto fail;
  306. }
  307. s->format = ff_AMediaCodec_getOutputFormat(s->codec);
  308. if (s->format) {
  309. if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
  310. av_log(avctx, AV_LOG_ERROR,
  311. "Failed to configure context\n");
  312. goto fail;
  313. }
  314. }
  315. av_log(avctx, AV_LOG_DEBUG, "MediaCodec %p started successfully\n", s->codec);
  316. return 0;
  317. fail:
  318. av_log(avctx, AV_LOG_ERROR, "MediaCodec %p failed to start\n", s->codec);
  319. ff_mediacodec_dec_close(avctx, s);
  320. return ret;
  321. }
  322. int ff_mediacodec_dec_decode(AVCodecContext *avctx, MediaCodecDecContext *s,
  323. AVFrame *frame, int *got_frame,
  324. AVPacket *pkt)
  325. {
  326. int ret;
  327. int offset = 0;
  328. int need_flushing = 0;
  329. uint8_t *data;
  330. ssize_t index;
  331. size_t size;
  332. FFAMediaCodec *codec = s->codec;
  333. FFAMediaCodecBufferInfo info = { 0 };
  334. int status;
  335. int64_t input_dequeue_timeout_us = INPUT_DEQUEUE_TIMEOUT_US;
  336. int64_t output_dequeue_timeout_us = OUTPUT_DEQUEUE_TIMEOUT_US;
  337. if (pkt->size == 0) {
  338. need_flushing = 1;
  339. }
  340. if (s->flushing && need_flushing && s->queued_buffer_nb <= 0) {
  341. return 0;
  342. }
  343. while (offset < pkt->size || (need_flushing && !s->flushing)) {
  344. int size;
  345. index = ff_AMediaCodec_dequeueInputBuffer(codec, input_dequeue_timeout_us);
  346. if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
  347. break;
  348. }
  349. if (index < 0) {
  350. av_log(avctx, AV_LOG_ERROR, "Failed to dequeue input buffer (status=%zd)\n", index);
  351. return AVERROR_EXTERNAL;
  352. }
  353. data = ff_AMediaCodec_getInputBuffer(codec, index, &size);
  354. if (!data) {
  355. av_log(avctx, AV_LOG_ERROR, "Failed to get input buffer\n");
  356. return AVERROR_EXTERNAL;
  357. }
  358. if (need_flushing) {
  359. uint32_t flags = ff_AMediaCodec_getBufferFlagEndOfStream(codec);
  360. av_log(avctx, AV_LOG_DEBUG, "Sending End Of Stream signal\n");
  361. status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, 0, pkt->pts, flags);
  362. if (status < 0) {
  363. av_log(avctx, AV_LOG_ERROR, "Failed to queue input empty buffer (status = %d)\n", status);
  364. return AVERROR_EXTERNAL;
  365. }
  366. s->flushing = 1;
  367. break;
  368. } else {
  369. size = FFMIN(pkt->size - offset, size);
  370. memcpy(data, pkt->data + offset, size);
  371. offset += size;
  372. status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, size, pkt->pts, 0);
  373. if (status < 0) {
  374. av_log(avctx, AV_LOG_ERROR, "Failed to queue input buffer (status = %d)\n", status);
  375. return AVERROR_EXTERNAL;
  376. }
  377. s->queued_buffer_nb++;
  378. if (s->queued_buffer_nb > s->queued_buffer_max)
  379. s->queued_buffer_max = s->queued_buffer_nb;
  380. }
  381. }
  382. if (s->flushing) {
  383. /* If the codec is flushing, block for a fair amount of time to
  384. * ensure we got a frame */
  385. output_dequeue_timeout_us = OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US;
  386. } else if (s->dequeued_buffer_nb == 0) {
  387. /* If the codec hasn't produced any frames, do not block so we
  388. * can push data to it as fast as possible, and get the first
  389. * frame */
  390. output_dequeue_timeout_us = 0;
  391. }
  392. index = ff_AMediaCodec_dequeueOutputBuffer(codec, &info, output_dequeue_timeout_us);
  393. if (index >= 0) {
  394. int ret;
  395. if (!s->first_buffer++) {
  396. av_log(avctx, AV_LOG_DEBUG, "Got first buffer after %fms\n", (av_gettime() - s->first_buffer_at) / 1000);
  397. }
  398. av_log(avctx, AV_LOG_DEBUG, "Got output buffer %zd"
  399. " offset=%" PRIi32 " size=%" PRIi32 " ts=%" PRIi64
  400. " flags=%" PRIu32 "\n", index, info.offset, info.size,
  401. info.presentationTimeUs, info.flags);
  402. data = ff_AMediaCodec_getOutputBuffer(codec, index, &size);
  403. if (!data) {
  404. av_log(avctx, AV_LOG_ERROR, "Failed to get output buffer\n");
  405. return AVERROR_EXTERNAL;
  406. }
  407. if ((ret = mediacodec_wrap_buffer(avctx, s, data, size, index, &info, frame)) < 0) {
  408. av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n");
  409. return ret;
  410. }
  411. *got_frame = 1;
  412. s->queued_buffer_nb--;
  413. s->dequeued_buffer_nb++;
  414. } else if (ff_AMediaCodec_infoOutputFormatChanged(codec, index)) {
  415. char *format = NULL;
  416. if (s->format) {
  417. status = ff_AMediaFormat_delete(s->format);
  418. if (status < 0) {
  419. av_log(avctx, AV_LOG_ERROR, "Failed to delete MediaFormat %p\n", s->format);
  420. }
  421. }
  422. s->format = ff_AMediaCodec_getOutputFormat(codec);
  423. if (!s->format) {
  424. av_log(avctx, AV_LOG_ERROR, "Failed to get output format\n");
  425. return AVERROR_EXTERNAL;
  426. }
  427. format = ff_AMediaFormat_toString(s->format);
  428. if (!format) {
  429. return AVERROR_EXTERNAL;
  430. }
  431. av_log(avctx, AV_LOG_INFO, "Output MediaFormat changed to %s\n", format);
  432. av_freep(&format);
  433. if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
  434. return ret;
  435. }
  436. } else if (ff_AMediaCodec_infoOutputBuffersChanged(codec, index)) {
  437. ff_AMediaCodec_cleanOutputBuffers(codec);
  438. } else if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
  439. if (s->flushing) {
  440. av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer within %" PRIi64 "ms "
  441. "while flushing remaining frames, output will probably lack last %d frames\n",
  442. output_dequeue_timeout_us / 1000, s->queued_buffer_nb);
  443. } else {
  444. av_log(avctx, AV_LOG_DEBUG, "No output buffer available, try again later\n");
  445. }
  446. } else {
  447. av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer (status=%zd)\n", index);
  448. return AVERROR_EXTERNAL;
  449. }
  450. return offset;
  451. }
  452. int ff_mediacodec_dec_flush(AVCodecContext *avctx, MediaCodecDecContext *s)
  453. {
  454. FFAMediaCodec *codec = s->codec;
  455. int status;
  456. s->queued_buffer_nb = 0;
  457. s->dequeued_buffer_nb = 0;
  458. s->flushing = 0;
  459. status = ff_AMediaCodec_flush(codec);
  460. if (status < 0) {
  461. av_log(avctx, AV_LOG_ERROR, "Failed to flush codec\n");
  462. return AVERROR_EXTERNAL;
  463. }
  464. s->first_buffer = 0;
  465. s->first_buffer_at = av_gettime();
  466. return 0;
  467. }
  468. int ff_mediacodec_dec_close(AVCodecContext *avctx, MediaCodecDecContext *s)
  469. {
  470. if (s->codec) {
  471. ff_AMediaCodec_delete(s->codec);
  472. s->codec = NULL;
  473. }
  474. if (s->format) {
  475. ff_AMediaFormat_delete(s->format);
  476. s->format = NULL;
  477. }
  478. av_freep(&s->codec_name);
  479. return 0;
  480. }