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.

1586 lines
52KB

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