You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

575 lines
18KB

  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(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(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(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(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(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_reset(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_rgb[3] = { 'R', 'G', 'B' };
  196. const uint8_t components_yuv[3] = { 1, 2, 3 };
  197. const uint8_t *components;
  198. int t, i, quant_scale, len;
  199. av_assert0(pic->type == PICTURE_TYPE_IDR);
  200. desc = av_pix_fmt_desc_get(priv->common.input_frames->sw_format);
  201. av_assert0(desc);
  202. if (desc->flags & AV_PIX_FMT_FLAG_RGB)
  203. components = components_rgb;
  204. else
  205. components = components_yuv;
  206. // Frame header.
  207. fh->P = 8;
  208. fh->Y = avctx->height;
  209. fh->X = avctx->width;
  210. fh->Nf = desc->nb_components;
  211. for (i = 0; i < fh->Nf; i++) {
  212. fh->C[i] = components[i];
  213. fh->H[i] = 1 + (i == 0 ? desc->log2_chroma_w : 0);
  214. fh->V[i] = 1 + (i == 0 ? desc->log2_chroma_h : 0);
  215. fh->Tq[i] = !!i;
  216. }
  217. fh->Lf = 8 + 3 * fh->Nf;
  218. // JFIF header.
  219. if (priv->jfif) {
  220. JPEGRawApplicationData *app = &priv->jfif_header;
  221. AVRational sar = pic->input_image->sample_aspect_ratio;
  222. int sar_w, sar_h;
  223. PutByteContext pbc;
  224. bytestream2_init_writer(&pbc, priv->jfif_data,
  225. sizeof(priv->jfif_data));
  226. bytestream2_put_buffer(&pbc, "JFIF", 5);
  227. bytestream2_put_be16(&pbc, 0x0102);
  228. bytestream2_put_byte(&pbc, 0);
  229. av_reduce(&sar_w, &sar_h, sar.num, sar.den, 65535);
  230. if (sar_w && sar_h) {
  231. bytestream2_put_be16(&pbc, sar_w);
  232. bytestream2_put_be16(&pbc, sar_h);
  233. } else {
  234. bytestream2_put_be16(&pbc, 1);
  235. bytestream2_put_be16(&pbc, 1);
  236. }
  237. bytestream2_put_byte(&pbc, 0);
  238. bytestream2_put_byte(&pbc, 0);
  239. av_assert0(bytestream2_get_bytes_left_p(&pbc) == 0);
  240. app->Lp = 2 + sizeof(priv->jfif_data);
  241. app->Ap = priv->jfif_data;
  242. app->Ap_ref = NULL;
  243. }
  244. // Quantisation tables.
  245. if (priv->quality < 50)
  246. quant_scale = 5000 / priv->quality;
  247. else
  248. quant_scale = 200 - 2 * priv->quality;
  249. len = 2;
  250. for (t = 0; t < 1 + (fh->Nf > 1); t++) {
  251. JPEGRawQuantisationTable *quant = &priv->quant_tables.table[t];
  252. const uint8_t *data = t == 0 ?
  253. vaapi_encode_mjpeg_quant_luminance :
  254. vaapi_encode_mjpeg_quant_chrominance;
  255. quant->Pq = 0;
  256. quant->Tq = t;
  257. for (i = 0; i < 64; i++)
  258. quant->Q[i] = av_clip(data[i] * quant_scale / 100, 1, 255);
  259. len += 65;
  260. }
  261. priv->quant_tables.Lq = len;
  262. // Huffman tables.
  263. len = 2;
  264. for (t = 0; t < 2 + 2 * (fh->Nf > 1); t++) {
  265. JPEGRawHuffmanTable *huff = &priv->huffman_tables.table[t];
  266. const uint8_t *lengths, *values;
  267. int k;
  268. switch (t) {
  269. case 0:
  270. lengths = avpriv_mjpeg_bits_dc_luminance + 1;
  271. values = avpriv_mjpeg_val_dc;
  272. break;
  273. case 1:
  274. lengths = avpriv_mjpeg_bits_ac_luminance + 1;
  275. values = avpriv_mjpeg_val_ac_luminance;
  276. break;
  277. case 2:
  278. lengths = avpriv_mjpeg_bits_dc_chrominance + 1;
  279. values = avpriv_mjpeg_val_dc;
  280. break;
  281. case 3:
  282. lengths = avpriv_mjpeg_bits_ac_chrominance + 1;
  283. values = avpriv_mjpeg_val_ac_chrominance;
  284. break;
  285. }
  286. huff->Tc = t % 2;
  287. huff->Th = t / 2;
  288. for (i = k = 0; i < 16; i++)
  289. k += (huff->L[i] = lengths[i]);
  290. for (i = 0; i < k; i++)
  291. huff->V[i] = values[i];
  292. len += 17 + k;
  293. }
  294. priv->huffman_tables.Lh = len;
  295. // Scan header.
  296. sh->Ns = fh->Nf;
  297. for (i = 0; i < fh->Nf; i++) {
  298. sh->Cs[i] = fh->C[i];
  299. sh->Td[i] = i > 0;
  300. sh->Ta[i] = i > 0;
  301. }
  302. sh->Ss = 0;
  303. sh->Se = 63;
  304. sh->Ah = 0;
  305. sh->Al = 0;
  306. sh->Ls = 6 + 2 * sh->Ns;
  307. *vpic = (VAEncPictureParameterBufferJPEG) {
  308. .reconstructed_picture = pic->recon_surface,
  309. .coded_buf = pic->output_buffer,
  310. .picture_width = fh->X,
  311. .picture_height = fh->Y,
  312. .pic_flags.bits = {
  313. .profile = 0,
  314. .progressive = 0,
  315. .huffman = 1,
  316. .interleaved = 0,
  317. .differential = 0,
  318. },
  319. .sample_bit_depth = fh->P,
  320. .num_scan = 1,
  321. .num_components = fh->Nf,
  322. // The driver modifies the provided quantisation tables according
  323. // to this quality value; the middle value of 50 makes that the
  324. // identity so that they are used unchanged.
  325. .quality = 50,
  326. };
  327. for (i = 0; i < fh->Nf; i++) {
  328. vpic->component_id[i] = fh->C[i];
  329. vpic->quantiser_table_selector[i] = fh->Tq[i];
  330. }
  331. pic->nb_slices = 1;
  332. return 0;
  333. }
  334. static int vaapi_encode_mjpeg_init_slice_params(AVCodecContext *avctx,
  335. VAAPIEncodePicture *pic,
  336. VAAPIEncodeSlice *slice)
  337. {
  338. VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
  339. JPEGRawScanHeader *sh = &priv->scan.header;
  340. VAEncSliceParameterBufferJPEG *vslice = slice->codec_slice_params;
  341. int i;
  342. *vslice = (VAEncSliceParameterBufferJPEG) {
  343. .restart_interval = 0,
  344. .num_components = sh->Ns,
  345. };
  346. for (i = 0; i < sh->Ns; i++) {
  347. vslice->components[i].component_selector = sh->Cs[i];
  348. vslice->components[i].dc_table_selector = sh->Td[i];
  349. vslice->components[i].ac_table_selector = sh->Ta[i];
  350. }
  351. return 0;
  352. }
  353. static av_cold int vaapi_encode_mjpeg_configure(AVCodecContext *avctx)
  354. {
  355. VAAPIEncodeContext *ctx = avctx->priv_data;
  356. VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
  357. int err;
  358. priv->quality = ctx->rc_quality;
  359. if (priv->quality < 1 || priv->quality > 100) {
  360. av_log(avctx, AV_LOG_ERROR, "Invalid quality value %d "
  361. "(must be 1-100).\n", priv->quality);
  362. return AVERROR(EINVAL);
  363. }
  364. // Hack: the implementation calls the JPEG image header (which we
  365. // will use in the same way as a slice header) generic "raw data".
  366. // Therefore, if after the packed header capability check we have
  367. // PACKED_HEADER_RAW_DATA available, rewrite it as
  368. // PACKED_HEADER_SLICE so that the header-writing code can do the
  369. // right thing.
  370. if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_RAW_DATA) {
  371. ctx->va_packed_headers &= ~VA_ENC_PACKED_HEADER_RAW_DATA;
  372. ctx->va_packed_headers |= VA_ENC_PACKED_HEADER_SLICE;
  373. }
  374. err = ff_cbs_init(&priv->cbc, AV_CODEC_ID_MJPEG, avctx);
  375. if (err < 0)
  376. return err;
  377. return 0;
  378. }
  379. static const VAAPIEncodeProfile vaapi_encode_mjpeg_profiles[] = {
  380. { FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
  381. 8, 1, 0, 0, VAProfileJPEGBaseline },
  382. { FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
  383. 8, 3, 1, 1, VAProfileJPEGBaseline },
  384. { FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
  385. 8, 3, 1, 0, VAProfileJPEGBaseline },
  386. { FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
  387. 8, 3, 0, 0, VAProfileJPEGBaseline },
  388. { FF_PROFILE_UNKNOWN }
  389. };
  390. static const VAAPIEncodeType vaapi_encode_type_mjpeg = {
  391. .profiles = vaapi_encode_mjpeg_profiles,
  392. .flags = FLAG_CONSTANT_QUALITY_ONLY |
  393. FLAG_INTRA_ONLY,
  394. .configure = &vaapi_encode_mjpeg_configure,
  395. .default_quality = 80,
  396. .picture_params_size = sizeof(VAEncPictureParameterBufferJPEG),
  397. .init_picture_params = &vaapi_encode_mjpeg_init_picture_params,
  398. .slice_params_size = sizeof(VAEncSliceParameterBufferJPEG),
  399. .init_slice_params = &vaapi_encode_mjpeg_init_slice_params,
  400. .slice_header_type = VAEncPackedHeaderRawData,
  401. .write_slice_header = &vaapi_encode_mjpeg_write_image_header,
  402. .write_extra_buffer = &vaapi_encode_mjpeg_write_extra_buffer,
  403. };
  404. static av_cold int vaapi_encode_mjpeg_init(AVCodecContext *avctx)
  405. {
  406. VAAPIEncodeContext *ctx = avctx->priv_data;
  407. ctx->codec = &vaapi_encode_type_mjpeg;
  408. // The JPEG image header - see note above.
  409. ctx->desired_packed_headers =
  410. VA_ENC_PACKED_HEADER_RAW_DATA;
  411. ctx->surface_width = FFALIGN(avctx->width, 8);
  412. ctx->surface_height = FFALIGN(avctx->height, 8);
  413. return ff_vaapi_encode_init(avctx);
  414. }
  415. static av_cold int vaapi_encode_mjpeg_close(AVCodecContext *avctx)
  416. {
  417. VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
  418. ff_cbs_fragment_free(&priv->current_fragment);
  419. ff_cbs_close(&priv->cbc);
  420. return ff_vaapi_encode_close(avctx);
  421. }
  422. #define OFFSET(x) offsetof(VAAPIEncodeMJPEGContext, x)
  423. #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
  424. static const AVOption vaapi_encode_mjpeg_options[] = {
  425. VAAPI_ENCODE_COMMON_OPTIONS,
  426. { "jfif", "Include JFIF header",
  427. OFFSET(jfif), AV_OPT_TYPE_BOOL,
  428. { .i64 = 0 }, 0, 1, FLAGS },
  429. { "huffman", "Include huffman tables",
  430. OFFSET(huffman), AV_OPT_TYPE_BOOL,
  431. { .i64 = 1 }, 0, 1, FLAGS },
  432. { NULL },
  433. };
  434. static const AVCodecDefault vaapi_encode_mjpeg_defaults[] = {
  435. { "b", "0" },
  436. { NULL },
  437. };
  438. static const AVClass vaapi_encode_mjpeg_class = {
  439. .class_name = "mjpeg_vaapi",
  440. .item_name = av_default_item_name,
  441. .option = vaapi_encode_mjpeg_options,
  442. .version = LIBAVUTIL_VERSION_INT,
  443. };
  444. AVCodec ff_mjpeg_vaapi_encoder = {
  445. .name = "mjpeg_vaapi",
  446. .long_name = NULL_IF_CONFIG_SMALL("MJPEG (VAAPI)"),
  447. .type = AVMEDIA_TYPE_VIDEO,
  448. .id = AV_CODEC_ID_MJPEG,
  449. .priv_data_size = sizeof(VAAPIEncodeMJPEGContext),
  450. .init = &vaapi_encode_mjpeg_init,
  451. .receive_packet = &ff_vaapi_encode_receive_packet,
  452. .close = &vaapi_encode_mjpeg_close,
  453. .priv_class = &vaapi_encode_mjpeg_class,
  454. .capabilities = AV_CODEC_CAP_HARDWARE | AV_CODEC_CAP_DR1,
  455. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  456. .defaults = vaapi_encode_mjpeg_defaults,
  457. .pix_fmts = (const enum AVPixelFormat[]) {
  458. AV_PIX_FMT_VAAPI,
  459. AV_PIX_FMT_NONE,
  460. },
  461. .hw_configs = ff_vaapi_encode_hw_configs,
  462. .wrapper_name = "vaapi",
  463. };