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

📄 mjpeg.c

📁 Trolltech公司发布的图形界面操作系统。可在qt-embedded-2.3.10平台上编译为嵌入式图形界面操作系统。
💻 C
📖 第 1 页 / 共 5 页
字号:
    put_bits(&s->pb, 4, s->mjpeg_hsample[0]); /* H factor */    put_bits(&s->pb, 4, s->mjpeg_vsample[0]); /* V factor */    put_bits(&s->pb, 8, 0); /* select matrix */        /* Cb component */    put_bits(&s->pb, 8, 2); /* component number */    put_bits(&s->pb, 4, s->mjpeg_hsample[1]); /* H factor */    put_bits(&s->pb, 4, s->mjpeg_vsample[1]); /* V factor */#ifdef TWOMATRIXES    put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */#else    put_bits(&s->pb, 8, 0); /* select matrix */#endif    /* Cr component */    put_bits(&s->pb, 8, 3); /* component number */    put_bits(&s->pb, 4, s->mjpeg_hsample[2]); /* H factor */    put_bits(&s->pb, 4, s->mjpeg_vsample[2]); /* V factor */#ifdef TWOMATRIXES    put_bits(&s->pb, 8, lossless ? 0 : 1); /* select matrix */#else    put_bits(&s->pb, 8, 0); /* select matrix */#endif    }    /* scan header */    put_marker(&s->pb, SOS);    put_bits(&s->pb, 16, 12); /* length */    put_bits(&s->pb, 8, 3); /* 3 components */        /* Y component */    put_bits(&s->pb, 8, 1); /* index */    put_bits(&s->pb, 4, 0); /* DC huffman table index */    put_bits(&s->pb, 4, 0); /* AC huffman table index */        /* Cb component */    put_bits(&s->pb, 8, 2); /* index */    put_bits(&s->pb, 4, 1); /* DC huffman table index */    put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */        /* Cr component */    put_bits(&s->pb, 8, 3); /* index */    put_bits(&s->pb, 4, 1); /* DC huffman table index */    put_bits(&s->pb, 4, lossless ? 0 : 1); /* AC huffman table index */    put_bits(&s->pb, 8, lossless ? s->avctx->prediction_method+1 : 0); /* Ss (not used) */    put_bits(&s->pb, 8, lossless ? 0 : 63); /* Se (not used) */    put_bits(&s->pb, 8, 0); /* Ah/Al (not used) */}static void escape_FF(MpegEncContext *s, int start){    int size= get_bit_count(&s->pb) - start*8;    int i, ff_count;    uint8_t *buf= s->pb.buf + start;    int align= (-(size_t)(buf))&3;        assert((size&7) == 0);    size >>= 3;        ff_count=0;    for(i=0; i<size && i<align; i++){        if(buf[i]==0xFF) ff_count++;    }    for(; i<size-15; i+=16){        int acc, v;        v= *(uint32_t*)(&buf[i]);        acc= (((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;        v= *(uint32_t*)(&buf[i+4]);        acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;        v= *(uint32_t*)(&buf[i+8]);        acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;        v= *(uint32_t*)(&buf[i+12]);        acc+=(((v & (v>>4))&0x0F0F0F0F)+0x01010101)&0x10101010;        acc>>=4;        acc+= (acc>>16);        acc+= (acc>>8);        ff_count+= acc&0xFF;    }    for(; i<size; i++){        if(buf[i]==0xFF) ff_count++;    }    if(ff_count==0) return;        /* skip put bits */    for(i=0; i<ff_count-3; i+=4)        put_bits(&s->pb, 32, 0);    put_bits(&s->pb, (ff_count-i)*8, 0);    flush_put_bits(&s->pb);     for(i=size-1; ff_count; i--){        int v= buf[i];        if(v==0xFF){//printf("%d %d\n", i, ff_count);            buf[i+ff_count]= 0;            ff_count--;        }        buf[i+ff_count]= v;    }}void mjpeg_picture_trailer(MpegEncContext *s){    int pad= (-get_bit_count(&s->pb))&7;        put_bits(&s->pb, pad,0xFF>>(8-pad));    flush_put_bits(&s->pb);    assert((s->header_bits&7)==0);        escape_FF(s, s->header_bits>>3);    put_marker(&s->pb, EOI);}static inline void mjpeg_encode_dc(MpegEncContext *s, int val,				   uint8_t *huff_size, uint16_t *huff_code){    int mant, nbits;    if (val == 0) {        put_bits(&s->pb, huff_size[0], huff_code[0]);    } else {        mant = val;        if (val < 0) {            val = -val;            mant--;        }                nbits= av_log2_16bit(val) + 1;                    put_bits(&s->pb, huff_size[nbits], huff_code[nbits]);                put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));    }}static void encode_block(MpegEncContext *s, DCTELEM *block, int n){    int mant, nbits, code, i, j;    int component, dc, run, last_index, val;    MJpegContext *m = s->mjpeg_ctx;    uint8_t *huff_size_ac;    uint16_t *huff_code_ac;        /* DC coef */    component = (n <= 3 ? 0 : n - 4 + 1);    dc = block[0]; /* overflow is impossible */    val = dc - s->last_dc[component];    if (n < 4) {        mjpeg_encode_dc(s, val, m->huff_size_dc_luminance, m->huff_code_dc_luminance);        huff_size_ac = m->huff_size_ac_luminance;        huff_code_ac = m->huff_code_ac_luminance;    } else {        mjpeg_encode_dc(s, val, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);        huff_size_ac = m->huff_size_ac_chrominance;        huff_code_ac = m->huff_code_ac_chrominance;    }    s->last_dc[component] = dc;        /* AC coefs */        run = 0;    last_index = s->block_last_index[n];    for(i=1;i<=last_index;i++) {        j = s->intra_scantable.permutated[i];        val = block[j];        if (val == 0) {            run++;        } else {            while (run >= 16) {                put_bits(&s->pb, huff_size_ac[0xf0], huff_code_ac[0xf0]);                run -= 16;            }            mant = val;            if (val < 0) {                val = -val;                mant--;            }                        nbits= av_log2(val) + 1;            code = (run << 4) | nbits;            put_bits(&s->pb, huff_size_ac[code], huff_code_ac[code]);                    put_bits(&s->pb, nbits, mant & ((1 << nbits) - 1));            run = 0;        }    }    /* output EOB only if not already 64 values */    if (last_index < 63 || run != 0)        put_bits(&s->pb, huff_size_ac[0], huff_code_ac[0]);}void mjpeg_encode_mb(MpegEncContext *s,                      DCTELEM block[6][64]){    int i;    for(i=0;i<6;i++) {        encode_block(s, block[i], i);    }}static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){    MpegEncContext * const s = avctx->priv_data;    MJpegContext * const m = s->mjpeg_ctx;    AVFrame *pict = data;    const int width= s->width;    const int height= s->height;    AVFrame * const p= (AVFrame*)&s->current_picture;    const int predictor= avctx->prediction_method+1;    init_put_bits(&s->pb, buf, buf_size);    *p = *pict;    p->pict_type= FF_I_TYPE;    p->key_frame= 1;        mjpeg_picture_header(s);    s->header_bits= get_bit_count(&s->pb);    if(avctx->pix_fmt == PIX_FMT_RGBA32){        int x, y, i;        const int linesize= p->linesize[0];        uint16_t buffer[2048][4];        int left[3], top[3], topleft[3];        for(i=0; i<3; i++){            buffer[0][i]= 1 << (9 - 1);        }        for(y = 0; y < height; y++) {            const int modified_predictor= y ? predictor : 1;            uint8_t *ptr = p->data[0] + (linesize * y);            for(i=0; i<3; i++){                top[i]= left[i]= topleft[i]= buffer[0][i];            }            for(x = 0; x < width; x++) {                buffer[x][1] = ptr[4*x+0] - ptr[4*x+1] + 0x100;                buffer[x][2] = ptr[4*x+2] - ptr[4*x+1] + 0x100;                buffer[x][0] = (ptr[4*x+0] + 2*ptr[4*x+1] + ptr[4*x+2])>>2;                for(i=0;i<3;i++) {                    int pred, diff;                    PREDICT(pred, topleft[i], top[i], left[i], modified_predictor);                                            topleft[i]= top[i];                    top[i]= buffer[x+1][i];                                        left[i]= buffer[x][i];                    diff= ((left[i] - pred + 0x100)&0x1FF) - 0x100;                                        if(i==0)                        mjpeg_encode_dc(s, diff, m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly                    else                        mjpeg_encode_dc(s, diff, m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);                }            }        }    }else{        int mb_x, mb_y, i;        const int mb_width  = (width  + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];        const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];                for(mb_y = 0; mb_y < mb_height; mb_y++) {            for(mb_x = 0; mb_x < mb_width; mb_x++) {                if(mb_x==0 || mb_y==0){                    for(i=0;i<3;i++) {                        uint8_t *ptr;                        int x, y, h, v, linesize;                        h = s->mjpeg_hsample[i];                        v = s->mjpeg_vsample[i];                        linesize= p->linesize[i];                        for(y=0; y<v; y++){                            for(x=0; x<h; x++){                                int pred;                                ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap                                if(y==0 && mb_y==0){                                    if(x==0 && mb_x==0){                                        pred= 128;                                    }else{                                        pred= ptr[-1];                                    }                                }else{                                    if(x==0 && mb_x==0){                                        pred= ptr[-linesize];                                    }else{                                        PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);                                    }                                }                                                                if(i==0)                                    mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly                                else                                    mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);                            }                        }                    }                }else{                    for(i=0;i<3;i++) {                        uint8_t *ptr;                        int x, y, h, v, linesize;                        h = s->mjpeg_hsample[i];                        v = s->mjpeg_vsample[i];                        linesize= p->linesize[i];                                                     for(y=0; y<v; y++){                            for(x=0; x<h; x++){                                int pred;                                ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap//printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr);                                 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);                                if(i==0)                                    mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_luminance, m->huff_code_dc_luminance); //FIXME ugly                                else                                    mjpeg_encode_dc(s, (int8_t)(*ptr - pred), m->huff_size_dc_chrominance, m->huff_code_dc_chrominance);                            }                        }                    }                }            }        }    }    emms_c();        mjpeg_picture_trailer(s);    s->picture_number++;    flush_put_bits(&s->pb);    return pbBufPtr(&s->pb) - s->pb.buf;//    return (get_bit_count(&f->pb)+7)/8;}#endif //CONFIG_ENCODERS/******************************************//* decoding */#define MAX_COMPONENTS 4typedef struct MJpegDecodeContext {    AVCodecContext *avctx;    GetBitContext gb;    int mpeg_enc_ctx_allocated; /* true if decoding context allocated */    int start_code; /* current start code */    int buffer_size;    uint8_t *buffer;    int16_t quant_matrixes[4][64];    VLC vlcs[2][4];    int qscale[4];      ///< quantizer scale calculated from quant_matrixes    int org_width, org_height;  /* size given at codec init */    int first_picture;    /* true if decoding first picture */    int interlaced;     /* true if interlaced */    int bottom_field;   /* true if bottom field */    int lossless;    int rgb;    int rct;            /* standard rct */      int pegasus_rct;    /* pegasus reversible colorspace transform */      int bits;           /* bits per component */    int width, height;    int mb_width, mb_height;    int nb_components;    int component_id[MAX_COMPONENTS];    int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */    int v_count[MAX_COMPONENTS];    int comp_index[MAX_COMPONENTS];    int dc_index[MAX_COMPONENTS];    int ac_index[MAX_COMPONENTS];    int nb_blocks[MAX_COMPONENTS];    int h_scount[MAX_COMPONENTS];    int v_scount[MAX_COMPONENTS];    int h_max, v_max; /* maximum h and v counts */    int quant_index[4];   /* quant table index for each component */    int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */    AVFrame picture; /* picture structure */    int linesize[MAX_COMPONENTS];                   ///< linesize << interlaced    uint8_t *qscale_table;    DCTELEM block[64] __align8;    ScanTable scantable;    void (*idct_put)(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/);    int restart_interval;    int restart_count;    int buggy_avid;    int interlace_polarity;} MJpegDecodeContext;static int mjpeg_decode_dht(MJpegDecodeContext *s);static int build_vlc(VLC *vlc, const uint8_t *bits_table, const uint8_t *val_table,                       int nb_codes){    uint8_t huff_size[256];    uint16_t huff_code[256];    memset(huff_size, 0, sizeof(huff_size));    build_huffman_codes(huff_size, huff_code, bits_table, val_table);        return init_vlc(vlc, 9, nb_codes, huff_size, 1, 1, huff_code, 2, 2);}static int mjpeg_decode_init(AVCodecContext *avctx){    MJpegDecodeContext *s = avctx->priv_data;    MpegEncContext s2;    s->avctx = avctx;

⌨️ 快捷键说明

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