hw_decode.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2017 Jun Zhao
  3. * Copyright (c) 2017 Kaixuan Liu
  4. *
  5. * HW Acceleration API (video decoding) decode sample
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. /**
  26. * @file HW-accelerated decoding API usage.example
  27. * @example hw_decode.c
  28. *
  29. * Perform HW-accelerated decoding with output frames from HW video
  30. * surfaces.
  31. */
  32. #include <stdio.h>
  33. #include <libavcodec/avcodec.h>
  34. #include <libavformat/avformat.h>
  35. #include <libavutil/mem.h>
  36. #include <libavutil/pixdesc.h>
  37. #include <libavutil/hwcontext.h>
  38. #include <libavutil/opt.h>
  39. #include <libavutil/avassert.h>
  40. #include <libavutil/imgutils.h>
  41. static AVBufferRef *hw_device_ctx = NULL;
  42. static enum AVPixelFormat hw_pix_fmt;
  43. static FILE *output_file = NULL;
  44. static int hw_decoder_init(AVCodecContext *ctx, const enum AVHWDeviceType type)
  45. {
  46. int err = 0;
  47. if ((err = av_hwdevice_ctx_create(&hw_device_ctx, type,
  48. NULL, NULL, 0)) < 0) {
  49. fprintf(stderr, "Failed to create specified HW device.\n");
  50. return err;
  51. }
  52. ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
  53. return err;
  54. }
  55. static enum AVPixelFormat get_hw_format(AVCodecContext *ctx,
  56. const enum AVPixelFormat *pix_fmts)
  57. {
  58. const enum AVPixelFormat *p;
  59. for (p = pix_fmts; *p != -1; p++) {
  60. if (*p == hw_pix_fmt)
  61. return *p;
  62. }
  63. fprintf(stderr, "Failed to get HW surface format.\n");
  64. return AV_PIX_FMT_NONE;
  65. }
  66. static int decode_write(AVCodecContext *avctx, AVPacket *packet)
  67. {
  68. AVFrame *frame = NULL, *sw_frame = NULL;
  69. AVFrame *tmp_frame = NULL;
  70. uint8_t *buffer = NULL;
  71. int size;
  72. int ret = 0;
  73. ret = avcodec_send_packet(avctx, packet);
  74. if (ret < 0) {
  75. fprintf(stderr, "Error during decoding\n");
  76. return ret;
  77. }
  78. while (1) {
  79. if (!(frame = av_frame_alloc()) || !(sw_frame = av_frame_alloc())) {
  80. fprintf(stderr, "Can not alloc frame\n");
  81. ret = AVERROR(ENOMEM);
  82. goto fail;
  83. }
  84. ret = avcodec_receive_frame(avctx, frame);
  85. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  86. av_frame_free(&frame);
  87. av_frame_free(&sw_frame);
  88. return 0;
  89. } else if (ret < 0) {
  90. fprintf(stderr, "Error while decoding\n");
  91. goto fail;
  92. }
  93. if (frame->format == hw_pix_fmt) {
  94. /* retrieve data from GPU to CPU */
  95. if ((ret = av_hwframe_transfer_data(sw_frame, frame, 0)) < 0) {
  96. fprintf(stderr, "Error transferring the data to system memory\n");
  97. goto fail;
  98. }
  99. tmp_frame = sw_frame;
  100. } else
  101. tmp_frame = frame;
  102. size = av_image_get_buffer_size(tmp_frame->format, tmp_frame->width,
  103. tmp_frame->height, 1);
  104. buffer = av_malloc(size);
  105. if (!buffer) {
  106. fprintf(stderr, "Can not alloc buffer\n");
  107. ret = AVERROR(ENOMEM);
  108. goto fail;
  109. }
  110. ret = av_image_copy_to_buffer(buffer, size,
  111. (const uint8_t * const *)tmp_frame->data,
  112. (const int *)tmp_frame->linesize, tmp_frame->format,
  113. tmp_frame->width, tmp_frame->height, 1);
  114. if (ret < 0) {
  115. fprintf(stderr, "Can not copy image to buffer\n");
  116. goto fail;
  117. }
  118. if ((ret = fwrite(buffer, 1, size, output_file)) < 0) {
  119. fprintf(stderr, "Failed to dump raw data.\n");
  120. goto fail;
  121. }
  122. fail:
  123. av_frame_free(&frame);
  124. av_frame_free(&sw_frame);
  125. av_freep(&buffer);
  126. if (ret < 0)
  127. return ret;
  128. }
  129. }
  130. int main(int argc, char *argv[])
  131. {
  132. AVFormatContext *input_ctx = NULL;
  133. int video_stream, ret;
  134. AVStream *video = NULL;
  135. AVCodecContext *decoder_ctx = NULL;
  136. const AVCodec *decoder = NULL;
  137. AVPacket *packet = NULL;
  138. enum AVHWDeviceType type;
  139. int i;
  140. if (argc < 4) {
  141. fprintf(stderr, "Usage: %s <device type> <input file> <output file>\n", argv[0]);
  142. return -1;
  143. }
  144. type = av_hwdevice_find_type_by_name(argv[1]);
  145. if (type == AV_HWDEVICE_TYPE_NONE) {
  146. fprintf(stderr, "Device type %s is not supported.\n", argv[1]);
  147. fprintf(stderr, "Available device types:");
  148. while((type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)
  149. fprintf(stderr, " %s", av_hwdevice_get_type_name(type));
  150. fprintf(stderr, "\n");
  151. return -1;
  152. }
  153. packet = av_packet_alloc();
  154. if (!packet) {
  155. fprintf(stderr, "Failed to allocate AVPacket\n");
  156. return -1;
  157. }
  158. /* open the input file */
  159. if (avformat_open_input(&input_ctx, argv[2], NULL, NULL) != 0) {
  160. fprintf(stderr, "Cannot open input file '%s'\n", argv[2]);
  161. return -1;
  162. }
  163. if (avformat_find_stream_info(input_ctx, NULL) < 0) {
  164. fprintf(stderr, "Cannot find input stream information.\n");
  165. return -1;
  166. }
  167. /* find the video stream information */
  168. ret = av_find_best_stream(input_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0);
  169. if (ret < 0) {
  170. fprintf(stderr, "Cannot find a video stream in the input file\n");
  171. return -1;
  172. }
  173. video_stream = ret;
  174. for (i = 0;; i++) {
  175. const AVCodecHWConfig *config = avcodec_get_hw_config(decoder, i);
  176. if (!config) {
  177. fprintf(stderr, "Decoder %s does not support device type %s.\n",
  178. decoder->name, av_hwdevice_get_type_name(type));
  179. return -1;
  180. }
  181. if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
  182. config->device_type == type) {
  183. hw_pix_fmt = config->pix_fmt;
  184. break;
  185. }
  186. }
  187. if (!(decoder_ctx = avcodec_alloc_context3(decoder)))
  188. return AVERROR(ENOMEM);
  189. video = input_ctx->streams[video_stream];
  190. if (avcodec_parameters_to_context(decoder_ctx, video->codecpar) < 0)
  191. return -1;
  192. decoder_ctx->get_format = get_hw_format;
  193. if (hw_decoder_init(decoder_ctx, type) < 0)
  194. return -1;
  195. if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0) {
  196. fprintf(stderr, "Failed to open codec for stream #%u\n", video_stream);
  197. return -1;
  198. }
  199. /* open the file to dump raw data */
  200. output_file = fopen(argv[3], "w+b");
  201. /* actual decoding and dump the raw data */
  202. while (ret >= 0) {
  203. if ((ret = av_read_frame(input_ctx, packet)) < 0)
  204. break;
  205. if (video_stream == packet->stream_index)
  206. ret = decode_write(decoder_ctx, packet);
  207. av_packet_unref(packet);
  208. }
  209. /* flush the decoder */
  210. ret = decode_write(decoder_ctx, NULL);
  211. if (output_file)
  212. fclose(output_file);
  213. av_packet_free(&packet);
  214. avcodec_free_context(&decoder_ctx);
  215. avformat_close_input(&input_ctx);
  216. av_buffer_unref(&hw_device_ctx);
  217. return 0;
  218. }