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.

223 lines
6.6KB

  1. /*
  2. * Video Acceleration API (video encoding) encode sample
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Intel VAAPI-accelerated encoding example.
  23. *
  24. * @example vaapi_encode.c
  25. * This example shows how to do VAAPI-accelerated encoding. now only support NV12
  26. * raw file, usage like: vaapi_encode 1920 1080 input.yuv output.h264
  27. *
  28. */
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <errno.h>
  32. #include <libavcodec/avcodec.h>
  33. #include <libavutil/pixdesc.h>
  34. #include <libavutil/hwcontext.h>
  35. static int width, height;
  36. static AVBufferRef *hw_device_ctx = NULL;
  37. static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx)
  38. {
  39. AVBufferRef *hw_frames_ref;
  40. AVHWFramesContext *frames_ctx = NULL;
  41. int err = 0;
  42. if (!(hw_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx))) {
  43. fprintf(stderr, "Failed to create VAAPI frame context.\n");
  44. return -1;
  45. }
  46. frames_ctx = (AVHWFramesContext *)(hw_frames_ref->data);
  47. frames_ctx->format = AV_PIX_FMT_VAAPI;
  48. frames_ctx->sw_format = AV_PIX_FMT_NV12;
  49. frames_ctx->width = width;
  50. frames_ctx->height = height;
  51. frames_ctx->initial_pool_size = 20;
  52. if ((err = av_hwframe_ctx_init(hw_frames_ref)) < 0) {
  53. fprintf(stderr, "Failed to initialize VAAPI frame context."
  54. "Error code: %s\n",av_err2str(err));
  55. av_buffer_unref(&hw_frames_ref);
  56. return err;
  57. }
  58. ctx->hw_frames_ctx = av_buffer_ref(hw_frames_ref);
  59. if (!ctx->hw_frames_ctx)
  60. err = AVERROR(ENOMEM);
  61. av_buffer_unref(&hw_frames_ref);
  62. return err;
  63. }
  64. static int encode_write(AVCodecContext *avctx, AVFrame *frame, FILE *fout)
  65. {
  66. int ret = 0;
  67. AVPacket enc_pkt;
  68. av_init_packet(&enc_pkt);
  69. enc_pkt.data = NULL;
  70. enc_pkt.size = 0;
  71. if ((ret = avcodec_send_frame(avctx, frame)) < 0) {
  72. fprintf(stderr, "Error code: %s\n", av_err2str(ret));
  73. goto end;
  74. }
  75. while (1) {
  76. ret = avcodec_receive_packet(avctx, &enc_pkt);
  77. if (ret)
  78. break;
  79. enc_pkt.stream_index = 0;
  80. ret = fwrite(enc_pkt.data, enc_pkt.size, 1, fout);
  81. av_packet_unref(&enc_pkt);
  82. }
  83. end:
  84. ret = ((ret == AVERROR(EAGAIN)) ? 0 : -1);
  85. return ret;
  86. }
  87. int main(int argc, char *argv[])
  88. {
  89. int size, err;
  90. FILE *fin = NULL, *fout = NULL;
  91. AVFrame *sw_frame = NULL, *hw_frame = NULL;
  92. AVCodecContext *avctx = NULL;
  93. AVCodec *codec = NULL;
  94. const char *enc_name = "h264_vaapi";
  95. if (argc < 5) {
  96. fprintf(stderr, "Usage: %s <width> <height> <input file> <output file>\n", argv[0]);
  97. return -1;
  98. }
  99. width = atoi(argv[1]);
  100. height = atoi(argv[2]);
  101. size = width * height;
  102. if (!(fin = fopen(argv[3], "r"))) {
  103. fprintf(stderr, "Fail to open input file : %s\n", strerror(errno));
  104. return -1;
  105. }
  106. if (!(fout = fopen(argv[4], "w+b"))) {
  107. fprintf(stderr, "Fail to open output file : %s\n", strerror(errno));
  108. err = -1;
  109. goto close;
  110. }
  111. err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI,
  112. NULL, NULL, 0);
  113. if (err < 0) {
  114. fprintf(stderr, "Failed to create a VAAPI device. Error code: %s\n", av_err2str(err));
  115. goto close;
  116. }
  117. if (!(codec = avcodec_find_encoder_by_name(enc_name))) {
  118. fprintf(stderr, "Could not find encoder.\n");
  119. err = -1;
  120. goto close;
  121. }
  122. if (!(avctx = avcodec_alloc_context3(codec))) {
  123. err = AVERROR(ENOMEM);
  124. goto close;
  125. }
  126. avctx->width = width;
  127. avctx->height = height;
  128. avctx->time_base = (AVRational){1, 25};
  129. avctx->framerate = (AVRational){25, 1};
  130. avctx->sample_aspect_ratio = (AVRational){1, 1};
  131. avctx->pix_fmt = AV_PIX_FMT_VAAPI;
  132. /* set hw_frames_ctx for encoder's AVCodecContext */
  133. if ((err = set_hwframe_ctx(avctx, hw_device_ctx)) < 0) {
  134. fprintf(stderr, "Failed to set hwframe context.\n");
  135. goto close;
  136. }
  137. if ((err = avcodec_open2(avctx, codec, NULL)) < 0) {
  138. fprintf(stderr, "Cannot open video encoder codec. Error code: %s\n", av_err2str(err));
  139. goto close;
  140. }
  141. while (1) {
  142. if (!(sw_frame = av_frame_alloc())) {
  143. err = AVERROR(ENOMEM);
  144. goto close;
  145. }
  146. /* read data into software frame, and transfer them into hw frame */
  147. sw_frame->width = width;
  148. sw_frame->height = height;
  149. sw_frame->format = AV_PIX_FMT_NV12;
  150. if ((err = av_frame_get_buffer(sw_frame, 32)) < 0)
  151. goto close;
  152. if ((err = fread((uint8_t*)(sw_frame->data[0]), size, 1, fin)) <= 0)
  153. break;
  154. if ((err = fread((uint8_t*)(sw_frame->data[1]), size/2, 1, fin)) <= 0)
  155. break;
  156. if (!(hw_frame = av_frame_alloc())) {
  157. err = AVERROR(ENOMEM);
  158. goto close;
  159. }
  160. if ((err = av_hwframe_get_buffer(avctx->hw_frames_ctx, hw_frame, 0)) < 0) {
  161. fprintf(stderr, "Error code: %s.\n", av_err2str(err));
  162. goto close;
  163. }
  164. if (!hw_frame->hw_frames_ctx) {
  165. err = AVERROR(ENOMEM);
  166. goto close;
  167. }
  168. if ((err = av_hwframe_transfer_data(hw_frame, sw_frame, 0)) < 0) {
  169. fprintf(stderr, "Error while transferring frame data to surface."
  170. "Error code: %s.\n", av_err2str(err));
  171. goto close;
  172. }
  173. if ((err = (encode_write(avctx, hw_frame, fout))) < 0) {
  174. fprintf(stderr, "Failed to encode.\n");
  175. goto close;
  176. }
  177. av_frame_free(&hw_frame);
  178. av_frame_free(&sw_frame);
  179. }
  180. /* flush encoder */
  181. err = encode_write(avctx, NULL, fout);
  182. if (err == AVERROR_EOF)
  183. err = 0;
  184. close:
  185. if (fin)
  186. fclose(fin);
  187. if (fout)
  188. fclose(fout);
  189. av_frame_free(&sw_frame);
  190. av_frame_free(&hw_frame);
  191. avcodec_free_context(&avctx);
  192. av_buffer_unref(&hw_device_ctx);
  193. return err;
  194. }