vaapi_encode.c 7.0 KB

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