static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx) { LibOpenJPEGContext *ctx = avctx->priv_data; if(ctx->image.data[0]) ff_thread_release_buffer(avctx, &ctx->image); return 0 ; }
av_cold int ffv1_close(AVCodecContext *avctx) { FFV1Context *s = avctx->priv_data; int i, j; if (s->picture.f) ff_thread_release_buffer(avctx, &s->picture); av_frame_free(&s->picture.f); if (s->last_picture.f) ff_thread_release_buffer(avctx, &s->last_picture); av_frame_free(&s->last_picture.f); for (j = 0; j < s->slice_count; j++) { FFV1Context *fs = s->slice_context[j]; for (i = 0; i < s->plane_count; i++) { PlaneContext *p = &fs->plane[i]; av_freep(&p->state); av_freep(&p->vlc_state); } av_freep(&fs->sample_buffer); } av_freep(&avctx->stats_out); for (j = 0; j < s->quant_table_count; j++) { av_freep(&s->initial_states[j]); for (i = 0; i < s->slice_count; i++) { FFV1Context *sf = s->slice_context[i]; av_freep(&sf->rc_stat2[j]); } av_freep(&s->rc_stat2[j]); } for (i = 0; i < s->slice_count; i++) av_freep(&s->slice_context[i]); return 0; }
void ff_h264_unref_picture(H264Context *h, H264Picture *pic) { int off = offsetof(H264Picture, tf) + sizeof(pic->tf); int i; if (!pic->f.buf[0]) return; ff_thread_release_buffer(h->avctx, &pic->tf); av_buffer_unref(&pic->hwaccel_priv_buf); av_buffer_unref(&pic->qscale_table_buf); av_buffer_unref(&pic->mb_type_buf); for (i = 0; i < 2; i++) { av_buffer_unref(&pic->motion_val_buf[i]); av_buffer_unref(&pic->ref_index_buf[i]); } memset((uint8_t*)pic + off, 0, sizeof(*pic) - off); }
static int libopenjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt) { uint8_t *buf = avpkt->data; int buf_size = avpkt->size; LibOpenJPEGContext *ctx = avctx->priv_data; AVFrame *picture = &ctx->image, *output = data; opj_dinfo_t *dec; opj_cio_t *stream; opj_image_t *image; int width, height, ret = -1; int pixel_size = 0; int ispacked = 0; *data_size = 0; // Check if input is a raw jpeg2k codestream or in jp2 wrapping if((AV_RB32(buf) == 12) && (AV_RB32(buf + 4) == JP2_SIG_TYPE) && (AV_RB32(buf + 8) == JP2_SIG_VALUE)) { dec = opj_create_decompress(CODEC_JP2); } else { // If the AVPacket contains a jp2c box, then skip to // the starting byte of the codestream. if (AV_RB32(buf + 4) == AV_RB32("jp2c")) buf += 8; dec = opj_create_decompress(CODEC_J2K); } if(!dec) { av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n"); return -1; } opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL); ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER; // Tie decoder with decoding parameters opj_setup_decoder(dec, &ctx->dec_params); stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size); if(!stream) { av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n"); opj_destroy_decompress(dec); return -1; } // Decode the header only image = opj_decode_with_info(dec, stream, NULL); opj_cio_close(stream); if(!image) { av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n"); opj_destroy_decompress(dec); return -1; } width = image->x1 - image->x0; height = image->y1 - image->y0; if(av_image_check_size(width, height, 0, avctx) < 0) { av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height); goto done; } avcodec_set_dimensions(avctx, width, height); if (avctx->pix_fmt != PIX_FMT_NONE) { if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt)) { avctx->pix_fmt = PIX_FMT_NONE; } } if (avctx->pix_fmt == PIX_FMT_NONE) { avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image); } if (avctx->pix_fmt == PIX_FMT_NONE) { av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n"); goto done; } if(picture->data[0]) ff_thread_release_buffer(avctx, picture); if(ff_thread_get_buffer(avctx, picture) < 0){ av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n"); goto done; } ctx->dec_params.cp_limit_decoding = NO_LIMITATION; ctx->dec_params.cp_reduce = avctx->lowres; // Tie decoder with decoding parameters opj_setup_decoder(dec, &ctx->dec_params); stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size); if(!stream) { av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n"); goto done; } opj_image_destroy(image); // Decode the codestream image = opj_decode_with_info(dec, stream, NULL); opj_cio_close(stream); if(!image) { av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n"); goto done; } pixel_size = av_pix_fmt_descriptors[avctx->pix_fmt].comp[0].step_minus1 + 1; ispacked = libopenjpeg_ispacked(avctx->pix_fmt); switch (pixel_size) { case 1: if (ispacked) { libopenjpeg_copy_to_packed8(picture, image); } else { libopenjpeg_copyto8(picture, image); } break; case 2: if (ispacked) { libopenjpeg_copy_to_packed8(picture, image); } else { libopenjpeg_copyto16(picture, image); } break; case 3: case 4: if (ispacked) { libopenjpeg_copy_to_packed8(picture, image); } break; case 6: case 8: if (ispacked) { libopenjpeg_copy_to_packed16(picture, image); } break; default: av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size); goto done; } *output = ctx->image; *data_size = sizeof(AVPicture); ret = buf_size; done: opj_image_destroy(image); opj_destroy_decompress(dec); return ret; }
static int libopenjpeg_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt) { uint8_t *buf = avpkt->data; int buf_size = avpkt->size; LibOpenJPEGContext *ctx = avctx->priv_data; AVFrame *picture = &ctx->image, *output = data; opj_dinfo_t *dec; opj_cio_t *stream; opj_image_t *image; int width, height, has_alpha = 0, ret = -1; int x, y, index; uint8_t *img_ptr; int adjust[4]; *data_size = 0; // Check if input is a raw jpeg2k codestream or in jp2 wrapping if((AV_RB32(buf) == 12) && (AV_RB32(buf + 4) == JP2_SIG_TYPE) && (AV_RB32(buf + 8) == JP2_SIG_VALUE)) { dec = opj_create_decompress(CODEC_JP2); } else { // If the AVPacket contains a jp2c box, then skip to // the starting byte of the codestream. if (AV_RB32(buf + 4) == AV_RB32("jp2c")) buf += 8; dec = opj_create_decompress(CODEC_J2K); } if(!dec) { av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n"); return -1; } opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL); ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER; // Tie decoder with decoding parameters opj_setup_decoder(dec, &ctx->dec_params); stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size); if(!stream) { av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n"); opj_destroy_decompress(dec); return -1; } // Decode the header only image = opj_decode_with_info(dec, stream, NULL); opj_cio_close(stream); if(!image) { av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n"); opj_destroy_decompress(dec); return -1; } width = image->x1 - image->x0; height = image->y1 - image->y0; if(av_image_check_size(width, height, 0, avctx) < 0) { av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height); goto done; } avcodec_set_dimensions(avctx, width, height); switch(image->numcomps) { case 1: avctx->pix_fmt = PIX_FMT_GRAY8; break; case 3: if(check_image_attributes(image)) { avctx->pix_fmt = PIX_FMT_RGB24; } else { avctx->pix_fmt = PIX_FMT_GRAY8; av_log(avctx, AV_LOG_ERROR, "Only first component will be used.\n"); } break; case 4: has_alpha = 1; avctx->pix_fmt = PIX_FMT_RGBA; break; default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps); goto done; } if(picture->data[0]) ff_thread_release_buffer(avctx, picture); if(ff_thread_get_buffer(avctx, picture) < 0) { av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n"); return -1; } ff_thread_finish_setup(avctx); ctx->dec_params.cp_limit_decoding = NO_LIMITATION; ctx->dec_params.cp_reduce = avctx->lowres; // Tie decoder with decoding parameters opj_setup_decoder(dec, &ctx->dec_params); stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size); if(!stream) { av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n"); opj_destroy_decompress(dec); return -1; } // Decode the codestream image = opj_decode_with_info(dec, stream, NULL); opj_cio_close(stream); for(x = 0; x < image->numcomps; x++) { adjust[x] = FFMAX(image->comps[x].prec - 8, 0); } for(y = 0; y < avctx->height; y++) { index = y*avctx->width; img_ptr = picture->data[0] + y*picture->linesize[0]; for(x = 0; x < avctx->width; x++, index++) { *img_ptr++ = image->comps[0].data[index] >> adjust[0]; if(image->numcomps > 2 && check_image_attributes(image)) { *img_ptr++ = image->comps[1].data[index] >> adjust[1]; *img_ptr++ = image->comps[2].data[index] >> adjust[2]; if(has_alpha) *img_ptr++ = image->comps[3].data[index] >> adjust[3]; } } }