📄 mjpeg.c
字号:
s->h_count[i] = get_bits(&s->gb, 4); s->v_count[i] = get_bits(&s->gb, 4); /* compute hmax and vmax (only used in interleaved case) */ if (s->h_count[i] > s->h_max) s->h_max = s->h_count[i]; if (s->v_count[i] > s->v_max) s->v_max = s->v_count[i]; s->quant_index[i] = get_bits(&s->gb, 8); if (s->quant_index[i] >= 4) return -1; dprintf("component %d %d:%d id: %d quant:%d\n", i, s->h_count[i], s->v_count[i], s->component_id[i], s->quant_index[i]); } /* if different size, realloc/alloc picture */ /* XXX: also check h_count and v_count */ if (width != s->width || height != s->height) { for(i=0;i<MAX_COMPONENTS;i++) av_freep(&s->current_picture[i]); s->width = width; s->height = height; /* test interlaced mode */ if (s->first_picture && s->org_height != 0 && s->height < ((s->org_height * 3) / 4)) { s->interlaced = 1;// s->bottom_field = (s->interlace_polarity) ? 1 : 0; s->bottom_field = 0; } for(i=0;i<nb_components;i++) { int w, h; w = (s->width + 8 * s->h_max - 1) / (8 * s->h_max); h = (s->height + 8 * s->v_max - 1) / (8 * s->v_max); w = w * 8 * s->h_count[i]; h = h * 8 * s->v_count[i]; if (s->interlaced) w *= 2; s->linesize[i] = w; /* memory test is done in mjpeg_decode_sos() */ s->current_picture[i] = av_mallocz(w * h); } s->first_picture = 0; } if (len != (8+(3*nb_components))) { dprintf("decode_sof0: error, len(%d) mismatch\n", len); } return 0;}static inline int mjpeg_decode_dc(MJpegDecodeContext *s, int dc_index){ int code, diff;#if 1 code = get_vlc2(&s->gb, s->vlcs[0][dc_index].table, 9, 2);#else code = get_vlc(&s->gb, &s->vlcs[0][dc_index]);#endif if (code < 0) { dprintf("mjpeg_decode_dc: bad vlc: %d:%d (%p)\n", 0, dc_index, &s->vlcs[0][dc_index]); return 0xffff; } if (code == 0) { diff = 0; } else { diff = get_bits(&s->gb, code); if ((diff & (1 << (code - 1))) == 0) diff = (-1 << code) | (diff + 1); } return diff;}/* decode block and dequantize */static int decode_block(MJpegDecodeContext *s, DCTELEM *block, int component, int dc_index, int ac_index, int quant_index){ int nbits, code, i, j, level; int run, val; VLC *ac_vlc; INT16 *quant_matrix; /* DC coef */ val = mjpeg_decode_dc(s, dc_index); if (val == 0xffff) { dprintf("error dc\n"); return -1; } quant_matrix = s->quant_matrixes[quant_index]; val = val * quant_matrix[0] + s->last_dc[component]; s->last_dc[component] = val; block[0] = val; /* AC coefs */ ac_vlc = &s->vlcs[1][ac_index]; i = 1; for(;;) {#if 1 code = get_vlc2(&s->gb, s->vlcs[1][ac_index].table, 9, 2);#else code = get_vlc(&s->gb, ac_vlc);#endif if (code < 0) { dprintf("error ac\n"); return -1; } /* EOB */ if (code == 0) break; if (code == 0xf0) { i += 16; } else { run = code >> 4; nbits = code & 0xf; level = get_bits(&s->gb, nbits); if ((level & (1 << (nbits - 1))) == 0) level = (-1 << nbits) | (level + 1); i += run; if (i >= 64) { dprintf("error count: %d\n", i); return -1; } j = s->scantable.permutated[i]; block[j] = level * quant_matrix[j]; i++; if (i >= 64) break; } } return 0;}static int mjpeg_decode_sos(MJpegDecodeContext *s){ int len, nb_components, i, j, n, h, v, ret; int mb_width, mb_height, mb_x, mb_y, vmax, hmax, index, id; int comp_index[4]; int dc_index[4]; int ac_index[4]; int nb_blocks[4]; int h_count[4]; int v_count[4]; /* XXX: verify len field validity */ len = get_bits(&s->gb, 16); nb_components = get_bits(&s->gb, 8); if (len != 6+2*nb_components) { dprintf("decode_sos: invalid len (%d)\n", len); return -1; } /* XXX: only interleaved scan accepted */ if (nb_components != 3) { dprintf("decode_sos: components(%d) mismatch\n", nb_components); return -1; } vmax = 0; hmax = 0; for(i=0;i<nb_components;i++) { id = get_bits(&s->gb, 8) - 1; dprintf("component: %d\n", id); /* find component index */ for(index=0;index<s->nb_components;index++) if (id == s->component_id[index]) break; if (index == s->nb_components) { dprintf("decode_sos: index(%d) out of components\n", index); return -1; } comp_index[i] = index; nb_blocks[i] = s->h_count[index] * s->v_count[index]; h_count[i] = s->h_count[index]; v_count[i] = s->v_count[index]; dc_index[i] = get_bits(&s->gb, 4); ac_index[i] = get_bits(&s->gb, 4); if (dc_index[i] < 0 || ac_index[i] < 0 || dc_index[i] >= 4 || ac_index[i] >= 4) goto out_of_range; switch(s->start_code) { case SOF0: if (dc_index[i] > 1 || ac_index[i] > 1) goto out_of_range; break; case SOF1: case SOF2: if (dc_index[i] > 3 || ac_index[i] > 3) goto out_of_range; break; case SOF3: if (dc_index[i] > 3 || ac_index[i] != 0) goto out_of_range; break; } } skip_bits(&s->gb, 8); /* Ss */ skip_bits(&s->gb, 8); /* Se */ skip_bits(&s->gb, 8); /* Ah and Al (each are 4 bits) */ for(i=0;i<nb_components;i++) s->last_dc[i] = 1024; if (nb_components > 1) { /* interleaved stream */ mb_width = (s->width + s->h_max * 8 - 1) / (s->h_max * 8); mb_height = (s->height + s->v_max * 8 - 1) / (s->v_max * 8); } else { h = s->h_max / s->h_count[comp_index[0]]; v = s->v_max / s->v_count[comp_index[0]]; mb_width = (s->width + h * 8 - 1) / (h * 8); mb_height = (s->height + v * 8 - 1) / (v * 8); nb_blocks[0] = 1; h_count[0] = 1; v_count[0] = 1; } for(mb_y = 0; mb_y < mb_height; mb_y++) { for(mb_x = 0; mb_x < mb_width; mb_x++) { for(i=0;i<nb_components;i++) { UINT8 *ptr; int x, y, c; n = nb_blocks[i]; c = comp_index[i]; h = h_count[i]; v = v_count[i]; x = 0; y = 0; if (s->restart_interval && !s->restart_count) s->restart_count = s->restart_interval; for(j=0;j<n;j++) { memset(s->block, 0, sizeof(s->block)); if (decode_block(s, s->block, i, dc_index[i], ac_index[i], s->quant_index[c]) < 0) { dprintf("error y=%d x=%d\n", mb_y, mb_x); ret = -1; goto the_end; }// dprintf("mb: %d %d processed\n", mb_y, mb_x); ptr = s->current_picture[c] + (s->linesize[c] * (v * mb_y + y) * 8) + (h * mb_x + x) * 8; if (s->interlaced && s->bottom_field) ptr += s->linesize[c] >> 1; s->idct_put(ptr, s->linesize[c], s->block); if (++x == h) { x = 0; y++; } } } /* (< 1350) buggy workaround for Spectralfan.mov, should be fixed */ if (s->restart_interval && (s->restart_interval < 1350) && !--s->restart_count) { align_get_bits(&s->gb); skip_bits(&s->gb, 16); /* skip RSTn */ for (j=0; j<nb_components; j++) /* reset dc */ s->last_dc[j] = 1024; } } } ret = 0; the_end: emms_c(); return ret; out_of_range: dprintf("decode_sos: ac/dc index out of range\n"); return -1;}static int mjpeg_decode_dri(MJpegDecodeContext *s){ if (get_bits(&s->gb, 16) != 4) return -1; s->restart_interval = get_bits(&s->gb, 16); dprintf("restart interval: %d\n", s->restart_interval); return 0;}static int mjpeg_decode_app(MJpegDecodeContext *s){ int len, id; /* XXX: verify len field validity */ len = get_bits(&s->gb, 16); if (len < 5) return -1; id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); id = be2me_32(id); len -= 6; /* buggy AVID, it puts EOI only at every 10th frame */ /* also this fourcc is used by non-avid files too, it holds some informations, but it's always present in AVID creates files */ if (id == ff_get_fourcc("AVI1")) { /* structure: 4bytes AVI1 1bytes polarity 1bytes always zero 4bytes field_size 4bytes field_size_less_padding */ s->buggy_avid = 1;// if (s->first_picture)// printf("mjpeg: workarounding buggy AVID\n"); s->interlace_polarity = get_bits(&s->gb, 8);#if 0 skip_bits(&s->gb, 8); skip_bits(&s->gb, 32); skip_bits(&s->gb, 32); len -= 10;#endif// if (s->interlace_polarity)// printf("mjpeg: interlace polarity: %d\n", s->interlace_polarity); goto out; } // len -= 2; if (id == ff_get_fourcc("JFIF")) { int t_w, t_h; skip_bits(&s->gb, 8); /* the trailing zero-byte */ printf("mjpeg: JFIF header found (version: %x.%x)\n", get_bits(&s->gb, 8), get_bits(&s->gb, 8)); if (get_bits(&s->gb, 8) == 0) { s->avctx->aspect_ratio_info = FF_ASPECT_EXTENDED; s->avctx->aspected_width = get_bits(&s->gb, 16); s->avctx->aspected_height = get_bits(&s->gb, 16); } else { skip_bits(&s->gb, 16); skip_bits(&s->gb, 16); } t_w = get_bits(&s->gb, 8); t_h = get_bits(&s->gb, 8); if (t_w && t_h) { /* skip thumbnail */ if (len-10-(t_w*t_h*3) > 0) len -= t_w*t_h*3; } len -= 10; goto out; } if (id == ff_get_fourcc("Adob") && (get_bits(&s->gb, 8) == 'e')) { printf("mjpeg: Adobe header found\n"); skip_bits(&s->gb, 16); /* version */ skip_bits(&s->gb, 16); /* flags0 */ skip_bits(&s->gb, 16); /* flags1 */ skip_bits(&s->gb, 8); /* transform */ len -= 7; goto out; } /* Apple MJPEG-A */ if ((s->start_code == APP1) && (len > (0x28 - 8))) { id = (get_bits(&s->gb, 16) << 16) | get_bits(&s->gb, 16); id = be2me_32(id); len -= 4; if (id == ff_get_fourcc("mjpg")) /* Apple MJPEG-A */ {#if 0 skip_bits(&s->gb, 32); /* field size */ skip_bits(&s->gb, 32); /* pad field size */ skip_bits(&s->gb, 32); /* next off */ skip_bits(&s->gb, 32); /* quant off */ skip_bits(&s->gb, 32); /* huff off */ skip_bits(&s->gb, 32); /* image off */ skip_bits(&s->gb, 32); /* scan off */ skip_bits(&s->gb, 32); /* data off */#endif if (s->first_picture) printf("mjpeg: Apple MJPEG-A header found\n"); } }out: /* slow but needed for extreme adobe jpegs */ if (len < 0) printf("mjpeg: error, decode_app parser read over the end\n"); while(--len > 0) skip_bits(&s->gb, 8); return 0;}static int mjpeg_decode_com(MJpegDecodeContext *s){ int len, i; UINT8 *cbuf; /* XXX: verify len field validity */ len = get_bits(&s->gb, 16)-2; cbuf = av_malloc(len+1); for (i = 0; i < len; i++) cbuf[i] = get_bits(&s->gb, 8); if (cbuf[i-1] == '\n') cbuf[i-1] = 0; else cbuf[i] = 0; printf("mjpeg comment: '%s'\n", cbuf);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -