FFmpeg
videotoolbox_vp9.c
Go to the documentation of this file.
1 /*
2  * Videotoolbox hardware acceleration for VP9
3  *
4  * copyright (c) 2021 rcombs
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 
23 #include "config.h"
24 #include "videotoolbox.h"
26 #include "vt_internal.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/avutil.h"
29 #include "libavutil/frame.h"
30 #include "libavutil/hwcontext.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/pixdesc.h"
33 #include "decode.h"
34 #include "internal.h"
35 #include "vp9shared.h"
36 
38 {
43 };
44 
45 static int get_vpx_chroma_subsampling(enum AVPixelFormat pixel_format,
46  enum AVChromaLocation chroma_location)
47 {
48  int chroma_w, chroma_h;
49  if (av_pix_fmt_get_chroma_sub_sample(pixel_format, &chroma_w, &chroma_h) == 0) {
50  if (chroma_w == 1 && chroma_h == 1) {
51  return (chroma_location == AVCHROMA_LOC_LEFT)
54  } else if (chroma_w == 1 && chroma_h == 0) {
55  return VPX_SUBSAMPLING_422;
56  } else if (chroma_w == 0 && chroma_h == 0) {
57  return VPX_SUBSAMPLING_444;
58  }
59  }
60  return -1;
61 }
62 
64 {
65  const VP9SharedContext *h = avctx->priv_data;
66  CFDataRef data = NULL;
67  uint8_t *p;
68  int vt_extradata_size;
69  uint8_t *vt_extradata;
70  int subsampling = get_vpx_chroma_subsampling(avctx->sw_pix_fmt, avctx->chroma_sample_location);
71 
72  vt_extradata_size = 1 + 3 + 6 + 2;
73  vt_extradata = av_malloc(vt_extradata_size);
74 
75  if (subsampling < 0)
76  return NULL;
77 
78  if (!vt_extradata)
79  return NULL;
80 
81  p = vt_extradata;
82 
83  *p++ = 1; /* version */
84  AV_WB24(p + 1, 0); /* flags */
85  p += 3;
86 
87  *p++ = h->h.profile;
88  *p++ = avctx->level;
89  *p++ = (h->h.bpp << 4) | (subsampling << 1) | (avctx->color_range == AVCOL_RANGE_JPEG);
90  *p++ = avctx->color_primaries;
91  *p++ = avctx->color_trc;
92  *p++ = avctx->colorspace;
93 
94  AV_WB16(p + 0, 0);
95  p += 2;
96 
97  av_assert0(p - vt_extradata == vt_extradata_size);
98 
99  data = CFDataCreate(kCFAllocatorDefault, vt_extradata, vt_extradata_size);
100  av_free(vt_extradata);
101  return data;
102 }
103 
105  const uint8_t *buffer,
106  uint32_t size)
107 {
108  return 0;
109 }
110 
112  const uint8_t *buffer,
113  uint32_t size)
114 {
115  VTContext *vtctx = avctx->internal->hwaccel_priv_data;
116 
117  return ff_videotoolbox_buffer_copy(vtctx, buffer, size);
118 }
119 
121 {
122  const VP9SharedContext *h = avctx->priv_data;
123  AVFrame *frame = h->frames[CUR_FRAME].tf.f;
124 
126 }
127 
129  .name = "vp9_videotoolbox",
130  .type = AVMEDIA_TYPE_VIDEO,
131  .id = AV_CODEC_ID_VP9,
132  .pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX,
133  .alloc_frame = ff_videotoolbox_alloc_frame,
134  .start_frame = videotoolbox_vp9_start_frame,
135  .decode_slice = videotoolbox_vp9_decode_slice,
136  .end_frame = videotoolbox_vp9_end_frame,
137  .frame_params = ff_videotoolbox_frame_params,
139  .uninit = ff_videotoolbox_uninit,
140  .priv_data_size = sizeof(VTContext),
141 };
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
ff_videotoolbox_common_end_frame
int ff_videotoolbox_common_end_frame(AVCodecContext *avctx, AVFrame *frame)
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1002
videotoolbox_vp9_decode_slice
static int videotoolbox_vp9_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: videotoolbox_vp9.c:111
ff_videotoolbox_uninit
int ff_videotoolbox_uninit(AVCodecContext *avctx)
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
pixdesc.h
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:995
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:661
internal.h
data
const char data[16]
Definition: mxf.c:146
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
VPX_SUBSAMPLING_444
@ VPX_SUBSAMPLING_444
Definition: videotoolbox_vp9.c:42
AVHWAccel
Definition: avcodec.h:2076
av_pix_fmt_get_chroma_sub_sample
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:2916
ff_videotoolbox_common_init
int ff_videotoolbox_common_init(AVCodecContext *avctx)
videotoolbox.h
vp9shared.h
avassert.h
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:988
vt_internal.h
intreadwrite.h
VP9SharedContext
Definition: vp9shared.h:165
videotoolbox_vp9_end_frame
static int videotoolbox_vp9_end_frame(AVCodecContext *avctx)
Definition: videotoolbox_vp9.c:120
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:220
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
ff_videotoolbox_vpcc_extradata_create
CFDataRef ff_videotoolbox_vpcc_extradata_create(AVCodecContext *avctx)
Definition: videotoolbox_vp9.c:63
videotoolbox_vp9_start_frame
static int videotoolbox_vp9_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size)
Definition: videotoolbox_vp9.c:104
decode.h
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1009
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:405
AVCHROMA_LOC_LEFT
@ AVCHROMA_LOC_LEFT
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:682
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:461
VPX_SUBSAMPLING_420_VERTICAL
@ VPX_SUBSAMPLING_420_VERTICAL
Definition: videotoolbox_vp9.c:39
AVCodecContext::level
int level
level
Definition: avcodec.h:1691
ff_videotoolbox_frame_params
int ff_videotoolbox_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
VPX_CHROMA_SUBSAMPLING
VPX_CHROMA_SUBSAMPLING
Definition: videotoolbox_vp9.c:37
hwcontext_videotoolbox.h
AVCodecInternal::hwaccel_priv_data
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:137
size
int size
Definition: twinvq_data.h:10344
AV_WB24
#define AV_WB24(p, d)
Definition: intreadwrite.h:450
frame.h
VTContext
Definition: vt_internal.h:25
AVChromaLocation
AVChromaLocation
Location of chroma samples.
Definition: pixfmt.h:680
AVHWAccel::name
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:2082
AV_PIX_FMT_VIDEOTOOLBOX
@ AV_PIX_FMT_VIDEOTOOLBOX
hardware decoding through Videotoolbox
Definition: pixfmt.h:302
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1016
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
ff_videotoolbox_alloc_frame
int ff_videotoolbox_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: videotoolbox.c:124
AVCodecContext
main external API structure.
Definition: avcodec.h:426
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
avutil.h
CUR_FRAME
#define CUR_FRAME
Definition: vp9shared.h:169
VPX_SUBSAMPLING_420_COLLOCATED_WITH_LUMA
@ VPX_SUBSAMPLING_420_COLLOCATED_WITH_LUMA
Definition: videotoolbox_vp9.c:40
ff_videotoolbox_buffer_copy
int ff_videotoolbox_buffer_copy(VTContext *vtctx, const uint8_t *buffer, uint32_t size)
Definition: videotoolbox.c:73
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:453
get_vpx_chroma_subsampling
static int get_vpx_chroma_subsampling(enum AVPixelFormat pixel_format, enum AVChromaLocation chroma_location)
Definition: videotoolbox_vp9.c:45
hwcontext.h
h
h
Definition: vp9dsp_template.c:2038
AVCodecContext::sw_pix_fmt
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1757
ff_vp9_videotoolbox_hwaccel
const AVHWAccel ff_vp9_videotoolbox_hwaccel
Definition: videotoolbox_vp9.c:128
VPX_SUBSAMPLING_422
@ VPX_SUBSAMPLING_422
Definition: videotoolbox_vp9.c:41