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.

2034 lines
69KB

  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 <inttypes.h>
  19. #include <string.h>
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/common.h"
  22. #include "libavutil/log.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "vaapi_encode.h"
  25. #include "avcodec.h"
  26. static const char * const picture_type_name[] = { "IDR", "I", "P", "B" };
  27. static int vaapi_encode_make_packed_header(AVCodecContext *avctx,
  28. VAAPIEncodePicture *pic,
  29. int type, char *data, size_t bit_len)
  30. {
  31. VAAPIEncodeContext *ctx = avctx->priv_data;
  32. VAStatus vas;
  33. VABufferID param_buffer, data_buffer;
  34. VABufferID *tmp;
  35. VAEncPackedHeaderParameterBuffer params = {
  36. .type = type,
  37. .bit_length = bit_len,
  38. .has_emulation_bytes = 1,
  39. };
  40. tmp = av_realloc_array(pic->param_buffers, sizeof(*tmp), pic->nb_param_buffers + 2);
  41. if (!tmp)
  42. return AVERROR(ENOMEM);
  43. pic->param_buffers = tmp;
  44. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  45. VAEncPackedHeaderParameterBufferType,
  46. sizeof(params), 1, &params, &param_buffer);
  47. if (vas != VA_STATUS_SUCCESS) {
  48. av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer "
  49. "for packed header (type %d): %d (%s).\n",
  50. type, vas, vaErrorStr(vas));
  51. return AVERROR(EIO);
  52. }
  53. pic->param_buffers[pic->nb_param_buffers++] = param_buffer;
  54. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  55. VAEncPackedHeaderDataBufferType,
  56. (bit_len + 7) / 8, 1, data, &data_buffer);
  57. if (vas != VA_STATUS_SUCCESS) {
  58. av_log(avctx, AV_LOG_ERROR, "Failed to create data buffer "
  59. "for packed header (type %d): %d (%s).\n",
  60. type, vas, vaErrorStr(vas));
  61. return AVERROR(EIO);
  62. }
  63. pic->param_buffers[pic->nb_param_buffers++] = data_buffer;
  64. av_log(avctx, AV_LOG_DEBUG, "Packed header buffer (%d) is %#x/%#x "
  65. "(%zu bits).\n", type, param_buffer, data_buffer, bit_len);
  66. return 0;
  67. }
  68. static int vaapi_encode_make_param_buffer(AVCodecContext *avctx,
  69. VAAPIEncodePicture *pic,
  70. int type, char *data, size_t len)
  71. {
  72. VAAPIEncodeContext *ctx = avctx->priv_data;
  73. VAStatus vas;
  74. VABufferID *tmp;
  75. VABufferID buffer;
  76. tmp = av_realloc_array(pic->param_buffers, sizeof(*tmp), pic->nb_param_buffers + 1);
  77. if (!tmp)
  78. return AVERROR(ENOMEM);
  79. pic->param_buffers = tmp;
  80. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  81. type, len, 1, data, &buffer);
  82. if (vas != VA_STATUS_SUCCESS) {
  83. av_log(avctx, AV_LOG_ERROR, "Failed to create parameter buffer "
  84. "(type %d): %d (%s).\n", type, vas, vaErrorStr(vas));
  85. return AVERROR(EIO);
  86. }
  87. pic->param_buffers[pic->nb_param_buffers++] = buffer;
  88. av_log(avctx, AV_LOG_DEBUG, "Param buffer (%d) is %#x.\n",
  89. type, buffer);
  90. return 0;
  91. }
  92. static int vaapi_encode_wait(AVCodecContext *avctx,
  93. VAAPIEncodePicture *pic)
  94. {
  95. VAAPIEncodeContext *ctx = avctx->priv_data;
  96. VAStatus vas;
  97. av_assert0(pic->encode_issued);
  98. if (pic->encode_complete) {
  99. // Already waited for this picture.
  100. return 0;
  101. }
  102. av_log(avctx, AV_LOG_DEBUG, "Sync to pic %"PRId64"/%"PRId64" "
  103. "(input surface %#x).\n", pic->display_order,
  104. pic->encode_order, pic->input_surface);
  105. vas = vaSyncSurface(ctx->hwctx->display, pic->input_surface);
  106. if (vas != VA_STATUS_SUCCESS) {
  107. av_log(avctx, AV_LOG_ERROR, "Failed to sync to picture completion: "
  108. "%d (%s).\n", vas, vaErrorStr(vas));
  109. return AVERROR(EIO);
  110. }
  111. // Input is definitely finished with now.
  112. av_frame_free(&pic->input_image);
  113. pic->encode_complete = 1;
  114. return 0;
  115. }
  116. static int vaapi_encode_issue(AVCodecContext *avctx,
  117. VAAPIEncodePicture *pic)
  118. {
  119. VAAPIEncodeContext *ctx = avctx->priv_data;
  120. VAAPIEncodeSlice *slice;
  121. VAStatus vas;
  122. int err, i;
  123. char data[MAX_PARAM_BUFFER_SIZE];
  124. size_t bit_len;
  125. av_log(avctx, AV_LOG_DEBUG, "Issuing encode for pic %"PRId64"/%"PRId64" "
  126. "as type %s.\n", pic->display_order, pic->encode_order,
  127. picture_type_name[pic->type]);
  128. if (pic->nb_refs == 0) {
  129. av_log(avctx, AV_LOG_DEBUG, "No reference pictures.\n");
  130. } else {
  131. av_log(avctx, AV_LOG_DEBUG, "Refers to:");
  132. for (i = 0; i < pic->nb_refs; i++) {
  133. av_log(avctx, AV_LOG_DEBUG, " %"PRId64"/%"PRId64,
  134. pic->refs[i]->display_order, pic->refs[i]->encode_order);
  135. }
  136. av_log(avctx, AV_LOG_DEBUG, ".\n");
  137. }
  138. av_assert0(pic->input_available && !pic->encode_issued);
  139. for (i = 0; i < pic->nb_refs; i++) {
  140. av_assert0(pic->refs[i]);
  141. // If we are serialised then the references must have already
  142. // completed. If not, they must have been issued but need not
  143. // have completed yet.
  144. if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING)
  145. av_assert0(pic->refs[i]->encode_complete);
  146. else
  147. av_assert0(pic->refs[i]->encode_issued);
  148. }
  149. av_log(avctx, AV_LOG_DEBUG, "Input surface is %#x.\n", pic->input_surface);
  150. pic->recon_image = av_frame_alloc();
  151. if (!pic->recon_image) {
  152. err = AVERROR(ENOMEM);
  153. goto fail;
  154. }
  155. err = av_hwframe_get_buffer(ctx->recon_frames_ref, pic->recon_image, 0);
  156. if (err < 0) {
  157. err = AVERROR(ENOMEM);
  158. goto fail;
  159. }
  160. pic->recon_surface = (VASurfaceID)(uintptr_t)pic->recon_image->data[3];
  161. av_log(avctx, AV_LOG_DEBUG, "Recon surface is %#x.\n", pic->recon_surface);
  162. pic->output_buffer_ref = av_buffer_pool_get(ctx->output_buffer_pool);
  163. if (!pic->output_buffer_ref) {
  164. err = AVERROR(ENOMEM);
  165. goto fail;
  166. }
  167. pic->output_buffer = (VABufferID)(uintptr_t)pic->output_buffer_ref->data;
  168. av_log(avctx, AV_LOG_DEBUG, "Output buffer is %#x.\n",
  169. pic->output_buffer);
  170. if (ctx->codec->picture_params_size > 0) {
  171. pic->codec_picture_params = av_malloc(ctx->codec->picture_params_size);
  172. if (!pic->codec_picture_params)
  173. goto fail;
  174. memcpy(pic->codec_picture_params, ctx->codec_picture_params,
  175. ctx->codec->picture_params_size);
  176. } else {
  177. av_assert0(!ctx->codec_picture_params);
  178. }
  179. pic->nb_param_buffers = 0;
  180. if (pic->type == PICTURE_TYPE_IDR && ctx->codec->init_sequence_params) {
  181. err = vaapi_encode_make_param_buffer(avctx, pic,
  182. VAEncSequenceParameterBufferType,
  183. ctx->codec_sequence_params,
  184. ctx->codec->sequence_params_size);
  185. if (err < 0)
  186. goto fail;
  187. }
  188. if (pic->type == PICTURE_TYPE_IDR) {
  189. for (i = 0; i < ctx->nb_global_params; i++) {
  190. err = vaapi_encode_make_param_buffer(avctx, pic,
  191. VAEncMiscParameterBufferType,
  192. (char*)ctx->global_params[i],
  193. ctx->global_params_size[i]);
  194. if (err < 0)
  195. goto fail;
  196. }
  197. }
  198. if (ctx->codec->init_picture_params) {
  199. err = ctx->codec->init_picture_params(avctx, pic);
  200. if (err < 0) {
  201. av_log(avctx, AV_LOG_ERROR, "Failed to initialise picture "
  202. "parameters: %d.\n", err);
  203. goto fail;
  204. }
  205. err = vaapi_encode_make_param_buffer(avctx, pic,
  206. VAEncPictureParameterBufferType,
  207. pic->codec_picture_params,
  208. ctx->codec->picture_params_size);
  209. if (err < 0)
  210. goto fail;
  211. }
  212. if (pic->type == PICTURE_TYPE_IDR) {
  213. if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
  214. ctx->codec->write_sequence_header) {
  215. bit_len = 8 * sizeof(data);
  216. err = ctx->codec->write_sequence_header(avctx, data, &bit_len);
  217. if (err < 0) {
  218. av_log(avctx, AV_LOG_ERROR, "Failed to write per-sequence "
  219. "header: %d.\n", err);
  220. goto fail;
  221. }
  222. err = vaapi_encode_make_packed_header(avctx, pic,
  223. ctx->codec->sequence_header_type,
  224. data, bit_len);
  225. if (err < 0)
  226. goto fail;
  227. }
  228. }
  229. if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_PICTURE &&
  230. ctx->codec->write_picture_header) {
  231. bit_len = 8 * sizeof(data);
  232. err = ctx->codec->write_picture_header(avctx, pic, data, &bit_len);
  233. if (err < 0) {
  234. av_log(avctx, AV_LOG_ERROR, "Failed to write per-picture "
  235. "header: %d.\n", err);
  236. goto fail;
  237. }
  238. err = vaapi_encode_make_packed_header(avctx, pic,
  239. ctx->codec->picture_header_type,
  240. data, bit_len);
  241. if (err < 0)
  242. goto fail;
  243. }
  244. if (ctx->codec->write_extra_buffer) {
  245. for (i = 0;; i++) {
  246. size_t len = sizeof(data);
  247. int type;
  248. err = ctx->codec->write_extra_buffer(avctx, pic, i, &type,
  249. data, &len);
  250. if (err == AVERROR_EOF)
  251. break;
  252. if (err < 0) {
  253. av_log(avctx, AV_LOG_ERROR, "Failed to write extra "
  254. "buffer %d: %d.\n", i, err);
  255. goto fail;
  256. }
  257. err = vaapi_encode_make_param_buffer(avctx, pic, type,
  258. data, len);
  259. if (err < 0)
  260. goto fail;
  261. }
  262. }
  263. if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_MISC &&
  264. ctx->codec->write_extra_header) {
  265. for (i = 0;; i++) {
  266. int type;
  267. bit_len = 8 * sizeof(data);
  268. err = ctx->codec->write_extra_header(avctx, pic, i, &type,
  269. data, &bit_len);
  270. if (err == AVERROR_EOF)
  271. break;
  272. if (err < 0) {
  273. av_log(avctx, AV_LOG_ERROR, "Failed to write extra "
  274. "header %d: %d.\n", i, err);
  275. goto fail;
  276. }
  277. err = vaapi_encode_make_packed_header(avctx, pic, type,
  278. data, bit_len);
  279. if (err < 0)
  280. goto fail;
  281. }
  282. }
  283. if (pic->nb_slices == 0)
  284. pic->nb_slices = ctx->nb_slices;
  285. if (pic->nb_slices > 0) {
  286. int rounding;
  287. pic->slices = av_mallocz_array(pic->nb_slices, sizeof(*pic->slices));
  288. if (!pic->slices) {
  289. err = AVERROR(ENOMEM);
  290. goto fail;
  291. }
  292. for (i = 0; i < pic->nb_slices; i++)
  293. pic->slices[i].row_size = ctx->slice_size;
  294. rounding = ctx->slice_block_rows - ctx->nb_slices * ctx->slice_size;
  295. if (rounding > 0) {
  296. // Place rounding error at top and bottom of frame.
  297. av_assert0(rounding < pic->nb_slices);
  298. // Some Intel drivers contain a bug where the encoder will fail
  299. // if the last slice is smaller than the one before it. Since
  300. // that's straightforward to avoid here, just do so.
  301. if (rounding <= 2) {
  302. for (i = 0; i < rounding; i++)
  303. ++pic->slices[i].row_size;
  304. } else {
  305. for (i = 0; i < (rounding + 1) / 2; i++)
  306. ++pic->slices[pic->nb_slices - i - 1].row_size;
  307. for (i = 0; i < rounding / 2; i++)
  308. ++pic->slices[i].row_size;
  309. }
  310. } else if (rounding < 0) {
  311. // Remove rounding error from last slice only.
  312. av_assert0(rounding < ctx->slice_size);
  313. pic->slices[pic->nb_slices - 1].row_size += rounding;
  314. }
  315. }
  316. for (i = 0; i < pic->nb_slices; i++) {
  317. slice = &pic->slices[i];
  318. slice->index = i;
  319. if (i == 0) {
  320. slice->row_start = 0;
  321. slice->block_start = 0;
  322. } else {
  323. const VAAPIEncodeSlice *prev = &pic->slices[i - 1];
  324. slice->row_start = prev->row_start + prev->row_size;
  325. slice->block_start = prev->block_start + prev->block_size;
  326. }
  327. slice->block_size = slice->row_size * ctx->slice_block_cols;
  328. av_log(avctx, AV_LOG_DEBUG, "Slice %d: %d-%d (%d rows), "
  329. "%d-%d (%d blocks).\n", i, slice->row_start,
  330. slice->row_start + slice->row_size - 1, slice->row_size,
  331. slice->block_start, slice->block_start + slice->block_size - 1,
  332. slice->block_size);
  333. if (ctx->codec->slice_params_size > 0) {
  334. slice->codec_slice_params = av_mallocz(ctx->codec->slice_params_size);
  335. if (!slice->codec_slice_params) {
  336. err = AVERROR(ENOMEM);
  337. goto fail;
  338. }
  339. }
  340. if (ctx->codec->init_slice_params) {
  341. err = ctx->codec->init_slice_params(avctx, pic, slice);
  342. if (err < 0) {
  343. av_log(avctx, AV_LOG_ERROR, "Failed to initialise slice "
  344. "parameters: %d.\n", err);
  345. goto fail;
  346. }
  347. }
  348. if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SLICE &&
  349. ctx->codec->write_slice_header) {
  350. bit_len = 8 * sizeof(data);
  351. err = ctx->codec->write_slice_header(avctx, pic, slice,
  352. data, &bit_len);
  353. if (err < 0) {
  354. av_log(avctx, AV_LOG_ERROR, "Failed to write per-slice "
  355. "header: %d.\n", err);
  356. goto fail;
  357. }
  358. err = vaapi_encode_make_packed_header(avctx, pic,
  359. ctx->codec->slice_header_type,
  360. data, bit_len);
  361. if (err < 0)
  362. goto fail;
  363. }
  364. if (ctx->codec->init_slice_params) {
  365. err = vaapi_encode_make_param_buffer(avctx, pic,
  366. VAEncSliceParameterBufferType,
  367. slice->codec_slice_params,
  368. ctx->codec->slice_params_size);
  369. if (err < 0)
  370. goto fail;
  371. }
  372. }
  373. vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context,
  374. pic->input_surface);
  375. if (vas != VA_STATUS_SUCCESS) {
  376. av_log(avctx, AV_LOG_ERROR, "Failed to begin picture encode issue: "
  377. "%d (%s).\n", vas, vaErrorStr(vas));
  378. err = AVERROR(EIO);
  379. goto fail_with_picture;
  380. }
  381. vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
  382. pic->param_buffers, pic->nb_param_buffers);
  383. if (vas != VA_STATUS_SUCCESS) {
  384. av_log(avctx, AV_LOG_ERROR, "Failed to upload encode parameters: "
  385. "%d (%s).\n", vas, vaErrorStr(vas));
  386. err = AVERROR(EIO);
  387. goto fail_with_picture;
  388. }
  389. vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
  390. if (vas != VA_STATUS_SUCCESS) {
  391. av_log(avctx, AV_LOG_ERROR, "Failed to end picture encode issue: "
  392. "%d (%s).\n", vas, vaErrorStr(vas));
  393. err = AVERROR(EIO);
  394. // vaRenderPicture() has been called here, so we should not destroy
  395. // the parameter buffers unless separate destruction is required.
  396. if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
  397. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
  398. goto fail;
  399. else
  400. goto fail_at_end;
  401. }
  402. if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
  403. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS) {
  404. for (i = 0; i < pic->nb_param_buffers; i++) {
  405. vas = vaDestroyBuffer(ctx->hwctx->display,
  406. pic->param_buffers[i]);
  407. if (vas != VA_STATUS_SUCCESS) {
  408. av_log(avctx, AV_LOG_ERROR, "Failed to destroy "
  409. "param buffer %#x: %d (%s).\n",
  410. pic->param_buffers[i], vas, vaErrorStr(vas));
  411. // And ignore.
  412. }
  413. }
  414. }
  415. pic->encode_issued = 1;
  416. if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING)
  417. return vaapi_encode_wait(avctx, pic);
  418. else
  419. return 0;
  420. fail_with_picture:
  421. vaEndPicture(ctx->hwctx->display, ctx->va_context);
  422. fail:
  423. for(i = 0; i < pic->nb_param_buffers; i++)
  424. vaDestroyBuffer(ctx->hwctx->display, pic->param_buffers[i]);
  425. for (i = 0; i < pic->nb_slices; i++) {
  426. if (pic->slices) {
  427. av_freep(&pic->slices[i].priv_data);
  428. av_freep(&pic->slices[i].codec_slice_params);
  429. }
  430. }
  431. fail_at_end:
  432. av_freep(&pic->codec_picture_params);
  433. av_freep(&pic->param_buffers);
  434. av_freep(&pic->slices);
  435. av_frame_free(&pic->recon_image);
  436. av_buffer_unref(&pic->output_buffer_ref);
  437. pic->output_buffer = VA_INVALID_ID;
  438. return err;
  439. }
  440. static int vaapi_encode_output(AVCodecContext *avctx,
  441. VAAPIEncodePicture *pic, AVPacket *pkt)
  442. {
  443. VAAPIEncodeContext *ctx = avctx->priv_data;
  444. VACodedBufferSegment *buf_list, *buf;
  445. VAStatus vas;
  446. int err;
  447. err = vaapi_encode_wait(avctx, pic);
  448. if (err < 0)
  449. return err;
  450. buf_list = NULL;
  451. vas = vaMapBuffer(ctx->hwctx->display, pic->output_buffer,
  452. (void**)&buf_list);
  453. if (vas != VA_STATUS_SUCCESS) {
  454. av_log(avctx, AV_LOG_ERROR, "Failed to map output buffers: "
  455. "%d (%s).\n", vas, vaErrorStr(vas));
  456. err = AVERROR(EIO);
  457. goto fail;
  458. }
  459. for (buf = buf_list; buf; buf = buf->next) {
  460. av_log(avctx, AV_LOG_DEBUG, "Output buffer: %u bytes "
  461. "(status %08x).\n", buf->size, buf->status);
  462. err = av_new_packet(pkt, buf->size);
  463. if (err < 0)
  464. goto fail_mapped;
  465. memcpy(pkt->data, buf->buf, buf->size);
  466. }
  467. if (pic->type == PICTURE_TYPE_IDR)
  468. pkt->flags |= AV_PKT_FLAG_KEY;
  469. pkt->pts = pic->pts;
  470. vas = vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
  471. if (vas != VA_STATUS_SUCCESS) {
  472. av_log(avctx, AV_LOG_ERROR, "Failed to unmap output buffers: "
  473. "%d (%s).\n", vas, vaErrorStr(vas));
  474. err = AVERROR(EIO);
  475. goto fail;
  476. }
  477. av_buffer_unref(&pic->output_buffer_ref);
  478. pic->output_buffer = VA_INVALID_ID;
  479. av_log(avctx, AV_LOG_DEBUG, "Output read for pic %"PRId64"/%"PRId64".\n",
  480. pic->display_order, pic->encode_order);
  481. return 0;
  482. fail_mapped:
  483. vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
  484. fail:
  485. av_buffer_unref(&pic->output_buffer_ref);
  486. pic->output_buffer = VA_INVALID_ID;
  487. return err;
  488. }
  489. static int vaapi_encode_discard(AVCodecContext *avctx,
  490. VAAPIEncodePicture *pic)
  491. {
  492. vaapi_encode_wait(avctx, pic);
  493. if (pic->output_buffer_ref) {
  494. av_log(avctx, AV_LOG_DEBUG, "Discard output for pic "
  495. "%"PRId64"/%"PRId64".\n",
  496. pic->display_order, pic->encode_order);
  497. av_buffer_unref(&pic->output_buffer_ref);
  498. pic->output_buffer = VA_INVALID_ID;
  499. }
  500. return 0;
  501. }
  502. static VAAPIEncodePicture *vaapi_encode_alloc(void)
  503. {
  504. VAAPIEncodePicture *pic;
  505. pic = av_mallocz(sizeof(*pic));
  506. if (!pic)
  507. return NULL;
  508. pic->input_surface = VA_INVALID_ID;
  509. pic->recon_surface = VA_INVALID_ID;
  510. pic->output_buffer = VA_INVALID_ID;
  511. return pic;
  512. }
  513. static int vaapi_encode_free(AVCodecContext *avctx,
  514. VAAPIEncodePicture *pic)
  515. {
  516. int i;
  517. if (pic->encode_issued)
  518. vaapi_encode_discard(avctx, pic);
  519. for (i = 0; i < pic->nb_slices; i++) {
  520. if (pic->slices) {
  521. av_freep(&pic->slices[i].priv_data);
  522. av_freep(&pic->slices[i].codec_slice_params);
  523. }
  524. }
  525. av_freep(&pic->codec_picture_params);
  526. av_frame_free(&pic->input_image);
  527. av_frame_free(&pic->recon_image);
  528. av_freep(&pic->param_buffers);
  529. av_freep(&pic->slices);
  530. // Output buffer should already be destroyed.
  531. av_assert0(pic->output_buffer == VA_INVALID_ID);
  532. av_freep(&pic->priv_data);
  533. av_freep(&pic->codec_picture_params);
  534. av_free(pic);
  535. return 0;
  536. }
  537. static int vaapi_encode_step(AVCodecContext *avctx,
  538. VAAPIEncodePicture *target)
  539. {
  540. VAAPIEncodeContext *ctx = avctx->priv_data;
  541. VAAPIEncodePicture *pic;
  542. int i, err;
  543. if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING ||
  544. ctx->issue_mode == ISSUE_MODE_MINIMISE_LATENCY) {
  545. // These two modes are equivalent, except that we wait for
  546. // immediate completion on each operation if serialised.
  547. if (!target) {
  548. // No target, nothing to do yet.
  549. return 0;
  550. }
  551. if (target->encode_complete) {
  552. // Already done.
  553. return 0;
  554. }
  555. pic = target;
  556. for (i = 0; i < pic->nb_refs; i++) {
  557. if (!pic->refs[i]->encode_complete) {
  558. err = vaapi_encode_step(avctx, pic->refs[i]);
  559. if (err < 0)
  560. return err;
  561. }
  562. }
  563. err = vaapi_encode_issue(avctx, pic);
  564. if (err < 0)
  565. return err;
  566. } else if (ctx->issue_mode == ISSUE_MODE_MAXIMISE_THROUGHPUT) {
  567. int activity;
  568. // Run through the list of all available pictures repeatedly
  569. // and issue the first one found which has all dependencies
  570. // available (including previously-issued but not necessarily
  571. // completed pictures).
  572. do {
  573. activity = 0;
  574. for (pic = ctx->pic_start; pic; pic = pic->next) {
  575. if (!pic->input_available || pic->encode_issued)
  576. continue;
  577. for (i = 0; i < pic->nb_refs; i++) {
  578. if (!pic->refs[i]->encode_issued)
  579. break;
  580. }
  581. if (i < pic->nb_refs)
  582. continue;
  583. err = vaapi_encode_issue(avctx, pic);
  584. if (err < 0)
  585. return err;
  586. activity = 1;
  587. // Start again from the beginning of the list,
  588. // because issuing this picture may have satisfied
  589. // forward dependencies of earlier ones.
  590. break;
  591. }
  592. } while(activity);
  593. // If we had a defined target for this step then it will
  594. // always have been issued by now.
  595. if (target) {
  596. av_assert0(target->encode_issued && "broken dependencies?");
  597. }
  598. } else {
  599. av_assert0(0);
  600. }
  601. return 0;
  602. }
  603. static int vaapi_encode_get_next(AVCodecContext *avctx,
  604. VAAPIEncodePicture **pic_out)
  605. {
  606. VAAPIEncodeContext *ctx = avctx->priv_data;
  607. VAAPIEncodePicture *start, *end, *pic;
  608. int i;
  609. for (pic = ctx->pic_start; pic; pic = pic->next) {
  610. if (pic->next)
  611. av_assert0(pic->display_order + 1 == pic->next->display_order);
  612. if (pic->display_order == ctx->input_order) {
  613. *pic_out = pic;
  614. return 0;
  615. }
  616. }
  617. pic = vaapi_encode_alloc();
  618. if (!pic)
  619. return AVERROR(ENOMEM);
  620. if (ctx->input_order == 0 || ctx->force_idr ||
  621. ctx->gop_counter >= ctx->gop_size) {
  622. pic->type = PICTURE_TYPE_IDR;
  623. ctx->force_idr = 0;
  624. ctx->gop_counter = 1;
  625. ctx->p_counter = 0;
  626. } else if (ctx->p_counter >= ctx->p_per_i) {
  627. pic->type = PICTURE_TYPE_I;
  628. ++ctx->gop_counter;
  629. ctx->p_counter = 0;
  630. } else {
  631. pic->type = PICTURE_TYPE_P;
  632. pic->refs[0] = ctx->pic_end;
  633. pic->nb_refs = 1;
  634. ++ctx->gop_counter;
  635. ++ctx->p_counter;
  636. }
  637. start = end = pic;
  638. if (pic->type != PICTURE_TYPE_IDR) {
  639. // If that was not an IDR frame, add B-frames display-before and
  640. // encode-after it, but not exceeding the GOP size.
  641. for (i = 0; i < ctx->b_per_p &&
  642. ctx->gop_counter < ctx->gop_size; i++) {
  643. pic = vaapi_encode_alloc();
  644. if (!pic)
  645. goto fail;
  646. pic->type = PICTURE_TYPE_B;
  647. pic->refs[0] = ctx->pic_end;
  648. pic->refs[1] = end;
  649. pic->nb_refs = 2;
  650. pic->next = start;
  651. pic->display_order = ctx->input_order + ctx->b_per_p - i - 1;
  652. pic->encode_order = pic->display_order + 1;
  653. start = pic;
  654. ++ctx->gop_counter;
  655. }
  656. }
  657. if (ctx->input_order == 0) {
  658. pic->display_order = 0;
  659. pic->encode_order = 0;
  660. ctx->pic_start = ctx->pic_end = pic;
  661. } else {
  662. for (i = 0, pic = start; pic; i++, pic = pic->next) {
  663. pic->display_order = ctx->input_order + i;
  664. if (end->type == PICTURE_TYPE_IDR)
  665. pic->encode_order = ctx->input_order + i;
  666. else if (pic == end)
  667. pic->encode_order = ctx->input_order;
  668. else
  669. pic->encode_order = ctx->input_order + i + 1;
  670. }
  671. av_assert0(ctx->pic_end);
  672. ctx->pic_end->next = start;
  673. ctx->pic_end = end;
  674. }
  675. *pic_out = start;
  676. av_log(avctx, AV_LOG_DEBUG, "Pictures:");
  677. for (pic = ctx->pic_start; pic; pic = pic->next) {
  678. av_log(avctx, AV_LOG_DEBUG, " %s (%"PRId64"/%"PRId64")",
  679. picture_type_name[pic->type],
  680. pic->display_order, pic->encode_order);
  681. }
  682. av_log(avctx, AV_LOG_DEBUG, "\n");
  683. return 0;
  684. fail:
  685. while (start) {
  686. pic = start->next;
  687. vaapi_encode_free(avctx, start);
  688. start = pic;
  689. }
  690. return AVERROR(ENOMEM);
  691. }
  692. static int vaapi_encode_truncate_gop(AVCodecContext *avctx)
  693. {
  694. VAAPIEncodeContext *ctx = avctx->priv_data;
  695. VAAPIEncodePicture *pic, *last_pic, *next;
  696. av_assert0(!ctx->pic_start || ctx->pic_start->input_available);
  697. // Find the last picture we actually have input for.
  698. for (pic = ctx->pic_start; pic; pic = pic->next) {
  699. if (!pic->input_available)
  700. break;
  701. last_pic = pic;
  702. }
  703. if (pic) {
  704. if (last_pic->type == PICTURE_TYPE_B) {
  705. // Some fixing up is required. Change the type of this
  706. // picture to P, then modify preceding B references which
  707. // point beyond it to point at it instead.
  708. last_pic->type = PICTURE_TYPE_P;
  709. last_pic->encode_order = last_pic->refs[1]->encode_order;
  710. for (pic = ctx->pic_start; pic != last_pic; pic = pic->next) {
  711. if (pic->type == PICTURE_TYPE_B &&
  712. pic->refs[1] == last_pic->refs[1])
  713. pic->refs[1] = last_pic;
  714. }
  715. last_pic->nb_refs = 1;
  716. last_pic->refs[1] = NULL;
  717. } else {
  718. // We can use the current structure (no references point
  719. // beyond the end), but there are unused pics to discard.
  720. }
  721. // Discard all following pics, they will never be used.
  722. for (pic = last_pic->next; pic; pic = next) {
  723. next = pic->next;
  724. vaapi_encode_free(avctx, pic);
  725. }
  726. last_pic->next = NULL;
  727. ctx->pic_end = last_pic;
  728. } else {
  729. // Input is available for all pictures, so we don't need to
  730. // mangle anything.
  731. }
  732. av_log(avctx, AV_LOG_DEBUG, "Pictures ending truncated GOP:");
  733. for (pic = ctx->pic_start; pic; pic = pic->next) {
  734. av_log(avctx, AV_LOG_DEBUG, " %s (%"PRId64"/%"PRId64")",
  735. picture_type_name[pic->type],
  736. pic->display_order, pic->encode_order);
  737. }
  738. av_log(avctx, AV_LOG_DEBUG, "\n");
  739. return 0;
  740. }
  741. static int vaapi_encode_clear_old(AVCodecContext *avctx)
  742. {
  743. VAAPIEncodeContext *ctx = avctx->priv_data;
  744. VAAPIEncodePicture *pic, *old;
  745. int i;
  746. while (ctx->pic_start != ctx->pic_end) {
  747. old = ctx->pic_start;
  748. if (old->encode_order > ctx->output_order)
  749. break;
  750. for (pic = old->next; pic; pic = pic->next) {
  751. if (pic->encode_complete)
  752. continue;
  753. for (i = 0; i < pic->nb_refs; i++) {
  754. if (pic->refs[i] == old) {
  755. // We still need this picture because it's referred to
  756. // directly by a later one, so it and all following
  757. // pictures have to stay.
  758. return 0;
  759. }
  760. }
  761. }
  762. pic = ctx->pic_start;
  763. ctx->pic_start = pic->next;
  764. vaapi_encode_free(avctx, pic);
  765. }
  766. return 0;
  767. }
  768. int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
  769. const AVFrame *input_image, int *got_packet)
  770. {
  771. VAAPIEncodeContext *ctx = avctx->priv_data;
  772. VAAPIEncodePicture *pic;
  773. int err;
  774. if (input_image) {
  775. av_log(avctx, AV_LOG_DEBUG, "Encode frame: %ux%u (%"PRId64").\n",
  776. input_image->width, input_image->height, input_image->pts);
  777. if (input_image->pict_type == AV_PICTURE_TYPE_I) {
  778. err = vaapi_encode_truncate_gop(avctx);
  779. if (err < 0)
  780. goto fail;
  781. ctx->force_idr = 1;
  782. }
  783. err = vaapi_encode_get_next(avctx, &pic);
  784. if (err) {
  785. av_log(avctx, AV_LOG_ERROR, "Input setup failed: %d.\n", err);
  786. return err;
  787. }
  788. pic->input_image = av_frame_alloc();
  789. if (!pic->input_image) {
  790. err = AVERROR(ENOMEM);
  791. goto fail;
  792. }
  793. err = av_frame_ref(pic->input_image, input_image);
  794. if (err < 0)
  795. goto fail;
  796. pic->input_surface = (VASurfaceID)(uintptr_t)input_image->data[3];
  797. pic->pts = input_image->pts;
  798. if (ctx->input_order == 0)
  799. ctx->first_pts = pic->pts;
  800. if (ctx->input_order == ctx->decode_delay)
  801. ctx->dts_pts_diff = pic->pts - ctx->first_pts;
  802. if (ctx->output_delay > 0)
  803. ctx->ts_ring[ctx->input_order % (3 * ctx->output_delay)] = pic->pts;
  804. pic->input_available = 1;
  805. } else {
  806. if (!ctx->end_of_stream) {
  807. err = vaapi_encode_truncate_gop(avctx);
  808. if (err < 0)
  809. goto fail;
  810. ctx->end_of_stream = 1;
  811. }
  812. }
  813. ++ctx->input_order;
  814. ++ctx->output_order;
  815. av_assert0(ctx->output_order + ctx->output_delay + 1 == ctx->input_order);
  816. for (pic = ctx->pic_start; pic; pic = pic->next)
  817. if (pic->encode_order == ctx->output_order)
  818. break;
  819. // pic can be null here if we don't have a specific target in this
  820. // iteration. We might still issue encodes if things can be overlapped,
  821. // even though we don't intend to output anything.
  822. err = vaapi_encode_step(avctx, pic);
  823. if (err < 0) {
  824. av_log(avctx, AV_LOG_ERROR, "Encode failed: %d.\n", err);
  825. goto fail;
  826. }
  827. if (!pic) {
  828. *got_packet = 0;
  829. } else {
  830. err = vaapi_encode_output(avctx, pic, pkt);
  831. if (err < 0) {
  832. av_log(avctx, AV_LOG_ERROR, "Output failed: %d.\n", err);
  833. goto fail;
  834. }
  835. if (ctx->output_delay == 0) {
  836. pkt->dts = pkt->pts;
  837. } else if (ctx->output_order < ctx->decode_delay) {
  838. if (ctx->ts_ring[ctx->output_order] < INT64_MIN + ctx->dts_pts_diff)
  839. pkt->dts = INT64_MIN;
  840. else
  841. pkt->dts = ctx->ts_ring[ctx->output_order] - ctx->dts_pts_diff;
  842. } else {
  843. pkt->dts = ctx->ts_ring[(ctx->output_order - ctx->decode_delay) %
  844. (3 * ctx->output_delay)];
  845. }
  846. *got_packet = 1;
  847. }
  848. err = vaapi_encode_clear_old(avctx);
  849. if (err < 0) {
  850. av_log(avctx, AV_LOG_ERROR, "List clearing failed: %d.\n", err);
  851. goto fail;
  852. }
  853. return 0;
  854. fail:
  855. // Unclear what to clean up on failure. There are probably some things we
  856. // could do usefully clean up here, but for now just leave them for uninit()
  857. // to do instead.
  858. return err;
  859. }
  860. static av_cold void vaapi_encode_add_global_param(AVCodecContext *avctx,
  861. VAEncMiscParameterBuffer *buffer,
  862. size_t size)
  863. {
  864. VAAPIEncodeContext *ctx = avctx->priv_data;
  865. av_assert0(ctx->nb_global_params < MAX_GLOBAL_PARAMS);
  866. ctx->global_params [ctx->nb_global_params] = buffer;
  867. ctx->global_params_size[ctx->nb_global_params] = size;
  868. ++ctx->nb_global_params;
  869. }
  870. typedef struct VAAPIEncodeRTFormat {
  871. const char *name;
  872. unsigned int value;
  873. int depth;
  874. int nb_components;
  875. int log2_chroma_w;
  876. int log2_chroma_h;
  877. } VAAPIEncodeRTFormat;
  878. static const VAAPIEncodeRTFormat vaapi_encode_rt_formats[] = {
  879. { "YUV400", VA_RT_FORMAT_YUV400, 8, 1, },
  880. { "YUV420", VA_RT_FORMAT_YUV420, 8, 3, 1, 1 },
  881. { "YUV422", VA_RT_FORMAT_YUV422, 8, 3, 1, 0 },
  882. { "YUV444", VA_RT_FORMAT_YUV444, 8, 3, 0, 0 },
  883. { "YUV411", VA_RT_FORMAT_YUV411, 8, 3, 2, 0 },
  884. #if VA_CHECK_VERSION(0, 38, 1)
  885. { "YUV420_10", VA_RT_FORMAT_YUV420_10BPP, 10, 3, 1, 1 },
  886. #endif
  887. };
  888. static const VAEntrypoint vaapi_encode_entrypoints_normal[] = {
  889. VAEntrypointEncSlice,
  890. VAEntrypointEncPicture,
  891. #if VA_CHECK_VERSION(0, 39, 2)
  892. VAEntrypointEncSliceLP,
  893. #endif
  894. 0
  895. };
  896. #if VA_CHECK_VERSION(0, 39, 2)
  897. static const VAEntrypoint vaapi_encode_entrypoints_low_power[] = {
  898. VAEntrypointEncSliceLP,
  899. 0
  900. };
  901. #endif
  902. static av_cold int vaapi_encode_profile_entrypoint(AVCodecContext *avctx)
  903. {
  904. VAAPIEncodeContext *ctx = avctx->priv_data;
  905. VAProfile *va_profiles = NULL;
  906. VAEntrypoint *va_entrypoints = NULL;
  907. VAStatus vas;
  908. const VAEntrypoint *usable_entrypoints;
  909. const VAAPIEncodeProfile *profile;
  910. const AVPixFmtDescriptor *desc;
  911. VAConfigAttrib rt_format_attr;
  912. const VAAPIEncodeRTFormat *rt_format;
  913. const char *profile_string, *entrypoint_string;
  914. int i, j, n, depth, err;
  915. if (ctx->low_power) {
  916. #if VA_CHECK_VERSION(0, 39, 2)
  917. usable_entrypoints = vaapi_encode_entrypoints_low_power;
  918. #else
  919. av_log(avctx, AV_LOG_ERROR, "Low-power encoding is not "
  920. "supported with this VAAPI version.\n");
  921. return AVERROR(EINVAL);
  922. #endif
  923. } else {
  924. usable_entrypoints = vaapi_encode_entrypoints_normal;
  925. }
  926. desc = av_pix_fmt_desc_get(ctx->input_frames->sw_format);
  927. if (!desc) {
  928. av_log(avctx, AV_LOG_ERROR, "Invalid input pixfmt (%d).\n",
  929. ctx->input_frames->sw_format);
  930. return AVERROR(EINVAL);
  931. }
  932. depth = desc->comp[0].depth;
  933. for (i = 1; i < desc->nb_components; i++) {
  934. if (desc->comp[i].depth != depth) {
  935. av_log(avctx, AV_LOG_ERROR, "Invalid input pixfmt (%s).\n",
  936. desc->name);
  937. return AVERROR(EINVAL);
  938. }
  939. }
  940. av_log(avctx, AV_LOG_VERBOSE, "Input surface format is %s.\n",
  941. desc->name);
  942. n = vaMaxNumProfiles(ctx->hwctx->display);
  943. va_profiles = av_malloc_array(n, sizeof(VAProfile));
  944. if (!va_profiles) {
  945. err = AVERROR(ENOMEM);
  946. goto fail;
  947. }
  948. vas = vaQueryConfigProfiles(ctx->hwctx->display, va_profiles, &n);
  949. if (vas != VA_STATUS_SUCCESS) {
  950. av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: %d (%s).\n",
  951. vas, vaErrorStr(vas));
  952. err = AVERROR_EXTERNAL;
  953. goto fail;
  954. }
  955. av_assert0(ctx->codec->profiles);
  956. for (i = 0; (ctx->codec->profiles[i].av_profile !=
  957. FF_PROFILE_UNKNOWN); i++) {
  958. profile = &ctx->codec->profiles[i];
  959. if (depth != profile->depth ||
  960. desc->nb_components != profile->nb_components)
  961. continue;
  962. if (desc->nb_components > 1 &&
  963. (desc->log2_chroma_w != profile->log2_chroma_w ||
  964. desc->log2_chroma_h != profile->log2_chroma_h))
  965. continue;
  966. if (avctx->profile != profile->av_profile &&
  967. avctx->profile != FF_PROFILE_UNKNOWN)
  968. continue;
  969. #if VA_CHECK_VERSION(1, 0, 0)
  970. profile_string = vaProfileStr(profile->va_profile);
  971. #else
  972. profile_string = "(no profile names)";
  973. #endif
  974. for (j = 0; j < n; j++) {
  975. if (va_profiles[j] == profile->va_profile)
  976. break;
  977. }
  978. if (j >= n) {
  979. av_log(avctx, AV_LOG_VERBOSE, "Matching profile %d is "
  980. "not supported by driver.\n", profile->va_profile);
  981. continue;
  982. }
  983. ctx->profile = profile;
  984. break;
  985. }
  986. if (!ctx->profile) {
  987. av_log(avctx, AV_LOG_ERROR, "No usable encoding profile found.\n");
  988. err = AVERROR(ENOSYS);
  989. goto fail;
  990. }
  991. avctx->profile = profile->av_profile;
  992. ctx->va_profile = profile->va_profile;
  993. av_log(avctx, AV_LOG_VERBOSE, "Using VAAPI profile %s (%d).\n",
  994. profile_string, ctx->va_profile);
  995. n = vaMaxNumEntrypoints(ctx->hwctx->display);
  996. va_entrypoints = av_malloc_array(n, sizeof(VAEntrypoint));
  997. if (!va_entrypoints) {
  998. err = AVERROR(ENOMEM);
  999. goto fail;
  1000. }
  1001. vas = vaQueryConfigEntrypoints(ctx->hwctx->display, ctx->va_profile,
  1002. va_entrypoints, &n);
  1003. if (vas != VA_STATUS_SUCCESS) {
  1004. av_log(avctx, AV_LOG_ERROR, "Failed to query entrypoints for "
  1005. "profile %s (%d): %d (%s).\n", profile_string,
  1006. ctx->va_profile, vas, vaErrorStr(vas));
  1007. err = AVERROR_EXTERNAL;
  1008. goto fail;
  1009. }
  1010. for (i = 0; i < n; i++) {
  1011. for (j = 0; usable_entrypoints[j]; j++) {
  1012. if (va_entrypoints[i] == usable_entrypoints[j])
  1013. break;
  1014. }
  1015. if (usable_entrypoints[j])
  1016. break;
  1017. }
  1018. if (i >= n) {
  1019. av_log(avctx, AV_LOG_ERROR, "No usable encoding entrypoint found "
  1020. "for profile %s (%d).\n", profile_string, ctx->va_profile);
  1021. err = AVERROR(ENOSYS);
  1022. goto fail;
  1023. }
  1024. ctx->va_entrypoint = va_entrypoints[i];
  1025. #if VA_CHECK_VERSION(1, 0, 0)
  1026. entrypoint_string = vaEntrypointStr(ctx->va_entrypoint);
  1027. #else
  1028. entrypoint_string = "(no entrypoint names)";
  1029. #endif
  1030. av_log(avctx, AV_LOG_VERBOSE, "Using VAAPI entrypoint %s (%d).\n",
  1031. entrypoint_string, ctx->va_entrypoint);
  1032. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_encode_rt_formats); i++) {
  1033. rt_format = &vaapi_encode_rt_formats[i];
  1034. if (rt_format->depth == depth &&
  1035. rt_format->nb_components == profile->nb_components &&
  1036. rt_format->log2_chroma_w == profile->log2_chroma_w &&
  1037. rt_format->log2_chroma_h == profile->log2_chroma_h)
  1038. break;
  1039. }
  1040. if (i >= FF_ARRAY_ELEMS(vaapi_encode_rt_formats)) {
  1041. av_log(avctx, AV_LOG_ERROR, "No usable render target format "
  1042. "found for profile %s (%d) entrypoint %s (%d).\n",
  1043. profile_string, ctx->va_profile,
  1044. entrypoint_string, ctx->va_entrypoint);
  1045. err = AVERROR(ENOSYS);
  1046. goto fail;
  1047. }
  1048. rt_format_attr = (VAConfigAttrib) { VAConfigAttribRTFormat };
  1049. vas = vaGetConfigAttributes(ctx->hwctx->display,
  1050. ctx->va_profile, ctx->va_entrypoint,
  1051. &rt_format_attr, 1);
  1052. if (vas != VA_STATUS_SUCCESS) {
  1053. av_log(avctx, AV_LOG_ERROR, "Failed to query RT format "
  1054. "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
  1055. err = AVERROR_EXTERNAL;
  1056. goto fail;
  1057. }
  1058. if (rt_format_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
  1059. av_log(avctx, AV_LOG_VERBOSE, "RT format config attribute not "
  1060. "supported by driver: assuming surface RT format %s "
  1061. "is valid.\n", rt_format->name);
  1062. } else if (!(rt_format_attr.value & rt_format->value)) {
  1063. av_log(avctx, AV_LOG_ERROR, "Surface RT format %s not supported "
  1064. "by driver for encoding profile %s (%d) entrypoint %s (%d).\n",
  1065. rt_format->name, profile_string, ctx->va_profile,
  1066. entrypoint_string, ctx->va_entrypoint);
  1067. err = AVERROR(ENOSYS);
  1068. goto fail;
  1069. } else {
  1070. av_log(avctx, AV_LOG_VERBOSE, "Using VAAPI render target "
  1071. "format %s (%#x).\n", rt_format->name, rt_format->value);
  1072. ctx->config_attributes[ctx->nb_config_attributes++] =
  1073. (VAConfigAttrib) {
  1074. .type = VAConfigAttribRTFormat,
  1075. .value = rt_format->value,
  1076. };
  1077. }
  1078. err = 0;
  1079. fail:
  1080. av_freep(&va_profiles);
  1081. av_freep(&va_entrypoints);
  1082. return err;
  1083. }
  1084. static av_cold int vaapi_encode_init_rate_control(AVCodecContext *avctx)
  1085. {
  1086. VAAPIEncodeContext *ctx = avctx->priv_data;
  1087. int64_t rc_bits_per_second;
  1088. int rc_target_percentage;
  1089. int rc_window_size;
  1090. int64_t hrd_buffer_size;
  1091. int64_t hrd_initial_buffer_fullness;
  1092. int fr_num, fr_den;
  1093. VAConfigAttrib rc_attr = { VAConfigAttribRateControl };
  1094. VAStatus vas;
  1095. vas = vaGetConfigAttributes(ctx->hwctx->display,
  1096. ctx->va_profile, ctx->va_entrypoint,
  1097. &rc_attr, 1);
  1098. if (vas != VA_STATUS_SUCCESS) {
  1099. av_log(avctx, AV_LOG_ERROR, "Failed to query rate control "
  1100. "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
  1101. return AVERROR_EXTERNAL;
  1102. }
  1103. if (rc_attr.value == VA_ATTRIB_NOT_SUPPORTED) {
  1104. av_log(avctx, AV_LOG_VERBOSE, "Driver does not report any "
  1105. "supported rate control modes: assuming constant-quality.\n");
  1106. ctx->va_rc_mode = VA_RC_CQP;
  1107. return 0;
  1108. }
  1109. if (avctx->flags & AV_CODEC_FLAG_QSCALE ||
  1110. avctx->bit_rate <= 0) {
  1111. if (rc_attr.value & VA_RC_CQP) {
  1112. av_log(avctx, AV_LOG_VERBOSE, "Using constant-quality mode.\n");
  1113. ctx->va_rc_mode = VA_RC_CQP;
  1114. if (avctx->bit_rate > 0 || avctx->rc_max_rate > 0) {
  1115. av_log(avctx, AV_LOG_WARNING, "Bitrate target parameters "
  1116. "ignored in constant-quality mode.\n");
  1117. }
  1118. return 0;
  1119. } else {
  1120. av_log(avctx, AV_LOG_ERROR, "Driver does not support "
  1121. "constant-quality mode (%#x).\n", rc_attr.value);
  1122. return AVERROR(EINVAL);
  1123. }
  1124. }
  1125. if (!(rc_attr.value & (VA_RC_CBR | VA_RC_VBR))) {
  1126. av_log(avctx, AV_LOG_ERROR, "Driver does not support any "
  1127. "bitrate-targetted rate control modes.\n");
  1128. return AVERROR(EINVAL);
  1129. }
  1130. if (avctx->rc_buffer_size)
  1131. hrd_buffer_size = avctx->rc_buffer_size;
  1132. else if (avctx->rc_max_rate > 0)
  1133. hrd_buffer_size = avctx->rc_max_rate;
  1134. else
  1135. hrd_buffer_size = avctx->bit_rate;
  1136. if (avctx->rc_initial_buffer_occupancy) {
  1137. if (avctx->rc_initial_buffer_occupancy > hrd_buffer_size) {
  1138. av_log(avctx, AV_LOG_ERROR, "Invalid RC buffer settings: "
  1139. "must have initial buffer size (%d) < "
  1140. "buffer size (%"PRId64").\n",
  1141. avctx->rc_initial_buffer_occupancy, hrd_buffer_size);
  1142. return AVERROR(EINVAL);
  1143. }
  1144. hrd_initial_buffer_fullness = avctx->rc_initial_buffer_occupancy;
  1145. } else {
  1146. hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
  1147. }
  1148. if (avctx->rc_max_rate && avctx->rc_max_rate < avctx->bit_rate) {
  1149. av_log(avctx, AV_LOG_ERROR, "Invalid bitrate settings: must have "
  1150. "bitrate (%"PRId64") <= maxrate (%"PRId64").\n",
  1151. avctx->bit_rate, avctx->rc_max_rate);
  1152. return AVERROR(EINVAL);
  1153. }
  1154. if (avctx->rc_max_rate > avctx->bit_rate) {
  1155. if (!(rc_attr.value & VA_RC_VBR)) {
  1156. av_log(avctx, AV_LOG_WARNING, "Driver does not support "
  1157. "VBR mode (%#x), using CBR mode instead.\n",
  1158. rc_attr.value);
  1159. ctx->va_rc_mode = VA_RC_CBR;
  1160. rc_bits_per_second = avctx->bit_rate;
  1161. rc_target_percentage = 100;
  1162. } else {
  1163. ctx->va_rc_mode = VA_RC_VBR;
  1164. rc_bits_per_second = avctx->rc_max_rate;
  1165. rc_target_percentage = (avctx->bit_rate * 100) /
  1166. avctx->rc_max_rate;
  1167. }
  1168. } else if (avctx->rc_max_rate == avctx->bit_rate) {
  1169. if (!(rc_attr.value & VA_RC_CBR)) {
  1170. av_log(avctx, AV_LOG_WARNING, "Driver does not support "
  1171. "CBR mode (%#x), using VBR mode instead.\n",
  1172. rc_attr.value);
  1173. ctx->va_rc_mode = VA_RC_VBR;
  1174. } else {
  1175. ctx->va_rc_mode = VA_RC_CBR;
  1176. }
  1177. rc_bits_per_second = avctx->bit_rate;
  1178. rc_target_percentage = 100;
  1179. } else {
  1180. if (rc_attr.value & VA_RC_VBR) {
  1181. ctx->va_rc_mode = VA_RC_VBR;
  1182. // We only have a target bitrate, but VAAPI requires that a
  1183. // maximum rate be supplied as well. Since the user has
  1184. // offered no particular constraint, arbitrarily pick a
  1185. // maximum rate of double the target rate.
  1186. rc_bits_per_second = 2 * avctx->bit_rate;
  1187. rc_target_percentage = 50;
  1188. } else {
  1189. ctx->va_rc_mode = VA_RC_CBR;
  1190. rc_bits_per_second = avctx->bit_rate;
  1191. rc_target_percentage = 100;
  1192. }
  1193. }
  1194. rc_window_size = (hrd_buffer_size * 1000) / rc_bits_per_second;
  1195. av_log(avctx, AV_LOG_VERBOSE, "RC mode: %s, %d%% of %"PRId64" bps "
  1196. "over %d ms.\n", ctx->va_rc_mode == VA_RC_VBR ? "VBR" : "CBR",
  1197. rc_target_percentage, rc_bits_per_second, rc_window_size);
  1198. av_log(avctx, AV_LOG_VERBOSE, "RC buffer: %"PRId64" bits, "
  1199. "initial fullness %"PRId64" bits.\n",
  1200. hrd_buffer_size, hrd_initial_buffer_fullness);
  1201. if (rc_bits_per_second > UINT32_MAX ||
  1202. hrd_buffer_size > UINT32_MAX ||
  1203. hrd_initial_buffer_fullness > UINT32_MAX) {
  1204. av_log(avctx, AV_LOG_ERROR, "RC parameters of 2^32 or "
  1205. "greater are not supported by VAAPI.\n");
  1206. return AVERROR(EINVAL);
  1207. }
  1208. ctx->va_bit_rate = rc_bits_per_second;
  1209. ctx->config_attributes[ctx->nb_config_attributes++] =
  1210. (VAConfigAttrib) {
  1211. .type = VAConfigAttribRateControl,
  1212. .value = ctx->va_rc_mode,
  1213. };
  1214. ctx->rc_params.misc.type = VAEncMiscParameterTypeRateControl;
  1215. ctx->rc_params.rc = (VAEncMiscParameterRateControl) {
  1216. .bits_per_second = rc_bits_per_second,
  1217. .target_percentage = rc_target_percentage,
  1218. .window_size = rc_window_size,
  1219. .initial_qp = 0,
  1220. .min_qp = (avctx->qmin > 0 ? avctx->qmin : 0),
  1221. .basic_unit_size = 0,
  1222. #if VA_CHECK_VERSION(1, 1, 0)
  1223. .max_qp = (avctx->qmax > 0 ? avctx->qmax : 0),
  1224. #endif
  1225. };
  1226. vaapi_encode_add_global_param(avctx, &ctx->rc_params.misc,
  1227. sizeof(ctx->rc_params));
  1228. ctx->hrd_params.misc.type = VAEncMiscParameterTypeHRD;
  1229. ctx->hrd_params.hrd = (VAEncMiscParameterHRD) {
  1230. .initial_buffer_fullness = hrd_initial_buffer_fullness,
  1231. .buffer_size = hrd_buffer_size,
  1232. };
  1233. vaapi_encode_add_global_param(avctx, &ctx->hrd_params.misc,
  1234. sizeof(ctx->hrd_params));
  1235. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1236. av_reduce(&fr_num, &fr_den,
  1237. avctx->framerate.num, avctx->framerate.den, 65535);
  1238. else
  1239. av_reduce(&fr_num, &fr_den,
  1240. avctx->time_base.den, avctx->time_base.num, 65535);
  1241. ctx->fr_params.misc.type = VAEncMiscParameterTypeFrameRate;
  1242. ctx->fr_params.fr.framerate = (unsigned int)fr_den << 16 | fr_num;
  1243. #if VA_CHECK_VERSION(0, 40, 0)
  1244. vaapi_encode_add_global_param(avctx, &ctx->fr_params.misc,
  1245. sizeof(ctx->fr_params));
  1246. #endif
  1247. return 0;
  1248. }
  1249. static av_cold int vaapi_encode_init_gop_structure(AVCodecContext *avctx)
  1250. {
  1251. VAAPIEncodeContext *ctx = avctx->priv_data;
  1252. VAStatus vas;
  1253. VAConfigAttrib attr = { VAConfigAttribEncMaxRefFrames };
  1254. uint32_t ref_l0, ref_l1;
  1255. vas = vaGetConfigAttributes(ctx->hwctx->display,
  1256. ctx->va_profile,
  1257. ctx->va_entrypoint,
  1258. &attr, 1);
  1259. if (vas != VA_STATUS_SUCCESS) {
  1260. av_log(avctx, AV_LOG_ERROR, "Failed to query reference frames "
  1261. "attribute: %d (%s).\n", vas, vaErrorStr(vas));
  1262. return AVERROR_EXTERNAL;
  1263. }
  1264. if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
  1265. ref_l0 = ref_l1 = 0;
  1266. } else {
  1267. ref_l0 = attr.value & 0xffff;
  1268. ref_l1 = attr.value >> 16 & 0xffff;
  1269. }
  1270. if (avctx->gop_size <= 1) {
  1271. av_log(avctx, AV_LOG_VERBOSE, "Using intra frames only.\n");
  1272. ctx->gop_size = 1;
  1273. } else if (ref_l0 < 1) {
  1274. av_log(avctx, AV_LOG_ERROR, "Driver does not support any "
  1275. "reference frames.\n");
  1276. return AVERROR(EINVAL);
  1277. } else if (ref_l1 < 1 || avctx->max_b_frames < 1) {
  1278. av_log(avctx, AV_LOG_VERBOSE, "Using intra and P-frames "
  1279. "(supported references: %d / %d).\n", ref_l0, ref_l1);
  1280. ctx->gop_size = avctx->gop_size;
  1281. ctx->p_per_i = INT_MAX;
  1282. ctx->b_per_p = 0;
  1283. } else {
  1284. av_log(avctx, AV_LOG_VERBOSE, "Using intra, P- and B-frames "
  1285. "(supported references: %d / %d).\n", ref_l0, ref_l1);
  1286. ctx->gop_size = avctx->gop_size;
  1287. ctx->p_per_i = INT_MAX;
  1288. ctx->b_per_p = avctx->max_b_frames;
  1289. }
  1290. return 0;
  1291. }
  1292. static av_cold int vaapi_encode_init_slice_structure(AVCodecContext *avctx)
  1293. {
  1294. VAAPIEncodeContext *ctx = avctx->priv_data;
  1295. VAConfigAttrib attr[2] = { { VAConfigAttribEncMaxSlices },
  1296. { VAConfigAttribEncSliceStructure } };
  1297. VAStatus vas;
  1298. uint32_t max_slices, slice_structure;
  1299. int req_slices;
  1300. if (!(ctx->codec->flags & FLAG_SLICE_CONTROL)) {
  1301. if (avctx->slices > 0) {
  1302. av_log(avctx, AV_LOG_WARNING, "Multiple slices were requested "
  1303. "but this codec does not support controlling slices.\n");
  1304. }
  1305. return 0;
  1306. }
  1307. ctx->slice_block_rows = (avctx->height + ctx->slice_block_height - 1) /
  1308. ctx->slice_block_height;
  1309. ctx->slice_block_cols = (avctx->width + ctx->slice_block_width - 1) /
  1310. ctx->slice_block_width;
  1311. if (avctx->slices <= 1) {
  1312. ctx->nb_slices = 1;
  1313. ctx->slice_size = ctx->slice_block_rows;
  1314. return 0;
  1315. }
  1316. vas = vaGetConfigAttributes(ctx->hwctx->display,
  1317. ctx->va_profile,
  1318. ctx->va_entrypoint,
  1319. attr, FF_ARRAY_ELEMS(attr));
  1320. if (vas != VA_STATUS_SUCCESS) {
  1321. av_log(avctx, AV_LOG_ERROR, "Failed to query slice "
  1322. "attributes: %d (%s).\n", vas, vaErrorStr(vas));
  1323. return AVERROR_EXTERNAL;
  1324. }
  1325. max_slices = attr[0].value;
  1326. slice_structure = attr[1].value;
  1327. if (max_slices == VA_ATTRIB_NOT_SUPPORTED ||
  1328. slice_structure == VA_ATTRIB_NOT_SUPPORTED) {
  1329. av_log(avctx, AV_LOG_ERROR, "Driver does not support encoding "
  1330. "pictures as multiple slices.\n.");
  1331. return AVERROR(EINVAL);
  1332. }
  1333. // For fixed-size slices currently we only support whole rows, making
  1334. // rectangular slices. This could be extended to arbitrary runs of
  1335. // blocks, but since slices tend to be a conformance requirement and
  1336. // most cases (such as broadcast or bluray) want rectangular slices
  1337. // only it would need to be gated behind another option.
  1338. if (avctx->slices > ctx->slice_block_rows) {
  1339. av_log(avctx, AV_LOG_WARNING, "Not enough rows to use "
  1340. "configured number of slices (%d < %d); using "
  1341. "maximum.\n", ctx->slice_block_rows, avctx->slices);
  1342. req_slices = ctx->slice_block_rows;
  1343. } else {
  1344. req_slices = avctx->slices;
  1345. }
  1346. if (slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_ROWS ||
  1347. slice_structure & VA_ENC_SLICE_STRUCTURE_ARBITRARY_MACROBLOCKS) {
  1348. ctx->nb_slices = req_slices;
  1349. ctx->slice_size = ctx->slice_block_rows / ctx->nb_slices;
  1350. } else if (slice_structure & VA_ENC_SLICE_STRUCTURE_POWER_OF_TWO_ROWS) {
  1351. int k;
  1352. for (k = 1;; k *= 2) {
  1353. if (2 * k * (req_slices - 1) + 1 >= ctx->slice_block_rows)
  1354. break;
  1355. }
  1356. ctx->nb_slices = (ctx->slice_block_rows + k - 1) / k;
  1357. ctx->slice_size = k;
  1358. #if VA_CHECK_VERSION(1, 0, 0)
  1359. } else if (slice_structure & VA_ENC_SLICE_STRUCTURE_EQUAL_ROWS) {
  1360. ctx->nb_slices = ctx->slice_block_rows;
  1361. ctx->slice_size = 1;
  1362. #endif
  1363. } else {
  1364. av_log(avctx, AV_LOG_ERROR, "Driver does not support any usable "
  1365. "slice structure modes (%#x).\n", slice_structure);
  1366. return AVERROR(EINVAL);
  1367. }
  1368. if (ctx->nb_slices > avctx->slices) {
  1369. av_log(avctx, AV_LOG_WARNING, "Slice count rounded up to "
  1370. "%d (from %d) due to driver constraints on slice "
  1371. "structure.\n", ctx->nb_slices, avctx->slices);
  1372. }
  1373. if (ctx->nb_slices > max_slices) {
  1374. av_log(avctx, AV_LOG_ERROR, "Driver does not support "
  1375. "encoding with %d slices (max %"PRIu32").\n",
  1376. ctx->nb_slices, max_slices);
  1377. return AVERROR(EINVAL);
  1378. }
  1379. av_log(avctx, AV_LOG_VERBOSE, "Encoding pictures with %d slices "
  1380. "(default size %d block rows).\n",
  1381. ctx->nb_slices, ctx->slice_size);
  1382. return 0;
  1383. }
  1384. static av_cold int vaapi_encode_init_packed_headers(AVCodecContext *avctx)
  1385. {
  1386. VAAPIEncodeContext *ctx = avctx->priv_data;
  1387. VAStatus vas;
  1388. VAConfigAttrib attr = { VAConfigAttribEncPackedHeaders };
  1389. vas = vaGetConfigAttributes(ctx->hwctx->display,
  1390. ctx->va_profile,
  1391. ctx->va_entrypoint,
  1392. &attr, 1);
  1393. if (vas != VA_STATUS_SUCCESS) {
  1394. av_log(avctx, AV_LOG_ERROR, "Failed to query packed headers "
  1395. "attribute: %d (%s).\n", vas, vaErrorStr(vas));
  1396. return AVERROR_EXTERNAL;
  1397. }
  1398. if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
  1399. if (ctx->desired_packed_headers) {
  1400. av_log(avctx, AV_LOG_WARNING, "Driver does not support any "
  1401. "packed headers (wanted %#x).\n",
  1402. ctx->desired_packed_headers);
  1403. } else {
  1404. av_log(avctx, AV_LOG_VERBOSE, "Driver does not support any "
  1405. "packed headers (none wanted).\n");
  1406. }
  1407. ctx->va_packed_headers = 0;
  1408. } else {
  1409. if (ctx->desired_packed_headers & ~attr.value) {
  1410. av_log(avctx, AV_LOG_WARNING, "Driver does not support some "
  1411. "wanted packed headers (wanted %#x, found %#x).\n",
  1412. ctx->desired_packed_headers, attr.value);
  1413. } else {
  1414. av_log(avctx, AV_LOG_VERBOSE, "All wanted packed headers "
  1415. "available (wanted %#x, found %#x).\n",
  1416. ctx->desired_packed_headers, attr.value);
  1417. }
  1418. ctx->va_packed_headers = ctx->desired_packed_headers & attr.value;
  1419. }
  1420. if (ctx->va_packed_headers) {
  1421. ctx->config_attributes[ctx->nb_config_attributes++] =
  1422. (VAConfigAttrib) {
  1423. .type = VAConfigAttribEncPackedHeaders,
  1424. .value = ctx->va_packed_headers,
  1425. };
  1426. }
  1427. if ( (ctx->desired_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
  1428. !(ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE) &&
  1429. (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) {
  1430. av_log(avctx, AV_LOG_WARNING, "Driver does not support packed "
  1431. "sequence headers, but a global header is requested.\n");
  1432. av_log(avctx, AV_LOG_WARNING, "No global header will be written: "
  1433. "this may result in a stream which is not usable for some "
  1434. "purposes (e.g. not muxable to some containers).\n");
  1435. }
  1436. return 0;
  1437. }
  1438. static av_cold int vaapi_encode_init_quality(AVCodecContext *avctx)
  1439. {
  1440. #if VA_CHECK_VERSION(0, 36, 0)
  1441. VAAPIEncodeContext *ctx = avctx->priv_data;
  1442. VAStatus vas;
  1443. VAConfigAttrib attr = { VAConfigAttribEncQualityRange };
  1444. int quality = avctx->compression_level;
  1445. vas = vaGetConfigAttributes(ctx->hwctx->display,
  1446. ctx->va_profile,
  1447. ctx->va_entrypoint,
  1448. &attr, 1);
  1449. if (vas != VA_STATUS_SUCCESS) {
  1450. av_log(avctx, AV_LOG_ERROR, "Failed to query quality "
  1451. "config attribute: %d (%s).\n", vas, vaErrorStr(vas));
  1452. return AVERROR_EXTERNAL;
  1453. }
  1454. if (attr.value == VA_ATTRIB_NOT_SUPPORTED) {
  1455. if (quality != 0) {
  1456. av_log(avctx, AV_LOG_WARNING, "Quality attribute is not "
  1457. "supported: will use default quality level.\n");
  1458. }
  1459. } else {
  1460. if (quality > attr.value) {
  1461. av_log(avctx, AV_LOG_WARNING, "Invalid quality level: "
  1462. "valid range is 0-%d, using %d.\n",
  1463. attr.value, attr.value);
  1464. quality = attr.value;
  1465. }
  1466. ctx->quality_params.misc.type = VAEncMiscParameterTypeQualityLevel;
  1467. ctx->quality_params.quality.quality_level = quality;
  1468. vaapi_encode_add_global_param(avctx, &ctx->quality_params.misc,
  1469. sizeof(ctx->quality_params));
  1470. }
  1471. #else
  1472. av_log(avctx, AV_LOG_WARNING, "The encode quality option is "
  1473. "not supported with this VAAPI version.\n");
  1474. #endif
  1475. return 0;
  1476. }
  1477. static void vaapi_encode_free_output_buffer(void *opaque,
  1478. uint8_t *data)
  1479. {
  1480. AVCodecContext *avctx = opaque;
  1481. VAAPIEncodeContext *ctx = avctx->priv_data;
  1482. VABufferID buffer_id;
  1483. buffer_id = (VABufferID)(uintptr_t)data;
  1484. vaDestroyBuffer(ctx->hwctx->display, buffer_id);
  1485. av_log(avctx, AV_LOG_DEBUG, "Freed output buffer %#x\n", buffer_id);
  1486. }
  1487. static AVBufferRef *vaapi_encode_alloc_output_buffer(void *opaque,
  1488. int size)
  1489. {
  1490. AVCodecContext *avctx = opaque;
  1491. VAAPIEncodeContext *ctx = avctx->priv_data;
  1492. VABufferID buffer_id;
  1493. VAStatus vas;
  1494. AVBufferRef *ref;
  1495. // The output buffer size is fixed, so it needs to be large enough
  1496. // to hold the largest possible compressed frame. We assume here
  1497. // that the uncompressed frame plus some header data is an upper
  1498. // bound on that.
  1499. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  1500. VAEncCodedBufferType,
  1501. 3 * ctx->surface_width * ctx->surface_height +
  1502. (1 << 16), 1, 0, &buffer_id);
  1503. if (vas != VA_STATUS_SUCCESS) {
  1504. av_log(avctx, AV_LOG_ERROR, "Failed to create bitstream "
  1505. "output buffer: %d (%s).\n", vas, vaErrorStr(vas));
  1506. return NULL;
  1507. }
  1508. av_log(avctx, AV_LOG_DEBUG, "Allocated output buffer %#x\n", buffer_id);
  1509. ref = av_buffer_create((uint8_t*)(uintptr_t)buffer_id,
  1510. sizeof(buffer_id),
  1511. &vaapi_encode_free_output_buffer,
  1512. avctx, AV_BUFFER_FLAG_READONLY);
  1513. if (!ref) {
  1514. vaDestroyBuffer(ctx->hwctx->display, buffer_id);
  1515. return NULL;
  1516. }
  1517. return ref;
  1518. }
  1519. static av_cold int vaapi_encode_create_recon_frames(AVCodecContext *avctx)
  1520. {
  1521. VAAPIEncodeContext *ctx = avctx->priv_data;
  1522. AVVAAPIHWConfig *hwconfig = NULL;
  1523. AVHWFramesConstraints *constraints = NULL;
  1524. enum AVPixelFormat recon_format;
  1525. int err, i;
  1526. hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
  1527. if (!hwconfig) {
  1528. err = AVERROR(ENOMEM);
  1529. goto fail;
  1530. }
  1531. hwconfig->config_id = ctx->va_config;
  1532. constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
  1533. hwconfig);
  1534. if (!constraints) {
  1535. err = AVERROR(ENOMEM);
  1536. goto fail;
  1537. }
  1538. // Probably we can use the input surface format as the surface format
  1539. // of the reconstructed frames. If not, we just pick the first (only?)
  1540. // format in the valid list and hope that it all works.
  1541. recon_format = AV_PIX_FMT_NONE;
  1542. if (constraints->valid_sw_formats) {
  1543. for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
  1544. if (ctx->input_frames->sw_format ==
  1545. constraints->valid_sw_formats[i]) {
  1546. recon_format = ctx->input_frames->sw_format;
  1547. break;
  1548. }
  1549. }
  1550. if (recon_format == AV_PIX_FMT_NONE) {
  1551. // No match. Just use the first in the supported list and
  1552. // hope for the best.
  1553. recon_format = constraints->valid_sw_formats[0];
  1554. }
  1555. } else {
  1556. // No idea what to use; copy input format.
  1557. recon_format = ctx->input_frames->sw_format;
  1558. }
  1559. av_log(avctx, AV_LOG_DEBUG, "Using %s as format of "
  1560. "reconstructed frames.\n", av_get_pix_fmt_name(recon_format));
  1561. if (ctx->surface_width < constraints->min_width ||
  1562. ctx->surface_height < constraints->min_height ||
  1563. ctx->surface_width > constraints->max_width ||
  1564. ctx->surface_height > constraints->max_height) {
  1565. av_log(avctx, AV_LOG_ERROR, "Hardware does not support encoding at "
  1566. "size %dx%d (constraints: width %d-%d height %d-%d).\n",
  1567. ctx->surface_width, ctx->surface_height,
  1568. constraints->min_width, constraints->max_width,
  1569. constraints->min_height, constraints->max_height);
  1570. err = AVERROR(EINVAL);
  1571. goto fail;
  1572. }
  1573. av_freep(&hwconfig);
  1574. av_hwframe_constraints_free(&constraints);
  1575. ctx->recon_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
  1576. if (!ctx->recon_frames_ref) {
  1577. err = AVERROR(ENOMEM);
  1578. goto fail;
  1579. }
  1580. ctx->recon_frames = (AVHWFramesContext*)ctx->recon_frames_ref->data;
  1581. ctx->recon_frames->format = AV_PIX_FMT_VAAPI;
  1582. ctx->recon_frames->sw_format = recon_format;
  1583. ctx->recon_frames->width = ctx->surface_width;
  1584. ctx->recon_frames->height = ctx->surface_height;
  1585. // At most three IDR/I/P frames and two runs of B frames can be in
  1586. // flight at any one time.
  1587. ctx->recon_frames->initial_pool_size = 3 + 2 * ctx->b_per_p;
  1588. err = av_hwframe_ctx_init(ctx->recon_frames_ref);
  1589. if (err < 0) {
  1590. av_log(avctx, AV_LOG_ERROR, "Failed to initialise reconstructed "
  1591. "frame context: %d.\n", err);
  1592. goto fail;
  1593. }
  1594. err = 0;
  1595. fail:
  1596. av_freep(&hwconfig);
  1597. av_hwframe_constraints_free(&constraints);
  1598. return err;
  1599. }
  1600. av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
  1601. {
  1602. VAAPIEncodeContext *ctx = avctx->priv_data;
  1603. AVVAAPIFramesContext *recon_hwctx = NULL;
  1604. VAStatus vas;
  1605. int err;
  1606. if (!avctx->hw_frames_ctx) {
  1607. av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
  1608. "required to associate the encoding device.\n");
  1609. return AVERROR(EINVAL);
  1610. }
  1611. ctx->va_config = VA_INVALID_ID;
  1612. ctx->va_context = VA_INVALID_ID;
  1613. ctx->input_frames_ref = av_buffer_ref(avctx->hw_frames_ctx);
  1614. if (!ctx->input_frames_ref) {
  1615. err = AVERROR(ENOMEM);
  1616. goto fail;
  1617. }
  1618. ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
  1619. ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
  1620. if (!ctx->device_ref) {
  1621. err = AVERROR(ENOMEM);
  1622. goto fail;
  1623. }
  1624. ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
  1625. ctx->hwctx = ctx->device->hwctx;
  1626. err = vaapi_encode_profile_entrypoint(avctx);
  1627. if (err < 0)
  1628. goto fail;
  1629. err = vaapi_encode_init_rate_control(avctx);
  1630. if (err < 0)
  1631. goto fail;
  1632. err = vaapi_encode_init_gop_structure(avctx);
  1633. if (err < 0)
  1634. goto fail;
  1635. err = vaapi_encode_init_slice_structure(avctx);
  1636. if (err < 0)
  1637. goto fail;
  1638. err = vaapi_encode_init_packed_headers(avctx);
  1639. if (err < 0)
  1640. goto fail;
  1641. if (avctx->compression_level >= 0) {
  1642. err = vaapi_encode_init_quality(avctx);
  1643. if (err < 0)
  1644. goto fail;
  1645. }
  1646. vas = vaCreateConfig(ctx->hwctx->display,
  1647. ctx->va_profile, ctx->va_entrypoint,
  1648. ctx->config_attributes, ctx->nb_config_attributes,
  1649. &ctx->va_config);
  1650. if (vas != VA_STATUS_SUCCESS) {
  1651. av_log(avctx, AV_LOG_ERROR, "Failed to create encode pipeline "
  1652. "configuration: %d (%s).\n", vas, vaErrorStr(vas));
  1653. err = AVERROR(EIO);
  1654. goto fail;
  1655. }
  1656. err = vaapi_encode_create_recon_frames(avctx);
  1657. if (err < 0)
  1658. goto fail;
  1659. recon_hwctx = ctx->recon_frames->hwctx;
  1660. vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
  1661. ctx->surface_width, ctx->surface_height,
  1662. VA_PROGRESSIVE,
  1663. recon_hwctx->surface_ids,
  1664. recon_hwctx->nb_surfaces,
  1665. &ctx->va_context);
  1666. if (vas != VA_STATUS_SUCCESS) {
  1667. av_log(avctx, AV_LOG_ERROR, "Failed to create encode pipeline "
  1668. "context: %d (%s).\n", vas, vaErrorStr(vas));
  1669. err = AVERROR(EIO);
  1670. goto fail;
  1671. }
  1672. ctx->output_buffer_pool =
  1673. av_buffer_pool_init2(sizeof(VABufferID), avctx,
  1674. &vaapi_encode_alloc_output_buffer, NULL);
  1675. if (!ctx->output_buffer_pool) {
  1676. err = AVERROR(ENOMEM);
  1677. goto fail;
  1678. }
  1679. if (ctx->codec->configure) {
  1680. err = ctx->codec->configure(avctx);
  1681. if (err < 0)
  1682. goto fail;
  1683. }
  1684. ctx->input_order = 0;
  1685. ctx->output_delay = ctx->b_per_p;
  1686. ctx->decode_delay = 1;
  1687. ctx->output_order = - ctx->output_delay - 1;
  1688. if (ctx->codec->sequence_params_size > 0) {
  1689. ctx->codec_sequence_params =
  1690. av_mallocz(ctx->codec->sequence_params_size);
  1691. if (!ctx->codec_sequence_params) {
  1692. err = AVERROR(ENOMEM);
  1693. goto fail;
  1694. }
  1695. }
  1696. if (ctx->codec->picture_params_size > 0) {
  1697. ctx->codec_picture_params =
  1698. av_mallocz(ctx->codec->picture_params_size);
  1699. if (!ctx->codec_picture_params) {
  1700. err = AVERROR(ENOMEM);
  1701. goto fail;
  1702. }
  1703. }
  1704. if (ctx->codec->init_sequence_params) {
  1705. err = ctx->codec->init_sequence_params(avctx);
  1706. if (err < 0) {
  1707. av_log(avctx, AV_LOG_ERROR, "Codec sequence initialisation "
  1708. "failed: %d.\n", err);
  1709. goto fail;
  1710. }
  1711. }
  1712. // This should be configurable somehow. (Needs testing on a machine
  1713. // where it actually overlaps properly, though.)
  1714. ctx->issue_mode = ISSUE_MODE_MAXIMISE_THROUGHPUT;
  1715. if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
  1716. ctx->codec->write_sequence_header &&
  1717. avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  1718. char data[MAX_PARAM_BUFFER_SIZE];
  1719. size_t bit_len = 8 * sizeof(data);
  1720. err = ctx->codec->write_sequence_header(avctx, data, &bit_len);
  1721. if (err < 0) {
  1722. av_log(avctx, AV_LOG_ERROR, "Failed to write sequence header "
  1723. "for extradata: %d.\n", err);
  1724. goto fail;
  1725. } else {
  1726. avctx->extradata_size = (bit_len + 7) / 8;
  1727. avctx->extradata = av_mallocz(avctx->extradata_size +
  1728. AV_INPUT_BUFFER_PADDING_SIZE);
  1729. if (!avctx->extradata) {
  1730. err = AVERROR(ENOMEM);
  1731. goto fail;
  1732. }
  1733. memcpy(avctx->extradata, data, avctx->extradata_size);
  1734. }
  1735. }
  1736. return 0;
  1737. fail:
  1738. ff_vaapi_encode_close(avctx);
  1739. return err;
  1740. }
  1741. av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
  1742. {
  1743. VAAPIEncodeContext *ctx = avctx->priv_data;
  1744. VAAPIEncodePicture *pic, *next;
  1745. for (pic = ctx->pic_start; pic; pic = next) {
  1746. next = pic->next;
  1747. vaapi_encode_free(avctx, pic);
  1748. }
  1749. av_buffer_pool_uninit(&ctx->output_buffer_pool);
  1750. if (ctx->va_context != VA_INVALID_ID) {
  1751. vaDestroyContext(ctx->hwctx->display, ctx->va_context);
  1752. ctx->va_context = VA_INVALID_ID;
  1753. }
  1754. if (ctx->va_config != VA_INVALID_ID) {
  1755. vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  1756. ctx->va_config = VA_INVALID_ID;
  1757. }
  1758. av_freep(&ctx->codec_sequence_params);
  1759. av_freep(&ctx->codec_picture_params);
  1760. av_buffer_unref(&ctx->recon_frames_ref);
  1761. av_buffer_unref(&ctx->input_frames_ref);
  1762. av_buffer_unref(&ctx->device_ref);
  1763. return 0;
  1764. }