[Libav-user] only avcodec_string can get right AVCodecContext's attribute (channels, sample_rate, etc)

王东琦 dongqi.wang at 163.com
Sat Mar 5 04:53:30 CET 2016


I meet a strange quesiton when I use ffmeg to decode aac files.


void avcodec_string2(char *buf, int buf_size, AVCodecContext *enc, int encode)
{
    const char *codec_type;
    const char *codec_name;
    const char *profile = NULL;
    int64_t bitrate;
    int new_line = 0;
    AVRational display_aspect_ratio;
    const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
    if (!buf || buf_size <= 0)
        return;
    codec_type = av_get_media_type_string(enc->codec_type);
    codec_name = avcodec_get_name(enc->codec_id);
    profile = avcodec_profile_name(enc->codec_id, enc->profile);
    snprintf(buf, buf_size, "%s: %s", codec_type ? codec_type : "unknown",
             codec_name);
    buf[0] ^= 'a' ^ 'A'; /* first letter in uppercase */
    if (enc->codec && strcmp(enc->codec->name, codec_name))
        snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", enc->codec->name);
    if (profile)
        snprintf(buf + strlen(buf), buf_size - strlen(buf), " (%s)", profile);
    return;
}
int aac_decoder_demo(char *url) 
{ 
    AVFormatContext *pFormatCtx; 
    int             i, audioStream;
    AVStream *pInStream;
    AVCodecContext  *pCodecCtx; 
    AVCodec         *pCodec; 
    AVPacket        *packet; 
    uint8_t         *out_buffer; 
    AVFrame         *pFrame; 
    int ret; 
    uint32_t len = 0; 
    int got_picture; 
    int index = 0; 
    int64_t in_channel_layout; 
    struct SwrContext *au_convert_ctx; 
 
    FILE *pFile=fopen("../out/output.pcm", "wb"); 
 
    av_register_all(); 
    avformat_network_init(); 

    //Open 
    pFormatCtx = avformat_alloc_context(); 
    if(avformat_open_input(&pFormatCtx, url, NULL, NULL)!=0)
    { 
        printf("Couldn't open input stream.\n"); 
        return -1; 
    }
    // Retrieve stream information 
    if(avformat_find_stream_info(pFormatCtx,NULL) < 0)
    { 
        printf("Couldn't find stream information.\n"); 
        return -1; 
    } 
    // Dump valid information onto standard error 
    av_dump_format(pFormatCtx, 0, url, false); 
 
    // Find the first audio stream 
    audioStream = -1; 
    for(i = 0; i < pFormatCtx->nb_streams; i++) 
    {
        if(pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
        { 
            printf("get audio stream at [%d / %d].\n", i, pFormatCtx->nb_streams);
            audioStream=i;            
            break; 
        } 
    }
   
    if(audioStream==-1)
    { 
        printf("Didn't find a audio stream.\n"); 
        return -1; 
    } 

    pInStream = pFormatCtx->streams[audioStream];
  
    // Get a pointer to the codec context for the audio stream 
    pCodecCtx = pInStream->codec;
    if (NULL == pCodecCtx)
    {
        printf("pCodecCtx is NULL\n"); 
        return -1;
       
    }
    printf("AVCodecContext[Line:%d]: mediaType[%d] codecID[%d] Profile[%d] SR[%d] channels[%d] channel_layout[%d] \n",
           __LINE__,  pCodecCtx->codec_type, pCodecCtx->codec_id, pCodecCtx->profile, pCodecCtx->sample_rate,
           pCodecCtx->channels, pCodecCtx->channel_layout);
   
    char debugBuf[1024];
    my_avcodec_string(debugBuf, sizeof(debugBuf), pCodecCtx);
    printf("my_avcodec_string: %s\n", debugBuf);

    // user avcodec_string in LIB, parse OK
    avcodec_string(debugBuf, sizeof(debugBuf), pCodecCtx, false);
 printf("avcodec_string: %s\n", debugBuf);
   
    avcodec_string2(debugBuf, sizeof(debugBuf), pCodecCtx, false);
    printf("avcodec_string2: %s\n", debugBuf);


    printf("AVCodecContext[Line:%d]: mediaType[%d] codecID[%d] Profile[%d] SR[%d] channels[%d] channel_layout[%d] \n",
           __LINE__,  pCodecCtx->codec_type, pCodecCtx->codec_id, pCodecCtx->profile, pCodecCtx->sample_rate,
           pCodecCtx->channels, pCodecCtx->channel_layout);
}





and the output is
AVCodecContext[Line:498]: mediaType[1] codecID[86018] Profile[0] SR[0] channels[0] channel_layout[1]
my_avcodec_string: Audio: aac (Main)1 channels (FL), u8, 30601641984 kb/s
avcodec_string: Audio: aac (LC), 12000 Hz, mono, fltp, 7 kb/s
avcodec_string2: Audio: aac (Main)
AVCodecContext[Line:517]: mediaType[1] codecID[86018] Profile[0] SR[0] channels[0] channel_layout[1]


Except codec_type, codec_id, others(profile, sample_rate, channels etc) is wrong.
only use avcodec_string can get right value for enc->profile, enc->sample_rate etc, any one know the reason?
thanks very much!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20160305/4d99a0e4/attachment.html>


More information about the Libav-user mailing list