Advertisement
Guest User

Untitled

a guest
Nov 13th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 4.73 KB | None | 0 0
  1. --- avconv_qsv.c        2016-11-13 14:16:50.934989800 -0300
  2. +++ ffmpeg_qsv.c        2016-11-13 14:16:20.301820600 -0300
  3. @@ -1,18 +1,18 @@
  4.  /*
  5. - * This file is part of Libav.
  6. + * This file is part of FFmpeg.
  7.   *
  8. - * Libav is free software; you can redistribute it and/or
  9. + * FFmpeg is free software; you can redistribute it and/or
  10.   * modify it under the terms of the GNU Lesser General Public
  11.   * License as published by the Free Software Foundation; either
  12.   * version 2.1 of the License, or (at your option) any later version.
  13.   *
  14. - * Libav is distributed in the hope that it will be useful,
  15. + * FFmpeg is distributed in the hope that it will be useful,
  16.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.   * Lesser General Public License for more details.
  19.   *
  20.   * You should have received a copy of the GNU Lesser General Public
  21. - * License along with Libav; if not, write to the Free Software
  22. + * License along with FFmpeg; if not, write to the Free Software
  23.   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24.   */
  25.  
  26. @@ -26,7 +26,7 @@
  27.  #include "libavutil/opt.h"
  28.  #include "libavcodec/qsv.h"
  29.  
  30. -#include "avconv.h"
  31. +#include "ffmpeg.h"
  32.  
  33.  static int qsv_get_buffer(AVCodecContext *s, AVFrame *frame, int flags)
  34.  {
  35. @@ -76,11 +76,11 @@
  36.      frames_ctx   = (AVHWFramesContext*)ist->hw_frames_ctx->data;
  37.      frames_hwctx = frames_ctx->hwctx;
  38.  
  39. -    frames_ctx->width             = s->coded_width;
  40. -    frames_ctx->height            = s->coded_height;
  41. +    frames_ctx->width             = FFALIGN(s->coded_width,  32);
  42. +    frames_ctx->height            = FFALIGN(s->coded_height, 32);
  43.      frames_ctx->format            = AV_PIX_FMT_QSV;
  44. -    frames_ctx->sw_format         = AV_PIX_FMT_NV12;
  45. -    frames_ctx->initial_pool_size = 32;
  46. +    frames_ctx->sw_format         = s->sw_pix_fmt;
  47. +    frames_ctx->initial_pool_size = 64;
  48.      frames_hwctx->frame_type      = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
  49.  
  50.      ret = av_hwframe_ctx_init(ist->hw_frames_ctx);
  51. @@ -94,3 +94,85 @@
  52.  
  53.      return 0;
  54.  }
  55. +
  56. +int qsv_transcode_init(OutputStream *ost)
  57. +{
  58. +    InputStream *ist;
  59. +    const enum AVPixelFormat *pix_fmt;
  60. +
  61. +    int err, i;
  62. +    AVBufferRef *encode_frames_ref = NULL;
  63. +    AVHWFramesContext *encode_frames;
  64. +    AVQSVFramesContext *qsv_frames;
  65. +
  66. +    /* check if the encoder supports QSV */
  67. +    if (!ost->enc->pix_fmts)
  68. +        return 0;
  69. +    for (pix_fmt = ost->enc->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++)
  70. +        if (*pix_fmt == AV_PIX_FMT_QSV)
  71. +            break;
  72. +    if (*pix_fmt == AV_PIX_FMT_NONE)
  73. +        return 0;
  74. +
  75. +    if (strcmp(ost->avfilter, "null") || ost->source_index < 0)
  76. +        return 0;
  77. +
  78. +    /* check if the decoder supports QSV and the output only goes to this stream */
  79. +    ist = input_streams[ost->source_index];
  80. +    if (ist->hwaccel_id != HWACCEL_QSV || !ist->dec || !ist->dec->pix_fmts)
  81. +        return 0;
  82. +    for (pix_fmt = ist->dec->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++)
  83. +        if (*pix_fmt == AV_PIX_FMT_QSV)
  84. +            break;
  85. +    if (*pix_fmt == AV_PIX_FMT_NONE)
  86. +        return 0;
  87. +
  88. +    for (i = 0; i < nb_output_streams; i++)
  89. +        if (output_streams[i] != ost &&
  90. +            output_streams[i]->source_index == ost->source_index)
  91. +            return 0;
  92. +
  93. +    av_log(NULL, AV_LOG_VERBOSE, "Setting up QSV transcoding\n");
  94. +
  95. +    if (!hw_device_ctx) {
  96. +        err = qsv_device_init(ist);
  97. +        if (err < 0)
  98. +            goto fail;
  99. +    }
  100. +
  101. +    // This creates a dummy hw_frames_ctx for the encoder to be
  102. +    // suitably initialised.  It only contains one real frame, so
  103. +    // hopefully doesn't waste too much memory.
  104. +
  105. +    encode_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx);
  106. +    if (!encode_frames_ref) {
  107. +        err = AVERROR(ENOMEM);
  108. +        goto fail;
  109. +    }
  110. +    encode_frames = (AVHWFramesContext*)encode_frames_ref->data;
  111. +    qsv_frames = encode_frames->hwctx;
  112. +
  113. +    encode_frames->width     = FFALIGN(ist->resample_width,  32);
  114. +    encode_frames->height    = FFALIGN(ist->resample_height, 32);
  115. +    encode_frames->format    = AV_PIX_FMT_QSV;
  116. +    encode_frames->sw_format = AV_PIX_FMT_NV12;
  117. +    encode_frames->initial_pool_size = 1;
  118. +
  119. +    qsv_frames->frame_type = MFX_MEMTYPE_VIDEO_MEMORY_DECODER_TARGET;
  120. +
  121. +    err = av_hwframe_ctx_init(encode_frames_ref);
  122. +    if (err < 0)
  123. +        goto fail;
  124. +
  125. +    ist->dec_ctx->pix_fmt       = AV_PIX_FMT_QSV;
  126. +    ist->resample_pix_fmt       = AV_PIX_FMT_QSV;
  127. +
  128. +    ost->enc_ctx->pix_fmt       = AV_PIX_FMT_QSV;
  129. +    ost->enc_ctx->hw_frames_ctx = encode_frames_ref;
  130. +
  131. +    return 0;
  132. +
  133. +fail:
  134. +    av_buffer_unref(&encode_frames_ref);
  135. +    return err;
  136. +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement