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.

270 lines
8.0KB

  1. /*
  2. * Copyright (c) 2018 Ronald S. Bultje <rsbultje gmail com>
  3. * Copyright (c) 2018 James Almer <jamrial gmail com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <dav1d/dav1d.h>
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/fifo.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/internal.h"
  26. #include "libavutil/opt.h"
  27. #include "avcodec.h"
  28. #include "decode.h"
  29. #include "internal.h"
  30. typedef struct Libdav1dContext {
  31. AVClass *class;
  32. Dav1dContext *c;
  33. AVFifoBuffer *cache;
  34. Dav1dData data;
  35. int tile_threads;
  36. } Libdav1dContext;
  37. static av_cold int libdav1d_init(AVCodecContext *c)
  38. {
  39. Libdav1dContext *dav1d = c->priv_data;
  40. Dav1dSettings s;
  41. int res;
  42. av_log(c, AV_LOG_INFO, "libdav1d %s\n", dav1d_version());
  43. dav1d_default_settings(&s);
  44. s.n_tile_threads = dav1d->tile_threads;
  45. s.n_frame_threads = FFMIN(c->thread_count ? c->thread_count : av_cpu_count(), 256);
  46. dav1d->cache = av_fifo_alloc(8 * sizeof(AVPacket));
  47. if (!dav1d->cache)
  48. return AVERROR(ENOMEM);
  49. res = dav1d_open(&dav1d->c, &s);
  50. if (res < 0)
  51. return AVERROR(ENOMEM);
  52. return 0;
  53. }
  54. static void libdav1d_flush(AVCodecContext *c)
  55. {
  56. Libdav1dContext *dav1d = c->priv_data;
  57. av_fifo_reset(dav1d->cache);
  58. dav1d_data_unref(&dav1d->data);
  59. dav1d_flush(dav1d->c);
  60. }
  61. static int libdav1d_fifo_write(void *src, void *dst, int dst_size) {
  62. AVPacket *pkt_dst = dst, *pkt_src = src;
  63. av_assert2(dst_size >= sizeof(AVPacket));
  64. pkt_src->buf = NULL;
  65. av_packet_free_side_data(pkt_src);
  66. *pkt_dst = *pkt_src;
  67. return sizeof(AVPacket);
  68. }
  69. static void libdav1d_data_free(const uint8_t *data, void *opaque) {
  70. AVBufferRef *buf = opaque;
  71. av_buffer_unref(&buf);
  72. }
  73. static void libdav1d_frame_free(void *opaque, uint8_t *data) {
  74. Dav1dPicture p = { 0 };
  75. p.ref = opaque;
  76. p.data[0] = (void *) 0x1; // this has to be non-NULL
  77. dav1d_picture_unref(&p);
  78. }
  79. static const enum AVPixelFormat pix_fmt[][2] = {
  80. [DAV1D_PIXEL_LAYOUT_I400] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10 },
  81. [DAV1D_PIXEL_LAYOUT_I420] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10 },
  82. [DAV1D_PIXEL_LAYOUT_I422] = { AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P10 },
  83. [DAV1D_PIXEL_LAYOUT_I444] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P10 },
  84. };
  85. // TODO: Update once 12bit support is added.
  86. static const int profile[] = {
  87. [DAV1D_PIXEL_LAYOUT_I400] = FF_PROFILE_AV1_MAIN,
  88. [DAV1D_PIXEL_LAYOUT_I420] = FF_PROFILE_AV1_MAIN,
  89. [DAV1D_PIXEL_LAYOUT_I422] = FF_PROFILE_AV1_PROFESSIONAL,
  90. [DAV1D_PIXEL_LAYOUT_I444] = FF_PROFILE_AV1_HIGH,
  91. };
  92. static int libdav1d_receive_frame(AVCodecContext *c, AVFrame *frame)
  93. {
  94. Libdav1dContext *dav1d = c->priv_data;
  95. Dav1dData *data = &dav1d->data;
  96. AVPacket pkt = { 0 };
  97. Dav1dPicture p = { 0 };
  98. int res;
  99. if (!data->sz) {
  100. res = ff_decode_get_packet(c, &pkt);
  101. if (res < 0 && res != AVERROR_EOF)
  102. return res;
  103. if (pkt.size) {
  104. if (!av_fifo_space(dav1d->cache)) {
  105. res = av_fifo_realloc2(dav1d->cache, av_fifo_size(dav1d->cache) + 8 * sizeof(pkt));
  106. if (res < 0) {
  107. av_packet_unref(&pkt);
  108. return res;
  109. }
  110. }
  111. res = dav1d_data_wrap(data, pkt.data, pkt.size, libdav1d_data_free, pkt.buf);
  112. if (res < 0) {
  113. av_packet_unref(&pkt);
  114. return res;
  115. }
  116. av_fifo_generic_write(dav1d->cache, &pkt, sizeof(pkt), libdav1d_fifo_write);
  117. } else {
  118. data = NULL;
  119. }
  120. }
  121. res = dav1d_decode(dav1d->c, data, &p);
  122. if (res < 0) {
  123. if (res == -EINVAL)
  124. res = AVERROR_INVALIDDATA;
  125. else if (res == -EAGAIN && c->internal->draining)
  126. res = AVERROR_EOF;
  127. return res;
  128. }
  129. av_assert0(p.data[0] != NULL);
  130. av_fifo_generic_read(dav1d->cache, &pkt, sizeof(pkt), NULL);
  131. frame->buf[0] = av_buffer_create(NULL, 0, libdav1d_frame_free,
  132. p.ref, AV_BUFFER_FLAG_READONLY);
  133. if (!frame->buf[0]) {
  134. dav1d_picture_unref(&p);
  135. return AVERROR(ENOMEM);
  136. }
  137. frame->data[0] = p.data[0];
  138. frame->data[1] = p.data[1];
  139. frame->data[2] = p.data[2];
  140. frame->linesize[0] = p.stride[0];
  141. frame->linesize[1] = p.stride[1];
  142. frame->linesize[2] = p.stride[1];
  143. c->profile = profile[p.p.layout];
  144. frame->format = c->pix_fmt = pix_fmt[p.p.layout][p.p.bpc == 10];
  145. frame->width = p.p.w;
  146. frame->height = p.p.h;
  147. if (c->width != p.p.w || c->height != p.p.h) {
  148. res = ff_set_dimensions(c, p.p.w, p.p.h);
  149. if (res < 0)
  150. return res;
  151. }
  152. switch (p.p.chr) {
  153. case DAV1D_CHR_VERTICAL:
  154. frame->chroma_location = c->chroma_sample_location = AVCHROMA_LOC_LEFT;
  155. break;
  156. case DAV1D_CHR_COLOCATED:
  157. frame->chroma_location = c->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  158. break;
  159. }
  160. frame->colorspace = c->colorspace = (enum AVColorSpace) p.p.mtrx;
  161. frame->color_primaries = c->color_primaries = (enum AVColorPrimaries) p.p.pri;
  162. frame->color_trc = c->color_trc = (enum AVColorTransferCharacteristic) p.p.trc;
  163. frame->color_range = c->color_range = p.p.fullrange ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  164. // match timestamps and packet size
  165. frame->pts = pkt.pts;
  166. #if FF_API_PKT_PTS
  167. FF_DISABLE_DEPRECATION_WARNINGS
  168. frame->pkt_pts = pkt.pts;
  169. FF_ENABLE_DEPRECATION_WARNINGS
  170. #endif
  171. frame->pkt_dts = pkt.dts;
  172. frame->key_frame = p.p.type == DAV1D_FRAME_TYPE_KEY;
  173. switch (p.p.type) {
  174. case DAV1D_FRAME_TYPE_KEY:
  175. case DAV1D_FRAME_TYPE_INTRA:
  176. frame->pict_type = AV_PICTURE_TYPE_I;
  177. break;
  178. case DAV1D_FRAME_TYPE_INTER:
  179. frame->pict_type = AV_PICTURE_TYPE_P;
  180. break;
  181. case DAV1D_FRAME_TYPE_SWITCH:
  182. frame->pict_type = AV_PICTURE_TYPE_SP;
  183. break;
  184. default:
  185. return AVERROR_INVALIDDATA;
  186. }
  187. return 0;
  188. }
  189. static av_cold int libdav1d_close(AVCodecContext *c)
  190. {
  191. Libdav1dContext *dav1d = c->priv_data;
  192. av_fifo_free(dav1d->cache);
  193. dav1d_data_unref(&dav1d->data);
  194. dav1d_close(&dav1d->c);
  195. return 0;
  196. }
  197. #define OFFSET(x) offsetof(Libdav1dContext, x)
  198. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  199. static const AVOption libdav1d_options[] = {
  200. { "tilethreads", "Tile threads", OFFSET(tile_threads), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 64, VD, NULL },
  201. { NULL }
  202. };
  203. static const AVClass libdav1d_class = {
  204. .class_name = "libdav1d decoder",
  205. .item_name = av_default_item_name,
  206. .option = libdav1d_options,
  207. .version = LIBAVUTIL_VERSION_INT,
  208. };
  209. AVCodec ff_libdav1d_decoder = {
  210. .name = "libdav1d",
  211. .long_name = NULL_IF_CONFIG_SMALL("dav1d AV1 decoder by VideoLAN"),
  212. .type = AVMEDIA_TYPE_VIDEO,
  213. .id = AV_CODEC_ID_AV1,
  214. .priv_data_size = sizeof(Libdav1dContext),
  215. .init = libdav1d_init,
  216. .close = libdav1d_close,
  217. .flush = libdav1d_flush,
  218. .receive_frame = libdav1d_receive_frame,
  219. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
  220. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |
  221. FF_CODEC_CAP_SETS_PKT_DTS,
  222. .priv_class = &libdav1d_class,
  223. .wrapper_name = "libdav1d",
  224. };