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.

1590 lines
53KB

  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->encode_order == 0) {
  181. // Global parameter buffers are set on the first picture only.
  182. for (i = 0; i < ctx->nb_global_params; i++) {
  183. err = vaapi_encode_make_param_buffer(avctx, pic,
  184. VAEncMiscParameterBufferType,
  185. (char*)ctx->global_params[i],
  186. ctx->global_params_size[i]);
  187. if (err < 0)
  188. goto fail;
  189. }
  190. }
  191. if (pic->type == PICTURE_TYPE_IDR && ctx->codec->init_sequence_params) {
  192. err = vaapi_encode_make_param_buffer(avctx, pic,
  193. VAEncSequenceParameterBufferType,
  194. ctx->codec_sequence_params,
  195. ctx->codec->sequence_params_size);
  196. if (err < 0)
  197. goto fail;
  198. }
  199. if (ctx->codec->init_picture_params) {
  200. err = ctx->codec->init_picture_params(avctx, pic);
  201. if (err < 0) {
  202. av_log(avctx, AV_LOG_ERROR, "Failed to initialise picture "
  203. "parameters: %d.\n", err);
  204. goto fail;
  205. }
  206. err = vaapi_encode_make_param_buffer(avctx, pic,
  207. VAEncPictureParameterBufferType,
  208. pic->codec_picture_params,
  209. ctx->codec->picture_params_size);
  210. if (err < 0)
  211. goto fail;
  212. }
  213. if (pic->type == PICTURE_TYPE_IDR) {
  214. if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
  215. ctx->codec->write_sequence_header) {
  216. bit_len = 8 * sizeof(data);
  217. err = ctx->codec->write_sequence_header(avctx, data, &bit_len);
  218. if (err < 0) {
  219. av_log(avctx, AV_LOG_ERROR, "Failed to write per-sequence "
  220. "header: %d.\n", err);
  221. goto fail;
  222. }
  223. err = vaapi_encode_make_packed_header(avctx, pic,
  224. ctx->codec->sequence_header_type,
  225. data, bit_len);
  226. if (err < 0)
  227. goto fail;
  228. }
  229. }
  230. if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_PICTURE &&
  231. ctx->codec->write_picture_header) {
  232. bit_len = 8 * sizeof(data);
  233. err = ctx->codec->write_picture_header(avctx, pic, data, &bit_len);
  234. if (err < 0) {
  235. av_log(avctx, AV_LOG_ERROR, "Failed to write per-picture "
  236. "header: %d.\n", err);
  237. goto fail;
  238. }
  239. err = vaapi_encode_make_packed_header(avctx, pic,
  240. ctx->codec->picture_header_type,
  241. data, bit_len);
  242. if (err < 0)
  243. goto fail;
  244. }
  245. if (ctx->codec->write_extra_buffer) {
  246. for (i = 0;; i++) {
  247. size_t len = sizeof(data);
  248. int type;
  249. err = ctx->codec->write_extra_buffer(avctx, pic, i, &type,
  250. data, &len);
  251. if (err == AVERROR_EOF)
  252. break;
  253. if (err < 0) {
  254. av_log(avctx, AV_LOG_ERROR, "Failed to write extra "
  255. "buffer %d: %d.\n", i, err);
  256. goto fail;
  257. }
  258. err = vaapi_encode_make_param_buffer(avctx, pic, type,
  259. data, len);
  260. if (err < 0)
  261. goto fail;
  262. }
  263. }
  264. if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_MISC &&
  265. ctx->codec->write_extra_header) {
  266. for (i = 0;; i++) {
  267. int type;
  268. bit_len = 8 * sizeof(data);
  269. err = ctx->codec->write_extra_header(avctx, pic, i, &type,
  270. data, &bit_len);
  271. if (err == AVERROR_EOF)
  272. break;
  273. if (err < 0) {
  274. av_log(avctx, AV_LOG_ERROR, "Failed to write extra "
  275. "header %d: %d.\n", i, err);
  276. goto fail;
  277. }
  278. err = vaapi_encode_make_packed_header(avctx, pic, type,
  279. data, bit_len);
  280. if (err < 0)
  281. goto fail;
  282. }
  283. }
  284. if (pic->nb_slices > 0) {
  285. pic->slices = av_mallocz_array(pic->nb_slices, sizeof(*pic->slices));
  286. if (!pic->slices) {
  287. err = AVERROR(ENOMEM);
  288. goto fail;
  289. }
  290. }
  291. for (i = 0; i < pic->nb_slices; i++) {
  292. slice = &pic->slices[i];
  293. slice->index = i;
  294. if (ctx->codec->slice_params_size > 0) {
  295. slice->codec_slice_params = av_mallocz(ctx->codec->slice_params_size);
  296. if (!slice->codec_slice_params) {
  297. err = AVERROR(ENOMEM);
  298. goto fail;
  299. }
  300. }
  301. if (ctx->codec->init_slice_params) {
  302. err = ctx->codec->init_slice_params(avctx, pic, slice);
  303. if (err < 0) {
  304. av_log(avctx, AV_LOG_ERROR, "Failed to initialise slice "
  305. "parameters: %d.\n", err);
  306. goto fail;
  307. }
  308. }
  309. if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SLICE &&
  310. ctx->codec->write_slice_header) {
  311. bit_len = 8 * sizeof(data);
  312. err = ctx->codec->write_slice_header(avctx, pic, slice,
  313. data, &bit_len);
  314. if (err < 0) {
  315. av_log(avctx, AV_LOG_ERROR, "Failed to write per-slice "
  316. "header: %d.\n", err);
  317. goto fail;
  318. }
  319. err = vaapi_encode_make_packed_header(avctx, pic,
  320. ctx->codec->slice_header_type,
  321. data, bit_len);
  322. if (err < 0)
  323. goto fail;
  324. }
  325. if (ctx->codec->init_slice_params) {
  326. err = vaapi_encode_make_param_buffer(avctx, pic,
  327. VAEncSliceParameterBufferType,
  328. slice->codec_slice_params,
  329. ctx->codec->slice_params_size);
  330. if (err < 0)
  331. goto fail;
  332. }
  333. }
  334. vas = vaBeginPicture(ctx->hwctx->display, ctx->va_context,
  335. pic->input_surface);
  336. if (vas != VA_STATUS_SUCCESS) {
  337. av_log(avctx, AV_LOG_ERROR, "Failed to begin picture encode issue: "
  338. "%d (%s).\n", vas, vaErrorStr(vas));
  339. err = AVERROR(EIO);
  340. goto fail_with_picture;
  341. }
  342. vas = vaRenderPicture(ctx->hwctx->display, ctx->va_context,
  343. pic->param_buffers, pic->nb_param_buffers);
  344. if (vas != VA_STATUS_SUCCESS) {
  345. av_log(avctx, AV_LOG_ERROR, "Failed to upload encode parameters: "
  346. "%d (%s).\n", vas, vaErrorStr(vas));
  347. err = AVERROR(EIO);
  348. goto fail_with_picture;
  349. }
  350. vas = vaEndPicture(ctx->hwctx->display, ctx->va_context);
  351. if (vas != VA_STATUS_SUCCESS) {
  352. av_log(avctx, AV_LOG_ERROR, "Failed to end picture encode issue: "
  353. "%d (%s).\n", vas, vaErrorStr(vas));
  354. err = AVERROR(EIO);
  355. // vaRenderPicture() has been called here, so we should not destroy
  356. // the parameter buffers unless separate destruction is required.
  357. if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
  358. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS)
  359. goto fail;
  360. else
  361. goto fail_at_end;
  362. }
  363. if (CONFIG_VAAPI_1 || ctx->hwctx->driver_quirks &
  364. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS) {
  365. for (i = 0; i < pic->nb_param_buffers; i++) {
  366. vas = vaDestroyBuffer(ctx->hwctx->display,
  367. pic->param_buffers[i]);
  368. if (vas != VA_STATUS_SUCCESS) {
  369. av_log(avctx, AV_LOG_ERROR, "Failed to destroy "
  370. "param buffer %#x: %d (%s).\n",
  371. pic->param_buffers[i], vas, vaErrorStr(vas));
  372. // And ignore.
  373. }
  374. }
  375. }
  376. pic->encode_issued = 1;
  377. if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING)
  378. return vaapi_encode_wait(avctx, pic);
  379. else
  380. return 0;
  381. fail_with_picture:
  382. vaEndPicture(ctx->hwctx->display, ctx->va_context);
  383. fail:
  384. for(i = 0; i < pic->nb_param_buffers; i++)
  385. vaDestroyBuffer(ctx->hwctx->display, pic->param_buffers[i]);
  386. for (i = 0; i < pic->nb_slices; i++) {
  387. if (pic->slices) {
  388. av_freep(&pic->slices[i].priv_data);
  389. av_freep(&pic->slices[i].codec_slice_params);
  390. }
  391. }
  392. fail_at_end:
  393. av_freep(&pic->codec_picture_params);
  394. av_freep(&pic->param_buffers);
  395. av_freep(&pic->slices);
  396. av_frame_free(&pic->recon_image);
  397. av_buffer_unref(&pic->output_buffer_ref);
  398. pic->output_buffer = VA_INVALID_ID;
  399. return err;
  400. }
  401. static int vaapi_encode_output(AVCodecContext *avctx,
  402. VAAPIEncodePicture *pic, AVPacket *pkt)
  403. {
  404. VAAPIEncodeContext *ctx = avctx->priv_data;
  405. VACodedBufferSegment *buf_list, *buf;
  406. VAStatus vas;
  407. int err;
  408. err = vaapi_encode_wait(avctx, pic);
  409. if (err < 0)
  410. return err;
  411. buf_list = NULL;
  412. vas = vaMapBuffer(ctx->hwctx->display, pic->output_buffer,
  413. (void**)&buf_list);
  414. if (vas != VA_STATUS_SUCCESS) {
  415. av_log(avctx, AV_LOG_ERROR, "Failed to map output buffers: "
  416. "%d (%s).\n", vas, vaErrorStr(vas));
  417. err = AVERROR(EIO);
  418. goto fail;
  419. }
  420. for (buf = buf_list; buf; buf = buf->next) {
  421. av_log(avctx, AV_LOG_DEBUG, "Output buffer: %u bytes "
  422. "(status %08x).\n", buf->size, buf->status);
  423. err = av_new_packet(pkt, buf->size);
  424. if (err < 0)
  425. goto fail_mapped;
  426. memcpy(pkt->data, buf->buf, buf->size);
  427. }
  428. if (pic->type == PICTURE_TYPE_IDR)
  429. pkt->flags |= AV_PKT_FLAG_KEY;
  430. pkt->pts = pic->pts;
  431. vas = vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
  432. if (vas != VA_STATUS_SUCCESS) {
  433. av_log(avctx, AV_LOG_ERROR, "Failed to unmap output buffers: "
  434. "%d (%s).\n", vas, vaErrorStr(vas));
  435. err = AVERROR(EIO);
  436. goto fail;
  437. }
  438. av_buffer_unref(&pic->output_buffer_ref);
  439. pic->output_buffer = VA_INVALID_ID;
  440. av_log(avctx, AV_LOG_DEBUG, "Output read for pic %"PRId64"/%"PRId64".\n",
  441. pic->display_order, pic->encode_order);
  442. return 0;
  443. fail_mapped:
  444. vaUnmapBuffer(ctx->hwctx->display, pic->output_buffer);
  445. fail:
  446. av_buffer_unref(&pic->output_buffer_ref);
  447. pic->output_buffer = VA_INVALID_ID;
  448. return err;
  449. }
  450. static int vaapi_encode_discard(AVCodecContext *avctx,
  451. VAAPIEncodePicture *pic)
  452. {
  453. vaapi_encode_wait(avctx, pic);
  454. if (pic->output_buffer_ref) {
  455. av_log(avctx, AV_LOG_DEBUG, "Discard output for pic "
  456. "%"PRId64"/%"PRId64".\n",
  457. pic->display_order, pic->encode_order);
  458. av_buffer_unref(&pic->output_buffer_ref);
  459. pic->output_buffer = VA_INVALID_ID;
  460. }
  461. return 0;
  462. }
  463. static VAAPIEncodePicture *vaapi_encode_alloc(void)
  464. {
  465. VAAPIEncodePicture *pic;
  466. pic = av_mallocz(sizeof(*pic));
  467. if (!pic)
  468. return NULL;
  469. pic->input_surface = VA_INVALID_ID;
  470. pic->recon_surface = VA_INVALID_ID;
  471. pic->output_buffer = VA_INVALID_ID;
  472. return pic;
  473. }
  474. static int vaapi_encode_free(AVCodecContext *avctx,
  475. VAAPIEncodePicture *pic)
  476. {
  477. int i;
  478. if (pic->encode_issued)
  479. vaapi_encode_discard(avctx, pic);
  480. for (i = 0; i < pic->nb_slices; i++) {
  481. if (pic->slices) {
  482. av_freep(&pic->slices[i].priv_data);
  483. av_freep(&pic->slices[i].codec_slice_params);
  484. }
  485. }
  486. av_freep(&pic->codec_picture_params);
  487. av_frame_free(&pic->input_image);
  488. av_frame_free(&pic->recon_image);
  489. av_freep(&pic->param_buffers);
  490. av_freep(&pic->slices);
  491. // Output buffer should already be destroyed.
  492. av_assert0(pic->output_buffer == VA_INVALID_ID);
  493. av_freep(&pic->priv_data);
  494. av_freep(&pic->codec_picture_params);
  495. av_free(pic);
  496. return 0;
  497. }
  498. static int vaapi_encode_step(AVCodecContext *avctx,
  499. VAAPIEncodePicture *target)
  500. {
  501. VAAPIEncodeContext *ctx = avctx->priv_data;
  502. VAAPIEncodePicture *pic;
  503. int i, err;
  504. if (ctx->issue_mode == ISSUE_MODE_SERIALISE_EVERYTHING ||
  505. ctx->issue_mode == ISSUE_MODE_MINIMISE_LATENCY) {
  506. // These two modes are equivalent, except that we wait for
  507. // immediate completion on each operation if serialised.
  508. if (!target) {
  509. // No target, nothing to do yet.
  510. return 0;
  511. }
  512. if (target->encode_complete) {
  513. // Already done.
  514. return 0;
  515. }
  516. pic = target;
  517. for (i = 0; i < pic->nb_refs; i++) {
  518. if (!pic->refs[i]->encode_complete) {
  519. err = vaapi_encode_step(avctx, pic->refs[i]);
  520. if (err < 0)
  521. return err;
  522. }
  523. }
  524. err = vaapi_encode_issue(avctx, pic);
  525. if (err < 0)
  526. return err;
  527. } else if (ctx->issue_mode == ISSUE_MODE_MAXIMISE_THROUGHPUT) {
  528. int activity;
  529. // Run through the list of all available pictures repeatedly
  530. // and issue the first one found which has all dependencies
  531. // available (including previously-issued but not necessarily
  532. // completed pictures).
  533. do {
  534. activity = 0;
  535. for (pic = ctx->pic_start; pic; pic = pic->next) {
  536. if (!pic->input_available || pic->encode_issued)
  537. continue;
  538. for (i = 0; i < pic->nb_refs; i++) {
  539. if (!pic->refs[i]->encode_issued)
  540. break;
  541. }
  542. if (i < pic->nb_refs)
  543. continue;
  544. err = vaapi_encode_issue(avctx, pic);
  545. if (err < 0)
  546. return err;
  547. activity = 1;
  548. // Start again from the beginning of the list,
  549. // because issuing this picture may have satisfied
  550. // forward dependencies of earlier ones.
  551. break;
  552. }
  553. } while(activity);
  554. // If we had a defined target for this step then it will
  555. // always have been issued by now.
  556. if (target) {
  557. av_assert0(target->encode_issued && "broken dependencies?");
  558. }
  559. } else {
  560. av_assert0(0);
  561. }
  562. return 0;
  563. }
  564. static int vaapi_encode_get_next(AVCodecContext *avctx,
  565. VAAPIEncodePicture **pic_out)
  566. {
  567. VAAPIEncodeContext *ctx = avctx->priv_data;
  568. VAAPIEncodePicture *start, *end, *pic;
  569. int i;
  570. for (pic = ctx->pic_start; pic; pic = pic->next) {
  571. if (pic->next)
  572. av_assert0(pic->display_order + 1 == pic->next->display_order);
  573. if (pic->display_order == ctx->input_order) {
  574. *pic_out = pic;
  575. return 0;
  576. }
  577. }
  578. pic = vaapi_encode_alloc();
  579. if (!pic)
  580. return AVERROR(ENOMEM);
  581. if (ctx->input_order == 0 || ctx->force_idr ||
  582. ctx->gop_counter >= avctx->gop_size) {
  583. pic->type = PICTURE_TYPE_IDR;
  584. ctx->force_idr = 0;
  585. ctx->gop_counter = 1;
  586. ctx->p_counter = 0;
  587. } else if (ctx->p_counter >= ctx->p_per_i) {
  588. pic->type = PICTURE_TYPE_I;
  589. ++ctx->gop_counter;
  590. ctx->p_counter = 0;
  591. } else {
  592. pic->type = PICTURE_TYPE_P;
  593. pic->refs[0] = ctx->pic_end;
  594. pic->nb_refs = 1;
  595. ++ctx->gop_counter;
  596. ++ctx->p_counter;
  597. }
  598. start = end = pic;
  599. if (pic->type != PICTURE_TYPE_IDR) {
  600. // If that was not an IDR frame, add B-frames display-before and
  601. // encode-after it, but not exceeding the GOP size.
  602. for (i = 0; i < ctx->b_per_p &&
  603. ctx->gop_counter < avctx->gop_size; i++) {
  604. pic = vaapi_encode_alloc();
  605. if (!pic)
  606. goto fail;
  607. pic->type = PICTURE_TYPE_B;
  608. pic->refs[0] = ctx->pic_end;
  609. pic->refs[1] = end;
  610. pic->nb_refs = 2;
  611. pic->next = start;
  612. pic->display_order = ctx->input_order + ctx->b_per_p - i - 1;
  613. pic->encode_order = pic->display_order + 1;
  614. start = pic;
  615. ++ctx->gop_counter;
  616. }
  617. }
  618. if (ctx->input_order == 0) {
  619. pic->display_order = 0;
  620. pic->encode_order = 0;
  621. ctx->pic_start = ctx->pic_end = pic;
  622. } else {
  623. for (i = 0, pic = start; pic; i++, pic = pic->next) {
  624. pic->display_order = ctx->input_order + i;
  625. if (end->type == PICTURE_TYPE_IDR)
  626. pic->encode_order = ctx->input_order + i;
  627. else if (pic == end)
  628. pic->encode_order = ctx->input_order;
  629. else
  630. pic->encode_order = ctx->input_order + i + 1;
  631. }
  632. av_assert0(ctx->pic_end);
  633. ctx->pic_end->next = start;
  634. ctx->pic_end = end;
  635. }
  636. *pic_out = start;
  637. av_log(avctx, AV_LOG_DEBUG, "Pictures:");
  638. for (pic = ctx->pic_start; pic; pic = pic->next) {
  639. av_log(avctx, AV_LOG_DEBUG, " %s (%"PRId64"/%"PRId64")",
  640. picture_type_name[pic->type],
  641. pic->display_order, pic->encode_order);
  642. }
  643. av_log(avctx, AV_LOG_DEBUG, "\n");
  644. return 0;
  645. fail:
  646. while (start) {
  647. pic = start->next;
  648. vaapi_encode_free(avctx, start);
  649. start = pic;
  650. }
  651. return AVERROR(ENOMEM);
  652. }
  653. static int vaapi_encode_truncate_gop(AVCodecContext *avctx)
  654. {
  655. VAAPIEncodeContext *ctx = avctx->priv_data;
  656. VAAPIEncodePicture *pic, *last_pic, *next;
  657. // Find the last picture we actually have input for.
  658. for (pic = ctx->pic_start; pic; pic = pic->next) {
  659. if (!pic->input_available)
  660. break;
  661. last_pic = pic;
  662. }
  663. if (pic) {
  664. av_assert0(last_pic);
  665. if (last_pic->type == PICTURE_TYPE_B) {
  666. // Some fixing up is required. Change the type of this
  667. // picture to P, then modify preceding B references which
  668. // point beyond it to point at it instead.
  669. last_pic->type = PICTURE_TYPE_P;
  670. last_pic->encode_order = last_pic->refs[1]->encode_order;
  671. for (pic = ctx->pic_start; pic != last_pic; pic = pic->next) {
  672. if (pic->type == PICTURE_TYPE_B &&
  673. pic->refs[1] == last_pic->refs[1])
  674. pic->refs[1] = last_pic;
  675. }
  676. last_pic->nb_refs = 1;
  677. last_pic->refs[1] = NULL;
  678. } else {
  679. // We can use the current structure (no references point
  680. // beyond the end), but there are unused pics to discard.
  681. }
  682. // Discard all following pics, they will never be used.
  683. for (pic = last_pic->next; pic; pic = next) {
  684. next = pic->next;
  685. vaapi_encode_free(avctx, pic);
  686. }
  687. last_pic->next = NULL;
  688. ctx->pic_end = last_pic;
  689. } else {
  690. // Input is available for all pictures, so we don't need to
  691. // mangle anything.
  692. }
  693. av_log(avctx, AV_LOG_DEBUG, "Pictures ending truncated GOP:");
  694. for (pic = ctx->pic_start; pic; pic = pic->next) {
  695. av_log(avctx, AV_LOG_DEBUG, " %s (%"PRId64"/%"PRId64")",
  696. picture_type_name[pic->type],
  697. pic->display_order, pic->encode_order);
  698. }
  699. av_log(avctx, AV_LOG_DEBUG, "\n");
  700. return 0;
  701. }
  702. static int vaapi_encode_clear_old(AVCodecContext *avctx)
  703. {
  704. VAAPIEncodeContext *ctx = avctx->priv_data;
  705. VAAPIEncodePicture *pic, *old;
  706. int i;
  707. while (ctx->pic_start != ctx->pic_end) {
  708. old = ctx->pic_start;
  709. if (old->encode_order > ctx->output_order)
  710. break;
  711. for (pic = old->next; pic; pic = pic->next) {
  712. if (pic->encode_complete)
  713. continue;
  714. for (i = 0; i < pic->nb_refs; i++) {
  715. if (pic->refs[i] == old) {
  716. // We still need this picture because it's referred to
  717. // directly by a later one, so it and all following
  718. // pictures have to stay.
  719. return 0;
  720. }
  721. }
  722. }
  723. pic = ctx->pic_start;
  724. ctx->pic_start = pic->next;
  725. vaapi_encode_free(avctx, pic);
  726. }
  727. return 0;
  728. }
  729. int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
  730. const AVFrame *input_image, int *got_packet)
  731. {
  732. VAAPIEncodeContext *ctx = avctx->priv_data;
  733. VAAPIEncodePicture *pic;
  734. int err;
  735. if (input_image) {
  736. av_log(avctx, AV_LOG_DEBUG, "Encode frame: %ux%u (%"PRId64").\n",
  737. input_image->width, input_image->height, input_image->pts);
  738. if (input_image->pict_type == AV_PICTURE_TYPE_I) {
  739. err = vaapi_encode_truncate_gop(avctx);
  740. if (err < 0)
  741. goto fail;
  742. ctx->force_idr = 1;
  743. }
  744. err = vaapi_encode_get_next(avctx, &pic);
  745. if (err) {
  746. av_log(avctx, AV_LOG_ERROR, "Input setup failed: %d.\n", err);
  747. return err;
  748. }
  749. pic->input_image = av_frame_alloc();
  750. if (!pic->input_image) {
  751. err = AVERROR(ENOMEM);
  752. goto fail;
  753. }
  754. err = av_frame_ref(pic->input_image, input_image);
  755. if (err < 0)
  756. goto fail;
  757. pic->input_surface = (VASurfaceID)(uintptr_t)input_image->data[3];
  758. pic->pts = input_image->pts;
  759. if (ctx->input_order == 0)
  760. ctx->first_pts = pic->pts;
  761. if (ctx->input_order == ctx->decode_delay)
  762. ctx->dts_pts_diff = pic->pts - ctx->first_pts;
  763. if (ctx->output_delay > 0)
  764. ctx->ts_ring[ctx->input_order % (3 * ctx->output_delay)] = pic->pts;
  765. pic->input_available = 1;
  766. } else {
  767. if (!ctx->end_of_stream) {
  768. err = vaapi_encode_truncate_gop(avctx);
  769. if (err < 0)
  770. goto fail;
  771. ctx->end_of_stream = 1;
  772. }
  773. }
  774. ++ctx->input_order;
  775. ++ctx->output_order;
  776. av_assert0(ctx->output_order + ctx->output_delay + 1 == ctx->input_order);
  777. for (pic = ctx->pic_start; pic; pic = pic->next)
  778. if (pic->encode_order == ctx->output_order)
  779. break;
  780. // pic can be null here if we don't have a specific target in this
  781. // iteration. We might still issue encodes if things can be overlapped,
  782. // even though we don't intend to output anything.
  783. err = vaapi_encode_step(avctx, pic);
  784. if (err < 0) {
  785. av_log(avctx, AV_LOG_ERROR, "Encode failed: %d.\n", err);
  786. goto fail;
  787. }
  788. if (!pic) {
  789. *got_packet = 0;
  790. } else {
  791. err = vaapi_encode_output(avctx, pic, pkt);
  792. if (err < 0) {
  793. av_log(avctx, AV_LOG_ERROR, "Output failed: %d.\n", err);
  794. goto fail;
  795. }
  796. if (ctx->output_delay == 0) {
  797. pkt->dts = pkt->pts;
  798. } else if (ctx->output_order < ctx->decode_delay) {
  799. if (ctx->ts_ring[ctx->output_order] < INT64_MIN + ctx->dts_pts_diff)
  800. pkt->dts = INT64_MIN;
  801. else
  802. pkt->dts = ctx->ts_ring[ctx->output_order] - ctx->dts_pts_diff;
  803. } else {
  804. pkt->dts = ctx->ts_ring[(ctx->output_order - ctx->decode_delay) %
  805. (3 * ctx->output_delay)];
  806. }
  807. *got_packet = 1;
  808. }
  809. err = vaapi_encode_clear_old(avctx);
  810. if (err < 0) {
  811. av_log(avctx, AV_LOG_ERROR, "List clearing failed: %d.\n", err);
  812. goto fail;
  813. }
  814. return 0;
  815. fail:
  816. // Unclear what to clean up on failure. There are probably some things we
  817. // could do usefully clean up here, but for now just leave them for uninit()
  818. // to do instead.
  819. return err;
  820. }
  821. static av_cold int vaapi_encode_config_attributes(AVCodecContext *avctx)
  822. {
  823. VAAPIEncodeContext *ctx = avctx->priv_data;
  824. VAStatus vas;
  825. int i, n, err;
  826. VAProfile *profiles = NULL;
  827. VAEntrypoint *entrypoints = NULL;
  828. VAConfigAttrib attr[] = {
  829. { VAConfigAttribRTFormat },
  830. { VAConfigAttribRateControl },
  831. { VAConfigAttribEncMaxRefFrames },
  832. { VAConfigAttribEncPackedHeaders },
  833. };
  834. n = vaMaxNumProfiles(ctx->hwctx->display);
  835. profiles = av_malloc_array(n, sizeof(VAProfile));
  836. if (!profiles) {
  837. err = AVERROR(ENOMEM);
  838. goto fail;
  839. }
  840. vas = vaQueryConfigProfiles(ctx->hwctx->display, profiles, &n);
  841. if (vas != VA_STATUS_SUCCESS) {
  842. av_log(ctx, AV_LOG_ERROR, "Failed to query profiles: %d (%s).\n",
  843. vas, vaErrorStr(vas));
  844. err = AVERROR(ENOSYS);
  845. goto fail;
  846. }
  847. for (i = 0; i < n; i++) {
  848. if (profiles[i] == ctx->va_profile)
  849. break;
  850. }
  851. if (i >= n) {
  852. av_log(ctx, AV_LOG_ERROR, "Encoding profile not found (%d).\n",
  853. ctx->va_profile);
  854. err = AVERROR(ENOSYS);
  855. goto fail;
  856. }
  857. n = vaMaxNumEntrypoints(ctx->hwctx->display);
  858. entrypoints = av_malloc_array(n, sizeof(VAEntrypoint));
  859. if (!entrypoints) {
  860. err = AVERROR(ENOMEM);
  861. goto fail;
  862. }
  863. vas = vaQueryConfigEntrypoints(ctx->hwctx->display, ctx->va_profile,
  864. entrypoints, &n);
  865. if (vas != VA_STATUS_SUCCESS) {
  866. av_log(ctx, AV_LOG_ERROR, "Failed to query entrypoints for "
  867. "profile %u: %d (%s).\n", ctx->va_profile,
  868. vas, vaErrorStr(vas));
  869. err = AVERROR(ENOSYS);
  870. goto fail;
  871. }
  872. for (i = 0; i < n; i++) {
  873. if (entrypoints[i] == ctx->va_entrypoint)
  874. break;
  875. }
  876. if (i >= n) {
  877. av_log(ctx, AV_LOG_ERROR, "Encoding entrypoint not found "
  878. "(%d / %d).\n", ctx->va_profile, ctx->va_entrypoint);
  879. err = AVERROR(ENOSYS);
  880. goto fail;
  881. }
  882. vas = vaGetConfigAttributes(ctx->hwctx->display,
  883. ctx->va_profile, ctx->va_entrypoint,
  884. attr, FF_ARRAY_ELEMS(attr));
  885. if (vas != VA_STATUS_SUCCESS) {
  886. av_log(avctx, AV_LOG_ERROR, "Failed to fetch config "
  887. "attributes: %d (%s).\n", vas, vaErrorStr(vas));
  888. return AVERROR(EINVAL);
  889. }
  890. for (i = 0; i < FF_ARRAY_ELEMS(attr); i++) {
  891. if (attr[i].value == VA_ATTRIB_NOT_SUPPORTED) {
  892. // Unfortunately we have to treat this as "don't know" and hope
  893. // for the best, because the Intel MJPEG encoder returns this
  894. // for all the interesting attributes.
  895. av_log(avctx, AV_LOG_DEBUG, "Attribute (%d) is not supported.\n",
  896. attr[i].type);
  897. continue;
  898. }
  899. switch (attr[i].type) {
  900. case VAConfigAttribRTFormat:
  901. if (!(ctx->va_rt_format & attr[i].value)) {
  902. av_log(avctx, AV_LOG_ERROR, "Surface RT format %#x "
  903. "is not supported (mask %#x).\n",
  904. ctx->va_rt_format, attr[i].value);
  905. err = AVERROR(EINVAL);
  906. goto fail;
  907. }
  908. ctx->config_attributes[ctx->nb_config_attributes++] =
  909. (VAConfigAttrib) {
  910. .type = VAConfigAttribRTFormat,
  911. .value = ctx->va_rt_format,
  912. };
  913. break;
  914. case VAConfigAttribRateControl:
  915. // Hack for backward compatibility: CBR was the only
  916. // usable RC mode for a long time, so old drivers will
  917. // only have it. Normal default options may now choose
  918. // VBR and then fail, however, so override it here with
  919. // CBR if that is the only supported mode.
  920. if (ctx->va_rc_mode == VA_RC_VBR &&
  921. !(attr[i].value & VA_RC_VBR) &&
  922. (attr[i].value & VA_RC_CBR)) {
  923. av_log(avctx, AV_LOG_WARNING, "VBR rate control is "
  924. "not supported with this driver version; "
  925. "using CBR instead.\n");
  926. ctx->va_rc_mode = VA_RC_CBR;
  927. }
  928. if (!(ctx->va_rc_mode & attr[i].value)) {
  929. av_log(avctx, AV_LOG_ERROR, "Rate control mode %#x "
  930. "is not supported (mask: %#x).\n",
  931. ctx->va_rc_mode, attr[i].value);
  932. err = AVERROR(EINVAL);
  933. goto fail;
  934. }
  935. ctx->config_attributes[ctx->nb_config_attributes++] =
  936. (VAConfigAttrib) {
  937. .type = VAConfigAttribRateControl,
  938. .value = ctx->va_rc_mode,
  939. };
  940. break;
  941. case VAConfigAttribEncMaxRefFrames:
  942. {
  943. unsigned int ref_l0 = attr[i].value & 0xffff;
  944. unsigned int ref_l1 = (attr[i].value >> 16) & 0xffff;
  945. if (avctx->gop_size > 1 && ref_l0 < 1) {
  946. av_log(avctx, AV_LOG_ERROR, "P frames are not "
  947. "supported (%#x).\n", attr[i].value);
  948. err = AVERROR(EINVAL);
  949. goto fail;
  950. }
  951. if (avctx->max_b_frames > 0 && ref_l1 < 1) {
  952. av_log(avctx, AV_LOG_ERROR, "B frames are not "
  953. "supported (%#x).\n", attr[i].value);
  954. err = AVERROR(EINVAL);
  955. goto fail;
  956. }
  957. }
  958. break;
  959. case VAConfigAttribEncPackedHeaders:
  960. if (ctx->va_packed_headers & ~attr[i].value) {
  961. // This isn't fatal, but packed headers are always
  962. // preferable because they are under our control.
  963. // When absent, the driver is generating them and some
  964. // features may not work (e.g. VUI or SEI in H.264).
  965. av_log(avctx, AV_LOG_WARNING, "Warning: some packed "
  966. "headers are not supported (want %#x, got %#x).\n",
  967. ctx->va_packed_headers, attr[i].value);
  968. ctx->va_packed_headers &= attr[i].value;
  969. }
  970. ctx->config_attributes[ctx->nb_config_attributes++] =
  971. (VAConfigAttrib) {
  972. .type = VAConfigAttribEncPackedHeaders,
  973. .value = ctx->va_packed_headers,
  974. };
  975. break;
  976. default:
  977. av_assert0(0 && "Unexpected config attribute.");
  978. }
  979. }
  980. err = 0;
  981. fail:
  982. av_freep(&profiles);
  983. av_freep(&entrypoints);
  984. return err;
  985. }
  986. static av_cold int vaapi_encode_init_rate_control(AVCodecContext *avctx)
  987. {
  988. VAAPIEncodeContext *ctx = avctx->priv_data;
  989. int rc_bits_per_second;
  990. int rc_target_percentage;
  991. int rc_window_size;
  992. int hrd_buffer_size;
  993. int hrd_initial_buffer_fullness;
  994. int fr_num, fr_den;
  995. if (avctx->bit_rate > INT32_MAX) {
  996. av_log(avctx, AV_LOG_ERROR, "Target bitrate of 2^31 bps or "
  997. "higher is not supported.\n");
  998. return AVERROR(EINVAL);
  999. }
  1000. if (avctx->rc_buffer_size)
  1001. hrd_buffer_size = avctx->rc_buffer_size;
  1002. else
  1003. hrd_buffer_size = avctx->bit_rate;
  1004. if (avctx->rc_initial_buffer_occupancy)
  1005. hrd_initial_buffer_fullness = avctx->rc_initial_buffer_occupancy;
  1006. else
  1007. hrd_initial_buffer_fullness = hrd_buffer_size * 3 / 4;
  1008. if (ctx->va_rc_mode == VA_RC_CBR) {
  1009. rc_bits_per_second = avctx->bit_rate;
  1010. rc_target_percentage = 100;
  1011. rc_window_size = 1000;
  1012. } else {
  1013. if (avctx->rc_max_rate < avctx->bit_rate) {
  1014. // Max rate is unset or invalid, just use the normal bitrate.
  1015. rc_bits_per_second = avctx->bit_rate;
  1016. rc_target_percentage = 100;
  1017. } else {
  1018. rc_bits_per_second = avctx->rc_max_rate;
  1019. rc_target_percentage = (avctx->bit_rate * 100) / rc_bits_per_second;
  1020. }
  1021. rc_window_size = (hrd_buffer_size * 1000) / avctx->bit_rate;
  1022. }
  1023. ctx->rc_params.misc.type = VAEncMiscParameterTypeRateControl;
  1024. ctx->rc_params.rc = (VAEncMiscParameterRateControl) {
  1025. .bits_per_second = rc_bits_per_second,
  1026. .target_percentage = rc_target_percentage,
  1027. .window_size = rc_window_size,
  1028. .initial_qp = 0,
  1029. .min_qp = (avctx->qmin > 0 ? avctx->qmin : 0),
  1030. .basic_unit_size = 0,
  1031. };
  1032. ctx->global_params[ctx->nb_global_params] =
  1033. &ctx->rc_params.misc;
  1034. ctx->global_params_size[ctx->nb_global_params++] =
  1035. sizeof(ctx->rc_params);
  1036. ctx->hrd_params.misc.type = VAEncMiscParameterTypeHRD;
  1037. ctx->hrd_params.hrd = (VAEncMiscParameterHRD) {
  1038. .initial_buffer_fullness = hrd_initial_buffer_fullness,
  1039. .buffer_size = hrd_buffer_size,
  1040. };
  1041. ctx->global_params[ctx->nb_global_params] =
  1042. &ctx->hrd_params.misc;
  1043. ctx->global_params_size[ctx->nb_global_params++] =
  1044. sizeof(ctx->hrd_params);
  1045. if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
  1046. av_reduce(&fr_num, &fr_den,
  1047. avctx->framerate.num, avctx->framerate.den, 65535);
  1048. else
  1049. av_reduce(&fr_num, &fr_den,
  1050. avctx->time_base.den, avctx->time_base.num, 65535);
  1051. ctx->fr_params.misc.type = VAEncMiscParameterTypeFrameRate;
  1052. ctx->fr_params.fr.framerate = (unsigned int)fr_den << 16 | fr_num;
  1053. #if VA_CHECK_VERSION(0, 40, 0)
  1054. ctx->global_params[ctx->nb_global_params] =
  1055. &ctx->fr_params.misc;
  1056. ctx->global_params_size[ctx->nb_global_params++] =
  1057. sizeof(ctx->fr_params);
  1058. #endif
  1059. return 0;
  1060. }
  1061. static void vaapi_encode_free_output_buffer(void *opaque,
  1062. uint8_t *data)
  1063. {
  1064. AVCodecContext *avctx = opaque;
  1065. VAAPIEncodeContext *ctx = avctx->priv_data;
  1066. VABufferID buffer_id;
  1067. buffer_id = (VABufferID)(uintptr_t)data;
  1068. vaDestroyBuffer(ctx->hwctx->display, buffer_id);
  1069. av_log(avctx, AV_LOG_DEBUG, "Freed output buffer %#x\n", buffer_id);
  1070. }
  1071. static AVBufferRef *vaapi_encode_alloc_output_buffer(void *opaque,
  1072. int size)
  1073. {
  1074. AVCodecContext *avctx = opaque;
  1075. VAAPIEncodeContext *ctx = avctx->priv_data;
  1076. VABufferID buffer_id;
  1077. VAStatus vas;
  1078. AVBufferRef *ref;
  1079. // The output buffer size is fixed, so it needs to be large enough
  1080. // to hold the largest possible compressed frame. We assume here
  1081. // that the uncompressed frame plus some header data is an upper
  1082. // bound on that.
  1083. vas = vaCreateBuffer(ctx->hwctx->display, ctx->va_context,
  1084. VAEncCodedBufferType,
  1085. 3 * ctx->surface_width * ctx->surface_height +
  1086. (1 << 16), 1, 0, &buffer_id);
  1087. if (vas != VA_STATUS_SUCCESS) {
  1088. av_log(avctx, AV_LOG_ERROR, "Failed to create bitstream "
  1089. "output buffer: %d (%s).\n", vas, vaErrorStr(vas));
  1090. return NULL;
  1091. }
  1092. av_log(avctx, AV_LOG_DEBUG, "Allocated output buffer %#x\n", buffer_id);
  1093. ref = av_buffer_create((uint8_t*)(uintptr_t)buffer_id,
  1094. sizeof(buffer_id),
  1095. &vaapi_encode_free_output_buffer,
  1096. avctx, AV_BUFFER_FLAG_READONLY);
  1097. if (!ref) {
  1098. vaDestroyBuffer(ctx->hwctx->display, buffer_id);
  1099. return NULL;
  1100. }
  1101. return ref;
  1102. }
  1103. static av_cold int vaapi_encode_create_recon_frames(AVCodecContext *avctx)
  1104. {
  1105. VAAPIEncodeContext *ctx = avctx->priv_data;
  1106. AVVAAPIHWConfig *hwconfig = NULL;
  1107. AVHWFramesConstraints *constraints = NULL;
  1108. enum AVPixelFormat recon_format;
  1109. int err, i;
  1110. hwconfig = av_hwdevice_hwconfig_alloc(ctx->device_ref);
  1111. if (!hwconfig) {
  1112. err = AVERROR(ENOMEM);
  1113. goto fail;
  1114. }
  1115. hwconfig->config_id = ctx->va_config;
  1116. constraints = av_hwdevice_get_hwframe_constraints(ctx->device_ref,
  1117. hwconfig);
  1118. if (!constraints) {
  1119. err = AVERROR(ENOMEM);
  1120. goto fail;
  1121. }
  1122. // Probably we can use the input surface format as the surface format
  1123. // of the reconstructed frames. If not, we just pick the first (only?)
  1124. // format in the valid list and hope that it all works.
  1125. recon_format = AV_PIX_FMT_NONE;
  1126. if (constraints->valid_sw_formats) {
  1127. for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
  1128. if (ctx->input_frames->sw_format ==
  1129. constraints->valid_sw_formats[i]) {
  1130. recon_format = ctx->input_frames->sw_format;
  1131. break;
  1132. }
  1133. }
  1134. if (recon_format == AV_PIX_FMT_NONE) {
  1135. // No match. Just use the first in the supported list and
  1136. // hope for the best.
  1137. recon_format = constraints->valid_sw_formats[0];
  1138. }
  1139. } else {
  1140. // No idea what to use; copy input format.
  1141. recon_format = ctx->input_frames->sw_format;
  1142. }
  1143. av_log(avctx, AV_LOG_DEBUG, "Using %s as format of "
  1144. "reconstructed frames.\n", av_get_pix_fmt_name(recon_format));
  1145. if (ctx->surface_width < constraints->min_width ||
  1146. ctx->surface_height < constraints->min_height ||
  1147. ctx->surface_width > constraints->max_width ||
  1148. ctx->surface_height > constraints->max_height) {
  1149. av_log(avctx, AV_LOG_ERROR, "Hardware does not support encoding at "
  1150. "size %dx%d (constraints: width %d-%d height %d-%d).\n",
  1151. ctx->surface_width, ctx->surface_height,
  1152. constraints->min_width, constraints->max_width,
  1153. constraints->min_height, constraints->max_height);
  1154. err = AVERROR(EINVAL);
  1155. goto fail;
  1156. }
  1157. av_freep(&hwconfig);
  1158. av_hwframe_constraints_free(&constraints);
  1159. ctx->recon_frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
  1160. if (!ctx->recon_frames_ref) {
  1161. err = AVERROR(ENOMEM);
  1162. goto fail;
  1163. }
  1164. ctx->recon_frames = (AVHWFramesContext*)ctx->recon_frames_ref->data;
  1165. ctx->recon_frames->format = AV_PIX_FMT_VAAPI;
  1166. ctx->recon_frames->sw_format = recon_format;
  1167. ctx->recon_frames->width = ctx->surface_width;
  1168. ctx->recon_frames->height = ctx->surface_height;
  1169. // At most three IDR/I/P frames and two runs of B frames can be in
  1170. // flight at any one time.
  1171. ctx->recon_frames->initial_pool_size = 3 + 2 * avctx->max_b_frames;
  1172. err = av_hwframe_ctx_init(ctx->recon_frames_ref);
  1173. if (err < 0) {
  1174. av_log(avctx, AV_LOG_ERROR, "Failed to initialise reconstructed "
  1175. "frame context: %d.\n", err);
  1176. goto fail;
  1177. }
  1178. err = 0;
  1179. fail:
  1180. av_freep(&hwconfig);
  1181. av_hwframe_constraints_free(&constraints);
  1182. return err;
  1183. }
  1184. av_cold int ff_vaapi_encode_init(AVCodecContext *avctx)
  1185. {
  1186. VAAPIEncodeContext *ctx = avctx->priv_data;
  1187. AVVAAPIFramesContext *recon_hwctx = NULL;
  1188. VAStatus vas;
  1189. int err;
  1190. if (!avctx->hw_frames_ctx) {
  1191. av_log(avctx, AV_LOG_ERROR, "A hardware frames reference is "
  1192. "required to associate the encoding device.\n");
  1193. return AVERROR(EINVAL);
  1194. }
  1195. ctx->codec_options = ctx->codec_options_data;
  1196. ctx->va_config = VA_INVALID_ID;
  1197. ctx->va_context = VA_INVALID_ID;
  1198. ctx->priv_data = av_mallocz(ctx->codec->priv_data_size);
  1199. if (!ctx->priv_data) {
  1200. err = AVERROR(ENOMEM);
  1201. goto fail;
  1202. }
  1203. ctx->input_frames_ref = av_buffer_ref(avctx->hw_frames_ctx);
  1204. if (!ctx->input_frames_ref) {
  1205. err = AVERROR(ENOMEM);
  1206. goto fail;
  1207. }
  1208. ctx->input_frames = (AVHWFramesContext*)ctx->input_frames_ref->data;
  1209. ctx->device_ref = av_buffer_ref(ctx->input_frames->device_ref);
  1210. if (!ctx->device_ref) {
  1211. err = AVERROR(ENOMEM);
  1212. goto fail;
  1213. }
  1214. ctx->device = (AVHWDeviceContext*)ctx->device_ref->data;
  1215. ctx->hwctx = ctx->device->hwctx;
  1216. err = vaapi_encode_config_attributes(avctx);
  1217. if (err < 0)
  1218. goto fail;
  1219. vas = vaCreateConfig(ctx->hwctx->display,
  1220. ctx->va_profile, ctx->va_entrypoint,
  1221. ctx->config_attributes, ctx->nb_config_attributes,
  1222. &ctx->va_config);
  1223. if (vas != VA_STATUS_SUCCESS) {
  1224. av_log(avctx, AV_LOG_ERROR, "Failed to create encode pipeline "
  1225. "configuration: %d (%s).\n", vas, vaErrorStr(vas));
  1226. err = AVERROR(EIO);
  1227. goto fail;
  1228. }
  1229. err = vaapi_encode_create_recon_frames(avctx);
  1230. if (err < 0)
  1231. goto fail;
  1232. recon_hwctx = ctx->recon_frames->hwctx;
  1233. vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
  1234. ctx->surface_width, ctx->surface_height,
  1235. VA_PROGRESSIVE,
  1236. recon_hwctx->surface_ids,
  1237. recon_hwctx->nb_surfaces,
  1238. &ctx->va_context);
  1239. if (vas != VA_STATUS_SUCCESS) {
  1240. av_log(avctx, AV_LOG_ERROR, "Failed to create encode pipeline "
  1241. "context: %d (%s).\n", vas, vaErrorStr(vas));
  1242. err = AVERROR(EIO);
  1243. goto fail;
  1244. }
  1245. ctx->output_buffer_pool =
  1246. av_buffer_pool_init2(sizeof(VABufferID), avctx,
  1247. &vaapi_encode_alloc_output_buffer, NULL);
  1248. if (!ctx->output_buffer_pool) {
  1249. err = AVERROR(ENOMEM);
  1250. goto fail;
  1251. }
  1252. if (ctx->va_rc_mode & ~VA_RC_CQP) {
  1253. err = vaapi_encode_init_rate_control(avctx);
  1254. if (err < 0)
  1255. goto fail;
  1256. }
  1257. if (ctx->codec->configure) {
  1258. err = ctx->codec->configure(avctx);
  1259. if (err < 0)
  1260. goto fail;
  1261. }
  1262. if (avctx->compression_level >= 0) {
  1263. #if VA_CHECK_VERSION(0, 36, 0)
  1264. VAConfigAttrib attr = { VAConfigAttribEncQualityRange };
  1265. vas = vaGetConfigAttributes(ctx->hwctx->display,
  1266. ctx->va_profile,
  1267. ctx->va_entrypoint,
  1268. &attr, 1);
  1269. if (vas != VA_STATUS_SUCCESS) {
  1270. av_log(avctx, AV_LOG_WARNING, "Failed to query quality "
  1271. "attribute: will use default compression level.\n");
  1272. } else {
  1273. if (avctx->compression_level > attr.value) {
  1274. av_log(avctx, AV_LOG_WARNING, "Invalid compression "
  1275. "level: valid range is 0-%d, using %d.\n",
  1276. attr.value, attr.value);
  1277. avctx->compression_level = attr.value;
  1278. }
  1279. ctx->quality_params.misc.type =
  1280. VAEncMiscParameterTypeQualityLevel;
  1281. ctx->quality_params.quality.quality_level =
  1282. avctx->compression_level;
  1283. ctx->global_params[ctx->nb_global_params] =
  1284. &ctx->quality_params.misc;
  1285. ctx->global_params_size[ctx->nb_global_params++] =
  1286. sizeof(ctx->quality_params);
  1287. }
  1288. #else
  1289. av_log(avctx, AV_LOG_WARNING, "The encode compression level "
  1290. "option is not supported with this VAAPI version.\n");
  1291. #endif
  1292. }
  1293. ctx->input_order = 0;
  1294. ctx->output_delay = avctx->max_b_frames;
  1295. ctx->decode_delay = 1;
  1296. ctx->output_order = - ctx->output_delay - 1;
  1297. // Currently we never generate I frames, only IDR.
  1298. ctx->p_per_i = INT_MAX;
  1299. ctx->b_per_p = avctx->max_b_frames;
  1300. if (ctx->codec->sequence_params_size > 0) {
  1301. ctx->codec_sequence_params =
  1302. av_mallocz(ctx->codec->sequence_params_size);
  1303. if (!ctx->codec_sequence_params) {
  1304. err = AVERROR(ENOMEM);
  1305. goto fail;
  1306. }
  1307. }
  1308. if (ctx->codec->picture_params_size > 0) {
  1309. ctx->codec_picture_params =
  1310. av_mallocz(ctx->codec->picture_params_size);
  1311. if (!ctx->codec_picture_params) {
  1312. err = AVERROR(ENOMEM);
  1313. goto fail;
  1314. }
  1315. }
  1316. if (ctx->codec->init_sequence_params) {
  1317. err = ctx->codec->init_sequence_params(avctx);
  1318. if (err < 0) {
  1319. av_log(avctx, AV_LOG_ERROR, "Codec sequence initialisation "
  1320. "failed: %d.\n", err);
  1321. goto fail;
  1322. }
  1323. }
  1324. // This should be configurable somehow. (Needs testing on a machine
  1325. // where it actually overlaps properly, though.)
  1326. ctx->issue_mode = ISSUE_MODE_MAXIMISE_THROUGHPUT;
  1327. if (ctx->va_packed_headers & VA_ENC_PACKED_HEADER_SEQUENCE &&
  1328. ctx->codec->write_sequence_header) {
  1329. char data[MAX_PARAM_BUFFER_SIZE];
  1330. size_t bit_len = 8 * sizeof(data);
  1331. err = ctx->codec->write_sequence_header(avctx, data, &bit_len);
  1332. if (err < 0) {
  1333. av_log(avctx, AV_LOG_ERROR, "Failed to write sequence header "
  1334. "for extradata: %d.\n", err);
  1335. goto fail;
  1336. } else {
  1337. avctx->extradata_size = (bit_len + 7) / 8;
  1338. avctx->extradata = av_mallocz(avctx->extradata_size +
  1339. AV_INPUT_BUFFER_PADDING_SIZE);
  1340. if (!avctx->extradata) {
  1341. err = AVERROR(ENOMEM);
  1342. goto fail;
  1343. }
  1344. memcpy(avctx->extradata, data, avctx->extradata_size);
  1345. }
  1346. }
  1347. return 0;
  1348. fail:
  1349. ff_vaapi_encode_close(avctx);
  1350. return err;
  1351. }
  1352. av_cold int ff_vaapi_encode_close(AVCodecContext *avctx)
  1353. {
  1354. VAAPIEncodeContext *ctx = avctx->priv_data;
  1355. VAAPIEncodePicture *pic, *next;
  1356. for (pic = ctx->pic_start; pic; pic = next) {
  1357. next = pic->next;
  1358. vaapi_encode_free(avctx, pic);
  1359. }
  1360. av_buffer_pool_uninit(&ctx->output_buffer_pool);
  1361. if (ctx->va_context != VA_INVALID_ID) {
  1362. vaDestroyContext(ctx->hwctx->display, ctx->va_context);
  1363. ctx->va_context = VA_INVALID_ID;
  1364. }
  1365. if (ctx->va_config != VA_INVALID_ID) {
  1366. vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
  1367. ctx->va_config = VA_INVALID_ID;
  1368. }
  1369. av_freep(&ctx->codec_sequence_params);
  1370. av_freep(&ctx->codec_picture_params);
  1371. av_buffer_unref(&ctx->recon_frames_ref);
  1372. av_buffer_unref(&ctx->input_frames_ref);
  1373. av_buffer_unref(&ctx->device_ref);
  1374. av_freep(&ctx->priv_data);
  1375. return 0;
  1376. }