utils.c

来自「linux下的MPEG1」· C语言 代码 · 共 1,355 行 · 第 1/5 页

C
1,355
字号
        avctx->priv_data = NULL;    }    if(avctx->coded_width && avctx->coded_height)        avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);    else if(avctx->width && avctx->height)        avcodec_set_dimensions(avctx, avctx->width, avctx->height);    if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height)){        av_freep(&avctx->priv_data);        goto end;    }    avctx->codec = codec;    avctx->codec_id = codec->id;    avctx->frame_number = 0;    ret = avctx->codec->init(avctx);    if (ret < 0) {        av_freep(&avctx->priv_data);        avctx->codec= NULL;        goto end;    }    ret=0;end:    entangled_thread_counter--;    return ret;}int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,                         const short *samples){    if(buf_size < FF_MIN_BUFFER_SIZE && 0){        av_log(avctx, AV_LOG_ERROR, "buffer smaller then minimum size\n");        return -1;    }    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || samples){        int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);        avctx->frame_number++;        return ret;    }else        return 0;}int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,                         const AVFrame *pict){    if(buf_size < FF_MIN_BUFFER_SIZE){        av_log(avctx, AV_LOG_ERROR, "buffer smaller then minimum size\n");        return -1;    }    if(avcodec_check_dimensions(avctx,avctx->width,avctx->height))        return -1;    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || pict){        int ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);        avctx->frame_number++;        emms_c(); //needed to avoid an emms_c() call before every return;        return ret;    }else        return 0;}int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size,                            const AVSubtitle *sub){    int ret;    ret = avctx->codec->encode(avctx, buf, buf_size, (void *)sub);    avctx->frame_number++;    return ret;}/** * decode a frame. * @param buf bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE larger then the actual read bytes * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end * @param buf_size the size of the buffer in bytes * @param got_picture_ptr zero if no frame could be decompressed, Otherwise, it is non zero * @return -1 if error, otherwise return the number of * bytes used. */int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,                         int *got_picture_ptr,                         uint8_t *buf, int buf_size){    int ret;    *got_picture_ptr= 0;    if((avctx->coded_width||avctx->coded_height) && avcodec_check_dimensions(avctx,avctx->coded_width,avctx->coded_height))        return -1;    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){        ret = avctx->codec->decode(avctx, picture, got_picture_ptr,                                buf, buf_size);        emms_c(); //needed to avoid an emms_c() call before every return;        if (*got_picture_ptr)            avctx->frame_number++;    }else        ret= 0;    return ret;}/* decode an audio frame. return -1 if error, otherwise return the   *number of bytes used. If no frame could be decompressed,   *frame_size_ptr is zero. Otherwise, it is the decompressed frame   *size in BYTES. */int avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,                         int *frame_size_ptr,                         uint8_t *buf, int buf_size){    int ret;    //FIXME remove the check below _after_ ensuring that all audio check that the available space is enough    if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){        av_log(avctx, AV_LOG_ERROR, "buffer smaller then AVCODEC_MAX_AUDIO_FRAME_SIZE\n");        return -1;    }    if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||       *frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t) ||       *frame_size_ptr < buf_size){        av_log(avctx, AV_LOG_ERROR, "buffer too small\n");        return -1;    }    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){        ret = avctx->codec->decode(avctx, samples, frame_size_ptr,                                buf, buf_size);        avctx->frame_number++;    }else{        ret= 0;        *frame_size_ptr=0;    }    return ret;}#if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,                         int *frame_size_ptr,                         uint8_t *buf, int buf_size){    *frame_size_ptr= AVCODEC_MAX_AUDIO_FRAME_SIZE;    return avcodec_decode_audio2(avctx, samples, frame_size_ptr, buf, buf_size);}#endif/* decode a subtitle message. return -1 if error, otherwise return the   *number of bytes used. If no subtitle could be decompressed,   *got_sub_ptr is zero. Otherwise, the subtitle is stored in *sub. */int avcodec_decode_subtitle(AVCodecContext *avctx, AVSubtitle *sub,                            int *got_sub_ptr,                            const uint8_t *buf, int buf_size){    int ret;    *got_sub_ptr = 0;    ret = avctx->codec->decode(avctx, sub, got_sub_ptr,                               (uint8_t *)buf, buf_size);    if (*got_sub_ptr)        avctx->frame_number++;    return ret;}int avcodec_close(AVCodecContext *avctx){    entangled_thread_counter++;    if(entangled_thread_counter != 1){        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking around avcodec_open/close()\n");        entangled_thread_counter--;        return -1;    }    if (avctx->codec->close)        avctx->codec->close(avctx);    avcodec_default_free_buffers(avctx);    av_freep(&avctx->priv_data);    avctx->codec = NULL;    entangled_thread_counter--;    return 0;}AVCodec *avcodec_find_encoder(enum CodecID id){    AVCodec *p;    p = first_avcodec;    while (p) {        if (p->encode != NULL && p->id == id)            return p;        p = p->next;    }    return NULL;}AVCodec *avcodec_find_encoder_by_name(const char *name){    AVCodec *p;    p = first_avcodec;    while (p) {        if (p->encode != NULL && strcmp(name,p->name) == 0)            return p;        p = p->next;    }    return NULL;}AVCodec *avcodec_find_decoder(enum CodecID id){    AVCodec *p;    p = first_avcodec;    while (p) {        if (p->decode != NULL && p->id == id)            return p;        p = p->next;    }    return NULL;}AVCodec *avcodec_find_decoder_by_name(const char *name){    AVCodec *p;    p = first_avcodec;    while (p) {        if (p->decode != NULL && strcmp(name,p->name) == 0)            return p;        p = p->next;    }    return NULL;}void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode){    const char *codec_name;    AVCodec *p;    char buf1[32];    char channels_str[100];    int bitrate;    if (encode)        p = avcodec_find_encoder(enc->codec_id);    else        p = avcodec_find_decoder(enc->codec_id);    if (p) {        codec_name = p->name;        if (!encode && enc->codec_id == CODEC_ID_MP3) {            if (enc->sub_id == 2)                codec_name = "mp2";            else if (enc->sub_id == 1)                codec_name = "mp1";        }    } else if (enc->codec_id == CODEC_ID_MPEG2TS) {        /* fake mpeg2 transport stream codec (currently not           registered) */        codec_name = "mpeg2ts";    } else if (enc->codec_name[0] != '\0') {        codec_name = enc->codec_name;    } else {        /* output avi tags */        if(   isprint(enc->codec_tag&0xFF) && isprint((enc->codec_tag>>8)&0xFF)           && isprint((enc->codec_tag>>16)&0xFF) && isprint((enc->codec_tag>>24)&0xFF)){            snprintf(buf1, sizeof(buf1), "%c%c%c%c / 0x%04X",                     enc->codec_tag & 0xff,                     (enc->codec_tag >> 8) & 0xff,                     (enc->codec_tag >> 16) & 0xff,                     (enc->codec_tag >> 24) & 0xff,                      enc->codec_tag);        } else {            snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);        }        codec_name = buf1;    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?