vaapi_encode.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Permission is hereby granted, free of charge, to any person obtaining a copy
  3. * of this software and associated documentation files (the "Software"), to deal
  4. * in the Software without restriction, including without limitation the rights
  5. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. * copies of the Software, and to permit persons to whom the Software is
  7. * furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. */
  20. /**
  21. * @file Intel VAAPI-accelerated encoding API usage example
  22. * @example vaapi_encode.c
  23. *
  24. * Perform VAAPI-accelerated encoding. Read input from an NV12 raw
  25. * file, and write the H.264 encoded data to an output raw file.
  26. * Usage: vaapi_encode 1920 1080 input.yuv output.h264
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <errno.h>
  31. #include <libavcodec/avcodec.h>
  32. #include <libavutil/pixdesc.h>
  33. #include <libavutil/hwcontext.h>
  34. static int width, height;
  35. static AVBufferRef *hw_device_ctx = NULL;
  36. static int set_hwframe_ctx(AVCodecContext *ctx, AVBufferRef *hw_device_ctx)
  37. {
  38. AVBufferRef *hw_frames_ref;
  39. AVHWFramesContext *frames_ctx = NULL;
  40. int err = 0;
  41. if (!(hw_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx))) {
  42. fprintf(stderr, "Failed to create VAAPI frame context.\n");
  43. return -1;
  44. }
  45. frames_ctx = (AVHWFramesContext *)(hw_frames_ref->data);
  46. frames_ctx->format = AV_PIX_FMT_VAAPI;
  47. frames_ctx->sw_format = AV_PIX_FMT_NV12;
  48. frames_ctx->width = width;
  49. frames_ctx->height = height;
  50. frames_ctx->initial_pool_size = 20;
  51. if ((err = av_hwframe_ctx_init(hw_frames_ref)) < 0) {
  52. fprintf(stderr, "Failed to initialize VAAPI frame context."
  53. "Error code: %s\n",av_err2str(err));
  54. av_buffer_unref(&hw_frames_ref);
  55. return err;
  56. }
  57. ctx->hw_frames_ctx = av_buffer_ref(hw_frames_ref);
  58. if (!ctx->hw_frames_ctx)
  59. err = AVERROR(ENOMEM);
  60. av_buffer_unref(&hw_frames_ref);
  61. return err;
  62. }
  63. static int encode_write(AVCodecContext *avctx, AVFrame *frame, FILE *fout)
  64. {
  65. int ret = 0;
  66. AVPacket *enc_pkt;
  67. if (!(enc_pkt = av_packet_alloc()))
  68. return AVERROR(ENOMEM);
  69. if ((ret = avcodec_send_frame(avctx, frame)) < 0) {
  70. fprintf(stderr, "Error code: %s\n", av_err2str(ret));
  71. goto end;
  72. }
  73. while (1) {
  74. ret = avcodec_receive_packet(avctx, enc_pkt);
  75. if (ret)
  76. break;
  77. enc_pkt->stream_index = 0;
  78. ret = fwrite(enc_pkt->data, enc_pkt->size, 1, fout);
  79. av_packet_unref(enc_pkt);
  80. }
  81. end:
  82. av_packet_free(&enc_pkt);
  83. ret = ((ret == AVERROR(EAGAIN)) ? 0 : -1);
  84. return ret;
  85. }
  86. int main(int argc, char *argv[])
  87. {
  88. int size, err;
  89. FILE *fin = NULL, *fout = NULL;
  90. AVFrame *sw_frame = NULL, *hw_frame = NULL;
  91. AVCodecContext *avctx = NULL;
  92. const AVCodec *codec = NULL;
  93. const char *enc_name = "h264_vaapi";
  94. if (argc < 5) {
  95. fprintf(stderr, "Usage: %s <width> <height> <input file> <output file>\n", argv[0]);
  96. return -1;
  97. }
  98. width = atoi(argv[1]);
  99. height = atoi(argv[2]);
  100. size = width * height;
  101. if (!(fin = fopen(argv[3], "r"))) {
  102. fprintf(stderr, "Fail to open input file : %s\n", strerror(errno));
  103. return -1;
  104. }
  105. if (!(fout = fopen(argv[4], "w+b"))) {
  106. fprintf(stderr, "Fail to open output file : %s\n", strerror(errno));
  107. err = -1;
  108. goto close;
  109. }
  110. err = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI,
  111. NULL, NULL, 0);
  112. if (err < 0) {
  113. fprintf(stderr, "Failed to create a VAAPI device. Error code: %s\n", av_err2str(err));
  114. goto close;
  115. }
  116. if (!(codec = avcodec_find_encoder_by_name(enc_name))) {
  117. fprintf(stderr, "Could not find encoder.\n");
  118. err = -1;
  119. goto close;
  120. }
  121. if (!(avctx = avcodec_alloc_context3(codec))) {
  122. err = AVERROR(ENOMEM);
  123. goto close;
  124. }
  125. avctx->width = width;
  126. avctx->height = height;
  127. avctx->time_base = (AVRational){1, 25};
  128. avctx->framerate = (AVRational){25, 1};
  129. avctx->sample_aspect_ratio = (AVRational){1, 1};
  130. avctx->pix_fmt = AV_PIX_FMT_VAAPI;
  131. /* set hw_frames_ctx for encoder's AVCodecContext */
  132. if ((err = set_hwframe_ctx(avctx, hw_device_ctx)) < 0) {
  133. fprintf(stderr, "Failed to set hwframe context.\n");
  134. goto close;
  135. }
  136. if ((err = avcodec_open2(avctx, codec, NULL)) < 0) {
  137. fprintf(stderr, "Cannot open video encoder codec. Error code: %s\n", av_err2str(err));
  138. goto close;
  139. }
  140. while (1) {
  141. if (!(sw_frame = av_frame_alloc())) {
  142. err = AVERROR(ENOMEM);
  143. goto close;
  144. }
  145. /* read data into software frame, and transfer them into hw frame */
  146. sw_frame->width = width;
  147. sw_frame->height = height;
  148. sw_frame->format = AV_PIX_FMT_NV12;
  149. if ((err = av_frame_get_buffer(sw_frame, 0)) < 0)
  150. goto close;
  151. if ((err = fread((uint8_t*)(sw_frame->data[0]), size, 1, fin)) <= 0)
  152. break;
  153. if ((err = fread((uint8_t*)(sw_frame->data[1]), size/2, 1, fin)) <= 0)
  154. break;
  155. if (!(hw_frame = av_frame_alloc())) {
  156. err = AVERROR(ENOMEM);
  157. goto close;
  158. }
  159. if ((err = av_hwframe_get_buffer(avctx->hw_frames_ctx, hw_frame, 0)) < 0) {
  160. fprintf(stderr, "Error code: %s.\n", av_err2str(err));
  161. goto close;
  162. }
  163. if (!hw_frame->hw_frames_ctx) {
  164. err = AVERROR(ENOMEM);
  165. goto close;
  166. }
  167. if ((err = av_hwframe_transfer_data(hw_frame, sw_frame, 0)) < 0) {
  168. fprintf(stderr, "Error while transferring frame data to surface."
  169. "Error code: %s.\n", av_err2str(err));
  170. goto close;
  171. }
  172. if ((err = (encode_write(avctx, hw_frame, fout))) < 0) {
  173. fprintf(stderr, "Failed to encode.\n");
  174. goto close;
  175. }
  176. av_frame_free(&hw_frame);
  177. av_frame_free(&sw_frame);
  178. }
  179. /* flush encoder */
  180. err = encode_write(avctx, NULL, fout);
  181. if (err == AVERROR_EOF)
  182. err = 0;
  183. close:
  184. if (fin)
  185. fclose(fin);
  186. if (fout)
  187. fclose(fout);
  188. av_frame_free(&sw_frame);
  189. av_frame_free(&hw_frame);
  190. avcodec_free_context(&avctx);
  191. av_buffer_unref(&hw_device_ctx);
  192. return err;
  193. }