⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utils.c

📁 arm平台下的H264编码和解码源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
int avcodec_encode_audio(AVCodecContext *avctx, uint8_t *buf, int buf_size,                          const short *samples){    int ret;    ret = avctx->codec->encode(avctx, buf, buf_size, (void *)samples);    avctx->frame_number++;    return ret;}int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size,                          const AVFrame *pict){    int ret;    ret = avctx->codec->encode(avctx, buf, buf_size, (void *)pict);        emms_c(); //needed to avoid a emms_c() call before every return;    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;        ret = avctx->codec->decode(avctx, picture, got_picture_ptr,                                buf, buf_size);    emms_c(); //needed to avoid a emms_c() call before every return;        if (*got_picture_ptr)                                   avctx->frame_number++;    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_audio(AVCodecContext *avctx, int16_t *samples,                          int *frame_size_ptr,                         uint8_t *buf, int buf_size){    int ret;    ret = avctx->codec->decode(avctx, samples, frame_size_ptr,                                buf, buf_size);    avctx->frame_number++;    return ret;}
int avcodec_close(AVCodecContext *avctx){    if (avctx->codec->close)        avctx->codec->close(avctx);    av_freep(&avctx->priv_data);    avctx->codec = NULL;    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;}AVCodec *avcodec_find(enum CodecID id){    AVCodec *p;    p = first_avcodec;    while (p) {        if (p->id == id)            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 (enc->codec_type == CODEC_TYPE_VIDEO) {            snprintf(buf1, sizeof(buf1), "%c%c%c%c",                      enc->codec_tag & 0xff,                     (enc->codec_tag >> 8) & 0xff,                     (enc->codec_tag >> 16) & 0xff,                     (enc->codec_tag >> 24) & 0xff);        } else {            snprintf(buf1, sizeof(buf1), "0x%04x", enc->codec_tag);        }        codec_name = buf1;    }    switch(enc->codec_type) {    case CODEC_TYPE_VIDEO:        snprintf(buf, buf_size,                 "Video: %s%s",                 codec_name, enc->mb_decision ? " (hq)" : "");        if (enc->codec_id == CODEC_ID_RAWVIDEO) {            snprintf(buf + strlen(buf), buf_size - strlen(buf),                     ", %s",                     avcodec_get_pix_fmt_name(enc->pix_fmt));        }        if (enc->width) {            snprintf(buf + strlen(buf), buf_size - strlen(buf),                     ", %dx%d, %0.2f fps",                     enc->width, enc->height,                      (float)enc->frame_rate / enc->frame_rate_base);        }        if (encode) {            snprintf(buf + strlen(buf), buf_size - strlen(buf),                     ", q=%d-%d", enc->qmin, enc->qmax);        }        bitrate = enc->bit_rate;        break;    case CODEC_TYPE_AUDIO:        snprintf(buf, buf_size,                 "Audio: %s",                 codec_name);        switch (enc->channels) {            case 1:                strcpy(channels_str, "mono");                break;            case 2:                strcpy(channels_str, "stereo");                break;            case 6:                strcpy(channels_str, "5:1");                break;            default:                sprintf(channels_str, "%d channels", enc->channels);                break;        }        if (enc->sample_rate) {            snprintf(buf + strlen(buf), buf_size - strlen(buf),                     ", %d Hz, %s",                     enc->sample_rate,                     channels_str);        }                /* for PCM codecs, compute bitrate directly */        switch(enc->codec_id) {        case CODEC_ID_PCM_S16LE:        case CODEC_ID_PCM_S16BE:        case CODEC_ID_PCM_U16LE:        case CODEC_ID_PCM_U16BE:            bitrate = enc->sample_rate * enc->channels * 16;            break;        case CODEC_ID_PCM_S8:        case CODEC_ID_PCM_U8:        case CODEC_ID_PCM_ALAW:        case CODEC_ID_PCM_MULAW:            bitrate = enc->sample_rate * enc->channels * 8;            break;        default:            bitrate = enc->bit_rate;            break;        }        break;    case CODEC_TYPE_DATA:        snprintf(buf, buf_size, "Data: %s", codec_name);        bitrate = enc->bit_rate;        break;    default:        av_abort();    }    if (encode) {        if (enc->flags & CODEC_FLAG_PASS1)            snprintf(buf + strlen(buf), buf_size - strlen(buf),                     ", pass 1");        if (enc->flags & CODEC_FLAG_PASS2)            snprintf(buf + strlen(buf), buf_size - strlen(buf),                     ", pass 2");    }    if (bitrate != 0) {        snprintf(buf + strlen(buf), buf_size - strlen(buf),                  ", %d kb/s", bitrate / 1000);    }}unsigned avcodec_version( void ){  return LIBAVCODEC_VERSION_INT;}unsigned avcodec_build( void ){  return LIBAVCODEC_BUILD;}/* must be called before any other functions */void avcodec_init(void){    static int inited = 0;    if (inited != 0)	return;    inited = 1;    dsputil_static_init();}/** * Flush buffers, should be called when seeking or when swicthing to a different stream. */void avcodec_flush_buffers(AVCodecContext *avctx){    if(avctx->codec->flush)        avctx->codec->flush(avctx);}void avcodec_default_free_buffers(AVCodecContext *s){    int i, j;    if(s->internal_buffer==NULL) return;        for(i=0; i<INTERNAL_BUFFER_SIZE; i++){        InternalBuffer *buf= &((InternalBuffer*)s->internal_buffer)[i];        for(j=0; j<4; j++){            av_freep(&buf->base[j]);            buf->data[j]= NULL;        }    }    av_freep(&s->internal_buffer);        s->internal_buffer_count=0;}char av_get_pict_type_char(int pict_type){    switch(pict_type){    case I_TYPE: return 'I';     case P_TYPE: return 'P';     case B_TYPE: return 'B';     case S_TYPE: return 'S';     case SI_TYPE:return 'i';     case SP_TYPE:return 'p';     default:     return '?';    }}int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){    int exact=1, sign=0;    int64_t gcd;
#ifndef WINCE //assert    assert(den != 0);
#endif    if(den < 0){        den= -den;        nom= -nom;    }        if(nom < 0){        nom= -nom;        sign= 1;    }        gcd = ff_gcd(nom, den);    nom /= gcd;    den /= gcd;        if(nom > max || den > max){        AVRational a0={0,1}, a1={1,0};        exact=0;        for(;;){            int64_t x= nom / den;            int64_t a2n= x*a1.num + a0.num;            int64_t a2d= x*a1.den + a0.den;            if(a2n > max || a2d > max) break;            nom %= den;                    a0= a1;
#ifdef WINCE //AVRational
            a1.num= a2n;
			a1.den= a2d;
#else            a1= (AVRational){a2n, a2d};
#endif            if(nom==0) break;            x= nom; nom=den; den=x;        }        nom= a1.num;        den= a1.den;    }

#ifndef WINCE //assert    assert(ff_gcd(nom, den) == 1);#endif
    if(sign) nom= -nom;        *dst_nom = nom;    *dst_den = den;        return exact;}int64_t av_rescale(int64_t a, int b, int c){    uint64_t h, l;#ifndef WINCE //assert
	assert(c > 0);    assert(b >=0);#endif    if(a<0) return -av_rescale(-a, b, c);        h= a>>32;    if(h==0) return a*b/c;        l= a&0xFFFFFFFF;    l *= b;    h *= b;    l += (h%c)<<32;    return ((h/c)<<32) + l/c;}
/* av_log API */static int av_log_level = AV_LOG_DEBUG;static void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl){    static int print_prefix=1;    AVClass* avc= ptr ? *(AVClass**)ptr : NULL;    if(level>av_log_level)	return;#undef fprintf    if(print_prefix && avc) {	    fprintf(stderr, "[%s @ %p]", avc->item_name(ptr), avc);    }#define fprintf please_use_av_log            print_prefix= strstr(fmt, "\n") != NULL;            vfprintf(stderr, fmt, vl);}static void (*av_log_callback)(void*, int, const char*, va_list) = av_log_default_callback;void av_log(void* avcl, int level, const char *fmt, ...){    va_list vl;    va_start(vl, fmt);    av_vlog(avcl, level, fmt, vl);    va_end(vl);}void av_vlog(void* avcl, int level, const char *fmt, va_list vl){    av_log_callback(avcl, level, fmt, vl);}int av_log_get_level(void){    return av_log_level;}void av_log_set_level(int level){    av_log_level = level;}void av_log_set_callback(void (*callback)(void*, int, const char*, va_list)){    av_log_callback = callback;}

⌨️ 快捷键说明

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