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.

160 lines
5.5KB

  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_dec_jpeg.h>
  20. #include "hwaccel.h"
  21. #include "vaapi_decode.h"
  22. #include "mjpegdec.h"
  23. static int vaapi_mjpeg_start_frame(AVCodecContext *avctx,
  24. av_unused const uint8_t *buffer,
  25. av_unused uint32_t size)
  26. {
  27. const MJpegDecodeContext *s = avctx->priv_data;
  28. VAAPIDecodePicture *pic = s->hwaccel_picture_private;
  29. VAPictureParameterBufferJPEGBaseline pp;
  30. int err, i;
  31. pic->output_surface = ff_vaapi_get_surface_id(s->picture_ptr);
  32. pp = (VAPictureParameterBufferJPEGBaseline) {
  33. .picture_width = avctx->width,
  34. .picture_height = avctx->height,
  35. .num_components = s->nb_components,
  36. };
  37. for (i = 0; i < s->nb_components; i++) {
  38. pp.components[i].component_id = s->component_id[i];
  39. pp.components[i].h_sampling_factor = s->h_count[i];
  40. pp.components[i].v_sampling_factor = s->v_count[i];
  41. pp.components[i].quantiser_table_selector = s->quant_index[i];
  42. }
  43. err = ff_vaapi_decode_make_param_buffer(avctx, pic,
  44. VAPictureParameterBufferType,
  45. &pp, sizeof(pp));
  46. if (err < 0)
  47. goto fail;
  48. return 0;
  49. fail:
  50. ff_vaapi_decode_cancel(avctx, pic);
  51. return err;
  52. }
  53. static int vaapi_mjpeg_end_frame(AVCodecContext *avctx)
  54. {
  55. const MJpegDecodeContext *s = avctx->priv_data;
  56. VAAPIDecodePicture *pic = s->hwaccel_picture_private;
  57. return ff_vaapi_decode_issue(avctx, pic);
  58. }
  59. static int vaapi_mjpeg_decode_slice(AVCodecContext *avctx,
  60. const uint8_t *buffer,
  61. uint32_t size)
  62. {
  63. const MJpegDecodeContext *s = avctx->priv_data;
  64. VAAPIDecodePicture *pic = s->hwaccel_picture_private;
  65. VAHuffmanTableBufferJPEGBaseline huff;
  66. VAIQMatrixBufferJPEGBaseline quant;
  67. VASliceParameterBufferJPEGBaseline sp;
  68. int err, i, j;
  69. memset(&huff, 0, sizeof(huff));
  70. for (i = 0; i < 2; i++) {
  71. huff.load_huffman_table[i] = 1;
  72. for (j = 0; j < 16; j++)
  73. huff.huffman_table[i].num_dc_codes[j] = s->raw_huffman_lengths[0][i][j];
  74. for (j = 0; j < 12; j++)
  75. huff.huffman_table[i].dc_values[j] = s->raw_huffman_values[0][i][j];
  76. for (j = 0; j < 16; j++)
  77. huff.huffman_table[i].num_ac_codes[j] = s->raw_huffman_lengths[1][i][j];
  78. for (j = 0; j < 162; j++)
  79. huff.huffman_table[i].ac_values[j] = s->raw_huffman_values[1][i][j];
  80. }
  81. err = ff_vaapi_decode_make_param_buffer(avctx, pic,
  82. VAHuffmanTableBufferType,
  83. &huff, sizeof(huff));
  84. if (err < 0)
  85. goto fail;
  86. memset(&quant, 0, sizeof(quant));
  87. for (i = 0; i < 4; i++) {
  88. quant.load_quantiser_table[i] = 1;
  89. for (j = 0; j < 64; j++)
  90. quant.quantiser_table[i][j] = s->quant_matrixes[i][j];
  91. }
  92. err = ff_vaapi_decode_make_param_buffer(avctx, pic,
  93. VAIQMatrixBufferType,
  94. &quant, sizeof(quant));
  95. if (err < 0)
  96. goto fail;
  97. sp = (VASliceParameterBufferJPEGBaseline) {
  98. .slice_data_size = size,
  99. .slice_data_offset = 0,
  100. .slice_data_flag = VA_SLICE_DATA_FLAG_ALL,
  101. .slice_horizontal_position = 0,
  102. .slice_vertical_position = 0,
  103. .restart_interval = s->restart_interval,
  104. .num_mcus = s->mb_width * s->mb_height,
  105. };
  106. sp.num_components = s->nb_components;
  107. for (i = 0; i < s->nb_components; i++) {
  108. sp.components[i].component_selector = s->component_id[s->comp_index[i]];
  109. sp.components[i].dc_table_selector = s->dc_index[i];
  110. sp.components[i].ac_table_selector = s->ac_index[i];
  111. }
  112. err = ff_vaapi_decode_make_slice_buffer(avctx, pic, &sp, sizeof(sp), buffer, size);
  113. if (err)
  114. goto fail;
  115. return 0;
  116. fail:
  117. ff_vaapi_decode_cancel(avctx, pic);
  118. return err;
  119. }
  120. const AVHWAccel ff_mjpeg_vaapi_hwaccel = {
  121. .name = "mjpeg_vaapi",
  122. .type = AVMEDIA_TYPE_VIDEO,
  123. .id = AV_CODEC_ID_MJPEG,
  124. .pix_fmt = AV_PIX_FMT_VAAPI,
  125. .start_frame = &vaapi_mjpeg_start_frame,
  126. .end_frame = &vaapi_mjpeg_end_frame,
  127. .decode_slice = &vaapi_mjpeg_decode_slice,
  128. .frame_priv_data_size = sizeof(VAAPIDecodePicture),
  129. .init = &ff_vaapi_decode_init,
  130. .uninit = &ff_vaapi_decode_uninit,
  131. .frame_params = &ff_vaapi_common_frame_params,
  132. .priv_data_size = sizeof(VAAPIDecodeContext),
  133. .caps_internal = HWACCEL_CAP_ASYNC_SAFE,
  134. };