vaapi_transcode.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Video Acceleration API (video transcoding) transcode 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 transcoding example.
  25. *
  26. * @example vaapi_transcode.c
  27. * This example shows how to do VAAPI-accelerated transcoding.
  28. * Usage: vaapi_transcode input_stream codec output_stream
  29. * e.g: - vaapi_transcode input.mp4 h264_vaapi output_h264.mp4
  30. * - vaapi_transcode input.mp4 vp9_vaapi output_vp9.ivf
  31. */
  32. #include <stdio.h>
  33. #include <errno.h>
  34. #include <libavutil/hwcontext.h>
  35. #include <libavcodec/avcodec.h>
  36. #include <libavformat/avformat.h>
  37. static AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;
  38. static AVBufferRef *hw_device_ctx = NULL;
  39. static AVCodecContext *decoder_ctx = NULL, *encoder_ctx = NULL;
  40. static int video_stream = -1;
  41. static AVStream *ost;
  42. static int initialized = 0;
  43. static enum AVPixelFormat get_vaapi_format(AVCodecContext *ctx,
  44. const enum AVPixelFormat *pix_fmts)
  45. {
  46. const enum AVPixelFormat *p;
  47. for (p = pix_fmts; *p != AV_PIX_FMT_NONE; p++) {
  48. if (*p == AV_PIX_FMT_VAAPI)
  49. return *p;
  50. }
  51. fprintf(stderr, "Unable to decode this file using VA-API.\n");
  52. return AV_PIX_FMT_NONE;
  53. }
  54. static int open_input_file(const char *filename)
  55. {
  56. int ret;
  57. AVCodec *decoder = NULL;
  58. AVStream *video = NULL;
  59. if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0) {
  60. fprintf(stderr, "Cannot open input file '%s', Error code: %s\n",
  61. filename, av_err2str(ret));
  62. return ret;
  63. }
  64. if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {
  65. fprintf(stderr, "Cannot find input stream information. Error code: %s\n",
  66. av_err2str(ret));
  67. return ret;
  68. }
  69. ret = av_find_best_stream(ifmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0);
  70. if (ret < 0) {
  71. fprintf(stderr, "Cannot find a video stream in the input file. "
  72. "Error code: %s\n", av_err2str(ret));
  73. return ret;
  74. }
  75. video_stream = ret;
  76. if (!(decoder_ctx = avcodec_alloc_context3(decoder)))
  77. return AVERROR(ENOMEM);
  78. video = ifmt_ctx->streams[video_stream];
  79. if ((ret = avcodec_parameters_to_context(decoder_ctx, video->codecpar)) < 0) {
  80. fprintf(stderr, "avcodec_parameters_to_context error. Error code: %s\n",
  81. av_err2str(ret));
  82. return ret;
  83. }
  84. decoder_ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
  85. if (!decoder_ctx->hw_device_ctx) {
  86. fprintf(stderr, "A hardware device reference create failed.\n");
  87. return AVERROR(ENOMEM);
  88. }
  89. decoder_ctx->get_format = get_vaapi_format;
  90. if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0)
  91. fprintf(stderr, "Failed to open codec for decoding. Error code: %s\n",
  92. av_err2str(ret));
  93. return ret;
  94. }
  95. static int encode_write(AVFrame *frame)
  96. {
  97. int ret = 0;
  98. AVPacket enc_pkt;
  99. av_init_packet(&enc_pkt);
  100. enc_pkt.data = NULL;
  101. enc_pkt.size = 0;
  102. if ((ret = avcodec_send_frame(encoder_ctx, frame)) < 0) {
  103. fprintf(stderr, "Error during encoding. Error code: %s\n", av_err2str(ret));
  104. goto end;
  105. }
  106. while (1) {
  107. ret = avcodec_receive_packet(encoder_ctx, &enc_pkt);
  108. if (ret)
  109. break;
  110. enc_pkt.stream_index = 0;
  111. av_packet_rescale_ts(&enc_pkt, ifmt_ctx->streams[video_stream]->time_base,
  112. ofmt_ctx->streams[0]->time_base);
  113. ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
  114. if (ret < 0) {
  115. fprintf(stderr, "Error during writing data to output file. "
  116. "Error code: %s\n", av_err2str(ret));
  117. return -1;
  118. }
  119. }
  120. end:
  121. if (ret == AVERROR_EOF)
  122. return 0;
  123. ret = ((ret == AVERROR(EAGAIN)) ? 0:-1);
  124. return ret;
  125. }
  126. static int dec_enc(AVPacket *pkt, AVCodec *enc_codec)
  127. {
  128. AVFrame *frame;
  129. int ret = 0;
  130. ret = avcodec_send_packet(decoder_ctx, pkt);
  131. if (ret < 0) {
  132. fprintf(stderr, "Error during decoding. Error code: %s\n", av_err2str(ret));
  133. return ret;
  134. }
  135. while (ret >= 0) {
  136. if (!(frame = av_frame_alloc()))
  137. return AVERROR(ENOMEM);
  138. ret = avcodec_receive_frame(decoder_ctx, frame);
  139. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  140. av_frame_free(&frame);
  141. return 0;
  142. } else if (ret < 0) {
  143. fprintf(stderr, "Error while decoding. Error code: %s\n", av_err2str(ret));
  144. goto fail;
  145. }
  146. if (!initialized) {
  147. /* we need to ref hw_frames_ctx of decoder to initialize encoder's codec.
  148. Only after we get a decoded frame, can we obtain its hw_frames_ctx */
  149. encoder_ctx->hw_frames_ctx = av_buffer_ref(decoder_ctx->hw_frames_ctx);
  150. if (!encoder_ctx->hw_frames_ctx) {
  151. ret = AVERROR(ENOMEM);
  152. goto fail;
  153. }
  154. /* set AVCodecContext Parameters for encoder, here we keep them stay
  155. * the same as decoder.
  156. * xxx: now the sample can't handle resolution change case.
  157. */
  158. encoder_ctx->time_base = av_inv_q(decoder_ctx->framerate);
  159. encoder_ctx->pix_fmt = AV_PIX_FMT_VAAPI;
  160. encoder_ctx->width = decoder_ctx->width;
  161. encoder_ctx->height = decoder_ctx->height;
  162. if ((ret = avcodec_open2(encoder_ctx, enc_codec, NULL)) < 0) {
  163. fprintf(stderr, "Failed to open encode codec. Error code: %s\n",
  164. av_err2str(ret));
  165. goto fail;
  166. }
  167. if (!(ost = avformat_new_stream(ofmt_ctx, enc_codec))) {
  168. fprintf(stderr, "Failed to allocate stream for output format.\n");
  169. ret = AVERROR(ENOMEM);
  170. goto fail;
  171. }
  172. ost->time_base = encoder_ctx->time_base;
  173. ret = avcodec_parameters_from_context(ost->codecpar, encoder_ctx);
  174. if (ret < 0) {
  175. fprintf(stderr, "Failed to copy the stream parameters. "
  176. "Error code: %s\n", av_err2str(ret));
  177. goto fail;
  178. }
  179. /* write the stream header */
  180. if ((ret = avformat_write_header(ofmt_ctx, NULL)) < 0) {
  181. fprintf(stderr, "Error while writing stream header. "
  182. "Error code: %s\n", av_err2str(ret));
  183. goto fail;
  184. }
  185. initialized = 1;
  186. }
  187. if ((ret = encode_write(frame)) < 0)
  188. fprintf(stderr, "Error during encoding and writing.\n");
  189. fail:
  190. av_frame_free(&frame);
  191. if (ret < 0)
  192. return ret;
  193. }
  194. return 0;
  195. }
  196. int main(int argc, char **argv)
  197. {
  198. int ret = 0;
  199. AVPacket dec_pkt;
  200. AVCodec *enc_codec;
  201. if (argc != 4) {
  202. fprintf(stderr, "Usage: %s <input file> <encode codec> <output file>\n"
  203. "The output format is guessed according to the file extension.\n"
  204. "\n", argv[0]);
  205. return -1;
  206. }
  207. ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, NULL, NULL, 0);
  208. if (ret < 0) {
  209. fprintf(stderr, "Failed to create a VAAPI device. Error code: %s\n", av_err2str(ret));
  210. return -1;
  211. }
  212. if ((ret = open_input_file(argv[1])) < 0)
  213. goto end;
  214. if (!(enc_codec = avcodec_find_encoder_by_name(argv[2]))) {
  215. fprintf(stderr, "Could not find encoder '%s'\n", argv[2]);
  216. ret = -1;
  217. goto end;
  218. }
  219. if ((ret = (avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, argv[3]))) < 0) {
  220. fprintf(stderr, "Failed to deduce output format from file extension. Error code: "
  221. "%s\n", av_err2str(ret));
  222. goto end;
  223. }
  224. if (!(encoder_ctx = avcodec_alloc_context3(enc_codec))) {
  225. ret = AVERROR(ENOMEM);
  226. goto end;
  227. }
  228. ret = avio_open(&ofmt_ctx->pb, argv[3], AVIO_FLAG_WRITE);
  229. if (ret < 0) {
  230. fprintf(stderr, "Cannot open output file. "
  231. "Error code: %s\n", av_err2str(ret));
  232. goto end;
  233. }
  234. /* read all packets and only transcoding video */
  235. while (ret >= 0) {
  236. if ((ret = av_read_frame(ifmt_ctx, &dec_pkt)) < 0)
  237. break;
  238. if (video_stream == dec_pkt.stream_index)
  239. ret = dec_enc(&dec_pkt, enc_codec);
  240. av_packet_unref(&dec_pkt);
  241. }
  242. /* flush decoder */
  243. dec_pkt.data = NULL;
  244. dec_pkt.size = 0;
  245. ret = dec_enc(&dec_pkt, enc_codec);
  246. av_packet_unref(&dec_pkt);
  247. /* flush encoder */
  248. ret = encode_write(NULL);
  249. /* write the trailer for output stream */
  250. av_write_trailer(ofmt_ctx);
  251. end:
  252. avformat_close_input(&ifmt_ctx);
  253. avformat_close_input(&ofmt_ctx);
  254. avcodec_free_context(&decoder_ctx);
  255. avcodec_free_context(&encoder_ctx);
  256. av_buffer_unref(&hw_device_ctx);
  257. return ret;
  258. }