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.

432 lines
15KB

  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/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. VAAPIEncodeContext common;
  54. int quality;
  55. int component_subsample_h[3];
  56. int component_subsample_v[3];
  57. VAQMatrixBufferJPEG quant_tables;
  58. VAHuffmanTableBufferJPEGBaseline huffman_tables;
  59. } VAAPIEncodeMJPEGContext;
  60. static av_cold void vaapi_encode_mjpeg_copy_huffman(unsigned char *dst_lengths,
  61. unsigned char *dst_values,
  62. const unsigned char *src_lengths,
  63. const unsigned char *src_values)
  64. {
  65. int i, mt;
  66. ++src_lengths;
  67. mt = 0;
  68. for (i = 0; i < 16; i++)
  69. mt += (dst_lengths[i] = src_lengths[i]);
  70. for (i = 0; i < mt; i++)
  71. dst_values[i] = src_values[i];
  72. }
  73. static av_cold void vaapi_encode_mjpeg_init_tables(AVCodecContext *avctx)
  74. {
  75. VAAPIEncodeMJPEGContext *priv = avctx->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. VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
  119. VAEncPictureParameterBufferJPEG *vpic = pic->codec_picture_params;
  120. VAEncSliceParameterBufferJPEG *vslice = slice->codec_slice_params;
  121. PutBitContext pbc;
  122. int t, i, quant_scale;
  123. init_put_bits(&pbc, data, *data_len);
  124. vaapi_encode_mjpeg_write_marker(&pbc, SOI);
  125. // Quantisation table coefficients are scaled for quality by the driver,
  126. // so we also need to do it ourselves here so that headers match.
  127. if (priv->quality < 50)
  128. quant_scale = 5000 / priv->quality;
  129. else
  130. quant_scale = 200 - 2 * priv->quality;
  131. for (t = 0; t < 2; t++) {
  132. int q;
  133. vaapi_encode_mjpeg_write_marker(&pbc, DQT);
  134. put_bits(&pbc, 16, 3 + 64); // Lq
  135. put_bits(&pbc, 4, 0); // Pq
  136. put_bits(&pbc, 4, t); // Tq
  137. for (i = 0; i < 64; i++) {
  138. q = i[t ? priv->quant_tables.chroma_quantiser_matrix
  139. : priv->quant_tables.lum_quantiser_matrix];
  140. q = (q * quant_scale) / 100;
  141. if (q < 1) q = 1;
  142. if (q > 255) q = 255;
  143. put_bits(&pbc, 8, q);
  144. }
  145. }
  146. vaapi_encode_mjpeg_write_marker(&pbc, SOF0);
  147. put_bits(&pbc, 16, 8 + 3 * vpic->num_components); // Lf
  148. put_bits(&pbc, 8, vpic->sample_bit_depth); // P
  149. put_bits(&pbc, 16, vpic->picture_height); // Y
  150. put_bits(&pbc, 16, vpic->picture_width); // X
  151. put_bits(&pbc, 8, vpic->num_components); // Nf
  152. for (i = 0; i < vpic->num_components; i++) {
  153. put_bits(&pbc, 8, vpic->component_id[i]); // Ci
  154. put_bits(&pbc, 4, priv->component_subsample_h[i]); // Hi
  155. put_bits(&pbc, 4, priv->component_subsample_v[i]); // Vi
  156. put_bits(&pbc, 8, vpic->quantiser_table_selector[i]); // Tqi
  157. }
  158. for (t = 0; t < 4; t++) {
  159. int mt;
  160. unsigned char *lengths, *values;
  161. vaapi_encode_mjpeg_write_marker(&pbc, DHT);
  162. if ((t & 1) == 0) {
  163. lengths = priv->huffman_tables.huffman_table[t / 2].num_dc_codes;
  164. values = priv->huffman_tables.huffman_table[t / 2].dc_values;
  165. } else {
  166. lengths = priv->huffman_tables.huffman_table[t / 2].num_ac_codes;
  167. values = priv->huffman_tables.huffman_table[t / 2].ac_values;
  168. }
  169. mt = 0;
  170. for (i = 0; i < 16; i++)
  171. mt += lengths[i];
  172. put_bits(&pbc, 16, 2 + 17 + mt); // Lh
  173. put_bits(&pbc, 4, t & 1); // Tc
  174. put_bits(&pbc, 4, t / 2); // Th
  175. for (i = 0; i < 16; i++)
  176. put_bits(&pbc, 8, lengths[i]);
  177. for (i = 0; i < mt; i++)
  178. put_bits(&pbc, 8, values[i]);
  179. }
  180. vaapi_encode_mjpeg_write_marker(&pbc, SOS);
  181. av_assert0(vpic->num_components == vslice->num_components);
  182. put_bits(&pbc, 16, 6 + 2 * vslice->num_components); // Ls
  183. put_bits(&pbc, 8, vslice->num_components); // Ns
  184. for (i = 0; i < vslice->num_components; i++) {
  185. put_bits(&pbc, 8, vslice->components[i].component_selector); // Csj
  186. put_bits(&pbc, 4, vslice->components[i].dc_table_selector); // Tdj
  187. put_bits(&pbc, 4, vslice->components[i].ac_table_selector); // Taj
  188. }
  189. put_bits(&pbc, 8, 0); // Ss
  190. put_bits(&pbc, 8, 63); // Se
  191. put_bits(&pbc, 4, 0); // Ah
  192. put_bits(&pbc, 4, 0); // Al
  193. *data_len = put_bits_count(&pbc);
  194. flush_put_bits(&pbc);
  195. return 0;
  196. }
  197. static int vaapi_encode_mjpeg_write_extra_buffer(AVCodecContext *avctx,
  198. VAAPIEncodePicture *pic,
  199. int index, int *type,
  200. char *data, size_t *data_len)
  201. {
  202. VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
  203. if (index == 0) {
  204. // Write quantisation tables.
  205. if (*data_len < sizeof(priv->quant_tables))
  206. return AVERROR(EINVAL);
  207. *type = VAQMatrixBufferType;
  208. memcpy(data, &priv->quant_tables,
  209. *data_len = sizeof(priv->quant_tables));
  210. } else if (index == 1) {
  211. // Write huffman tables.
  212. if (*data_len < sizeof(priv->huffman_tables))
  213. return AVERROR(EINVAL);
  214. *type = VAHuffmanTableBufferType;
  215. memcpy(data, &priv->huffman_tables,
  216. *data_len = sizeof(priv->huffman_tables));
  217. } else {
  218. return AVERROR_EOF;
  219. }
  220. return 0;
  221. }
  222. static int vaapi_encode_mjpeg_init_picture_params(AVCodecContext *avctx,
  223. VAAPIEncodePicture *pic)
  224. {
  225. VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
  226. VAEncPictureParameterBufferJPEG *vpic = pic->codec_picture_params;
  227. vpic->reconstructed_picture = pic->recon_surface;
  228. vpic->coded_buf = pic->output_buffer;
  229. vpic->picture_width = avctx->width;
  230. vpic->picture_height = avctx->height;
  231. vpic->pic_flags.bits.profile = 0;
  232. vpic->pic_flags.bits.progressive = 0;
  233. vpic->pic_flags.bits.huffman = 1;
  234. vpic->pic_flags.bits.interleaved = 0;
  235. vpic->pic_flags.bits.differential = 0;
  236. vpic->sample_bit_depth = 8;
  237. vpic->num_scan = 1;
  238. vpic->num_components = 3;
  239. vpic->component_id[0] = 1;
  240. vpic->component_id[1] = 2;
  241. vpic->component_id[2] = 3;
  242. priv->component_subsample_h[0] = 2;
  243. priv->component_subsample_v[0] = 2;
  244. priv->component_subsample_h[1] = 1;
  245. priv->component_subsample_v[1] = 1;
  246. priv->component_subsample_h[2] = 1;
  247. priv->component_subsample_v[2] = 1;
  248. vpic->quantiser_table_selector[0] = 0;
  249. vpic->quantiser_table_selector[1] = 1;
  250. vpic->quantiser_table_selector[2] = 1;
  251. vpic->quality = priv->quality;
  252. pic->nb_slices = 1;
  253. return 0;
  254. }
  255. static int vaapi_encode_mjpeg_init_slice_params(AVCodecContext *avctx,
  256. VAAPIEncodePicture *pic,
  257. VAAPIEncodeSlice *slice)
  258. {
  259. VAEncPictureParameterBufferJPEG *vpic = pic->codec_picture_params;
  260. VAEncSliceParameterBufferJPEG *vslice = slice->codec_slice_params;
  261. int i;
  262. vslice->restart_interval = 0;
  263. vslice->num_components = vpic->num_components;
  264. for (i = 0; i < vslice->num_components; i++) {
  265. vslice->components[i].component_selector = i + 1;
  266. vslice->components[i].dc_table_selector = (i > 0);
  267. vslice->components[i].ac_table_selector = (i > 0);
  268. }
  269. return 0;
  270. }
  271. static av_cold int vaapi_encode_mjpeg_configure(AVCodecContext *avctx)
  272. {
  273. VAAPIEncodeContext *ctx = avctx->priv_data;
  274. VAAPIEncodeMJPEGContext *priv = avctx->priv_data;
  275. priv->quality = avctx->global_quality;
  276. if (priv->quality < 1 || priv->quality > 100) {
  277. av_log(avctx, AV_LOG_ERROR, "Invalid quality value %d "
  278. "(must be 1-100).\n", priv->quality);
  279. return AVERROR(EINVAL);
  280. }
  281. // Hack: the implementation calls the JPEG image header (which we
  282. // will use in the same way as a slice header) generic "raw data".
  283. // Therefore, if after the packed header capability check we have
  284. // PACKED_HEADER_RAW_DATA available, rewrite it as
  285. // PACKED_HEADER_SLICE so that the header-writing code can do the
  286. // right thing.
  287. if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_RAW_DATA) {
  288. ctx->va_packed_headers &= ~VA_ENC_PACKED_HEADER_RAW_DATA;
  289. ctx->va_packed_headers |= VA_ENC_PACKED_HEADER_SLICE;
  290. }
  291. vaapi_encode_mjpeg_init_tables(avctx);
  292. return 0;
  293. }
  294. static const VAAPIEncodeProfile vaapi_encode_mjpeg_profiles[] = {
  295. { FF_PROFILE_MJPEG_HUFFMAN_BASELINE_DCT,
  296. 8, 3, 1, 1, VAProfileJPEGBaseline },
  297. { FF_PROFILE_UNKNOWN }
  298. };
  299. static const VAAPIEncodeType vaapi_encode_type_mjpeg = {
  300. .profiles = vaapi_encode_mjpeg_profiles,
  301. .configure = &vaapi_encode_mjpeg_configure,
  302. .picture_params_size = sizeof(VAEncPictureParameterBufferJPEG),
  303. .init_picture_params = &vaapi_encode_mjpeg_init_picture_params,
  304. .slice_params_size = sizeof(VAEncSliceParameterBufferJPEG),
  305. .init_slice_params = &vaapi_encode_mjpeg_init_slice_params,
  306. .slice_header_type = VAEncPackedHeaderRawData,
  307. .write_slice_header = &vaapi_encode_mjpeg_write_image_header,
  308. .write_extra_buffer = &vaapi_encode_mjpeg_write_extra_buffer,
  309. };
  310. static av_cold int vaapi_encode_mjpeg_init(AVCodecContext *avctx)
  311. {
  312. VAAPIEncodeContext *ctx = avctx->priv_data;
  313. ctx->codec = &vaapi_encode_type_mjpeg;
  314. // The JPEG image header - see note above.
  315. ctx->va_packed_headers =
  316. VA_ENC_PACKED_HEADER_RAW_DATA;
  317. ctx->surface_width = FFALIGN(avctx->width, 8);
  318. ctx->surface_height = FFALIGN(avctx->height, 8);
  319. return ff_vaapi_encode_init(avctx);
  320. }
  321. static const AVCodecDefault vaapi_encode_mjpeg_defaults[] = {
  322. { "global_quality", "80" },
  323. { "b", "0" },
  324. { "g", "1" },
  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(VAAPIEncodeMJPEGContext),
  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. .capabilities = AV_CODEC_CAP_HARDWARE,
  343. .defaults = vaapi_encode_mjpeg_defaults,
  344. .pix_fmts = (const enum AVPixelFormat[]) {
  345. AV_PIX_FMT_VAAPI,
  346. AV_PIX_FMT_NONE,
  347. },
  348. .wrapper_name = "vaapi",
  349. };