2016-03-24 01:50:47 +02:00
|
|
|
/*
|
2016-05-08 23:46:52 +02:00
|
|
|
* This file is part of FFmpeg.
|
2016-03-24 01:50:47 +02:00
|
|
|
*
|
2016-05-08 23:46:52 +02:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2016-03-24 01:50:47 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2016-05-08 23:46:52 +02:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2016-03-24 01:50:47 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2016-05-08 23:46:52 +02:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2016-03-24 01:50:47 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <va/va.h>
|
|
|
|
#include <va/va_enc_jpeg.h>
|
|
|
|
|
|
|
|
#include "libavutil/avassert.h"
|
|
|
|
#include "libavutil/common.h"
|
|
|
|
#include "libavutil/internal.h"
|
|
|
|
#include "libavutil/opt.h"
|
2018-09-19 00:30:54 +02:00
|
|
|
#include "libavutil/pixdesc.h"
|
2016-03-24 01:50:47 +02:00
|
|
|
|
|
|
|
#include "avcodec.h"
|
2018-09-19 00:30:54 +02:00
|
|
|
#include "bytestream.h"
|
|
|
|
#include "cbs.h"
|
|
|
|
#include "cbs_jpeg.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2016-03-24 01:50:47 +02:00
|
|
|
#include "jpegtables.h"
|
|
|
|
#include "mjpeg.h"
|
|
|
|
#include "put_bits.h"
|
|
|
|
#include "vaapi_encode.h"
|
|
|
|
|
|
|
|
|
|
|
|
// Standard JPEG quantisation tables, in zigzag order.
|
|
|
|
static const unsigned char vaapi_encode_mjpeg_quant_luminance[64] = {
|
|
|
|
16, 11, 12, 14, 12, 10, 16, 14,
|
|
|
|
13, 14, 18, 17, 16, 19, 24, 40,
|
|
|
|
26, 24, 22, 22, 24, 49, 35, 37,
|
|
|
|
29, 40, 58, 51, 61, 60, 57, 51,
|
|
|
|
56, 55, 64, 72, 92, 78, 64, 68,
|
|
|
|
87, 69, 55, 56, 80, 109, 81, 87,
|
|
|
|
95, 98, 103, 104, 103, 62, 77, 113,
|
|
|
|
121, 112, 100, 120, 92, 101, 103, 99,
|
|
|
|
};
|
|
|
|
static const unsigned char vaapi_encode_mjpeg_quant_chrominance[64] = {
|
|
|
|
17, 18, 18, 24, 21, 24, 47, 26,
|
|
|
|
26, 47, 99, 66, 56, 66, 99, 99,
|
|
|
|
99, 99, 99, 99, 99, 99, 99, 99,
|
|
|
|
99, 99, 99, 99, 99, 99, 99, 99,
|
|
|
|
99, 99, 99, 99, 99, 99, 99, 99,
|
|
|
|
99, 99, 99, 99, 99, 99, 99, 99,
|
|
|
|
99, 99, 99, 99, 99, 99, 99, 99,
|
|
|
|
99, 99, 99, 99, 99, 99, 99, 99,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct VAAPIEncodeMJPEGContext {
|
2018-08-23 01:44:36 +02:00
|
|
|
VAAPIEncodeContext common;
|
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
// User options.
|
|
|
|
int jfif;
|
|
|
|
int huffman;
|
|
|
|
|
|
|
|
// Derived settings.
|
2016-03-24 01:50:47 +02:00
|
|
|
int quality;
|
2018-09-19 00:30:54 +02:00
|
|
|
uint8_t jfif_data[14];
|
|
|
|
|
|
|
|
// Writer structures.
|
|
|
|
JPEGRawFrameHeader frame_header;
|
|
|
|
JPEGRawScan scan;
|
|
|
|
JPEGRawApplicationData jfif_header;
|
|
|
|
JPEGRawQuantisationTableSpecification quant_tables;
|
|
|
|
JPEGRawHuffmanTableSpecification huffman_tables;
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
CodedBitstreamContext *cbc;
|
|
|
|
CodedBitstreamFragment current_fragment;
|
2016-03-24 01:50:47 +02:00
|
|
|
} VAAPIEncodeMJPEGContext;
|
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
static int vaapi_encode_mjpeg_write_image_header(AVCodecContext *avctx,
|
|
|
|
VAAPIEncodePicture *pic,
|
|
|
|
VAAPIEncodeSlice *slice,
|
|
|
|
char *data, size_t *data_len)
|
2016-03-24 01:50:47 +02:00
|
|
|
{
|
2018-09-19 00:30:54 +02:00
|
|
|
VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
|
|
|
|
CodedBitstreamFragment *frag = &priv->current_fragment;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (priv->jfif) {
|
2020-07-06 00:34:25 +02:00
|
|
|
err = ff_cbs_insert_unit_content(frag, -1,
|
2018-09-19 00:30:54 +02:00
|
|
|
JPEG_MARKER_APPN + 0,
|
|
|
|
&priv->jfif_header, NULL);
|
|
|
|
if (err < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2020-07-06 00:34:25 +02:00
|
|
|
err = ff_cbs_insert_unit_content(frag, -1,
|
2018-09-19 00:30:54 +02:00
|
|
|
JPEG_MARKER_DQT,
|
|
|
|
&priv->quant_tables, NULL);
|
|
|
|
if (err < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2020-07-06 00:34:25 +02:00
|
|
|
err = ff_cbs_insert_unit_content(frag, -1,
|
2018-09-19 00:30:54 +02:00
|
|
|
JPEG_MARKER_SOF0,
|
|
|
|
&priv->frame_header, NULL);
|
|
|
|
if (err < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (priv->huffman) {
|
2020-07-06 00:34:25 +02:00
|
|
|
err = ff_cbs_insert_unit_content(frag, -1,
|
2018-09-19 00:30:54 +02:00
|
|
|
JPEG_MARKER_DHT,
|
|
|
|
&priv->huffman_tables, NULL);
|
|
|
|
if (err < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2020-07-06 00:34:25 +02:00
|
|
|
err = ff_cbs_insert_unit_content(frag, -1,
|
2018-09-19 00:30:54 +02:00
|
|
|
JPEG_MARKER_SOS,
|
|
|
|
&priv->scan, NULL);
|
|
|
|
if (err < 0)
|
|
|
|
goto fail;
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
err = ff_cbs_write_fragment_data(priv->cbc, frag);
|
|
|
|
if (err < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to write image header.\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
if (*data_len < 8 * frag->data_size) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Image header too large: "
|
|
|
|
"%zu < %zu.\n", *data_len, 8 * frag->data_size);
|
|
|
|
err = AVERROR(ENOSPC);
|
|
|
|
goto fail;
|
2016-03-24 01:50:47 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
// Remove the EOI at the end of the fragment.
|
|
|
|
memcpy(data, frag->data, frag->data_size - 2);
|
|
|
|
*data_len = 8 * (frag->data_size - 2);
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
err = 0;
|
|
|
|
fail:
|
2020-07-06 00:34:25 +02:00
|
|
|
ff_cbs_fragment_reset(frag);
|
2018-09-19 00:30:54 +02:00
|
|
|
return err;
|
2016-03-24 01:50:47 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
static int vaapi_encode_mjpeg_write_extra_buffer(AVCodecContext *avctx,
|
2016-03-24 01:50:47 +02:00
|
|
|
VAAPIEncodePicture *pic,
|
2018-09-19 00:30:54 +02:00
|
|
|
int index, int *type,
|
2016-03-24 01:50:47 +02:00
|
|
|
char *data, size_t *data_len)
|
|
|
|
{
|
2018-09-19 00:30:54 +02:00
|
|
|
VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
|
|
|
|
int t, i, k;
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
if (index == 0) {
|
|
|
|
// Write quantisation tables.
|
|
|
|
JPEGRawFrameHeader *fh = &priv->frame_header;
|
|
|
|
JPEGRawQuantisationTableSpecification *dqt = &priv->quant_tables;
|
|
|
|
VAQMatrixBufferJPEG *quant;
|
|
|
|
|
|
|
|
if (*data_len < sizeof(*quant))
|
|
|
|
return AVERROR(ENOSPC);
|
|
|
|
*type = VAQMatrixBufferType;
|
|
|
|
*data_len = sizeof(*quant);
|
|
|
|
|
|
|
|
quant = (VAQMatrixBufferJPEG*)data;
|
|
|
|
memset(quant, 0, sizeof(*quant));
|
|
|
|
|
|
|
|
quant->load_lum_quantiser_matrix = 1;
|
|
|
|
for (i = 0; i < 64; i++)
|
|
|
|
quant->lum_quantiser_matrix[i] = dqt->table[fh->Tq[0]].Q[i];
|
|
|
|
|
|
|
|
if (fh->Nf > 1) {
|
|
|
|
quant->load_chroma_quantiser_matrix = 1;
|
|
|
|
for (i = 0; i < 64; i++)
|
|
|
|
quant->chroma_quantiser_matrix[i] =
|
|
|
|
dqt->table[fh->Tq[1]].Q[i];
|
|
|
|
}
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
} else if (index == 1) {
|
|
|
|
// Write huffman tables.
|
|
|
|
JPEGRawScanHeader *sh = &priv->scan.header;
|
|
|
|
JPEGRawHuffmanTableSpecification *dht = &priv->huffman_tables;
|
|
|
|
VAHuffmanTableBufferJPEGBaseline *huff;
|
|
|
|
|
|
|
|
if (*data_len < sizeof(*huff))
|
|
|
|
return AVERROR(ENOSPC);
|
|
|
|
*type = VAHuffmanTableBufferType;
|
|
|
|
*data_len = sizeof(*huff);
|
|
|
|
|
|
|
|
huff = (VAHuffmanTableBufferJPEGBaseline*)data;
|
|
|
|
memset(huff, 0, sizeof(*huff));
|
|
|
|
|
|
|
|
for (t = 0; t < 1 + (sh->Ns > 1); t++) {
|
|
|
|
const JPEGRawHuffmanTable *ht;
|
|
|
|
|
|
|
|
huff->load_huffman_table[t] = 1;
|
|
|
|
|
|
|
|
ht = &dht->table[2 * t];
|
|
|
|
for (i = k = 0; i < 16; i++)
|
|
|
|
k += (huff->huffman_table[t].num_dc_codes[i] = ht->L[i]);
|
|
|
|
av_assert0(k <= sizeof(huff->huffman_table[t].dc_values));
|
|
|
|
for (i = 0; i < k; i++)
|
|
|
|
huff->huffman_table[t].dc_values[i] = ht->V[i];
|
|
|
|
|
|
|
|
ht = &dht->table[2 * t + 1];
|
|
|
|
for (i = k = 0; i < 16; i++)
|
|
|
|
k += (huff->huffman_table[t].num_ac_codes[i] = ht->L[i]);
|
|
|
|
av_assert0(k <= sizeof(huff->huffman_table[t].ac_values));
|
|
|
|
for (i = 0; i < k; i++)
|
|
|
|
huff->huffman_table[t].ac_values[i] = ht->V[i];
|
|
|
|
}
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
} else {
|
|
|
|
return AVERROR_EOF;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vaapi_encode_mjpeg_init_picture_params(AVCodecContext *avctx,
|
|
|
|
VAAPIEncodePicture *pic)
|
|
|
|
{
|
|
|
|
VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
|
|
|
|
JPEGRawFrameHeader *fh = &priv->frame_header;
|
|
|
|
JPEGRawScanHeader *sh = &priv->scan.header;
|
|
|
|
VAEncPictureParameterBufferJPEG *vpic = pic->codec_picture_params;
|
|
|
|
const AVPixFmtDescriptor *desc;
|
2019-06-07 23:45:33 +02:00
|
|
|
const uint8_t components_rgb[3] = { 'R', 'G', 'B' };
|
|
|
|
const uint8_t components_yuv[3] = { 1, 2, 3 };
|
2018-09-19 00:30:54 +02:00
|
|
|
const uint8_t *components;
|
|
|
|
int t, i, quant_scale, len;
|
|
|
|
|
2018-12-20 22:39:56 +02:00
|
|
|
av_assert0(pic->type == PICTURE_TYPE_IDR);
|
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
desc = av_pix_fmt_desc_get(priv->common.input_frames->sw_format);
|
|
|
|
av_assert0(desc);
|
|
|
|
if (desc->flags & AV_PIX_FMT_FLAG_RGB)
|
2019-06-07 23:45:33 +02:00
|
|
|
components = components_rgb;
|
2016-03-24 01:50:47 +02:00
|
|
|
else
|
2019-06-07 23:45:33 +02:00
|
|
|
components = components_yuv;
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
// Frame header.
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
fh->P = 8;
|
|
|
|
fh->Y = avctx->height;
|
|
|
|
fh->X = avctx->width;
|
|
|
|
fh->Nf = desc->nb_components;
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
for (i = 0; i < fh->Nf; i++) {
|
|
|
|
fh->C[i] = components[i];
|
|
|
|
fh->H[i] = 1 + (i == 0 ? desc->log2_chroma_w : 0);
|
|
|
|
fh->V[i] = 1 + (i == 0 ? desc->log2_chroma_h : 0);
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
fh->Tq[i] = !!i;
|
2016-03-24 01:50:47 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
fh->Lf = 8 + 3 * fh->Nf;
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
// JFIF header.
|
|
|
|
if (priv->jfif) {
|
|
|
|
JPEGRawApplicationData *app = &priv->jfif_header;
|
|
|
|
AVRational sar = pic->input_image->sample_aspect_ratio;
|
|
|
|
int sar_w, sar_h;
|
|
|
|
PutByteContext pbc;
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
bytestream2_init_writer(&pbc, priv->jfif_data,
|
|
|
|
sizeof(priv->jfif_data));
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
bytestream2_put_buffer(&pbc, "JFIF", 5);
|
|
|
|
bytestream2_put_be16(&pbc, 0x0102);
|
|
|
|
bytestream2_put_byte(&pbc, 0);
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
av_reduce(&sar_w, &sar_h, sar.num, sar.den, 65535);
|
|
|
|
if (sar_w && sar_h) {
|
|
|
|
bytestream2_put_be16(&pbc, sar_w);
|
|
|
|
bytestream2_put_be16(&pbc, sar_h);
|
2016-03-24 01:50:47 +02:00
|
|
|
} else {
|
2018-09-19 00:30:54 +02:00
|
|
|
bytestream2_put_be16(&pbc, 1);
|
|
|
|
bytestream2_put_be16(&pbc, 1);
|
2016-03-24 01:50:47 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
bytestream2_put_byte(&pbc, 0);
|
|
|
|
bytestream2_put_byte(&pbc, 0);
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
av_assert0(bytestream2_get_bytes_left_p(&pbc) == 0);
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
app->Lp = 2 + sizeof(priv->jfif_data);
|
|
|
|
app->Ap = priv->jfif_data;
|
|
|
|
app->Ap_ref = NULL;
|
2016-03-24 01:50:47 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
// Quantisation tables.
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
if (priv->quality < 50)
|
|
|
|
quant_scale = 5000 / priv->quality;
|
|
|
|
else
|
|
|
|
quant_scale = 200 - 2 * priv->quality;
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
len = 2;
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
for (t = 0; t < 1 + (fh->Nf > 1); t++) {
|
|
|
|
JPEGRawQuantisationTable *quant = &priv->quant_tables.table[t];
|
|
|
|
const uint8_t *data = t == 0 ?
|
|
|
|
vaapi_encode_mjpeg_quant_luminance :
|
|
|
|
vaapi_encode_mjpeg_quant_chrominance;
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
quant->Pq = 0;
|
|
|
|
quant->Tq = t;
|
|
|
|
for (i = 0; i < 64; i++)
|
|
|
|
quant->Q[i] = av_clip(data[i] * quant_scale / 100, 1, 255);
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
len += 65;
|
|
|
|
}
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
priv->quant_tables.Lq = len;
|
|
|
|
|
|
|
|
// Huffman tables.
|
|
|
|
|
|
|
|
len = 2;
|
|
|
|
|
|
|
|
for (t = 0; t < 2 + 2 * (fh->Nf > 1); t++) {
|
|
|
|
JPEGRawHuffmanTable *huff = &priv->huffman_tables.table[t];
|
|
|
|
const uint8_t *lengths, *values;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
switch (t) {
|
|
|
|
case 0:
|
2021-02-10 02:19:44 +02:00
|
|
|
lengths = ff_mjpeg_bits_dc_luminance + 1;
|
|
|
|
values = ff_mjpeg_val_dc;
|
2018-09-19 00:30:54 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
2021-02-10 02:19:44 +02:00
|
|
|
lengths = ff_mjpeg_bits_ac_luminance + 1;
|
|
|
|
values = ff_mjpeg_val_ac_luminance;
|
2018-09-19 00:30:54 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2021-02-10 02:19:44 +02:00
|
|
|
lengths = ff_mjpeg_bits_dc_chrominance + 1;
|
|
|
|
values = ff_mjpeg_val_dc;
|
2018-09-19 00:30:54 +02:00
|
|
|
break;
|
|
|
|
case 3:
|
2021-02-10 02:19:44 +02:00
|
|
|
lengths = ff_mjpeg_bits_ac_chrominance + 1;
|
|
|
|
values = ff_mjpeg_val_ac_chrominance;
|
2018-09-19 00:30:54 +02:00
|
|
|
break;
|
|
|
|
}
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
huff->Tc = t % 2;
|
|
|
|
huff->Th = t / 2;
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
for (i = k = 0; i < 16; i++)
|
|
|
|
k += (huff->L[i] = lengths[i]);
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
for (i = 0; i < k; i++)
|
|
|
|
huff->V[i] = values[i];
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
len += 17 + k;
|
2016-03-24 01:50:47 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
priv->huffman_tables.Lh = len;
|
|
|
|
|
|
|
|
// Scan header.
|
|
|
|
|
|
|
|
sh->Ns = fh->Nf;
|
|
|
|
|
|
|
|
for (i = 0; i < fh->Nf; i++) {
|
|
|
|
sh->Cs[i] = fh->C[i];
|
|
|
|
sh->Td[i] = i > 0;
|
|
|
|
sh->Ta[i] = i > 0;
|
|
|
|
}
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
sh->Ss = 0;
|
|
|
|
sh->Se = 63;
|
|
|
|
sh->Ah = 0;
|
|
|
|
sh->Al = 0;
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
sh->Ls = 6 + 2 * sh->Ns;
|
2016-03-24 01:50:47 +02:00
|
|
|
|
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
*vpic = (VAEncPictureParameterBufferJPEG) {
|
|
|
|
.reconstructed_picture = pic->recon_surface,
|
|
|
|
.coded_buf = pic->output_buffer,
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
.picture_width = fh->X,
|
|
|
|
.picture_height = fh->Y,
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
.pic_flags.bits = {
|
|
|
|
.profile = 0,
|
|
|
|
.progressive = 0,
|
|
|
|
.huffman = 1,
|
|
|
|
.interleaved = 0,
|
|
|
|
.differential = 0,
|
|
|
|
},
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
.sample_bit_depth = fh->P,
|
|
|
|
.num_scan = 1,
|
|
|
|
.num_components = fh->Nf,
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
// The driver modifies the provided quantisation tables according
|
|
|
|
// to this quality value; the middle value of 50 makes that the
|
|
|
|
// identity so that they are used unchanged.
|
|
|
|
.quality = 50,
|
|
|
|
};
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
for (i = 0; i < fh->Nf; i++) {
|
|
|
|
vpic->component_id[i] = fh->C[i];
|
|
|
|
vpic->quantiser_table_selector[i] = fh->Tq[i];
|
|
|
|
}
|
2016-03-24 01:50:47 +02:00
|
|
|
|
|
|
|
pic->nb_slices = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vaapi_encode_mjpeg_init_slice_params(AVCodecContext *avctx,
|
|
|
|
VAAPIEncodePicture *pic,
|
|
|
|
VAAPIEncodeSlice *slice)
|
|
|
|
{
|
2018-09-19 00:30:54 +02:00
|
|
|
VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
|
|
|
|
JPEGRawScanHeader *sh = &priv->scan.header;
|
2016-03-24 01:50:47 +02:00
|
|
|
VAEncSliceParameterBufferJPEG *vslice = slice->codec_slice_params;
|
|
|
|
int i;
|
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
*vslice = (VAEncSliceParameterBufferJPEG) {
|
|
|
|
.restart_interval = 0,
|
|
|
|
.num_components = sh->Ns,
|
|
|
|
};
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
for (i = 0; i < sh->Ns; i++) {
|
|
|
|
vslice->components[i].component_selector = sh->Cs[i];
|
|
|
|
vslice->components[i].dc_table_selector = sh->Td[i];
|
|
|
|
vslice->components[i].ac_table_selector = sh->Ta[i];
|
2016-03-24 01:50:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-17 08:41:50 +02:00
|
|
|
static av_cold int vaapi_encode_mjpeg_get_encoder_caps(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
VAAPIEncodeContext *ctx = avctx->priv_data;
|
|
|
|
const AVPixFmtDescriptor *desc;
|
|
|
|
|
|
|
|
desc = av_pix_fmt_desc_get(ctx->input_frames->sw_format);
|
|
|
|
av_assert0(desc);
|
|
|
|
|
|
|
|
ctx->surface_width = FFALIGN(avctx->width, 8 << desc->log2_chroma_w);
|
|
|
|
ctx->surface_height = FFALIGN(avctx->height, 8 << desc->log2_chroma_h);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-18 15:55:26 +02:00
|
|
|
static av_cold int vaapi_encode_mjpeg_configure(AVCodecContext *avctx)
|
2016-03-24 01:50:47 +02:00
|
|
|
{
|
|
|
|
VAAPIEncodeContext *ctx = avctx->priv_data;
|
2018-08-23 01:44:36 +02:00
|
|
|
VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
|
2018-09-19 00:30:54 +02:00
|
|
|
int err;
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2019-02-10 21:47:01 +02:00
|
|
|
priv->quality = ctx->rc_quality;
|
2016-03-24 01:50:47 +02:00
|
|
|
if (priv->quality < 1 || priv->quality > 100) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Invalid quality value %d "
|
|
|
|
"(must be 1-100).\n", priv->quality);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2016-09-18 15:59:59 +02:00
|
|
|
// Hack: the implementation calls the JPEG image header (which we
|
|
|
|
// will use in the same way as a slice header) generic "raw data".
|
|
|
|
// Therefore, if after the packed header capability check we have
|
|
|
|
// PACKED_HEADER_RAW_DATA available, rewrite it as
|
|
|
|
// PACKED_HEADER_SLICE so that the header-writing code can do the
|
|
|
|
// right thing.
|
|
|
|
if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_RAW_DATA) {
|
|
|
|
ctx->va_packed_headers &= ~VA_ENC_PACKED_HEADER_RAW_DATA;
|
|
|
|
ctx->va_packed_headers |= VA_ENC_PACKED_HEADER_SLICE;
|
|
|
|
}
|
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_MJPEG, avctx);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2016-03-24 01:50:47 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-19 00:30:40 +02:00
|
|
|
static const VAAPIEncodeProfile vaapi_encode_mjpeg_profiles[] = {
|
2018-09-19 00:30:54 +02:00
|
|
|
{ FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
|
|
|
|
8, 1, 0, 0, VAProfileJPEGBaseline },
|
2018-09-19 00:30:40 +02:00
|
|
|
{ FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
|
|
|
|
8, 3, 1, 1, VAProfileJPEGBaseline },
|
2018-09-19 00:30:54 +02:00
|
|
|
{ FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
|
|
|
|
8, 3, 1, 0, VAProfileJPEGBaseline },
|
|
|
|
{ FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
|
|
|
|
8, 3, 0, 0, VAProfileJPEGBaseline },
|
2018-09-19 00:30:40 +02:00
|
|
|
{ FF_PROFILE_UNKNOWN }
|
|
|
|
};
|
|
|
|
|
2016-07-03 10:09:36 +02:00
|
|
|
static const VAAPIEncodeType vaapi_encode_type_mjpeg = {
|
2018-09-19 00:30:40 +02:00
|
|
|
.profiles = vaapi_encode_mjpeg_profiles,
|
|
|
|
|
2018-12-20 22:39:56 +02:00
|
|
|
.flags = FLAG_CONSTANT_QUALITY_ONLY |
|
|
|
|
FLAG_INTRA_ONLY,
|
2018-09-23 23:53:00 +02:00
|
|
|
|
2022-03-17 08:41:50 +02:00
|
|
|
.get_encoder_caps = &vaapi_encode_mjpeg_get_encoder_caps,
|
2016-09-18 15:55:26 +02:00
|
|
|
.configure = &vaapi_encode_mjpeg_configure,
|
2016-03-24 01:50:47 +02:00
|
|
|
|
2019-02-10 21:47:01 +02:00
|
|
|
.default_quality = 80,
|
|
|
|
|
2016-03-24 01:50:47 +02:00
|
|
|
.picture_params_size = sizeof(VAEncPictureParameterBufferJPEG),
|
|
|
|
.init_picture_params = &vaapi_encode_mjpeg_init_picture_params,
|
|
|
|
|
|
|
|
.slice_params_size = sizeof(VAEncSliceParameterBufferJPEG),
|
|
|
|
.init_slice_params = &vaapi_encode_mjpeg_init_slice_params,
|
|
|
|
|
|
|
|
.slice_header_type = VAEncPackedHeaderRawData,
|
|
|
|
.write_slice_header = &vaapi_encode_mjpeg_write_image_header,
|
|
|
|
|
|
|
|
.write_extra_buffer = &vaapi_encode_mjpeg_write_extra_buffer,
|
|
|
|
};
|
|
|
|
|
|
|
|
static av_cold int vaapi_encode_mjpeg_init(AVCodecContext *avctx)
|
|
|
|
{
|
2016-09-18 15:55:26 +02:00
|
|
|
VAAPIEncodeContext *ctx = avctx->priv_data;
|
|
|
|
|
|
|
|
ctx->codec = &vaapi_encode_type_mjpeg;
|
|
|
|
|
2016-09-18 15:59:59 +02:00
|
|
|
// The JPEG image header - see note above.
|
2018-09-19 00:30:48 +02:00
|
|
|
ctx->desired_packed_headers =
|
2016-09-18 15:59:59 +02:00
|
|
|
VA_ENC_PACKED_HEADER_RAW_DATA;
|
|
|
|
|
2016-09-18 15:55:26 +02:00
|
|
|
return ff_vaapi_encode_init(avctx);
|
2016-03-24 01:50:47 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 00:30:54 +02:00
|
|
|
static av_cold int vaapi_encode_mjpeg_close(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
|
|
|
|
|
2020-07-06 00:34:25 +02:00
|
|
|
ff_cbs_fragment_free(&priv->current_fragment);
|
2018-09-19 00:30:54 +02:00
|
|
|
ff_cbs_close(&priv->cbc);
|
|
|
|
|
|
|
|
return ff_vaapi_encode_close(avctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(VAAPIEncodeMJPEGContext, x)
|
|
|
|
#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
|
|
|
|
static const AVOption vaapi_encode_mjpeg_options[] = {
|
|
|
|
VAAPI_ENCODE_COMMON_OPTIONS,
|
|
|
|
|
|
|
|
{ "jfif", "Include JFIF header",
|
|
|
|
OFFSET(jfif), AV_OPT_TYPE_BOOL,
|
|
|
|
{ .i64 = 0 }, 0, 1, FLAGS },
|
|
|
|
{ "huffman", "Include huffman tables",
|
|
|
|
OFFSET(huffman), AV_OPT_TYPE_BOOL,
|
|
|
|
{ .i64 = 1 }, 0, 1, FLAGS },
|
|
|
|
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2022-03-16 22:26:11 +02:00
|
|
|
static const FFCodecDefault vaapi_encode_mjpeg_defaults[] = {
|
2018-09-19 00:30:45 +02:00
|
|
|
{ "b", "0" },
|
2016-03-24 01:50:47 +02:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass vaapi_encode_mjpeg_class = {
|
|
|
|
.class_name = "mjpeg_vaapi",
|
|
|
|
.item_name = av_default_item_name,
|
2018-09-19 00:30:54 +02:00
|
|
|
.option = vaapi_encode_mjpeg_options,
|
2016-03-24 01:50:47 +02:00
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_mjpeg_vaapi_encoder = {
|
|
|
|
.p.name = "mjpeg_vaapi",
|
2022-08-29 13:38:02 +02:00
|
|
|
CODEC_LONG_NAME("MJPEG (VAAPI)"),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_MJPEG,
|
2018-08-23 01:44:36 +02:00
|
|
|
.priv_data_size = sizeof(VAAPIEncodeMJPEGContext),
|
2016-03-24 01:50:47 +02:00
|
|
|
.init = &vaapi_encode_mjpeg_init,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_RECEIVE_PACKET_CB(&ff_vaapi_encode_receive_packet),
|
2018-09-19 00:30:54 +02:00
|
|
|
.close = &vaapi_encode_mjpeg_close,
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.priv_class = &vaapi_encode_mjpeg_class,
|
|
|
|
.p.capabilities = AV_CODEC_CAP_HARDWARE | AV_CODEC_CAP_DR1,
|
2022-07-09 22:25:41 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
|
|
|
|
FF_CODEC_CAP_INIT_CLEANUP,
|
2016-03-24 01:50:47 +02:00
|
|
|
.defaults = vaapi_encode_mjpeg_defaults,
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.pix_fmts = (const enum AVPixelFormat[]) {
|
2016-03-24 01:50:47 +02:00
|
|
|
AV_PIX_FMT_VAAPI,
|
|
|
|
AV_PIX_FMT_NONE,
|
|
|
|
},
|
2020-04-13 17:33:17 +02:00
|
|
|
.hw_configs = ff_vaapi_encode_hw_configs,
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.wrapper_name = "vaapi",
|
2016-03-24 01:50:47 +02:00
|
|
|
};
|