vaapi_encode_mjpeg.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  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.
  8. *
  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.
  13. *
  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
  17. */
  18. #include <va/va.h>
  19. #include <va/va_enc_jpeg.h>
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/common.h"
  22. #include "libavutil/internal.h"
  23. #include "libavutil/opt.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. #include "cbs.h"
  28. #include "cbs_jpeg.h"
  29. #include "internal.h"
  30. #include "jpegtables.h"
  31. #include "mjpeg.h"
  32. #include "put_bits.h"
  33. #include "vaapi_encode.h"
  34. // Standard JPEG quantisation tables, in zigzag order.
  35. static const unsigned char vaapi_encode_mjpeg_quant_luminance[64] = {
  36. 16, 11, 12, 14, 12, 10, 16, 14,
  37. 13, 14, 18, 17, 16, 19, 24, 40,
  38. 26, 24, 22, 22, 24, 49, 35, 37,
  39. 29, 40, 58, 51, 61, 60, 57, 51,
  40. 56, 55, 64, 72, 92, 78, 64, 68,
  41. 87, 69, 55, 56, 80, 109, 81, 87,
  42. 95, 98, 103, 104, 103, 62, 77, 113,
  43. 121, 112, 100, 120, 92, 101, 103, 99,
  44. };
  45. static const unsigned char vaapi_encode_mjpeg_quant_chrominance[64] = {
  46. 17, 18, 18, 24, 21, 24, 47, 26,
  47. 26, 47, 99, 66, 56, 66, 99, 99,
  48. 99, 99, 99, 99, 99, 99, 99, 99,
  49. 99, 99, 99, 99, 99, 99, 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. };
  55. typedef struct VAAPIEncodeMJPEGContext {
  56. VAAPIEncodeContext common;
  57. // User options.
  58. int jfif;
  59. int huffman;
  60. // Derived settings.
  61. int quality;
  62. uint8_t jfif_data[14];
  63. // Writer structures.
  64. JPEGRawFrameHeader frame_header;
  65. JPEGRawScan scan;
  66. JPEGRawApplicationData jfif_header;
  67. JPEGRawQuantisationTableSpecification quant_tables;
  68. JPEGRawHuffmanTableSpecification huffman_tables;
  69. CodedBitstreamContext *cbc;
  70. CodedBitstreamFragment current_fragment;
  71. } VAAPIEncodeMJPEGContext;
  72. static int vaapi_encode_mjpeg_write_image_header(AVCodecContext *avctx,
  73. VAAPIEncodePicture *pic,
  74. VAAPIEncodeSlice *slice,
  75. char *data, size_t *data_len)
  76. {
  77. VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
  78. CodedBitstreamFragment *frag = &priv->current_fragment;
  79. int err;
  80. if (priv->jfif) {
  81. err = ff_cbs_insert_unit_content(priv->cbc, frag, -1,
  82. JPEG_MARKER_APPN + 0,
  83. &priv->jfif_header, NULL);
  84. if (err < 0)
  85. goto fail;
  86. }
  87. err = ff_cbs_insert_unit_content(priv->cbc, frag, -1,
  88. JPEG_MARKER_DQT,
  89. &priv->quant_tables, NULL);
  90. if (err < 0)
  91. goto fail;
  92. err = ff_cbs_insert_unit_content(priv->cbc, frag, -1,
  93. JPEG_MARKER_SOF0,
  94. &priv->frame_header, NULL);
  95. if (err < 0)
  96. goto fail;
  97. if (priv->huffman) {
  98. err = ff_cbs_insert_unit_content(priv->cbc, frag, -1,
  99. JPEG_MARKER_DHT,
  100. &priv->huffman_tables, NULL);
  101. if (err < 0)
  102. goto fail;
  103. }
  104. err = ff_cbs_insert_unit_content(priv->cbc, frag, -1,
  105. JPEG_MARKER_SOS,
  106. &priv->scan, NULL);
  107. if (err < 0)
  108. goto fail;
  109. err = ff_cbs_write_fragment_data(priv->cbc, frag);
  110. if (err < 0) {
  111. av_log(avctx, AV_LOG_ERROR, "Failed to write image header.\n");
  112. goto fail;
  113. }
  114. if (*data_len < 8 * frag->data_size) {
  115. av_log(avctx, AV_LOG_ERROR, "Image header too large: "
  116. "%zu < %zu.\n", *data_len, 8 * frag->data_size);
  117. err = AVERROR(ENOSPC);
  118. goto fail;
  119. }
  120. // Remove the EOI at the end of the fragment.
  121. memcpy(data, frag->data, frag->data_size - 2);
  122. *data_len = 8 * (frag->data_size - 2);
  123. err = 0;
  124. fail:
  125. ff_cbs_fragment_uninit(priv->cbc, frag);
  126. return err;
  127. }
  128. static int vaapi_encode_mjpeg_write_extra_buffer(AVCodecContext *avctx,
  129. VAAPIEncodePicture *pic,
  130. int index, int *type,
  131. char *data, size_t *data_len)
  132. {
  133. VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
  134. int t, i, k;
  135. if (index == 0) {
  136. // Write quantisation tables.
  137. JPEGRawFrameHeader *fh = &priv->frame_header;
  138. JPEGRawQuantisationTableSpecification *dqt = &priv->quant_tables;
  139. VAQMatrixBufferJPEG *quant;
  140. if (*data_len < sizeof(*quant))
  141. return AVERROR(ENOSPC);
  142. *type = VAQMatrixBufferType;
  143. *data_len = sizeof(*quant);
  144. quant = (VAQMatrixBufferJPEG*)data;
  145. memset(quant, 0, sizeof(*quant));
  146. quant->load_lum_quantiser_matrix = 1;
  147. for (i = 0; i < 64; i++)
  148. quant->lum_quantiser_matrix[i] = dqt->table[fh->Tq[0]].Q[i];
  149. if (fh->Nf > 1) {
  150. quant->load_chroma_quantiser_matrix = 1;
  151. for (i = 0; i < 64; i++)
  152. quant->chroma_quantiser_matrix[i] =
  153. dqt->table[fh->Tq[1]].Q[i];
  154. }
  155. } else if (index == 1) {
  156. // Write huffman tables.
  157. JPEGRawScanHeader *sh = &priv->scan.header;
  158. JPEGRawHuffmanTableSpecification *dht = &priv->huffman_tables;
  159. VAHuffmanTableBufferJPEGBaseline *huff;
  160. if (*data_len < sizeof(*huff))
  161. return AVERROR(ENOSPC);
  162. *type = VAHuffmanTableBufferType;
  163. *data_len = sizeof(*huff);
  164. huff = (VAHuffmanTableBufferJPEGBaseline*)data;
  165. memset(huff, 0, sizeof(*huff));
  166. for (t = 0; t < 1 + (sh->Ns > 1); t++) {
  167. const JPEGRawHuffmanTable *ht;
  168. huff->load_huffman_table[t] = 1;
  169. ht = &dht->table[2 * t];
  170. for (i = k = 0; i < 16; i++)
  171. k += (huff->huffman_table[t].num_dc_codes[i] = ht->L[i]);
  172. av_assert0(k <= sizeof(huff->huffman_table[t].dc_values));
  173. for (i = 0; i < k; i++)
  174. huff->huffman_table[t].dc_values[i] = ht->V[i];
  175. ht = &dht->table[2 * t + 1];
  176. for (i = k = 0; i < 16; i++)
  177. k += (huff->huffman_table[t].num_ac_codes[i] = ht->L[i]);
  178. av_assert0(k <= sizeof(huff->huffman_table[t].ac_values));
  179. for (i = 0; i < k; i++)
  180. huff->huffman_table[t].ac_values[i] = ht->V[i];
  181. }
  182. } else {
  183. return AVERROR_EOF;
  184. }
  185. return 0;
  186. }
  187. static int vaapi_encode_mjpeg_init_picture_params(AVCodecContext *avctx,
  188. VAAPIEncodePicture *pic)
  189. {
  190. VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
  191. JPEGRawFrameHeader *fh = &priv->frame_header;
  192. JPEGRawScanHeader *sh = &priv->scan.header;
  193. VAEncPictureParameterBufferJPEG *vpic = pic->codec_picture_params;
  194. const AVPixFmtDescriptor *desc;
  195. const uint8_t *components;
  196. int t, i, quant_scale, len;
  197. desc = av_pix_fmt_desc_get(priv->common.input_frames->sw_format);
  198. av_assert0(desc);
  199. if (desc->flags & AV_PIX_FMT_FLAG_RGB)
  200. components = (uint8_t[3]) { 'R', 'G', 'B' };
  201. else
  202. components = (uint8_t[3]) { 1, 2, 3 };
  203. // Frame header.
  204. fh->P = 8;
  205. fh->Y = avctx->height;
  206. fh->X = avctx->width;
  207. fh->Nf = desc->nb_components;
  208. for (i = 0; i < fh->Nf; i++) {
  209. fh->C[i] = components[i];
  210. fh->H[i] = 1 + (i == 0 ? desc->log2_chroma_w : 0);
  211. fh->V[i] = 1 + (i == 0 ? desc->log2_chroma_h : 0);
  212. fh->Tq[i] = !!i;
  213. }
  214. fh->Lf = 8 + 3 * fh->Nf;
  215. // JFIF header.
  216. if (priv->jfif) {
  217. JPEGRawApplicationData *app = &priv->jfif_header;
  218. AVRational sar = pic->input_image->sample_aspect_ratio;
  219. int sar_w, sar_h;
  220. PutByteContext pbc;
  221. bytestream2_init_writer(&pbc, priv->jfif_data,
  222. sizeof(priv->jfif_data));
  223. bytestream2_put_buffer(&pbc, "JFIF", 5);
  224. bytestream2_put_be16(&pbc, 0x0102);
  225. bytestream2_put_byte(&pbc, 0);
  226. av_reduce(&sar_w, &sar_h, sar.num, sar.den, 65535);
  227. if (sar_w && sar_h) {
  228. bytestream2_put_be16(&pbc, sar_w);
  229. bytestream2_put_be16(&pbc, sar_h);
  230. } else {
  231. bytestream2_put_be16(&pbc, 1);
  232. bytestream2_put_be16(&pbc, 1);
  233. }
  234. bytestream2_put_byte(&pbc, 0);
  235. bytestream2_put_byte(&pbc, 0);
  236. av_assert0(bytestream2_get_bytes_left_p(&pbc) == 0);
  237. app->Lp = 2 + sizeof(priv->jfif_data);
  238. app->Ap = priv->jfif_data;
  239. app->Ap_ref = NULL;
  240. }
  241. // Quantisation tables.
  242. if (priv->quality < 50)
  243. quant_scale = 5000 / priv->quality;
  244. else
  245. quant_scale = 200 - 2 * priv->quality;
  246. len = 2;
  247. for (t = 0; t < 1 + (fh->Nf > 1); t++) {
  248. JPEGRawQuantisationTable *quant = &priv->quant_tables.table[t];
  249. const uint8_t *data = t == 0 ?
  250. vaapi_encode_mjpeg_quant_luminance :
  251. vaapi_encode_mjpeg_quant_chrominance;
  252. quant->Pq = 0;
  253. quant->Tq = t;
  254. for (i = 0; i < 64; i++)
  255. quant->Q[i] = av_clip(data[i] * quant_scale / 100, 1, 255);
  256. len += 65;
  257. }
  258. priv->quant_tables.Lq = len;
  259. // Huffman tables.
  260. len = 2;
  261. for (t = 0; t < 2 + 2 * (fh->Nf > 1); t++) {
  262. JPEGRawHuffmanTable *huff = &priv->huffman_tables.table[t];
  263. const uint8_t *lengths, *values;
  264. int k;
  265. switch (t) {
  266. case 0:
  267. lengths = avpriv_mjpeg_bits_dc_luminance + 1;
  268. values = avpriv_mjpeg_val_dc;
  269. break;
  270. case 1:
  271. lengths = avpriv_mjpeg_bits_ac_luminance + 1;
  272. values = avpriv_mjpeg_val_ac_luminance;
  273. break;
  274. case 2:
  275. lengths = avpriv_mjpeg_bits_dc_chrominance + 1;
  276. values = avpriv_mjpeg_val_dc;
  277. break;
  278. case 3:
  279. lengths = avpriv_mjpeg_bits_ac_chrominance + 1;
  280. values = avpriv_mjpeg_val_ac_chrominance;
  281. break;
  282. }
  283. huff->Tc = t % 2;
  284. huff->Th = t / 2;
  285. for (i = k = 0; i < 16; i++)
  286. k += (huff->L[i] = lengths[i]);
  287. for (i = 0; i < k; i++)
  288. huff->V[i] = values[i];
  289. len += 17 + k;
  290. }
  291. priv->huffman_tables.Lh = len;
  292. // Scan header.
  293. sh->Ns = fh->Nf;
  294. for (i = 0; i < fh->Nf; i++) {
  295. sh->Cs[i] = fh->C[i];
  296. sh->Td[i] = i > 0;
  297. sh->Ta[i] = i > 0;
  298. }
  299. sh->Ss = 0;
  300. sh->Se = 63;
  301. sh->Ah = 0;
  302. sh->Al = 0;
  303. sh->Ls = 6 + 2 * sh->Ns;
  304. *vpic = (VAEncPictureParameterBufferJPEG) {
  305. .reconstructed_picture = pic->recon_surface,
  306. .coded_buf = pic->output_buffer,
  307. .picture_width = fh->X,
  308. .picture_height = fh->Y,
  309. .pic_flags.bits = {
  310. .profile = 0,
  311. .progressive = 0,
  312. .huffman = 1,
  313. .interleaved = 0,
  314. .differential = 0,
  315. },
  316. .sample_bit_depth = fh->P,
  317. .num_scan = 1,
  318. .num_components = fh->Nf,
  319. // The driver modifies the provided quantisation tables according
  320. // to this quality value; the middle value of 50 makes that the
  321. // identity so that they are used unchanged.
  322. .quality = 50,
  323. };
  324. for (i = 0; i < fh->Nf; i++) {
  325. vpic->component_id[i] = fh->C[i];
  326. vpic->quantiser_table_selector[i] = fh->Tq[i];
  327. }
  328. pic->nb_slices = 1;
  329. return 0;
  330. }
  331. static int vaapi_encode_mjpeg_init_slice_params(AVCodecContext *avctx,
  332. VAAPIEncodePicture *pic,
  333. VAAPIEncodeSlice *slice)
  334. {
  335. VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
  336. JPEGRawScanHeader *sh = &priv->scan.header;
  337. VAEncSliceParameterBufferJPEG *vslice = slice->codec_slice_params;
  338. int i;
  339. *vslice = (VAEncSliceParameterBufferJPEG) {
  340. .restart_interval = 0,
  341. .num_components = sh->Ns,
  342. };
  343. for (i = 0; i < sh->Ns; i++) {
  344. vslice->components[i].component_selector = sh->Cs[i];
  345. vslice->components[i].dc_table_selector = sh->Td[i];
  346. vslice->components[i].ac_table_selector = sh->Ta[i];
  347. }
  348. return 0;
  349. }
  350. static av_cold int vaapi_encode_mjpeg_configure(AVCodecContext *avctx)
  351. {
  352. VAAPIEncodeContext *ctx = avctx->priv_data;
  353. VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
  354. int err;
  355. priv->quality = avctx->global_quality;
  356. if (priv->quality < 1 || priv->quality > 100) {
  357. av_log(avctx, AV_LOG_ERROR, "Invalid quality value %d "
  358. "(must be 1-100).\n", priv->quality);
  359. return AVERROR(EINVAL);
  360. }
  361. // Hack: the implementation calls the JPEG image header (which we
  362. // will use in the same way as a slice header) generic "raw data".
  363. // Therefore, if after the packed header capability check we have
  364. // PACKED_HEADER_RAW_DATA available, rewrite it as
  365. // PACKED_HEADER_SLICE so that the header-writing code can do the
  366. // right thing.
  367. if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_RAW_DATA) {
  368. ctx->va_packed_headers &= ~VA_ENC_PACKED_HEADER_RAW_DATA;
  369. ctx->va_packed_headers |= VA_ENC_PACKED_HEADER_SLICE;
  370. }
  371. err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_MJPEG, avctx);
  372. if (err < 0)
  373. return err;
  374. return 0;
  375. }
  376. static const VAAPIEncodeProfile vaapi_encode_mjpeg_profiles[] = {
  377. { FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
  378. 8, 1, 0, 0, VAProfileJPEGBaseline },
  379. { FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
  380. 8, 3, 1, 1, VAProfileJPEGBaseline },
  381. { FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
  382. 8, 3, 1, 0, VAProfileJPEGBaseline },
  383. { FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
  384. 8, 3, 0, 0, VAProfileJPEGBaseline },
  385. { FF_PROFILE_UNKNOWN }
  386. };
  387. static const VAAPIEncodeType vaapi_encode_type_mjpeg = {
  388. .profiles = vaapi_encode_mjpeg_profiles,
  389. .configure = &vaapi_encode_mjpeg_configure,
  390. .picture_params_size = sizeof(VAEncPictureParameterBufferJPEG),
  391. .init_picture_params = &vaapi_encode_mjpeg_init_picture_params,
  392. .slice_params_size = sizeof(VAEncSliceParameterBufferJPEG),
  393. .init_slice_params = &vaapi_encode_mjpeg_init_slice_params,
  394. .slice_header_type = VAEncPackedHeaderRawData,
  395. .write_slice_header = &vaapi_encode_mjpeg_write_image_header,
  396. .write_extra_buffer = &vaapi_encode_mjpeg_write_extra_buffer,
  397. };
  398. static av_cold int vaapi_encode_mjpeg_init(AVCodecContext *avctx)
  399. {
  400. VAAPIEncodeContext *ctx = avctx->priv_data;
  401. ctx->codec = &vaapi_encode_type_mjpeg;
  402. // The JPEG image header - see note above.
  403. ctx->desired_packed_headers =
  404. VA_ENC_PACKED_HEADER_RAW_DATA;
  405. ctx->surface_width = FFALIGN(avctx->width, 8);
  406. ctx->surface_height = FFALIGN(avctx->height, 8);
  407. return ff_vaapi_encode_init(avctx);
  408. }
  409. static av_cold int vaapi_encode_mjpeg_close(AVCodecContext *avctx)
  410. {
  411. VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
  412. ff_cbs_close(&priv->cbc);
  413. return ff_vaapi_encode_close(avctx);
  414. }
  415. #define OFFSET(x) offsetof(VAAPIEncodeMJPEGContext, x)
  416. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  417. static const AVOption vaapi_encode_mjpeg_options[] = {
  418. VAAPI_ENCODE_COMMON_OPTIONS,
  419. { "jfif", "Include JFIF header",
  420. OFFSET(jfif), AV_OPT_TYPE_BOOL,
  421. { .i64 = 0 }, 0, 1, FLAGS },
  422. { "huffman", "Include huffman tables",
  423. OFFSET(huffman), AV_OPT_TYPE_BOOL,
  424. { .i64 = 1 }, 0, 1, FLAGS },
  425. { NULL },
  426. };
  427. static const AVCodecDefault vaapi_encode_mjpeg_defaults[] = {
  428. { "global_quality", "80" },
  429. { "b", "0" },
  430. { "g", "1" },
  431. { NULL },
  432. };
  433. static const AVClass vaapi_encode_mjpeg_class = {
  434. .class_name = "mjpeg_vaapi",
  435. .item_name = av_default_item_name,
  436. .option = vaapi_encode_mjpeg_options,
  437. .version = LIBAVUTIL_VERSION_INT,
  438. };
  439. AVCodec ff_mjpeg_vaapi_encoder = {
  440. .name = "mjpeg_vaapi",
  441. .long_name = NULL_IF_CONFIG_SMALL("MJPEG (VAAPI)"),
  442. .type = AVMEDIA_TYPE_VIDEO,
  443. .id = AV_CODEC_ID_MJPEG,
  444. .priv_data_size = sizeof(VAAPIEncodeMJPEGContext),
  445. .init = &vaapi_encode_mjpeg_init,
  446. .encode2 = &ff_vaapi_encode2,
  447. .close = &vaapi_encode_mjpeg_close,
  448. .priv_class = &vaapi_encode_mjpeg_class,
  449. .capabilities = AV_CODEC_CAP_HARDWARE,
  450. .defaults = vaapi_encode_mjpeg_defaults,
  451. .pix_fmts = (const enum AVPixelFormat[]) {
  452. AV_PIX_FMT_VAAPI,
  453. AV_PIX_FMT_NONE,
  454. },
  455. .wrapper_name = "vaapi",
  456. };