qsvdec.c 8.1 KB

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