如何禁用libav自动旋转显示矩阵

如何禁用libav自动旋转显示矩阵

本文介绍了如何禁用libav自动旋转显示矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从手机上以人像模式拍摄了一个视频.这是有关视​​频的转储信息:

I have a video taken from my mobile in portrait mode. Here is the dumped info about the video:

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video.MOV':
Metadata:
major_brand     : qt
minor_version   : 0
compatible_brands: qt
creation_time   : 2017-05-04 02:21:37
Duration: 00:00:06.91, start: 0.000023, bitrate: 4700 kb/s
Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 90 kb/s (default)
Metadata:
  creation_time   : 2017-05-04 02:21:37
  handler_name    : Core Media Data Handler
Stream #0:1(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720, 4602 kb/s, 29.98 fps, 29.97 tbr, 600 tbn, 1200 tbc (default)
Metadata:
  rotate          : 90
  creation_time   : 2017-05-04 02:21:37
  handler_name    : Core Media Data Handler
  encoder         : H.264
Side data:
  displaymatrix: rotation of -90.00 degrees
  5.78 A-V: -0.028 fd=   1 aq=   14KB vq=  351KB sq=    0B f=0/0

我正在使用libav api解码/编码我的视频.编码后,我将视频旋转了90度.

I am using libav api to decode/encode my video. After encoding, I get the rotated version of the video by -90 degrees.

如何停止解码器以防止自动旋转?

How can I stop decoder to prevent auto-rotation?

推荐答案

在长时间处理了显示矩阵边数据导致的问题之后,我遇到了以下解决方案:

After a long time dealing with problems caused by display matrix side data, I came across the following solutions:

AVStream *in_stream; //input stream
AVStream *out_stream; //output stream
....
if(in_stream->side_data!=NULL){

  av_log(NULL, AV_LOG_ERROR, "side data size: %d , size: %d\n", in_stream->side_data->size, sizeof(uint8_t*));

        if(av_stream_get_side_data(in_stream , AV_PKT_DATA_DISPLAYMATRIX, sd_size)!=NULL){

            uint8_t* resp=(uint8_t*) av_mallocz(in_stream->side_data->size+
                                                AV_INPUT_BUFFER_MIN_SIZE);


            resp  = av_stream_get_side_data(in_stream , AV_PKT_DATA_DISPLAYMATRIX, sd_size);

              av_log(NULL, AV_LOG_DEBUG,"side data detected, size :%d vs %d, "
                                        "nb_side_data %d, sizeof data %d,"
                                        " sizeof resp %d\n",
                     *sd_size, in_stream->side_data->size ,in_stream->nb_side_data,
                     in_stream->side_data->size*sizeof(uint8_t*),sizeof(resp));
              in_stream->side_data->data = (uint8_t*) av_mallocz(
                          in_stream->side_data->size*sizeof(uint8_t*)
                          );
              hasRotation = true; /* it will be used for
              something like ffmpeg does with noautorotate flag */
              av_stream_add_side_data(out_stream, AV_PKT_DATA_DISPLAYMATRIX, resp, *sd_size);

        }
    }

生成的视频将具有显示矩阵,并且一些播放器会正确显示它们.

The resulting video will have a display matrix, and some players will show them right.

static int init_filters(void)
{
    const char *filter_spec;
    unsigned int i;
    int ret;
    filter_ctx = (FilteringContext*) av_malloc_array(ifmt_ctx->nb_streams, sizeof(*filter_ctx));
if (!filter_ctx)
    return AVERROR(ENOMEM);
     for (i = 0; i < ifmt_ctx->nb_streams; i++) {
       filter_ctx[i].buffersrc_ctx  = NULL;
       filter_ctx[i].buffersink_ctx = NULL;
       filter_ctx[i].filter_graph   = NULL;
       if (!(ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO
            || ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO))
        continue;
        if (ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
        filter_spec = isRotated ?"transpose=clock":"null"; /* passthrough (dummy) filter for video */
    else
        filter_spec = "anull"; /* passthrough (dummy) filter for audio */
    ret = init_filter(&filter_ctx[i], stream_ctx[i].dec_ctx,
            stream_ctx[i].enc_ctx, filter_spec);
    if (ret)
        return ret;
}
return 0;
}

请注意,还应交换编码器尺寸.这意味着:

Note that the encoder dimensions should also be swapped. It means:

enc_ctx->width = dec_ctx->height;
enc_ctx->height = enc_ctx->width;

我希望它能对其他人有所帮助.

I hope it would help others.

这篇关于如何禁用libav自动旋转显示矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 07:54