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.

271 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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/opt.h"
  25. #include "avcodec.h"
  26. #include "decode.h"
  27. #include "internal.h"
  28. typedef struct Libdav1dContext {
  29. AVClass *class;
  30. Dav1dContext *c;
  31. AVFifoBuffer *cache;
  32. Dav1dData data;
  33. int tile_threads;
  34. } Libdav1dContext;
  35. static av_cold int libdav1d_init(AVCodecContext *c)
  36. {
  37. Libdav1dContext *dav1d = c->priv_data;
  38. Dav1dSettings s;
  39. int res;
  40. av_log(c, AV_LOG_INFO, "libdav1d %s\n", dav1d_version());
  41. dav1d_default_settings(&s);
  42. s.n_tile_threads = dav1d->tile_threads;
  43. s.n_frame_threads = FFMIN(c->thread_count ? c->thread_count : av_cpu_count(), 256);
  44. dav1d->cache = av_fifo_alloc(8 * sizeof(AVPacket));
  45. if (!dav1d->cache)
  46. return AVERROR(ENOMEM);
  47. res = dav1d_open(&dav1d->c, &s);
  48. if (res < 0)
  49. return AVERROR(ENOMEM);
  50. return 0;
  51. }
  52. static void libdav1d_flush(AVCodecContext *c)
  53. {
  54. Libdav1dContext *dav1d = c->priv_data;
  55. av_fifo_reset(dav1d->cache);
  56. dav1d_data_unref(&dav1d->data);
  57. dav1d_flush(dav1d->c);
  58. }
  59. static int libdav1d_fifo_write(void *src, void *dst, int dst_size) {
  60. AVPacket *pkt_dst = dst, *pkt_src = src;
  61. av_assert2(dst_size >= sizeof(AVPacket));
  62. pkt_src->buf = NULL;
  63. av_packet_free_side_data(pkt_src);
  64. *pkt_dst = *pkt_src;
  65. return sizeof(AVPacket);
  66. }
  67. static void libdav1d_data_free(const uint8_t *data, void *opaque) {
  68. AVBufferRef *buf = opaque;
  69. av_buffer_unref(&buf);
  70. }
  71. static void libdav1d_frame_free(void *opaque, uint8_t *data) {
  72. Dav1dPicture p = { 0 };
  73. p.ref = opaque;
  74. p.data[0] = (void *) 0x1; // this has to be non-NULL
  75. dav1d_picture_unref(&p);
  76. }
  77. static const enum AVPixelFormat pix_fmt[][2] = {
  78. [DAV1D_PIXEL_LAYOUT_I400] = { AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10 },
  79. [DAV1D_PIXEL_LAYOUT_I420] = { AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10 },
  80. [DAV1D_PIXEL_LAYOUT_I422] = { AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P10 },
  81. [DAV1D_PIXEL_LAYOUT_I444] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P10 },
  82. };
  83. // TODO: Update once 12bit support is added.
  84. static const int profile[] = {
  85. [DAV1D_PIXEL_LAYOUT_I400] = FF_PROFILE_AV1_MAIN,
  86. [DAV1D_PIXEL_LAYOUT_I420] = FF_PROFILE_AV1_MAIN,
  87. [DAV1D_PIXEL_LAYOUT_I422] = FF_PROFILE_AV1_PROFESSIONAL,
  88. [DAV1D_PIXEL_LAYOUT_I444] = FF_PROFILE_AV1_HIGH,
  89. };
  90. static int libdav1d_receive_frame(AVCodecContext *c, AVFrame *frame)
  91. {
  92. Libdav1dContext *dav1d = c->priv_data;
  93. Dav1dData *data = &dav1d->data;
  94. AVPacket pkt = { 0 };
  95. Dav1dPicture p = { 0 };
  96. int res;
  97. if (!data->sz) {
  98. res = ff_decode_get_packet(c, &pkt);
  99. if (res < 0 && res != AVERROR_EOF)
  100. return res;
  101. if (pkt.size) {
  102. if (!av_fifo_space(dav1d->cache)) {
  103. res = av_fifo_grow(dav1d->cache, 8 * sizeof(pkt));
  104. if (res < 0) {
  105. av_packet_unref(&pkt);
  106. return res;
  107. }
  108. }
  109. res = dav1d_data_wrap(data, pkt.data, pkt.size, libdav1d_data_free, pkt.buf);
  110. if (res < 0) {
  111. av_packet_unref(&pkt);
  112. return res;
  113. }
  114. av_fifo_generic_write(dav1d->cache, &pkt, sizeof(pkt), libdav1d_fifo_write);
  115. } else {
  116. data = NULL;
  117. }
  118. }
  119. res = dav1d_decode(dav1d->c, data, &p);
  120. if (res < 0) {
  121. if (res == -EINVAL)
  122. res = AVERROR_INVALIDDATA;
  123. else if (res == -EAGAIN && c->internal->draining)
  124. res = AVERROR_EOF;
  125. return res;
  126. }
  127. av_assert0(p.data[0] != NULL);
  128. av_fifo_generic_read(dav1d->cache, &pkt, sizeof(pkt), NULL);
  129. frame->buf[0] = av_buffer_create(NULL, 0, libdav1d_frame_free,
  130. p.ref, AV_BUFFER_FLAG_READONLY);
  131. if (!frame->buf[0]) {
  132. dav1d_picture_unref(&p);
  133. return AVERROR(ENOMEM);
  134. }
  135. frame->data[0] = p.data[0];
  136. frame->data[1] = p.data[1];
  137. frame->data[2] = p.data[2];
  138. frame->linesize[0] = p.stride[0];
  139. frame->linesize[1] = p.stride[1];
  140. frame->linesize[2] = p.stride[1];
  141. c->profile = profile[p.p.layout];
  142. frame->format = c->pix_fmt = pix_fmt[p.p.layout][p.p.bpc == 10];
  143. frame->width = p.p.w;
  144. frame->height = p.p.h;
  145. if (c->width != p.p.w || c->height != p.p.h) {
  146. res = ff_set_dimensions(c, p.p.w, p.p.h);
  147. if (res < 0)
  148. return res;
  149. }
  150. switch (p.p.chr) {
  151. case DAV1D_CHR_VERTICAL:
  152. frame->chroma_location = c->chroma_sample_location = AVCHROMA_LOC_LEFT;
  153. break;
  154. case DAV1D_CHR_COLOCATED:
  155. frame->chroma_location = c->chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
  156. break;
  157. }
  158. frame->colorspace = c->colorspace = (enum AVColorSpace) p.p.mtrx;
  159. frame->color_primaries = c->color_primaries = (enum AVColorPrimaries) p.p.pri;
  160. frame->color_trc = c->color_trc = (enum AVColorTransferCharacteristic) p.p.trc;
  161. frame->color_range = c->color_range = p.p.fullrange ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  162. // match timestamps and packet size
  163. frame->pts = frame->best_effort_timestamp = pkt.pts;
  164. #if FF_API_PKT_PTS
  165. FF_DISABLE_DEPRECATION_WARNINGS
  166. frame->pkt_pts = pkt.pts;
  167. FF_ENABLE_DEPRECATION_WARNINGS
  168. #endif
  169. frame->pkt_dts = pkt.dts;
  170. frame->pkt_pos = pkt.pos;
  171. frame->pkt_size = pkt.size;
  172. frame->pkt_duration = pkt.duration;
  173. frame->key_frame = p.p.type == DAV1D_FRAME_TYPE_KEY;
  174. switch (p.p.type) {
  175. case DAV1D_FRAME_TYPE_KEY:
  176. case DAV1D_FRAME_TYPE_INTRA:
  177. frame->pict_type = AV_PICTURE_TYPE_I;
  178. break;
  179. case DAV1D_FRAME_TYPE_INTER:
  180. frame->pict_type = AV_PICTURE_TYPE_P;
  181. break;
  182. case DAV1D_FRAME_TYPE_SWITCH:
  183. frame->pict_type = AV_PICTURE_TYPE_SP;
  184. break;
  185. default:
  186. return AVERROR_INVALIDDATA;
  187. }
  188. return 0;
  189. }
  190. static av_cold int libdav1d_close(AVCodecContext *c)
  191. {
  192. Libdav1dContext *dav1d = c->priv_data;
  193. av_fifo_freep(&dav1d->cache);
  194. dav1d_data_unref(&dav1d->data);
  195. dav1d_close(&dav1d->c);
  196. return 0;
  197. }
  198. #define OFFSET(x) offsetof(Libdav1dContext, x)
  199. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  200. static const AVOption libdav1d_options[] = {
  201. { "tilethreads", "Tile threads", OFFSET(tile_threads), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 64, VD, NULL },
  202. { NULL }
  203. };
  204. static const AVClass libdav1d_class = {
  205. .class_name = "libdav1d decoder",
  206. .item_name = av_default_item_name,
  207. .option = libdav1d_options,
  208. .version = LIBAVUTIL_VERSION_INT,
  209. };
  210. AVCodec ff_libdav1d_decoder = {
  211. .name = "libdav1d",
  212. .long_name = NULL_IF_CONFIG_SMALL("dav1d AV1 decoder by VideoLAN"),
  213. .type = AVMEDIA_TYPE_VIDEO,
  214. .id = AV_CODEC_ID_AV1,
  215. .priv_data_size = sizeof(Libdav1dContext),
  216. .init = libdav1d_init,
  217. .close = libdav1d_close,
  218. .flush = libdav1d_flush,
  219. .receive_frame = libdav1d_receive_frame,
  220. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
  221. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |
  222. FF_CODEC_CAP_SETS_PKT_DTS,
  223. .priv_class = &libdav1d_class,
  224. .wrapper_name = "libdav1d",
  225. };