static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size) { NuvContext *c = avctx->priv_data; AVFrame *picture = data; int orig_size = buf_size; enum {NUV_UNCOMPRESSED = '0', NUV_RTJPEG = '1', NUV_RTJPEG_IN_LZO = '2', NUV_LZO = '3', NUV_BLACK = 'N', NUV_COPY_LAST = 'L'} comptype; if (buf_size < 12) { av_log(avctx, AV_LOG_ERROR, "coded frame too small\n"); return -1; } if (c->pic.data[0]) avctx->release_buffer(avctx, &c->pic); c->pic.reference = 1; c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; if (avctx->get_buffer(avctx, &c->pic) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return -1; } // codec data (rtjpeg quant tables) if (buf[0] == 'D' && buf[1] == 'R') { int ret; // skip rest of the frameheader. buf = &buf[12]; buf_size -= 12; ret = get_quant(avctx, c, buf, buf_size); if (ret < 0) return ret; rtjpeg_decode_init(&c->rtj, &c->dsp, c->width, c->height, c->lq, c->cq); return orig_size; } if (buf[0] != 'V' || buf_size < 12) { av_log(avctx, AV_LOG_ERROR, "not a nuv video frame\n"); return -1; } comptype = buf[1]; // skip rest of the frameheader. buf = &buf[12]; buf_size -= 12; c->pic.pict_type = FF_I_TYPE; c->pic.key_frame = 1; // decompress/copy/whatever data switch (comptype) { case NUV_UNCOMPRESSED: { int height = c->height; if (buf_size < c->width * height * 3 / 2) { av_log(avctx, AV_LOG_ERROR, "uncompressed frame too short\n"); height = buf_size / c->width / 3 * 2; } copy_frame(&c->pic, buf, c->width, height); break; } case NUV_RTJPEG: { rtjpeg_decode_frame_yuv420(&c->rtj, &c->pic, buf, buf_size); break; } case NUV_RTJPEG_IN_LZO: { int outlen = c->decomp_size, inlen = buf_size; if (lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen)) av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n"); rtjpeg_decode_frame_yuv420(&c->rtj, &c->pic, c->decomp_buf, c->decomp_size); break; } case NUV_LZO: { int outlen = c->decomp_size, inlen = buf_size; if (lzo1x_decode(c->decomp_buf, &outlen, buf, &inlen)) av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n"); copy_frame(&c->pic, c->decomp_buf, c->width, c->height); break; } case NUV_BLACK: { memset(c->pic.data[0], 0, c->width * c->height); memset(c->pic.data[1], 128, c->width * c->height / 4); memset(c->pic.data[2], 128, c->width * c->height / 4); break; } case NUV_COPY_LAST: { c->pic.pict_type = FF_P_TYPE; c->pic.key_frame = 0; /* nothing more to do here */ break; } default: av_log(avctx, AV_LOG_ERROR, "unknown compression\n"); return -1; } *picture = c->pic; *data_size = sizeof(AVFrame); return orig_size; }
static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, const uint8_t *buf, int buf_size) { CamStudioContext *c = avctx->priv_data; AVFrame *picture = data; if (buf_size < 2) { av_log(avctx, AV_LOG_ERROR, "coded frame too small\n"); return -1; } if (c->pic.data[0]) avctx->release_buffer(avctx, &c->pic); c->pic.reference = 1; c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_READABLE | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; if (avctx->get_buffer(avctx, &c->pic) < 0) { av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); return -1; } // decompress data switch ((buf[0] >> 1) & 7) { case 0: { // lzo compression int outlen = c->decomp_size, inlen = buf_size - 2; if (lzo1x_decode(c->decomp_buf, &outlen, &buf[2], &inlen)) av_log(avctx, AV_LOG_ERROR, "error during lzo decompression\n"); break; } case 1: { // zlib compression #ifdef CONFIG_ZLIB unsigned long dlen = c->decomp_size; if (uncompress(c->decomp_buf, &dlen, &buf[2], buf_size - 2) != Z_OK) av_log(avctx, AV_LOG_ERROR, "error during zlib decompression\n"); break; #else av_log(avctx, AV_LOG_ERROR, "compiled without zlib support\n"); return -1; #endif } default: av_log(avctx, AV_LOG_ERROR, "unknown compression\n"); return -1; } // flip upside down, add difference frame if (buf[0] & 1) { // keyframe c->pic.pict_type = FF_I_TYPE; c->pic.key_frame = 1; switch (c->bpp) { case 16: copy_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height); break; case 32: copy_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height); break; default: copy_frame_default(&c->pic, c->decomp_buf, c->linelen, c->height); } } else { c->pic.pict_type = FF_P_TYPE; c->pic.key_frame = 0; switch (c->bpp) { case 16: add_frame_16(&c->pic, c->decomp_buf, c->linelen, c->height); break; case 32: add_frame_32(&c->pic, c->decomp_buf, c->linelen, c->height); break; default: add_frame_default(&c->pic, c->decomp_buf, c->linelen, c->height); } } *picture = c->pic; *data_size = sizeof(AVFrame); return buf_size; }
// decode a frame static mp_image_t* decode(sh_video_t *sh,void* data,int len,int flags) { int r; mp_image_t* mpi; lzo_context_t *priv = sh->context; int w = priv->bufsz; if (len <= 0) { return NULL; // skipped frame } r = lzo1x_decode(priv->buffer, &w, data, &len); if (r) { /* this should NEVER happen */ mp_msg (MSGT_DECVIDEO, MSGL_ERR, "[%s] internal error - decompression failed: %d\n", MOD_NAME, r); return NULL; } if (priv->codec == -1) { // detect RGB24 vs. YV12 via decoded size mp_msg (MSGT_DECVIDEO, MSGL_V, "[%s] 2 depth %d, format %d data %p len (%d) (%d)\n", MOD_NAME, sh->bih->biBitCount, sh->format, data, len, sh->bih->biSizeImage ); if (w == 0) { priv->codec = IMGFMT_BGR24; mp_msg (MSGT_DECVIDEO, MSGL_V, "[%s] codec choosen is BGR24\n", MOD_NAME); } else if (w == (sh->bih->biSizeImage)/2) { priv->codec = IMGFMT_YV12; mp_msg (MSGT_DECVIDEO, MSGL_V, "[%s] codec choosen is YV12\n", MOD_NAME); } else { priv->codec = -1; mp_msg(MSGT_DECVIDEO,MSGL_ERR,"[%s] Unsupported out_fmt\n", MOD_NAME); return NULL; } if(!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,priv->codec)) { priv->codec = -1; return NULL; } } mpi = mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0, sh->disp_w, sh->disp_h); if (!mpi) { mp_msg (MSGT_DECVIDEO, MSGL_ERR, "[%s] mpcodecs_get_image failed\n", MOD_NAME); return NULL; } mpi->planes[0] = priv->buffer; if (priv->codec == IMGFMT_BGR24) mpi->stride[0] = 3 * sh->disp_w; else { mpi->stride[0] = sh->disp_w; mpi->planes[2] = priv->buffer + sh->disp_w*sh->disp_h; mpi->stride[2] = sh->disp_w / 2; mpi->planes[1] = priv->buffer + sh->disp_w*sh->disp_h*5/4; mpi->stride[1] = sh->disp_w / 2; } mp_msg (MSGT_DECVIDEO, MSGL_DBG2, "[%s] decompressed %lu bytes into %lu bytes\n", MOD_NAME, (long) len, (long)w); return mpi; }
void decode_nuv( unsigned char *encoded, int encoded_size, unsigned char *decoded, int width, int height) { int r; unsigned int out_len = width * height + ( width * height ) / 2; struct rtframeheader *encodedh = ( struct rtframeheader* ) encoded; static unsigned char *buffer = 0; /* for RTJpeg with LZO decompress */ #ifdef KEEP_BUFFER static unsigned char *previous_buffer = 0; /* to support Last-frame-copy */ #endif // printf("frametype: %c, comtype: %c, encoded_size: %d, width: %d, height: %d\n", // encodedh->frametype, encodedh->comptype, encoded_size, width, height); le2me_rtframeheader(encodedh); switch(encodedh->frametype) { case 'D': /* additional data for compressors */ { /* tables are in encoded */ if (encodedh->comptype == 'R') { RTjpeg_init_decompress ( (unsigned long *)(encoded+12), width, height ); mp_msg(MSGT_DECVIDEO, MSGL_V, "Found RTjpeg tables (size: %d, width: %d, height: %d)\n", encoded_size-12, width, height); } break; } case 'V': { int in_len = encodedh->packetlength; #ifdef KEEP_BUFFER if (!previous_buffer) previous_buffer = ( unsigned char * ) malloc ( out_len + LZO_OUTPUT_PADDING ); #endif switch(encodedh->comptype) { case '0': /* raw YUV420 */ memcpy(decoded, encoded + 12, out_len); break; case '1': /* RTJpeg */ RTjpeg_decompressYUV420 ( ( __s8 * ) encoded + 12, decoded ); break; case '2': /* RTJpeg with LZO */ if (!buffer) buffer = ( unsigned char * ) malloc ( out_len + LZO_OUTPUT_PADDING ); if (!buffer) { mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Nuppelvideo: error decompressing\n"); break; } r = lzo1x_decode ( buffer, &out_len, encoded + 12, &in_len ); if ( r ) { mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Nuppelvideo: error decompressing\n"); break; } RTjpeg_decompressYUV420 ( ( __s8 * ) buffer, decoded ); break; case '3': /* raw YUV420 with LZO */ r = lzo1x_decode ( decoded, &out_len, encoded + 12, &in_len ); if ( r ) { mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Nuppelvideo: error decompressing\n"); break; } break; case 'N': /* black frame */ memset ( decoded, 0, width * height ); memset ( decoded + width * height, 127, width * height / 2); break; case 'L': /* copy last frame */ #ifdef KEEP_BUFFER memcpy ( decoded, previous_buffer, width*height*3/2); #endif break; } #ifdef KEEP_BUFFER memcpy(previous_buffer, decoded, width*height*3/2); #endif break; } default: mp_msg(MSGT_DECVIDEO, MSGL_V, "Nuppelvideo: unknwon frametype: %c\n", encodedh->frametype); } }