qsv_decode.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2015 Anton Khirnov
  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 Intel QSV-accelerated H.264 decoding API usage example
  24. * @example qsv_decode.c
  25. *
  26. * Perform QSV-accelerated H.264 decoding with output frames in the
  27. * GPU video surfaces, write the decoded frames to an output file.
  28. */
  29. #include "config.h"
  30. #include <stdio.h>
  31. #include "libavformat/avformat.h"
  32. #include "libavformat/avio.h"
  33. #include "libavcodec/avcodec.h"
  34. #include "libavutil/buffer.h"
  35. #include "libavutil/error.h"
  36. #include "libavutil/hwcontext.h"
  37. #include "libavutil/hwcontext_qsv.h"
  38. #include "libavutil/mem.h"
  39. static int get_format(AVCodecContext *avctx, const enum AVPixelFormat *pix_fmts)
  40. {
  41. while (*pix_fmts != AV_PIX_FMT_NONE) {
  42. if (*pix_fmts == AV_PIX_FMT_QSV) {
  43. return AV_PIX_FMT_QSV;
  44. }
  45. pix_fmts++;
  46. }
  47. fprintf(stderr, "The QSV pixel format not offered in get_format()\n");
  48. return AV_PIX_FMT_NONE;
  49. }
  50. static int decode_packet(AVCodecContext *decoder_ctx,
  51. AVFrame *frame, AVFrame *sw_frame,
  52. AVPacket *pkt, AVIOContext *output_ctx)
  53. {
  54. int ret = 0;
  55. ret = avcodec_send_packet(decoder_ctx, pkt);
  56. if (ret < 0) {
  57. fprintf(stderr, "Error during decoding\n");
  58. return ret;
  59. }
  60. while (ret >= 0) {
  61. int i, j;
  62. ret = avcodec_receive_frame(decoder_ctx, frame);
  63. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  64. break;
  65. else if (ret < 0) {
  66. fprintf(stderr, "Error during decoding\n");
  67. return ret;
  68. }
  69. /* A real program would do something useful with the decoded frame here.
  70. * We just retrieve the raw data and write it to a file, which is rather
  71. * useless but pedagogic. */
  72. ret = av_hwframe_transfer_data(sw_frame, frame, 0);
  73. if (ret < 0) {
  74. fprintf(stderr, "Error transferring the data to system memory\n");
  75. goto fail;
  76. }
  77. for (i = 0; i < FF_ARRAY_ELEMS(sw_frame->data) && sw_frame->data[i]; i++)
  78. for (j = 0; j < (sw_frame->height >> (i > 0)); j++)
  79. avio_write(output_ctx, sw_frame->data[i] + j * sw_frame->linesize[i], sw_frame->width);
  80. fail:
  81. av_frame_unref(sw_frame);
  82. av_frame_unref(frame);
  83. if (ret < 0)
  84. return ret;
  85. }
  86. return 0;
  87. }
  88. int main(int argc, char **argv)
  89. {
  90. AVFormatContext *input_ctx = NULL;
  91. AVStream *video_st = NULL;
  92. AVCodecContext *decoder_ctx = NULL;
  93. const AVCodec *decoder;
  94. AVPacket *pkt = NULL;
  95. AVFrame *frame = NULL, *sw_frame = NULL;
  96. AVIOContext *output_ctx = NULL;
  97. int ret, i;
  98. AVBufferRef *device_ref = NULL;
  99. if (argc < 3) {
  100. fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
  101. return 1;
  102. }
  103. /* open the input file */
  104. ret = avformat_open_input(&input_ctx, argv[1], NULL, NULL);
  105. if (ret < 0) {
  106. fprintf(stderr, "Cannot open input file '%s': ", argv[1]);
  107. goto finish;
  108. }
  109. /* find the first H.264 video stream */
  110. for (i = 0; i < input_ctx->nb_streams; i++) {
  111. AVStream *st = input_ctx->streams[i];
  112. if (st->codecpar->codec_id == AV_CODEC_ID_H264 && !video_st)
  113. video_st = st;
  114. else
  115. st->discard = AVDISCARD_ALL;
  116. }
  117. if (!video_st) {
  118. fprintf(stderr, "No H.264 video stream in the input file\n");
  119. goto finish;
  120. }
  121. /* open the hardware device */
  122. ret = av_hwdevice_ctx_create(&device_ref, AV_HWDEVICE_TYPE_QSV,
  123. "auto", NULL, 0);
  124. if (ret < 0) {
  125. fprintf(stderr, "Cannot open the hardware device\n");
  126. goto finish;
  127. }
  128. /* initialize the decoder */
  129. decoder = avcodec_find_decoder_by_name("h264_qsv");
  130. if (!decoder) {
  131. fprintf(stderr, "The QSV decoder is not present in libavcodec\n");
  132. goto finish;
  133. }
  134. decoder_ctx = avcodec_alloc_context3(decoder);
  135. if (!decoder_ctx) {
  136. ret = AVERROR(ENOMEM);
  137. goto finish;
  138. }
  139. decoder_ctx->codec_id = AV_CODEC_ID_H264;
  140. if (video_st->codecpar->extradata_size) {
  141. decoder_ctx->extradata = av_mallocz(video_st->codecpar->extradata_size +
  142. AV_INPUT_BUFFER_PADDING_SIZE);
  143. if (!decoder_ctx->extradata) {
  144. ret = AVERROR(ENOMEM);
  145. goto finish;
  146. }
  147. memcpy(decoder_ctx->extradata, video_st->codecpar->extradata,
  148. video_st->codecpar->extradata_size);
  149. decoder_ctx->extradata_size = video_st->codecpar->extradata_size;
  150. }
  151. decoder_ctx->hw_device_ctx = av_buffer_ref(device_ref);
  152. decoder_ctx->get_format = get_format;
  153. ret = avcodec_open2(decoder_ctx, NULL, NULL);
  154. if (ret < 0) {
  155. fprintf(stderr, "Error opening the decoder: ");
  156. goto finish;
  157. }
  158. /* open the output stream */
  159. ret = avio_open(&output_ctx, argv[2], AVIO_FLAG_WRITE);
  160. if (ret < 0) {
  161. fprintf(stderr, "Error opening the output context: ");
  162. goto finish;
  163. }
  164. frame = av_frame_alloc();
  165. sw_frame = av_frame_alloc();
  166. pkt = av_packet_alloc();
  167. if (!frame || !sw_frame || !pkt) {
  168. ret = AVERROR(ENOMEM);
  169. goto finish;
  170. }
  171. /* actual decoding */
  172. while (ret >= 0) {
  173. ret = av_read_frame(input_ctx, pkt);
  174. if (ret < 0)
  175. break;
  176. if (pkt->stream_index == video_st->index)
  177. ret = decode_packet(decoder_ctx, frame, sw_frame, pkt, output_ctx);
  178. av_packet_unref(pkt);
  179. }
  180. /* flush the decoder */
  181. ret = decode_packet(decoder_ctx, frame, sw_frame, NULL, output_ctx);
  182. finish:
  183. if (ret < 0) {
  184. char buf[1024];
  185. av_strerror(ret, buf, sizeof(buf));
  186. fprintf(stderr, "%s\n", buf);
  187. }
  188. avformat_close_input(&input_ctx);
  189. av_frame_free(&frame);
  190. av_frame_free(&sw_frame);
  191. av_packet_free(&pkt);
  192. avcodec_free_context(&decoder_ctx);
  193. av_buffer_unref(&device_ref);
  194. avio_close(output_ctx);
  195. return ret;
  196. }