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/pixdesc.h"
29 #include "bytestream.h"
33 #include "jpegtables.h"
36 #include "vaapi_encode.h"
39 // Standard JPEG quantisation tables, in zigzag order.
40 static const unsigned char vaapi_encode_mjpeg_quant_luminance[64] = {
41 16, 11, 12, 14, 12, 10, 16, 14,
42 13, 14, 18, 17, 16, 19, 24, 40,
43 26, 24, 22, 22, 24, 49, 35, 37,
44 29, 40, 58, 51, 61, 60, 57, 51,
45 56, 55, 64, 72, 92, 78, 64, 68,
46 87, 69, 55, 56, 80, 109, 81, 87,
47 95, 98, 103, 104, 103, 62, 77, 113,
48 121, 112, 100, 120, 92, 101, 103, 99,
50 static const unsigned char vaapi_encode_mjpeg_quant_chrominance[64] = {
51 17, 18, 18, 24, 21, 24, 47, 26,
52 26, 47, 99, 66, 56, 66, 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,
56 99, 99, 99, 99, 99, 99, 99, 99,
57 99, 99, 99, 99, 99, 99, 99, 99,
58 99, 99, 99, 99, 99, 99, 99, 99,
61 typedef struct VAAPIEncodeMJPEGContext {
62 VAAPIEncodeContext common;
70 uint8_t jfif_data[14];
73 JPEGRawFrameHeader frame_header;
75 JPEGRawApplicationData jfif_header;
76 JPEGRawQuantisationTableSpecification quant_tables;
77 JPEGRawHuffmanTableSpecification huffman_tables;
79 CodedBitstreamContext *cbc;
80 CodedBitstreamFragment current_fragment;
81 } VAAPIEncodeMJPEGContext;
83 static int vaapi_encode_mjpeg_write_image_header(AVCodecContext *avctx,
84 VAAPIEncodePicture *pic,
85 VAAPIEncodeSlice *slice,
86 char *data, size_t *data_len)
88 VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
89 CodedBitstreamFragment *frag = &priv->current_fragment;
93 err = ff_cbs_insert_unit_content(priv->cbc, frag, -1,
95 &priv->jfif_header, NULL);
100 err = ff_cbs_insert_unit_content(priv->cbc, frag, -1,
102 &priv->quant_tables, NULL);
106 err = ff_cbs_insert_unit_content(priv->cbc, frag, -1,
108 &priv->frame_header, NULL);
113 err = ff_cbs_insert_unit_content(priv->cbc, frag, -1,
115 &priv->huffman_tables, NULL);
120 err = ff_cbs_insert_unit_content(priv->cbc, frag, -1,
126 err = ff_cbs_write_fragment_data(priv->cbc, frag);
128 av_log(avctx, AV_LOG_ERROR, "Failed to write image header.\n");
132 if (*data_len < 8 * frag->data_size) {
133 av_log(avctx, AV_LOG_ERROR, "Image header too large: "
134 "%zu < %zu.\n", *data_len, 8 * frag->data_size);
135 err = AVERROR(ENOSPC);
139 // Remove the EOI at the end of the fragment.
140 memcpy(data, frag->data, frag->data_size - 2);
141 *data_len = 8 * (frag->data_size - 2);
145 ff_cbs_fragment_uninit(priv->cbc, frag);
149 static int vaapi_encode_mjpeg_write_extra_buffer(AVCodecContext *avctx,
150 VAAPIEncodePicture *pic,
151 int index, int *type,
152 char *data, size_t *data_len)
154 VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
158 // Write quantisation tables.
159 JPEGRawFrameHeader *fh = &priv->frame_header;
160 JPEGRawQuantisationTableSpecification *dqt = &priv->quant_tables;
161 VAQMatrixBufferJPEG *quant;
163 if (*data_len < sizeof(*quant))
164 return AVERROR(ENOSPC);
165 *type = VAQMatrixBufferType;
166 *data_len = sizeof(*quant);
168 quant = (VAQMatrixBufferJPEG*)data;
169 memset(quant, 0, sizeof(*quant));
171 quant->load_lum_quantiser_matrix = 1;
172 for (i = 0; i < 64; i++)
173 quant->lum_quantiser_matrix[i] = dqt->table[fh->Tq[0]].Q[i];
176 quant->load_chroma_quantiser_matrix = 1;
177 for (i = 0; i < 64; i++)
178 quant->chroma_quantiser_matrix[i] =
179 dqt->table[fh->Tq[1]].Q[i];
182 } else if (index == 1) {
183 // Write huffman tables.
184 JPEGRawScanHeader *sh = &priv->scan.header;
185 JPEGRawHuffmanTableSpecification *dht = &priv->huffman_tables;
186 VAHuffmanTableBufferJPEGBaseline *huff;
188 if (*data_len < sizeof(*huff))
189 return AVERROR(ENOSPC);
190 *type = VAHuffmanTableBufferType;
191 *data_len = sizeof(*huff);
193 huff = (VAHuffmanTableBufferJPEGBaseline*)data;
194 memset(huff, 0, sizeof(*huff));
196 for (t = 0; t < 1 + (sh->Ns > 1); t++) {
197 const JPEGRawHuffmanTable *ht;
199 huff->load_huffman_table[t] = 1;
201 ht = &dht->table[2 * t];
202 for (i = k = 0; i < 16; i++)
203 k += (huff->huffman_table[t].num_dc_codes[i] = ht->L[i]);
204 av_assert0(k <= sizeof(huff->huffman_table[t].dc_values));
205 for (i = 0; i < k; i++)
206 huff->huffman_table[t].dc_values[i] = ht->V[i];
208 ht = &dht->table[2 * t + 1];
209 for (i = k = 0; i < 16; i++)
210 k += (huff->huffman_table[t].num_ac_codes[i] = ht->L[i]);
211 av_assert0(k <= sizeof(huff->huffman_table[t].ac_values));
212 for (i = 0; i < k; i++)
213 huff->huffman_table[t].ac_values[i] = ht->V[i];
222 static int vaapi_encode_mjpeg_init_picture_params(AVCodecContext *avctx,
223 VAAPIEncodePicture *pic)
225 VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
226 JPEGRawFrameHeader *fh = &priv->frame_header;
227 JPEGRawScanHeader *sh = &priv->scan.header;
228 VAEncPictureParameterBufferJPEG *vpic = pic->codec_picture_params;
229 const AVPixFmtDescriptor *desc;
230 const uint8_t *components;
231 int t, i, quant_scale, len;
233 desc = av_pix_fmt_desc_get(priv->common.input_frames->sw_format);
235 if (desc->flags & AV_PIX_FMT_FLAG_RGB)
236 components = (uint8_t[3]) { 'R', 'G', 'B' };
238 components = (uint8_t[3]) { 1, 2, 3 };
243 fh->Y = avctx->height;
244 fh->X = avctx->width;
245 fh->Nf = desc->nb_components;
247 for (i = 0; i < fh->Nf; i++) {
248 fh->C[i] = components[i];
249 fh->H[i] = 1 + (i == 0 ? desc->log2_chroma_w : 0);
250 fh->V[i] = 1 + (i == 0 ? desc->log2_chroma_h : 0);
255 fh->Lf = 8 + 3 * fh->Nf;
259 JPEGRawApplicationData *app = &priv->jfif_header;
260 AVRational sar = pic->input_image->sample_aspect_ratio;
264 bytestream2_init_writer(&pbc, priv->jfif_data,
265 sizeof(priv->jfif_data));
267 bytestream2_put_buffer(&pbc, "JFIF", 5);
268 bytestream2_put_be16(&pbc, 0x0102);
269 bytestream2_put_byte(&pbc, 0);
271 av_reduce(&sar_w, &sar_h, sar.num, sar.den, 65535);
272 if (sar_w && sar_h) {
273 bytestream2_put_be16(&pbc, sar_w);
274 bytestream2_put_be16(&pbc, sar_h);
276 bytestream2_put_be16(&pbc, 1);
277 bytestream2_put_be16(&pbc, 1);
280 bytestream2_put_byte(&pbc, 0);
281 bytestream2_put_byte(&pbc, 0);
283 av_assert0(bytestream2_get_bytes_left_p(&pbc) == 0);
285 app->Lp = 2 + sizeof(priv->jfif_data);
286 app->Ap = priv->jfif_data;
290 // Quantisation tables.
292 if (priv->quality < 50)
293 quant_scale = 5000 / priv->quality;
295 quant_scale = 200 - 2 * priv->quality;
299 for (t = 0; t < 1 + (fh->Nf > 1); t++) {
300 JPEGRawQuantisationTable *quant = &priv->quant_tables.table[t];
301 const uint8_t *data = t == 0 ?
302 vaapi_encode_mjpeg_quant_luminance :
303 vaapi_encode_mjpeg_quant_chrominance;
307 for (i = 0; i < 64; i++)
308 quant->Q[i] = av_clip(data[i] * quant_scale / 100, 1, 255);
313 priv->quant_tables.Lq = len;
319 for (t = 0; t < 2 + 2 * (fh->Nf > 1); t++) {
320 JPEGRawHuffmanTable *huff = &priv->huffman_tables.table[t];
321 const uint8_t *lengths, *values;
326 lengths = avpriv_mjpeg_bits_dc_luminance + 1;
327 values = avpriv_mjpeg_val_dc;
330 lengths = avpriv_mjpeg_bits_ac_luminance + 1;
331 values = avpriv_mjpeg_val_ac_luminance;
334 lengths = avpriv_mjpeg_bits_dc_chrominance + 1;
335 values = avpriv_mjpeg_val_dc;
338 lengths = avpriv_mjpeg_bits_ac_chrominance + 1;
339 values = avpriv_mjpeg_val_ac_chrominance;
346 for (i = k = 0; i < 16; i++)
347 k += (huff->L[i] = lengths[i]);
349 for (i = 0; i < k; i++)
350 huff->V[i] = values[i];
355 priv->huffman_tables.Lh = len;
361 for (i = 0; i < fh->Nf; i++) {
362 sh->Cs[i] = fh->C[i];
372 sh->Ls = 6 + 2 * sh->Ns;
375 *vpic = (VAEncPictureParameterBufferJPEG) {
376 .reconstructed_picture = pic->recon_surface,
377 .coded_buf = pic->output_buffer,
379 .picture_width = fh->X,
380 .picture_height = fh->Y,
390 .sample_bit_depth = fh->P,
392 .num_components = fh->Nf,
394 // The driver modifies the provided quantisation tables according
395 // to this quality value; the middle value of 50 makes that the
396 // identity so that they are used unchanged.
400 for (i = 0; i < fh->Nf; i++) {
401 vpic->component_id[i] = fh->C[i];
402 vpic->quantiser_table_selector[i] = fh->Tq[i];
410 static int vaapi_encode_mjpeg_init_slice_params(AVCodecContext *avctx,
411 VAAPIEncodePicture *pic,
412 VAAPIEncodeSlice *slice)
414 VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
415 JPEGRawScanHeader *sh = &priv->scan.header;
416 VAEncSliceParameterBufferJPEG *vslice = slice->codec_slice_params;
419 *vslice = (VAEncSliceParameterBufferJPEG) {
420 .restart_interval = 0,
421 .num_components = sh->Ns,
424 for (i = 0; i < sh->Ns; i++) {
425 vslice->components[i].component_selector = sh->Cs[i];
426 vslice->components[i].dc_table_selector = sh->Td[i];
427 vslice->components[i].ac_table_selector = sh->Ta[i];
433 static av_cold int vaapi_encode_mjpeg_configure(AVCodecContext *avctx)
435 VAAPIEncodeContext *ctx = avctx->priv_data;
436 VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
439 priv->quality = avctx->global_quality;
440 if (priv->quality < 1 || priv->quality > 100) {
441 av_log(avctx, AV_LOG_ERROR, "Invalid quality value %d "
442 "(must be 1-100).\n", priv->quality);
443 return AVERROR(EINVAL);
446 // Hack: the implementation calls the JPEG image header (which we
447 // will use in the same way as a slice header) generic "raw data".
448 // Therefore, if after the packed header capability check we have
449 // PACKED_HEADER_RAW_DATA available, rewrite it as
450 // PACKED_HEADER_SLICE so that the header-writing code can do the
452 if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_RAW_DATA) {
453 ctx->va_packed_headers &= ~VA_ENC_PACKED_HEADER_RAW_DATA;
454 ctx->va_packed_headers |= VA_ENC_PACKED_HEADER_SLICE;
457 err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_MJPEG, avctx);
464 static const VAAPIEncodeProfile vaapi_encode_mjpeg_profiles[] = {
465 { FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
466 8, 1, 0, 0, VAProfileJPEGBaseline },
467 { FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
468 8, 3, 1, 1, VAProfileJPEGBaseline },
469 { FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
470 8, 3, 1, 0, VAProfileJPEGBaseline },
471 { FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
472 8, 3, 0, 0, VAProfileJPEGBaseline },
473 { FF_PROFILE_UNKNOWN }
476 static const VAAPIEncodeType vaapi_encode_type_mjpeg = {
477 .profiles = vaapi_encode_mjpeg_profiles,
479 .configure = &vaapi_encode_mjpeg_configure,
481 .picture_params_size = sizeof(VAEncPictureParameterBufferJPEG),
482 .init_picture_params = &vaapi_encode_mjpeg_init_picture_params,
484 .slice_params_size = sizeof(VAEncSliceParameterBufferJPEG),
485 .init_slice_params = &vaapi_encode_mjpeg_init_slice_params,
487 .slice_header_type = VAEncPackedHeaderRawData,
488 .write_slice_header = &vaapi_encode_mjpeg_write_image_header,
490 .write_extra_buffer = &vaapi_encode_mjpeg_write_extra_buffer,
493 static av_cold int vaapi_encode_mjpeg_init(AVCodecContext *avctx)
495 VAAPIEncodeContext *ctx = avctx->priv_data;
497 ctx->codec = &vaapi_encode_type_mjpeg;
499 // The JPEG image header - see note above.
500 ctx->desired_packed_headers =
501 VA_ENC_PACKED_HEADER_RAW_DATA;
503 ctx->surface_width = FFALIGN(avctx->width, 8);
504 ctx->surface_height = FFALIGN(avctx->height, 8);
506 return ff_vaapi_encode_init(avctx);
509 static av_cold int vaapi_encode_mjpeg_close(AVCodecContext *avctx)
511 VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
513 ff_cbs_close(&priv->cbc);
515 return ff_vaapi_encode_close(avctx);
518 #define OFFSET(x) offsetof(VAAPIEncodeMJPEGContext, x)
519 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
520 static const AVOption vaapi_encode_mjpeg_options[] = {
521 VAAPI_ENCODE_COMMON_OPTIONS,
523 { "jfif", "Include JFIF header",
524 OFFSET(jfif), AV_OPT_TYPE_BOOL,
525 { .i64 = 0 }, 0, 1, FLAGS },
526 { "huffman", "Include huffman tables",
527 OFFSET(huffman), AV_OPT_TYPE_BOOL,
528 { .i64 = 1 }, 0, 1, FLAGS },
533 static const AVCodecDefault vaapi_encode_mjpeg_defaults[] = {
534 { "global_quality", "80" },
540 static const AVClass vaapi_encode_mjpeg_class = {
541 .class_name = "mjpeg_vaapi",
542 .item_name = av_default_item_name,
543 .option = vaapi_encode_mjpeg_options,
544 .version = LIBAVUTIL_VERSION_INT,
547 AVCodec ff_mjpeg_vaapi_encoder = {
548 .name = "mjpeg_vaapi",
549 .long_name = NULL_IF_CONFIG_SMALL("MJPEG (VAAPI)"),
550 .type = AVMEDIA_TYPE_VIDEO,
551 .id = AV_CODEC_ID_MJPEG,
552 .priv_data_size = sizeof(VAAPIEncodeMJPEGContext),
553 .init = &vaapi_encode_mjpeg_init,
554 .encode2 = &ff_vaapi_encode2,
555 .close = &vaapi_encode_mjpeg_close,
556 .priv_class = &vaapi_encode_mjpeg_class,
557 .capabilities = AV_CODEC_CAP_HARDWARE |
558 AV_CODEC_CAP_INTRA_ONLY,
559 .defaults = vaapi_encode_mjpeg_defaults,
560 .pix_fmts = (const enum AVPixelFormat[]) {
564 .wrapper_name = "vaapi",