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.

575 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. frame->pkt_dts = AV_NOPTS_VALUE;
  146. av_log(avctx, AV_LOG_DEBUG,
  147. "Frame: width=%d stride=%d height=%d slice-height=%d "
  148. "crop-top=%d crop-bottom=%d crop-left=%d crop-right=%d encoder=%s\n"
  149. "destination linesizes=%d,%d,%d\n" ,
  150. avctx->width, s->stride, avctx->height, s->slice_height,
  151. s->crop_top, s->crop_bottom, s->crop_left, s->crop_right, s->codec_name,
  152. frame->linesize[0], frame->linesize[1], frame->linesize[2]);
  153. switch (s->color_format) {
  154. case COLOR_FormatYUV420Planar:
  155. ff_mediacodec_sw_buffer_copy_yuv420_planar(avctx, s, data, size, info, frame);
  156. break;
  157. case COLOR_FormatYUV420SemiPlanar:
  158. case COLOR_QCOM_FormatYUV420SemiPlanar:
  159. case COLOR_QCOM_FormatYUV420SemiPlanar32m:
  160. ff_mediacodec_sw_buffer_copy_yuv420_semi_planar(avctx, s, data, size, info, frame);
  161. break;
  162. case COLOR_TI_FormatYUV420PackedSemiPlanar:
  163. case COLOR_TI_FormatYUV420PackedSemiPlanarInterlaced:
  164. ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar(avctx, s, data, size, info, frame);
  165. break;
  166. case COLOR_QCOM_FormatYUV420PackedSemiPlanar64x32Tile2m8ka:
  167. ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar_64x32Tile2m8ka(avctx, s, data, size, info, frame);
  168. break;
  169. default:
  170. av_log(avctx, AV_LOG_ERROR, "Unsupported color format 0x%x (value=%d)\n",
  171. s->color_format, s->color_format);
  172. ret = AVERROR(EINVAL);
  173. goto done;
  174. }
  175. ret = 0;
  176. done:
  177. status = ff_AMediaCodec_releaseOutputBuffer(s->codec, index, 0);
  178. if (status < 0) {
  179. av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
  180. ret = AVERROR_EXTERNAL;
  181. }
  182. return ret;
  183. }
  184. static int mediacodec_dec_parse_format(AVCodecContext *avctx, MediaCodecDecContext *s)
  185. {
  186. int width = 0;
  187. int height = 0;
  188. int32_t value = 0;
  189. char *format = NULL;
  190. if (!s->format) {
  191. av_log(avctx, AV_LOG_ERROR, "Output MediaFormat is not set\n");
  192. return AVERROR(EINVAL);
  193. }
  194. format = ff_AMediaFormat_toString(s->format);
  195. if (!format) {
  196. return AVERROR_EXTERNAL;
  197. }
  198. av_log(avctx, AV_LOG_DEBUG, "Parsing MediaFormat %s\n", format);
  199. av_freep(&format);
  200. /* Mandatory fields */
  201. if (!ff_AMediaFormat_getInt32(s->format, "width", &value)) {
  202. format = ff_AMediaFormat_toString(s->format);
  203. av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "width", format);
  204. av_freep(&format);
  205. return AVERROR_EXTERNAL;
  206. }
  207. s->width = value;
  208. if (!ff_AMediaFormat_getInt32(s->format, "height", &value)) {
  209. format = ff_AMediaFormat_toString(s->format);
  210. av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "height", format);
  211. av_freep(&format);
  212. return AVERROR_EXTERNAL;
  213. }
  214. s->height = value;
  215. if (!ff_AMediaFormat_getInt32(s->format, "stride", &value)) {
  216. format = ff_AMediaFormat_toString(s->format);
  217. av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "stride", format);
  218. av_freep(&format);
  219. return AVERROR_EXTERNAL;
  220. }
  221. s->stride = value > 0 ? value : s->width;
  222. if (!ff_AMediaFormat_getInt32(s->format, "slice-height", &value)) {
  223. format = ff_AMediaFormat_toString(s->format);
  224. av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "slice-height", format);
  225. av_freep(&format);
  226. return AVERROR_EXTERNAL;
  227. }
  228. s->slice_height = value > 0 ? value : s->height;
  229. if (strstr(s->codec_name, "OMX.Nvidia.")) {
  230. s->slice_height = FFALIGN(s->height, 16);
  231. } else if (strstr(s->codec_name, "OMX.SEC.avc.dec")) {
  232. s->slice_height = avctx->height;
  233. s->stride = avctx->width;
  234. }
  235. if (!ff_AMediaFormat_getInt32(s->format, "color-format", &value)) {
  236. format = ff_AMediaFormat_toString(s->format);
  237. av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "color-format", format);
  238. av_freep(&format);
  239. return AVERROR_EXTERNAL;
  240. }
  241. s->color_format = value;
  242. s->pix_fmt = avctx->pix_fmt = mcdec_map_color_format(avctx, s, value);
  243. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  244. av_log(avctx, AV_LOG_ERROR, "Output color format is not supported\n");
  245. return AVERROR(EINVAL);
  246. }
  247. /* Optional fields */
  248. if (ff_AMediaFormat_getInt32(s->format, "crop-top", &value))
  249. s->crop_top = value;
  250. if (ff_AMediaFormat_getInt32(s->format, "crop-bottom", &value))
  251. s->crop_bottom = value;
  252. if (ff_AMediaFormat_getInt32(s->format, "crop-left", &value))
  253. s->crop_left = value;
  254. if (ff_AMediaFormat_getInt32(s->format, "crop-right", &value))
  255. s->crop_right = value;
  256. width = s->crop_right + 1 - s->crop_left;
  257. height = s->crop_bottom + 1 - s->crop_top;
  258. av_log(avctx, AV_LOG_INFO,
  259. "Output crop parameters top=%d bottom=%d left=%d right=%d, "
  260. "resulting dimensions width=%d height=%d\n",
  261. s->crop_top, s->crop_bottom, s->crop_left, s->crop_right,
  262. width, height);
  263. return ff_set_dimensions(avctx, width, height);
  264. }
  265. int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s,
  266. const char *mime, FFAMediaFormat *format)
  267. {
  268. int ret = 0;
  269. int status;
  270. int profile;
  271. s->first_buffer_at = av_gettime();
  272. profile = ff_AMediaCodecProfile_getProfileFromAVCodecContext(avctx);
  273. if (profile < 0) {
  274. av_log(avctx, AV_LOG_WARNING, "Unsupported or unknown profile");
  275. }
  276. s->codec_name = ff_AMediaCodecList_getCodecNameByType(mime, profile, 0, avctx);
  277. if (!s->codec_name) {
  278. ret = AVERROR_EXTERNAL;
  279. goto fail;
  280. }
  281. av_log(avctx, AV_LOG_DEBUG, "Found decoder %s\n", s->codec_name);
  282. s->codec = ff_AMediaCodec_createCodecByName(s->codec_name);
  283. if (!s->codec) {
  284. av_log(avctx, AV_LOG_ERROR, "Failed to create media decoder for type %s and name %s\n", mime, s->codec_name);
  285. ret = AVERROR_EXTERNAL;
  286. goto fail;
  287. }
  288. status = ff_AMediaCodec_configure(s->codec, format, NULL, NULL, 0);
  289. if (status < 0) {
  290. char *desc = ff_AMediaFormat_toString(format);
  291. av_log(avctx, AV_LOG_ERROR,
  292. "Failed to configure codec (status = %d) with format %s\n",
  293. status, desc);
  294. av_freep(&desc);
  295. ret = AVERROR_EXTERNAL;
  296. goto fail;
  297. }
  298. status = ff_AMediaCodec_start(s->codec);
  299. if (status < 0) {
  300. char *desc = ff_AMediaFormat_toString(format);
  301. av_log(avctx, AV_LOG_ERROR,
  302. "Failed to start codec (status = %d) with format %s\n",
  303. status, desc);
  304. av_freep(&desc);
  305. ret = AVERROR_EXTERNAL;
  306. goto fail;
  307. }
  308. s->format = ff_AMediaCodec_getOutputFormat(s->codec);
  309. if (s->format) {
  310. if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
  311. av_log(avctx, AV_LOG_ERROR,
  312. "Failed to configure context\n");
  313. goto fail;
  314. }
  315. }
  316. av_log(avctx, AV_LOG_DEBUG, "MediaCodec %p started successfully\n", s->codec);
  317. return 0;
  318. fail:
  319. av_log(avctx, AV_LOG_ERROR, "MediaCodec %p failed to start\n", s->codec);
  320. ff_mediacodec_dec_close(avctx, s);
  321. return ret;
  322. }
  323. int ff_mediacodec_dec_decode(AVCodecContext *avctx, MediaCodecDecContext *s,
  324. AVFrame *frame, int *got_frame,
  325. AVPacket *pkt)
  326. {
  327. int ret;
  328. int offset = 0;
  329. int need_flushing = 0;
  330. uint8_t *data;
  331. ssize_t index;
  332. size_t size;
  333. FFAMediaCodec *codec = s->codec;
  334. FFAMediaCodecBufferInfo info = { 0 };
  335. int status;
  336. int64_t input_dequeue_timeout_us = INPUT_DEQUEUE_TIMEOUT_US;
  337. int64_t output_dequeue_timeout_us = OUTPUT_DEQUEUE_TIMEOUT_US;
  338. if (pkt->size == 0) {
  339. need_flushing = 1;
  340. }
  341. if (s->flushing && need_flushing && s->queued_buffer_nb <= 0) {
  342. return 0;
  343. }
  344. while (offset < pkt->size || (need_flushing && !s->flushing)) {
  345. int size;
  346. index = ff_AMediaCodec_dequeueInputBuffer(codec, input_dequeue_timeout_us);
  347. if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
  348. break;
  349. }
  350. if (index < 0) {
  351. av_log(avctx, AV_LOG_ERROR, "Failed to dequeue input buffer (status=%zd)\n", index);
  352. return AVERROR_EXTERNAL;
  353. }
  354. data = ff_AMediaCodec_getInputBuffer(codec, index, &size);
  355. if (!data) {
  356. av_log(avctx, AV_LOG_ERROR, "Failed to get input buffer\n");
  357. return AVERROR_EXTERNAL;
  358. }
  359. if (need_flushing) {
  360. uint32_t flags = ff_AMediaCodec_getBufferFlagEndOfStream(codec);
  361. av_log(avctx, AV_LOG_DEBUG, "Sending End Of Stream signal\n");
  362. status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, 0, pkt->pts, flags);
  363. if (status < 0) {
  364. av_log(avctx, AV_LOG_ERROR, "Failed to queue input empty buffer (status = %d)\n", status);
  365. return AVERROR_EXTERNAL;
  366. }
  367. s->flushing = 1;
  368. break;
  369. } else {
  370. size = FFMIN(pkt->size - offset, size);
  371. memcpy(data, pkt->data + offset, size);
  372. offset += size;
  373. status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, size, pkt->pts, 0);
  374. if (status < 0) {
  375. av_log(avctx, AV_LOG_ERROR, "Failed to queue input buffer (status = %d)\n", status);
  376. return AVERROR_EXTERNAL;
  377. }
  378. s->queued_buffer_nb++;
  379. if (s->queued_buffer_nb > s->queued_buffer_max)
  380. s->queued_buffer_max = s->queued_buffer_nb;
  381. }
  382. }
  383. if (s->flushing) {
  384. /* If the codec is flushing, block for a fair amount of time to
  385. * ensure we got a frame */
  386. output_dequeue_timeout_us = OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US;
  387. } else if (s->dequeued_buffer_nb == 0) {
  388. /* If the codec hasn't produced any frames, do not block so we
  389. * can push data to it as fast as possible, and get the first
  390. * frame */
  391. output_dequeue_timeout_us = 0;
  392. }
  393. index = ff_AMediaCodec_dequeueOutputBuffer(codec, &info, output_dequeue_timeout_us);
  394. if (index >= 0) {
  395. int ret;
  396. if (!s->first_buffer++) {
  397. av_log(avctx, AV_LOG_DEBUG, "Got first buffer after %fms\n", (av_gettime() - s->first_buffer_at) / 1000);
  398. }
  399. av_log(avctx, AV_LOG_DEBUG, "Got output buffer %zd"
  400. " offset=%" PRIi32 " size=%" PRIi32 " ts=%" PRIi64
  401. " flags=%" PRIu32 "\n", index, info.offset, info.size,
  402. info.presentationTimeUs, info.flags);
  403. data = ff_AMediaCodec_getOutputBuffer(codec, index, &size);
  404. if (!data) {
  405. av_log(avctx, AV_LOG_ERROR, "Failed to get output buffer\n");
  406. return AVERROR_EXTERNAL;
  407. }
  408. if ((ret = mediacodec_wrap_buffer(avctx, s, data, size, index, &info, frame)) < 0) {
  409. av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n");
  410. return ret;
  411. }
  412. *got_frame = 1;
  413. s->queued_buffer_nb--;
  414. s->dequeued_buffer_nb++;
  415. } else if (ff_AMediaCodec_infoOutputFormatChanged(codec, index)) {
  416. char *format = NULL;
  417. if (s->format) {
  418. status = ff_AMediaFormat_delete(s->format);
  419. if (status < 0) {
  420. av_log(avctx, AV_LOG_ERROR, "Failed to delete MediaFormat %p\n", s->format);
  421. }
  422. }
  423. s->format = ff_AMediaCodec_getOutputFormat(codec);
  424. if (!s->format) {
  425. av_log(avctx, AV_LOG_ERROR, "Failed to get output format\n");
  426. return AVERROR_EXTERNAL;
  427. }
  428. format = ff_AMediaFormat_toString(s->format);
  429. if (!format) {
  430. return AVERROR_EXTERNAL;
  431. }
  432. av_log(avctx, AV_LOG_INFO, "Output MediaFormat changed to %s\n", format);
  433. av_freep(&format);
  434. if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
  435. return ret;
  436. }
  437. } else if (ff_AMediaCodec_infoOutputBuffersChanged(codec, index)) {
  438. ff_AMediaCodec_cleanOutputBuffers(codec);
  439. } else if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
  440. if (s->flushing) {
  441. av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer within %" PRIi64 "ms "
  442. "while flushing remaining frames, output will probably lack last %d frames\n",
  443. output_dequeue_timeout_us / 1000, s->queued_buffer_nb);
  444. } else {
  445. av_log(avctx, AV_LOG_DEBUG, "No output buffer available, try again later\n");
  446. }
  447. } else {
  448. av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer (status=%zd)\n", index);
  449. return AVERROR_EXTERNAL;
  450. }
  451. return offset;
  452. }
  453. int ff_mediacodec_dec_flush(AVCodecContext *avctx, MediaCodecDecContext *s)
  454. {
  455. FFAMediaCodec *codec = s->codec;
  456. int status;
  457. s->queued_buffer_nb = 0;
  458. s->dequeued_buffer_nb = 0;
  459. s->flushing = 0;
  460. status = ff_AMediaCodec_flush(codec);
  461. if (status < 0) {
  462. av_log(avctx, AV_LOG_ERROR, "Failed to flush codec\n");
  463. return AVERROR_EXTERNAL;
  464. }
  465. s->first_buffer = 0;
  466. s->first_buffer_at = av_gettime();
  467. return 0;
  468. }
  469. int ff_mediacodec_dec_close(AVCodecContext *avctx, MediaCodecDecContext *s)
  470. {
  471. if (s->codec) {
  472. ff_AMediaCodec_delete(s->codec);
  473. s->codec = NULL;
  474. }
  475. if (s->format) {
  476. ff_AMediaFormat_delete(s->format);
  477. s->format = NULL;
  478. }
  479. av_freep(&s->codec_name);
  480. return 0;
  481. }