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.

243 lines
7.6KB

  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. AVBSFContext *bsf;
  35. AVFifoBuffer *packet_fifo;
  36. AVPacket pkt_filtered;
  37. } SVCContext;
  38. static av_cold int svc_decode_close(AVCodecContext *avctx)
  39. {
  40. SVCContext *s = avctx->priv_data;
  41. AVPacket pkt;
  42. if (s->decoder)
  43. WelsDestroyDecoder(s->decoder);
  44. while (s->packet_fifo && av_fifo_size(s->packet_fifo) >= sizeof(pkt)) {
  45. av_fifo_generic_read(s->packet_fifo, &pkt, sizeof(pkt), NULL);
  46. av_packet_unref(&pkt);
  47. }
  48. av_bsf_free(&s->bsf);
  49. av_packet_unref(&s->pkt_filtered);
  50. av_fifo_free(s->packet_fifo);
  51. return 0;
  52. }
  53. static av_cold int svc_decode_init(AVCodecContext *avctx)
  54. {
  55. SVCContext *s = avctx->priv_data;
  56. SDecodingParam param = { 0 };
  57. int err;
  58. int log_level;
  59. WelsTraceCallback callback_function;
  60. if ((err = ff_libopenh264_check_version(avctx)) < 0)
  61. return err;
  62. s->packet_fifo = av_fifo_alloc(sizeof(AVPacket));
  63. if (!s->packet_fifo)
  64. return AVERROR(ENOMEM);
  65. if (WelsCreateDecoder(&s->decoder)) {
  66. av_log(avctx, AV_LOG_ERROR, "Unable to create decoder\n");
  67. return AVERROR_UNKNOWN;
  68. }
  69. // Pass all libopenh264 messages to our callback, to allow ourselves to filter them.
  70. log_level = WELS_LOG_DETAIL;
  71. callback_function = ff_libopenh264_trace_callback;
  72. (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_LEVEL, &log_level);
  73. (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_CALLBACK, (void *)&callback_function);
  74. (*s->decoder)->SetOption(s->decoder, DECODER_OPTION_TRACE_CALLBACK_CONTEXT, (void *)&avctx);
  75. #if !OPENH264_VER_AT_LEAST(1, 6)
  76. param.eOutputColorFormat = videoFormatI420;
  77. #endif
  78. param.eEcActiveIdc = ERROR_CON_DISABLE;
  79. param.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT;
  80. if ((*s->decoder)->Initialize(s->decoder, &param) != cmResultSuccess) {
  81. av_log(avctx, AV_LOG_ERROR, "Initialize failed\n");
  82. return AVERROR_UNKNOWN;
  83. }
  84. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  85. return 0;
  86. }
  87. static int init_bsf(AVCodecContext *avctx)
  88. {
  89. SVCContext *s = avctx->priv_data;
  90. const AVBitStreamFilter *filter;
  91. int ret;
  92. if (s->bsf)
  93. return 0;
  94. // If the input stream already is annex b, this BSF only passes the
  95. // packets through unchanged.
  96. filter = av_bsf_get_by_name("h264_mp4toannexb");
  97. if (!filter)
  98. return AVERROR_BUG;
  99. ret = av_bsf_alloc(filter, &s->bsf);
  100. if (ret < 0)
  101. return ret;
  102. ret = avcodec_parameters_from_context(s->bsf->par_in, avctx);
  103. if (ret < 0)
  104. return ret;
  105. s->bsf->time_base_in = avctx->time_base;
  106. ret = av_bsf_init(s->bsf);
  107. if (ret < 0)
  108. return ret;
  109. return ret;
  110. }
  111. static int svc_decode_frame(AVCodecContext *avctx, void *data,
  112. int *got_frame, AVPacket *avpkt)
  113. {
  114. SVCContext *s = avctx->priv_data;
  115. SBufferInfo info = { 0 };
  116. uint8_t* ptrs[3];
  117. int linesize[3];
  118. AVFrame *avframe = data;
  119. int ret;
  120. DECODING_STATE state;
  121. if ((ret = init_bsf(avctx)) < 0)
  122. return ret;
  123. if (avpkt->size) {
  124. AVPacket input_ref = { 0 };
  125. if (av_fifo_space(s->packet_fifo) < sizeof(input_ref)) {
  126. ret = av_fifo_realloc2(s->packet_fifo,
  127. av_fifo_size(s->packet_fifo) + sizeof(input_ref));
  128. if (ret < 0)
  129. return ret;
  130. }
  131. ret = av_packet_ref(&input_ref, avpkt);
  132. if (ret < 0)
  133. return ret;
  134. av_fifo_generic_write(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
  135. }
  136. while (!*got_frame) {
  137. /* prepare the input data -- convert to Annex B if needed */
  138. if (s->pkt_filtered.size <= 0) {
  139. AVPacket input_ref;
  140. /* no more data */
  141. if (av_fifo_size(s->packet_fifo) < sizeof(AVPacket))
  142. return avpkt->size ? avpkt->size : 0;
  143. av_packet_unref(&s->pkt_filtered);
  144. av_fifo_generic_read(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
  145. ret = av_bsf_send_packet(s->bsf, &input_ref);
  146. if (ret < 0) {
  147. av_packet_unref(&input_ref);
  148. return ret;
  149. }
  150. ret = av_bsf_receive_packet(s->bsf, &s->pkt_filtered);
  151. if (ret < 0)
  152. av_packet_move_ref(&s->pkt_filtered, &input_ref);
  153. else
  154. av_packet_unref(&input_ref);
  155. }
  156. state = (*s->decoder)->DecodeFrame2(s->decoder, s->pkt_filtered.data, s->pkt_filtered.size, ptrs, &info);
  157. s->pkt_filtered.size = 0;
  158. if (state != dsErrorFree) {
  159. av_log(avctx, AV_LOG_ERROR, "DecodeFrame2 failed\n");
  160. return AVERROR_UNKNOWN;
  161. }
  162. if (info.iBufferStatus != 1) {
  163. av_log(avctx, AV_LOG_DEBUG, "No frame produced\n");
  164. continue;
  165. }
  166. ff_set_dimensions(avctx, info.UsrData.sSystemBuffer.iWidth, info.UsrData.sSystemBuffer.iHeight);
  167. // The decoder doesn't (currently) support decoding into a user
  168. // provided buffer, so do a copy instead.
  169. if (ff_get_buffer(avctx, avframe, 0) < 0) {
  170. av_log(avctx, AV_LOG_ERROR, "Unable to allocate buffer\n");
  171. return AVERROR(ENOMEM);
  172. }
  173. linesize[0] = info.UsrData.sSystemBuffer.iStride[0];
  174. linesize[1] = linesize[2] = info.UsrData.sSystemBuffer.iStride[1];
  175. av_image_copy(avframe->data, avframe->linesize, (const uint8_t **) ptrs, linesize, avctx->pix_fmt, avctx->width, avctx->height);
  176. avframe->pts = s->pkt_filtered.pts;
  177. avframe->pkt_dts = s->pkt_filtered.dts;
  178. #if FF_API_PKT_PTS
  179. FF_DISABLE_DEPRECATION_WARNINGS
  180. avframe->pkt_pts = s->pkt_filtered.pts;
  181. FF_ENABLE_DEPRECATION_WARNINGS
  182. #endif
  183. *got_frame = 1;
  184. }
  185. return avpkt->size;
  186. }
  187. AVCodec ff_libopenh264_decoder = {
  188. .name = "libopenh264",
  189. .long_name = NULL_IF_CONFIG_SMALL("OpenH264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  190. .type = AVMEDIA_TYPE_VIDEO,
  191. .id = AV_CODEC_ID_H264,
  192. .priv_data_size = sizeof(SVCContext),
  193. .init = svc_decode_init,
  194. .decode = svc_decode_frame,
  195. .close = svc_decode_close,
  196. // The decoder doesn't currently support B-frames, and the decoder's API
  197. // doesn't support reordering/delay, but the BSF could incur delay.
  198. .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
  199. .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS | FF_CODEC_CAP_INIT_THREADSAFE |
  200. FF_CODEC_CAP_INIT_CLEANUP,
  201. };