<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Arial,Helvetica,sans-serif;" dir="ltr">
<p></p>
<div>Hi, </div>
<div><br>
</div>
<div>I'm trying to transcode some H.264 video to HEVC, and it crashes in function <span style="font-family: Calibri, Arial, Helvetica, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", NotoColorEmoji, "Segoe UI Symbol", "Android Emoji", EmojiSymbols; font-size: 16px;">ff_merge_formats()</span></div>
<div><br>
</div>
<div>AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b,</div>
<div>                                  enum AVMediaType type)</div>
<div>{</div>
<div>    AVFilterFormats *ret = NULL;</div>
<div>    int i, j;</div>
<div>    int alpha1=0, alpha2=0;</div>
<div>    int chroma1=0, chroma2=0;</div>
<div><br>
</div>
<div>    if (a == b)</div>
<div>        return a;</div>
<div><br>
</div>
<div>    /* Do not lose chroma or alpha in merging.</div>
<div>       It happens if both lists have formats with chroma (resp. alpha), but</div>
<div>       the only formats in common do not have it (e.g. YUV+gray vs.</div>
<div>       RGB+gray): in that case, the merging would select the gray format,</div>
<div>       possibly causing a lossy conversion elsewhere in the graph.</div>
<div>       To avoid that, pretend that there are no common formats to force the</div>
<div>       insertion of a conversion filter. */</div>
<div>    if (type == AVMEDIA_TYPE_VIDEO)</div>
<div>        for (i = 0; i < a->nb_formats; i++)</div>
<div>            for (j = 0; j < b->nb_formats; j++) {</div>
<div>                const AVPixFmtDescriptor *adesc = av_pix_fmt_desc_get(a->formats[i]);</div>
<div>                const AVPixFmtDescriptor *bdesc = av_pix_fmt_desc_get(b->formats[j]);</div>
<div>                alpha2 |= adesc->flags & bdesc->flags & AV_PIX_FMT_FLAG_ALPHA;</div>
<div>                chroma2|= adesc->nb_components > 1 && bdesc->nb_components > 1;  <<======= Access violation on this line.</div>
<br>
<p></p>
<p>I suspect I'm missing something when I setup the encoder's context, but couldn't figure out what. Could somebody point out what I'm doing wrong here? A snippet of sample code to do HEVC transcoding would also be appreciated!</p>
<p><br>
</p>
<p></p>
<div>           if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>encoder = avcodec_find_encoder_by_name("hevc_nvenc");</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>if (!encoder) {</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>av_log(NULL, AV_LOG_FATAL, "HEVC_NVENC encoder not found\n");</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>return AVERROR_INVALIDDATA;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>enc_ctx = avcodec_alloc_context3(encoder);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>if (!enc_ctx) {</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>fprintf(stderr, "Could not allocate hevc_nvenc codec context\n");</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>return -1;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>//use gloabal headers</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>//set the profile and preset attributes</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>ret = av_opt_set(enc_ctx->priv_data, "profile", "main", AV_OPT_SEARCH_CHILDREN);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>if (ret < 0) {</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>av_log(NULL, AV_LOG_ERROR, "Cannot set profile to be main\n");</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>return ret;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>ret = av_opt_set(enc_ctx->priv_data, "preset", "hq", AV_OPT_SEARCH_CHILDREN);</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>if (ret < 0) {</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>av_log(NULL, AV_LOG_ERROR, "Cannot set preset to hq\n");</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>return ret;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>}</div>
<div><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>enc_ctx->height = dec_ctx->height;</div>
<div>                enc_ctx->width = dec_ctx->width;</div>
<div>                enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;</div>
<div><span class="Apple-tab-span" style="font-size: 12pt; white-space: pre;"></span><span style="font-size: 12pt;">enc_ctx->pix_fmt = AV_PIX_FMT_YUV420P;</span><br>
</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>enc_ctx->color_range = AVCOL_RANGE_JPEG;</div>
<div><br>
</div>
<div>                /* video time_base can be set to whatever is handy and supported by encoder */</div>
<div>                enc_ctx->time_base = dec_ctx->time_base;</div>
<div><span class="Apple-tab-span" style="white-space:pre"></span>out_stream->time_base = dec_ctx->time_base;</div>
<br>
<p></p>
<p>thanks!</p>
<p>David</p>
</div>
</body>
</html>