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.

431 lines
15KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; 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/pixfmt.h"
  25. #include "avcodec.h"
  26. #include "internal.h"
  27. #include "jpegtables.h"
  28. #include "mjpeg.h"
  29. #include "put_bits.h"
  30. #include "vaapi_encode.h"
  31. // Standard JPEG quantisation tables, in zigzag order.
  32. static const unsigned char vaapi_encode_mjpeg_quant_luminance[64] = {
  33. 16, 11, 12, 14, 12, 10, 16, 14,
  34. 13, 14, 18, 17, 16, 19, 24, 40,
  35. 26, 24, 22, 22, 24, 49, 35, 37,
  36. 29, 40, 58, 51, 61, 60, 57, 51,
  37. 56, 55, 64, 72, 92, 78, 64, 68,
  38. 87, 69, 55, 56, 80, 109, 81, 87,
  39. 95, 98, 103, 104, 103, 62, 77, 113,
  40. 121, 112, 100, 120, 92, 101, 103, 99,
  41. };
  42. static const unsigned char vaapi_encode_mjpeg_quant_chrominance[64] = {
  43. 17, 18, 18, 24, 21, 24, 47, 26,
  44. 26, 47, 99, 66, 56, 66, 99, 99,
  45. 99, 99, 99, 99, 99, 99, 99, 99,
  46. 99, 99, 99, 99, 99, 99, 99, 99,
  47. 99, 99, 99, 99, 99, 99, 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. };
  52. typedef struct VAAPIEncodeMJPEGContext {
  53. int quality;
  54. int component_subsample_h[3];
  55. int component_subsample_v[3];
  56. VAQMatrixBufferJPEG quant_tables;
  57. VAHuffmanTableBufferJPEGBaseline huffman_tables;
  58. } VAAPIEncodeMJPEGContext;
  59. static av_cold void vaapi_encode_mjpeg_copy_huffman(unsigned char *dst_lengths,
  60. unsigned char *dst_values,
  61. const unsigned char *src_lengths,
  62. const unsigned char *src_values)
  63. {
  64. int i, mt;
  65. ++src_lengths;
  66. mt = 0;
  67. for (i = 0; i < 16; i++)
  68. mt += (dst_lengths[i] = src_lengths[i]);
  69. for (i = 0; i < mt; i++)
  70. dst_values[i] = src_values[i];
  71. }
  72. static av_cold void vaapi_encode_mjpeg_init_tables(AVCodecContext *avctx)
  73. {
  74. VAAPIEncodeContext *ctx = avctx->priv_data;
  75. VAAPIEncodeMJPEGContext *priv = ctx->priv_data;
  76. VAQMatrixBufferJPEG *quant = &priv->quant_tables;
  77. VAHuffmanTableBufferJPEGBaseline *huff = &priv->huffman_tables;
  78. int i;
  79. quant->load_lum_quantiser_matrix = 1;
  80. quant->load_chroma_quantiser_matrix = 1;
  81. for (i = 0; i < 64; i++) {
  82. quant->lum_quantiser_matrix[i] =
  83. vaapi_encode_mjpeg_quant_luminance[i];
  84. quant->chroma_quantiser_matrix[i] =
  85. vaapi_encode_mjpeg_quant_chrominance[i];
  86. }
  87. huff->load_huffman_table[0] = 1;
  88. vaapi_encode_mjpeg_copy_huffman(huff->huffman_table[0].num_dc_codes,
  89. huff->huffman_table[0].dc_values,
  90. avpriv_mjpeg_bits_dc_luminance,
  91. avpriv_mjpeg_val_dc);
  92. vaapi_encode_mjpeg_copy_huffman(huff->huffman_table[0].num_ac_codes,
  93. huff->huffman_table[0].ac_values,
  94. avpriv_mjpeg_bits_ac_luminance,
  95. avpriv_mjpeg_val_ac_luminance);
  96. memset(huff->huffman_table[0].pad, 0, sizeof(huff->huffman_table[0].pad));
  97. huff->load_huffman_table[1] = 1;
  98. vaapi_encode_mjpeg_copy_huffman(huff->huffman_table[1].num_dc_codes,
  99. huff->huffman_table[1].dc_values,
  100. avpriv_mjpeg_bits_dc_chrominance,
  101. avpriv_mjpeg_val_dc);
  102. vaapi_encode_mjpeg_copy_huffman(huff->huffman_table[1].num_ac_codes,
  103. huff->huffman_table[1].ac_values,
  104. avpriv_mjpeg_bits_ac_chrominance,
  105. avpriv_mjpeg_val_ac_chrominance);
  106. memset(huff->huffman_table[1].pad, 0, sizeof(huff->huffman_table[1].pad));
  107. }
  108. static void vaapi_encode_mjpeg_write_marker(PutBitContext *pbc, int marker)
  109. {
  110. put_bits(pbc, 8, 0xff);
  111. put_bits(pbc, 8, marker);
  112. }
  113. static int vaapi_encode_mjpeg_write_image_header(AVCodecContext *avctx,
  114. VAAPIEncodePicture *pic,
  115. VAAPIEncodeSlice *slice,
  116. char *data, size_t *data_len)
  117. {
  118. VAAPIEncodeContext *ctx = avctx->priv_data;
  119. VAEncPictureParameterBufferJPEG *vpic = pic->codec_picture_params;
  120. VAEncSliceParameterBufferJPEG *vslice = slice->codec_slice_params;
  121. VAAPIEncodeMJPEGContext *priv = ctx->priv_data;
  122. PutBitContext pbc;
  123. int t, i, quant_scale;
  124. init_put_bits(&pbc, data, *data_len);
  125. vaapi_encode_mjpeg_write_marker(&pbc, SOI);
  126. // Quantisation table coefficients are scaled for quality by the driver,
  127. // so we also need to do it ourselves here so that headers match.
  128. if (priv->quality < 50)
  129. quant_scale = 5000 / priv->quality;
  130. else
  131. quant_scale = 200 - 2 * priv->quality;
  132. for (t = 0; t < 2; t++) {
  133. int q;
  134. vaapi_encode_mjpeg_write_marker(&pbc, DQT);
  135. put_bits(&pbc, 16, 3 + 64); // Lq
  136. put_bits(&pbc, 4, 0); // Pq
  137. put_bits(&pbc, 4, t); // Tq
  138. for (i = 0; i < 64; i++) {
  139. q = i[t ? priv->quant_tables.chroma_quantiser_matrix
  140. : priv->quant_tables.lum_quantiser_matrix];
  141. q = (q * quant_scale) / 100;
  142. if (q < 1) q = 1;
  143. if (q > 255) q = 255;
  144. put_bits(&pbc, 8, q);
  145. }
  146. }
  147. vaapi_encode_mjpeg_write_marker(&pbc, SOF0);
  148. put_bits(&pbc, 16, 8 + 3 * vpic->num_components); // Lf
  149. put_bits(&pbc, 8, vpic->sample_bit_depth); // P
  150. put_bits(&pbc, 16, vpic->picture_height); // Y
  151. put_bits(&pbc, 16, vpic->picture_width); // X
  152. put_bits(&pbc, 8, vpic->num_components); // Nf
  153. for (i = 0; i < vpic->num_components; i++) {
  154. put_bits(&pbc, 8, vpic->component_id[i]); // Ci
  155. put_bits(&pbc, 4, priv->component_subsample_h[i]); // Hi
  156. put_bits(&pbc, 4, priv->component_subsample_v[i]); // Vi
  157. put_bits(&pbc, 8, vpic->quantiser_table_selector[i]); // Tqi
  158. }
  159. for (t = 0; t < 4; t++) {
  160. int mt;
  161. unsigned char *lengths, *values;
  162. vaapi_encode_mjpeg_write_marker(&pbc, DHT);
  163. if ((t & 1) == 0) {
  164. lengths = priv->huffman_tables.huffman_table[t / 2].num_dc_codes;
  165. values = priv->huffman_tables.huffman_table[t / 2].dc_values;
  166. } else {
  167. lengths = priv->huffman_tables.huffman_table[t / 2].num_ac_codes;
  168. values = priv->huffman_tables.huffman_table[t / 2].ac_values;
  169. }
  170. mt = 0;
  171. for (i = 0; i < 16; i++)
  172. mt += lengths[i];
  173. put_bits(&pbc, 16, 2 + 17 + mt); // Lh
  174. put_bits(&pbc, 4, t & 1); // Tc
  175. put_bits(&pbc, 4, t / 2); // Th
  176. for (i = 0; i < 16; i++)
  177. put_bits(&pbc, 8, lengths[i]);
  178. for (i = 0; i < mt; i++)
  179. put_bits(&pbc, 8, values[i]);
  180. }
  181. vaapi_encode_mjpeg_write_marker(&pbc, SOS);
  182. av_assert0(vpic->num_components == vslice->num_components);
  183. put_bits(&pbc, 16, 6 + 2 * vslice->num_components); // Ls
  184. put_bits(&pbc, 8, vslice->num_components); // Ns
  185. for (i = 0; i < vslice->num_components; i++) {
  186. put_bits(&pbc, 8, vslice->components[i].component_selector); // Csj
  187. put_bits(&pbc, 4, vslice->components[i].dc_table_selector); // Tdj
  188. put_bits(&pbc, 4, vslice->components[i].ac_table_selector); // Taj
  189. }
  190. put_bits(&pbc, 8, 0); // Ss
  191. put_bits(&pbc, 8, 63); // Se
  192. put_bits(&pbc, 4, 0); // Ah
  193. put_bits(&pbc, 4, 0); // Al
  194. *data_len = put_bits_count(&pbc);
  195. flush_put_bits(&pbc);
  196. return 0;
  197. }
  198. static int vaapi_encode_mjpeg_write_extra_buffer(AVCodecContext *avctx,
  199. VAAPIEncodePicture *pic,
  200. int index, int *type,
  201. char *data, size_t *data_len)
  202. {
  203. VAAPIEncodeContext *ctx = avctx->priv_data;
  204. VAAPIEncodeMJPEGContext *priv = ctx->priv_data;
  205. if (index == 0) {
  206. // Write quantisation tables.
  207. if (*data_len < sizeof(priv->quant_tables))
  208. return AVERROR(EINVAL);
  209. *type = VAQMatrixBufferType;
  210. memcpy(data, &priv->quant_tables,
  211. *data_len = sizeof(priv->quant_tables));
  212. } else if (index == 1) {
  213. // Write huffman tables.
  214. if (*data_len < sizeof(priv->huffman_tables))
  215. return AVERROR(EINVAL);
  216. *type = VAHuffmanTableBufferType;
  217. memcpy(data, &priv->huffman_tables,
  218. *data_len = sizeof(priv->huffman_tables));
  219. } else {
  220. return AVERROR_EOF;
  221. }
  222. return 0;
  223. }
  224. static int vaapi_encode_mjpeg_init_picture_params(AVCodecContext *avctx,
  225. VAAPIEncodePicture *pic)
  226. {
  227. VAAPIEncodeContext *ctx = avctx->priv_data;
  228. VAEncPictureParameterBufferJPEG *vpic = pic->codec_picture_params;
  229. VAAPIEncodeMJPEGContext *priv = ctx->priv_data;
  230. vpic->reconstructed_picture = pic->recon_surface;
  231. vpic->coded_buf = pic->output_buffer;
  232. vpic->picture_width = avctx->width;
  233. vpic->picture_height = avctx->height;
  234. vpic->pic_flags.bits.profile = 0;
  235. vpic->pic_flags.bits.progressive = 0;
  236. vpic->pic_flags.bits.huffman = 1;
  237. vpic->pic_flags.bits.interleaved = 0;
  238. vpic->pic_flags.bits.differential = 0;
  239. vpic->sample_bit_depth = 8;
  240. vpic->num_scan = 1;
  241. vpic->num_components = 3;
  242. vpic->component_id[0] = 1;
  243. vpic->component_id[1] = 2;
  244. vpic->component_id[2] = 3;
  245. priv->component_subsample_h[0] = 2;
  246. priv->component_subsample_v[0] = 2;
  247. priv->component_subsample_h[1] = 1;
  248. priv->component_subsample_v[1] = 1;
  249. priv->component_subsample_h[2] = 1;
  250. priv->component_subsample_v[2] = 1;
  251. vpic->quantiser_table_selector[0] = 0;
  252. vpic->quantiser_table_selector[1] = 1;
  253. vpic->quantiser_table_selector[2] = 1;
  254. vpic->quality = priv->quality;
  255. pic->nb_slices = 1;
  256. return 0;
  257. }
  258. static int vaapi_encode_mjpeg_init_slice_params(AVCodecContext *avctx,
  259. VAAPIEncodePicture *pic,
  260. VAAPIEncodeSlice *slice)
  261. {
  262. VAEncPictureParameterBufferJPEG *vpic = pic->codec_picture_params;
  263. VAEncSliceParameterBufferJPEG *vslice = slice->codec_slice_params;
  264. int i;
  265. vslice->restart_interval = 0;
  266. vslice->num_components = vpic->num_components;
  267. for (i = 0; i < vslice->num_components; i++) {
  268. vslice->components[i].component_selector = i + 1;
  269. vslice->components[i].dc_table_selector = (i > 0);
  270. vslice->components[i].ac_table_selector = (i > 0);
  271. }
  272. return 0;
  273. }
  274. static av_cold int vaapi_encode_mjpeg_configure(AVCodecContext *avctx)
  275. {
  276. VAAPIEncodeContext *ctx = avctx->priv_data;
  277. VAAPIEncodeMJPEGContext *priv = ctx->priv_data;
  278. priv->quality = avctx->global_quality;
  279. if (priv->quality < 1 || priv->quality > 100) {
  280. av_log(avctx, AV_LOG_ERROR, "Invalid quality value %d "
  281. "(must be 1-100).\n", priv->quality);
  282. return AVERROR(EINVAL);
  283. }
  284. // Hack: the implementation calls the JPEG image header (which we
  285. // will use in the same way as a slice header) generic "raw data".
  286. // Therefore, if after the packed header capability check we have
  287. // PACKED_HEADER_RAW_DATA available, rewrite it as
  288. // PACKED_HEADER_SLICE so that the header-writing code can do the
  289. // right thing.
  290. if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_RAW_DATA) {
  291. ctx->va_packed_headers &= ~VA_ENC_PACKED_HEADER_RAW_DATA;
  292. ctx->va_packed_headers |= VA_ENC_PACKED_HEADER_SLICE;
  293. }
  294. vaapi_encode_mjpeg_init_tables(avctx);
  295. return 0;
  296. }
  297. static const VAAPIEncodeType vaapi_encode_type_mjpeg = {
  298. .priv_data_size = sizeof(VAAPIEncodeMJPEGContext),
  299. .configure = &vaapi_encode_mjpeg_configure,
  300. .picture_params_size = sizeof(VAEncPictureParameterBufferJPEG),
  301. .init_picture_params = &vaapi_encode_mjpeg_init_picture_params,
  302. .slice_params_size = sizeof(VAEncSliceParameterBufferJPEG),
  303. .init_slice_params = &vaapi_encode_mjpeg_init_slice_params,
  304. .slice_header_type = VAEncPackedHeaderRawData,
  305. .write_slice_header = &vaapi_encode_mjpeg_write_image_header,
  306. .write_extra_buffer = &vaapi_encode_mjpeg_write_extra_buffer,
  307. };
  308. static av_cold int vaapi_encode_mjpeg_init(AVCodecContext *avctx)
  309. {
  310. VAAPIEncodeContext *ctx = avctx->priv_data;
  311. ctx->codec = &vaapi_encode_type_mjpeg;
  312. ctx->va_profile = VAProfileJPEGBaseline;
  313. ctx->va_entrypoint = VAEntrypointEncPicture;
  314. ctx->va_rt_format = VA_RT_FORMAT_YUV420;
  315. ctx->va_rc_mode = VA_RC_CQP;
  316. // The JPEG image header - see note above.
  317. ctx->va_packed_headers =
  318. VA_ENC_PACKED_HEADER_RAW_DATA;
  319. ctx->surface_width = FFALIGN(avctx->width, 8);
  320. ctx->surface_height = FFALIGN(avctx->height, 8);
  321. return ff_vaapi_encode_init(avctx);
  322. }
  323. static const AVCodecDefault vaapi_encode_mjpeg_defaults[] = {
  324. { "global_quality", "80" },
  325. { NULL },
  326. };
  327. static const AVClass vaapi_encode_mjpeg_class = {
  328. .class_name = "mjpeg_vaapi",
  329. .item_name = av_default_item_name,
  330. .version = LIBAVUTIL_VERSION_INT,
  331. };
  332. AVCodec ff_mjpeg_vaapi_encoder = {
  333. .name = "mjpeg_vaapi",
  334. .long_name = NULL_IF_CONFIG_SMALL("MJPEG (VAAPI)"),
  335. .type = AVMEDIA_TYPE_VIDEO,
  336. .id = AV_CODEC_ID_MJPEG,
  337. .priv_data_size = sizeof(VAAPIEncodeContext),
  338. .init = &vaapi_encode_mjpeg_init,
  339. .encode2 = &ff_vaapi_encode2,
  340. .close = &ff_vaapi_encode_close,
  341. .priv_class = &vaapi_encode_mjpeg_class,
  342. .defaults = vaapi_encode_mjpeg_defaults,
  343. .pix_fmts = (const enum AVPixelFormat[]) {
  344. AV_PIX_FMT_VAAPI,
  345. AV_PIX_FMT_NONE,
  346. },
  347. };