You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.9KB

  1. /*
  2. * OpenH264 video decoder
  3. * Copyright (C) 2016 Martin Storsjo
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <wels/codec_api.h>
  22. #include <wels/codec_ver.h>
  23. #include "libavutil/common.h"
  24. #include "libavutil/fifo.h"
  25. #include "libavutil/imgutils.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/mathematics.h"
  28. #include "libavutil/opt.h"
  29. #include "avcodec.h"
  30. #include "internal.h"
  31. #include "libopenh264.h"
  32. typedef struct SVCContext {
  33. ISVCDecoder *decoder;
  34. } SVCContext;
  35. static av_cold int svc_decode_close(AVCodecContext *avctx)
  36. {
  37. SVCContext *s = avctx->priv_data;
  38. if (s->decoder)
  39. WelsDestroyDecoder(s->decoder);
  40. return 0;
  41. }
  42. static av_cold int svc_decode_init(AVCodecContext *avctx)
  43. {
  44. SVCContext *s = avctx->priv_data;
  45. SDecodingParam param = { 0 };
  46. int err;
  47. int log_level;
  48. WelsTraceCallback callback_function;
  49. if ((err = ff_libopenh264_check_version(avctx)) < 0)
  50. return err;
  51. if (WelsCreateDecoder(&s->decoder)) {
  52. av_log(avctx, AV_LOG_ERROR, "Unable to create decoder\n");
  53. return AVERROR_UNKNOWN;
  54. }
  55. // Pass all libopenh264 messages to our callback, to allow ourselves to filter them.
  56. log_level = WELS_LOG_DETAIL;
  57. callback_function = ff_libopenh264_trace_callback;
  58. (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_LEVEL, &log_level);
  59. (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_CALLBACK, (void *)&callback_function);
  60. (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_CALLBACK_CONTEXT, (void *)&avctx);
  61. #if !OPENH264_VER_AT_LEAST(1, 6)
  62. param.eOutputColorFormat = videoFormatI420;
  63. #endif
  64. param.eEcActiveIdc = ERROR_CON_DISABLE;
  65. param.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT;
  66. if ((*s->decoder)->Initialize(s->decoder, &param) != cmResultSuccess) {
  67. av_log(avctx, AV_LOG_ERROR, "Initialize failed\n");
  68. return AVERROR_UNKNOWN;
  69. }
  70. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  71. return 0;
  72. }
  73. static int svc_decode_frame(AVCodecContext *avctx, void *data,
  74. int *got_frame, AVPacket *avpkt)
  75. {
  76. SVCContext *s = avctx->priv_data;
  77. SBufferInfo info = { 0 };
  78. uint8_t* ptrs[3];
  79. int linesize[3];
  80. AVFrame *avframe = data;
  81. DECODING_STATE state;
  82. state = (*s->decoder)->DecodeFrame2(s->decoder, avpkt->data, avpkt->size, ptrs, &info);
  83. if (state != dsErrorFree) {
  84. av_log(avctx, AV_LOG_ERROR, "DecodeFrame2 failed\n");
  85. return AVERROR_UNKNOWN;
  86. }
  87. if (info.iBufferStatus != 1) {
  88. av_log(avctx, AV_LOG_DEBUG, "No frame produced\n");
  89. return avpkt->size;
  90. }
  91. ff_set_dimensions(avctx, info.UsrData.sSystemBuffer.iWidth, info.UsrData.sSystemBuffer.iHeight);
  92. // The decoder doesn't (currently) support decoding into a user
  93. // provided buffer, so do a copy instead.
  94. if (ff_get_buffer(avctx, avframe, 0) < 0) {
  95. av_log(avctx, AV_LOG_ERROR, "Unable to allocate buffer\n");
  96. return AVERROR(ENOMEM);
  97. }
  98. linesize[0] = info.UsrData.sSystemBuffer.iStride[0];
  99. linesize[1] = linesize[2] = info.UsrData.sSystemBuffer.iStride[1];
  100. av_image_copy(avframe->data, avframe->linesize, (const uint8_t **) ptrs, linesize, avctx->pix_fmt, avctx->width, avctx->height);
  101. avframe->pts = avpkt->pts;
  102. avframe->pkt_dts = avpkt->dts;
  103. #if FF_API_PKT_PTS
  104. FF_DISABLE_DEPRECATION_WARNINGS
  105. avframe->pkt_pts = avpkt->pts;
  106. FF_ENABLE_DEPRECATION_WARNINGS
  107. #endif
  108. *got_frame = 1;
  109. return avpkt->size;
  110. }
  111. AVCodec ff_libopenh264_decoder = {
  112. .name = "libopenh264",
  113. .long_name = NULL_IF_CONFIG_SMALL("OpenH264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  114. .type = AVMEDIA_TYPE_VIDEO,
  115. .id = AV_CODEC_ID_H264,
  116. .priv_data_size = sizeof(SVCContext),
  117. .init = svc_decode_init,
  118. .decode = svc_decode_frame,
  119. .close = svc_decode_close,
  120. // The decoder doesn't currently support B-frames, and the decoder's API
  121. // doesn't support reordering/delay, but the BSF could incur delay.
  122. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
  123. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS | FF_CODEC_CAP_INIT_THREADSAFE |
  124. FF_CODEC_CAP_INIT_CLEANUP,
  125. .bsfs = "h264_mp4toannexb",
  126. };