vaapi_transcode.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. const 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(AVPacket *enc_pkt, AVFrame *frame)
  96. {
  97. int ret = 0;
  98. av_packet_unref(enc_pkt);
  99. if ((ret = avcodec_send_frame(encoder_ctx, frame)) < 0) {
  100. fprintf(stderr, "Error during encoding. Error code: %s\n", av_err2str(ret));
  101. goto end;
  102. }
  103. while (1) {
  104. ret = avcodec_receive_packet(encoder_ctx, enc_pkt);
  105. if (ret)
  106. break;
  107. enc_pkt->stream_index = 0;
  108. av_packet_rescale_ts(enc_pkt, ifmt_ctx->streams[video_stream]->time_base,
  109. ofmt_ctx->streams[0]->time_base);
  110. ret = av_interleaved_write_frame(ofmt_ctx, enc_pkt);
  111. if (ret < 0) {
  112. fprintf(stderr, "Error during writing data to output file. "
  113. "Error code: %s\n", av_err2str(ret));
  114. return -1;
  115. }
  116. }
  117. end:
  118. if (ret == AVERROR_EOF)
  119. return 0;
  120. ret = ((ret == AVERROR(EAGAIN)) ? 0:-1);
  121. return ret;
  122. }
  123. static int dec_enc(AVPacket *pkt, const AVCodec *enc_codec)
  124. {
  125. AVFrame *frame;
  126. int ret = 0;
  127. ret = avcodec_send_packet(decoder_ctx, pkt);
  128. if (ret < 0) {
  129. fprintf(stderr, "Error during decoding. Error code: %s\n", av_err2str(ret));
  130. return ret;
  131. }
  132. while (ret >= 0) {
  133. if (!(frame = av_frame_alloc()))
  134. return AVERROR(ENOMEM);
  135. ret = avcodec_receive_frame(decoder_ctx, frame);
  136. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  137. av_frame_free(&frame);
  138. return 0;
  139. } else if (ret < 0) {
  140. fprintf(stderr, "Error while decoding. Error code: %s\n", av_err2str(ret));
  141. goto fail;
  142. }
  143. if (!initialized) {
  144. /* we need to ref hw_frames_ctx of decoder to initialize encoder's codec.
  145. Only after we get a decoded frame, can we obtain its hw_frames_ctx */
  146. encoder_ctx->hw_frames_ctx = av_buffer_ref(decoder_ctx->hw_frames_ctx);
  147. if (!encoder_ctx->hw_frames_ctx) {
  148. ret = AVERROR(ENOMEM);
  149. goto fail;
  150. }
  151. /* set AVCodecContext Parameters for encoder, here we keep them stay
  152. * the same as decoder.
  153. * xxx: now the sample can't handle resolution change case.
  154. */
  155. encoder_ctx->time_base = av_inv_q(decoder_ctx->framerate);
  156. encoder_ctx->pix_fmt = AV_PIX_FMT_VAAPI;
  157. encoder_ctx->width = decoder_ctx->width;
  158. encoder_ctx->height = decoder_ctx->height;
  159. if ((ret = avcodec_open2(encoder_ctx, enc_codec, NULL)) < 0) {
  160. fprintf(stderr, "Failed to open encode codec. Error code: %s\n",
  161. av_err2str(ret));
  162. goto fail;
  163. }
  164. if (!(ost = avformat_new_stream(ofmt_ctx, enc_codec))) {
  165. fprintf(stderr, "Failed to allocate stream for output format.\n");
  166. ret = AVERROR(ENOMEM);
  167. goto fail;
  168. }
  169. ost->time_base = encoder_ctx->time_base;
  170. ret = avcodec_parameters_from_context(ost->codecpar, encoder_ctx);
  171. if (ret < 0) {
  172. fprintf(stderr, "Failed to copy the stream parameters. "
  173. "Error code: %s\n", av_err2str(ret));
  174. goto fail;
  175. }
  176. /* write the stream header */
  177. if ((ret = avformat_write_header(ofmt_ctx, NULL)) < 0) {
  178. fprintf(stderr, "Error while writing stream header. "
  179. "Error code: %s\n", av_err2str(ret));
  180. goto fail;
  181. }
  182. initialized = 1;
  183. }
  184. if ((ret = encode_write(pkt, frame)) < 0)
  185. fprintf(stderr, "Error during encoding and writing.\n");
  186. fail:
  187. av_frame_free(&frame);
  188. if (ret < 0)
  189. return ret;
  190. }
  191. return 0;
  192. }
  193. int main(int argc, char **argv)
  194. {
  195. const AVCodec *enc_codec;
  196. int ret = 0;
  197. AVPacket *dec_pkt;
  198. if (argc != 4) {
  199. fprintf(stderr, "Usage: %s <input file> <encode codec> <output file>\n"
  200. "The output format is guessed according to the file extension.\n"
  201. "\n", argv[0]);
  202. return -1;
  203. }
  204. ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, NULL, NULL, 0);
  205. if (ret < 0) {
  206. fprintf(stderr, "Failed to create a VAAPI device. Error code: %s\n", av_err2str(ret));
  207. return -1;
  208. }
  209. dec_pkt = av_packet_alloc();
  210. if (!dec_pkt) {
  211. fprintf(stderr, "Failed to allocate decode packet\n");
  212. goto end;
  213. }
  214. if ((ret = open_input_file(argv[1])) < 0)
  215. goto end;
  216. if (!(enc_codec = avcodec_find_encoder_by_name(argv[2]))) {
  217. fprintf(stderr, "Could not find encoder '%s'\n", argv[2]);
  218. ret = -1;
  219. goto end;
  220. }
  221. if ((ret = (avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, argv[3]))) < 0) {
  222. fprintf(stderr, "Failed to deduce output format from file extension. Error code: "
  223. "%s\n", av_err2str(ret));
  224. goto end;
  225. }
  226. if (!(encoder_ctx = avcodec_alloc_context3(enc_codec))) {
  227. ret = AVERROR(ENOMEM);
  228. goto end;
  229. }
  230. ret = avio_open(&ofmt_ctx->pb, argv[3], AVIO_FLAG_WRITE);
  231. if (ret < 0) {
  232. fprintf(stderr, "Cannot open output file. "
  233. "Error code: %s\n", av_err2str(ret));
  234. goto end;
  235. }
  236. /* read all packets and only transcoding video */
  237. while (ret >= 0) {
  238. if ((ret = av_read_frame(ifmt_ctx, dec_pkt)) < 0)
  239. break;
  240. if (video_stream == dec_pkt->stream_index)
  241. ret = dec_enc(dec_pkt, enc_codec);
  242. av_packet_unref(dec_pkt);
  243. }
  244. /* flush decoder */
  245. av_packet_unref(dec_pkt);
  246. ret = dec_enc(dec_pkt, enc_codec);
  247. /* flush encoder */
  248. ret = encode_write(dec_pkt, 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. av_packet_free(&dec_pkt);
  258. return ret;
  259. }