2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <va/va_enc_jpeg.h>
22 #include "libavutil/avassert.h"
23 #include "libavutil/common.h"
24 #include "libavutil/internal.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixfmt.h"
30 #include "jpegtables.h"
33 #include "vaapi_encode.h"
36 // Standard JPEG quantisation tables, in zigzag order.
37 static const unsigned char vaapi_encode_mjpeg_quant_luminance[64] = {
38 16, 11, 12, 14, 12, 10, 16, 14,
39 13, 14, 18, 17, 16, 19, 24, 40,
40 26, 24, 22, 22, 24, 49, 35, 37,
41 29, 40, 58, 51, 61, 60, 57, 51,
42 56, 55, 64, 72, 92, 78, 64, 68,
43 87, 69, 55, 56, 80, 109, 81, 87,
44 95, 98, 103, 104, 103, 62, 77, 113,
45 121, 112, 100, 120, 92, 101, 103, 99,
47 static const unsigned char vaapi_encode_mjpeg_quant_chrominance[64] = {
48 17, 18, 18, 24, 21, 24, 47, 26,
49 26, 47, 99, 66, 56, 66, 99, 99,
50 99, 99, 99, 99, 99, 99, 99, 99,
51 99, 99, 99, 99, 99, 99, 99, 99,
52 99, 99, 99, 99, 99, 99, 99, 99,
53 99, 99, 99, 99, 99, 99, 99, 99,
54 99, 99, 99, 99, 99, 99, 99, 99,
55 99, 99, 99, 99, 99, 99, 99, 99,
58 typedef struct VAAPIEncodeMJPEGContext {
59 VAAPIEncodeContext common;
62 int component_subsample_h[3];
63 int component_subsample_v[3];
65 VAQMatrixBufferJPEG quant_tables;
66 VAHuffmanTableBufferJPEGBaseline huffman_tables;
67 } VAAPIEncodeMJPEGContext;
69 static av_cold void vaapi_encode_mjpeg_copy_huffman(unsigned char *dst_lengths,
70 unsigned char *dst_values,
71 const unsigned char *src_lengths,
72 const unsigned char *src_values)
79 for (i = 0; i < 16; i++)
80 mt += (dst_lengths[i] = src_lengths[i]);
82 for (i = 0; i < mt; i++)
83 dst_values[i] = src_values[i];
86 static av_cold void vaapi_encode_mjpeg_init_tables(AVCodecContext *avctx)
88 VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
89 VAQMatrixBufferJPEG *quant = &priv->quant_tables;
90 VAHuffmanTableBufferJPEGBaseline *huff = &priv->huffman_tables;
93 quant->load_lum_quantiser_matrix = 1;
94 quant->load_chroma_quantiser_matrix = 1;
96 for (i = 0; i < 64; i++) {
97 quant->lum_quantiser_matrix[i] =
98 vaapi_encode_mjpeg_quant_luminance[i];
99 quant->chroma_quantiser_matrix[i] =
100 vaapi_encode_mjpeg_quant_chrominance[i];
103 huff->load_huffman_table[0] = 1;
104 vaapi_encode_mjpeg_copy_huffman(huff->huffman_table[0].num_dc_codes,
105 huff->huffman_table[0].dc_values,
106 avpriv_mjpeg_bits_dc_luminance,
107 avpriv_mjpeg_val_dc);
108 vaapi_encode_mjpeg_copy_huffman(huff->huffman_table[0].num_ac_codes,
109 huff->huffman_table[0].ac_values,
110 avpriv_mjpeg_bits_ac_luminance,
111 avpriv_mjpeg_val_ac_luminance);
112 memset(huff->huffman_table[0].pad, 0, sizeof(huff->huffman_table[0].pad));
114 huff->load_huffman_table[1] = 1;
115 vaapi_encode_mjpeg_copy_huffman(huff->huffman_table[1].num_dc_codes,
116 huff->huffman_table[1].dc_values,
117 avpriv_mjpeg_bits_dc_chrominance,
118 avpriv_mjpeg_val_dc);
119 vaapi_encode_mjpeg_copy_huffman(huff->huffman_table[1].num_ac_codes,
120 huff->huffman_table[1].ac_values,
121 avpriv_mjpeg_bits_ac_chrominance,
122 avpriv_mjpeg_val_ac_chrominance);
123 memset(huff->huffman_table[1].pad, 0, sizeof(huff->huffman_table[1].pad));
126 static void vaapi_encode_mjpeg_write_marker(PutBitContext *pbc, int marker)
128 put_bits(pbc, 8, 0xff);
129 put_bits(pbc, 8, marker);
132 static int vaapi_encode_mjpeg_write_image_header(AVCodecContext *avctx,
133 VAAPIEncodePicture *pic,
134 VAAPIEncodeSlice *slice,
135 char *data, size_t *data_len)
137 VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
138 VAEncPictureParameterBufferJPEG *vpic = pic->codec_picture_params;
139 VAEncSliceParameterBufferJPEG *vslice = slice->codec_slice_params;
141 int t, i, quant_scale;
143 init_put_bits(&pbc, data, *data_len);
145 vaapi_encode_mjpeg_write_marker(&pbc, SOI);
147 // Quantisation table coefficients are scaled for quality by the driver,
148 // so we also need to do it ourselves here so that headers match.
149 if (priv->quality < 50)
150 quant_scale = 5000 / priv->quality;
152 quant_scale = 200 - 2 * priv->quality;
154 for (t = 0; t < 2; t++) {
157 vaapi_encode_mjpeg_write_marker(&pbc, DQT);
159 put_bits(&pbc, 16, 3 + 64); // Lq
160 put_bits(&pbc, 4, 0); // Pq
161 put_bits(&pbc, 4, t); // Tq
163 for (i = 0; i < 64; i++) {
164 q = i[t ? priv->quant_tables.chroma_quantiser_matrix
165 : priv->quant_tables.lum_quantiser_matrix];
166 q = (q * quant_scale) / 100;
168 if (q > 255) q = 255;
169 put_bits(&pbc, 8, q);
173 vaapi_encode_mjpeg_write_marker(&pbc, SOF0);
175 put_bits(&pbc, 16, 8 + 3 * vpic->num_components); // Lf
176 put_bits(&pbc, 8, vpic->sample_bit_depth); // P
177 put_bits(&pbc, 16, vpic->picture_height); // Y
178 put_bits(&pbc, 16, vpic->picture_width); // X
179 put_bits(&pbc, 8, vpic->num_components); // Nf
181 for (i = 0; i < vpic->num_components; i++) {
182 put_bits(&pbc, 8, vpic->component_id[i]); // Ci
183 put_bits(&pbc, 4, priv->component_subsample_h[i]); // Hi
184 put_bits(&pbc, 4, priv->component_subsample_v[i]); // Vi
185 put_bits(&pbc, 8, vpic->quantiser_table_selector[i]); // Tqi
188 for (t = 0; t < 4; t++) {
190 unsigned char *lengths, *values;
192 vaapi_encode_mjpeg_write_marker(&pbc, DHT);
195 lengths = priv->huffman_tables.huffman_table[t / 2].num_dc_codes;
196 values = priv->huffman_tables.huffman_table[t / 2].dc_values;
198 lengths = priv->huffman_tables.huffman_table[t / 2].num_ac_codes;
199 values = priv->huffman_tables.huffman_table[t / 2].ac_values;
203 for (i = 0; i < 16; i++)
206 put_bits(&pbc, 16, 2 + 17 + mt); // Lh
207 put_bits(&pbc, 4, t & 1); // Tc
208 put_bits(&pbc, 4, t / 2); // Th
210 for (i = 0; i < 16; i++)
211 put_bits(&pbc, 8, lengths[i]);
212 for (i = 0; i < mt; i++)
213 put_bits(&pbc, 8, values[i]);
216 vaapi_encode_mjpeg_write_marker(&pbc, SOS);
218 av_assert0(vpic->num_components == vslice->num_components);
220 put_bits(&pbc, 16, 6 + 2 * vslice->num_components); // Ls
221 put_bits(&pbc, 8, vslice->num_components); // Ns
223 for (i = 0; i < vslice->num_components; i++) {
224 put_bits(&pbc, 8, vslice->components[i].component_selector); // Csj
225 put_bits(&pbc, 4, vslice->components[i].dc_table_selector); // Tdj
226 put_bits(&pbc, 4, vslice->components[i].ac_table_selector); // Taj
229 put_bits(&pbc, 8, 0); // Ss
230 put_bits(&pbc, 8, 63); // Se
231 put_bits(&pbc, 4, 0); // Ah
232 put_bits(&pbc, 4, 0); // Al
234 *data_len = put_bits_count(&pbc);
235 flush_put_bits(&pbc);
240 static int vaapi_encode_mjpeg_write_extra_buffer(AVCodecContext *avctx,
241 VAAPIEncodePicture *pic,
242 int index, int *type,
243 char *data, size_t *data_len)
245 VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
248 // Write quantisation tables.
249 if (*data_len < sizeof(priv->quant_tables))
250 return AVERROR(EINVAL);
251 *type = VAQMatrixBufferType;
252 memcpy(data, &priv->quant_tables,
253 *data_len = sizeof(priv->quant_tables));
255 } else if (index == 1) {
256 // Write huffman tables.
257 if (*data_len < sizeof(priv->huffman_tables))
258 return AVERROR(EINVAL);
259 *type = VAHuffmanTableBufferType;
260 memcpy(data, &priv->huffman_tables,
261 *data_len = sizeof(priv->huffman_tables));
269 static int vaapi_encode_mjpeg_init_picture_params(AVCodecContext *avctx,
270 VAAPIEncodePicture *pic)
272 VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
273 VAEncPictureParameterBufferJPEG *vpic = pic->codec_picture_params;
275 vpic->reconstructed_picture = pic->recon_surface;
276 vpic->coded_buf = pic->output_buffer;
278 vpic->picture_width = avctx->width;
279 vpic->picture_height = avctx->height;
281 vpic->pic_flags.bits.profile = 0;
282 vpic->pic_flags.bits.progressive = 0;
283 vpic->pic_flags.bits.huffman = 1;
284 vpic->pic_flags.bits.interleaved = 0;
285 vpic->pic_flags.bits.differential = 0;
287 vpic->sample_bit_depth = 8;
290 vpic->num_components = 3;
292 vpic->component_id[0] = 1;
293 vpic->component_id[1] = 2;
294 vpic->component_id[2] = 3;
296 priv->component_subsample_h[0] = 2;
297 priv->component_subsample_v[0] = 2;
298 priv->component_subsample_h[1] = 1;
299 priv->component_subsample_v[1] = 1;
300 priv->component_subsample_h[2] = 1;
301 priv->component_subsample_v[2] = 1;
303 vpic->quantiser_table_selector[0] = 0;
304 vpic->quantiser_table_selector[1] = 1;
305 vpic->quantiser_table_selector[2] = 1;
307 vpic->quality = priv->quality;
314 static int vaapi_encode_mjpeg_init_slice_params(AVCodecContext *avctx,
315 VAAPIEncodePicture *pic,
316 VAAPIEncodeSlice *slice)
318 VAEncPictureParameterBufferJPEG *vpic = pic->codec_picture_params;
319 VAEncSliceParameterBufferJPEG *vslice = slice->codec_slice_params;
322 vslice->restart_interval = 0;
324 vslice->num_components = vpic->num_components;
325 for (i = 0; i < vslice->num_components; i++) {
326 vslice->components[i].component_selector = i + 1;
327 vslice->components[i].dc_table_selector = (i > 0);
328 vslice->components[i].ac_table_selector = (i > 0);
334 static av_cold int vaapi_encode_mjpeg_configure(AVCodecContext *avctx)
336 VAAPIEncodeContext *ctx = avctx->priv_data;
337 VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
339 priv->quality = avctx->global_quality;
340 if (priv->quality < 1 || priv->quality > 100) {
341 av_log(avctx, AV_LOG_ERROR, "Invalid quality value %d "
342 "(must be 1-100).\n", priv->quality);
343 return AVERROR(EINVAL);
346 // Hack: the implementation calls the JPEG image header (which we
347 // will use in the same way as a slice header) generic "raw data".
348 // Therefore, if after the packed header capability check we have
349 // PACKED_HEADER_RAW_DATA available, rewrite it as
350 // PACKED_HEADER_SLICE so that the header-writing code can do the
352 if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_RAW_DATA) {
353 ctx->va_packed_headers &= ~VA_ENC_PACKED_HEADER_RAW_DATA;
354 ctx->va_packed_headers |= VA_ENC_PACKED_HEADER_SLICE;
357 vaapi_encode_mjpeg_init_tables(avctx);
362 static const VAAPIEncodeType vaapi_encode_type_mjpeg = {
363 .configure = &vaapi_encode_mjpeg_configure,
365 .picture_params_size = sizeof(VAEncPictureParameterBufferJPEG),
366 .init_picture_params = &vaapi_encode_mjpeg_init_picture_params,
368 .slice_params_size = sizeof(VAEncSliceParameterBufferJPEG),
369 .init_slice_params = &vaapi_encode_mjpeg_init_slice_params,
371 .slice_header_type = VAEncPackedHeaderRawData,
372 .write_slice_header = &vaapi_encode_mjpeg_write_image_header,
374 .write_extra_buffer = &vaapi_encode_mjpeg_write_extra_buffer,
377 static av_cold int vaapi_encode_mjpeg_init(AVCodecContext *avctx)
379 VAAPIEncodeContext *ctx = avctx->priv_data;
381 ctx->codec = &vaapi_encode_type_mjpeg;
383 ctx->va_profile = VAProfileJPEGBaseline;
384 ctx->va_entrypoint = VAEntrypointEncPicture;
386 ctx->va_rt_format = VA_RT_FORMAT_YUV420;
388 ctx->va_rc_mode = VA_RC_CQP;
390 // The JPEG image header - see note above.
391 ctx->va_packed_headers =
392 VA_ENC_PACKED_HEADER_RAW_DATA;
394 ctx->surface_width = FFALIGN(avctx->width, 8);
395 ctx->surface_height = FFALIGN(avctx->height, 8);
397 return ff_vaapi_encode_init(avctx);
400 static const AVCodecDefault vaapi_encode_mjpeg_defaults[] = {
401 { "global_quality", "80" },
405 static const AVClass vaapi_encode_mjpeg_class = {
406 .class_name = "mjpeg_vaapi",
407 .item_name = av_default_item_name,
408 .version = LIBAVUTIL_VERSION_INT,
411 AVCodec ff_mjpeg_vaapi_encoder = {
412 .name = "mjpeg_vaapi",
413 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (VAAPI)"),
414 .type = AVMEDIA_TYPE_VIDEO,
415 .id = AV_CODEC_ID_MJPEG,
416 .priv_data_size = sizeof(VAAPIEncodeMJPEGContext),
417 .init = &vaapi_encode_mjpeg_init,
418 .encode2 = &ff_vaapi_encode2,
419 .close = &ff_vaapi_encode_close,
420 .priv_class = &vaapi_encode_mjpeg_class,
421 .capabilities = AV_CODEC_CAP_HARDWARE,
422 .defaults = vaapi_encode_mjpeg_defaults,
423 .pix_fmts = (const enum AVPixelFormat[]) {
427 .wrapper_name = "vaapi",