ljpegenc: split bgr encoding into a separate function

pull/48/merge
Anton Khirnov 11 years ago
parent daffed3b17
commit fa4476815d
  1. 93
      libavcodec/ljpegenc.c

@ -57,59 +57,33 @@ typedef struct LJpegEncContext {
uint16_t (*scratch)[4];
} LJpegEncContext;
static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
const AVFrame *pict, int *got_packet)
static int ljpeg_encode_bgr(AVCodecContext *avctx, PutBitContext *pb,
const AVFrame *frame)
{
LJpegEncContext *s = avctx->priv_data;
PutBitContext pb;
const int width = avctx->width;
const int height = avctx->height;
const int predictor= avctx->prediction_method+1;
const int mb_width = (width + s->hsample[0] - 1) / s->hsample[0];
const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
int max_pkt_size = FF_MIN_BUFFER_SIZE;
int ret, header_bits;
if (avctx->pix_fmt == AV_PIX_FMT_BGR24)
max_pkt_size += width * height * 3 * 3;
else {
max_pkt_size += mb_width * mb_height * 3 * 4
* s->hsample[0] * s->vsample[0];
}
if ((ret = ff_alloc_packet(pkt, max_pkt_size)) < 0) {
av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", max_pkt_size);
return ret;
}
init_put_bits(&pb, pkt->data, pkt->size);
ff_mjpeg_encode_picture_header(avctx, &pb, &s->scantable,
s->matrix);
header_bits = put_bits_count(&pb);
if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
int x, y, i;
const int linesize = pict->linesize[0];
const int width = frame->width;
const int height = frame->height;
const int linesize = frame->linesize[0];
uint16_t (*buffer)[4] = s->scratch;
const int predictor = avctx->prediction_method+1;
int left[3], top[3], topleft[3];
int x, y, i;
for(i=0; i<3; i++){
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 = pict->data[0] + (linesize * y);
uint8_t *ptr = frame->data[0] + (linesize * y);
if(pb.buf_end - pb.buf - (put_bits_count(&pb) >> 3) < width * 3 * 3) {
if (pb->buf_end - pb->buf - (put_bits_count(pb) >> 3) < width * 3 * 3) {
av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
return -1;
}
for(i=0; i<3; i++){
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[3 * x + 0] - ptr[3 * x + 1] + 0x100;
buffer[x][2] = ptr[3 * x + 2] - ptr[3 * x + 1] + 0x100;
@ -128,12 +102,51 @@ static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
diff = ((left[i] - pred + 0x100) & 0x1FF) - 0x100;
if (i == 0)
ff_mjpeg_encode_dc(&pb, diff, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_luminance, s->huff_code_dc_luminance); //FIXME ugly
else
ff_mjpeg_encode_dc(&pb, diff, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
ff_mjpeg_encode_dc(pb, diff, s->huff_size_dc_chrominance, s->huff_code_dc_chrominance);
}
}
}
return 0;
}
static int ljpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
const AVFrame *pict, int *got_packet)
{
LJpegEncContext *s = avctx->priv_data;
PutBitContext pb;
const int width = avctx->width;
const int height = avctx->height;
const int predictor= avctx->prediction_method+1;
const int mb_width = (width + s->hsample[0] - 1) / s->hsample[0];
const int mb_height = (height + s->vsample[0] - 1) / s->vsample[0];
int max_pkt_size = FF_MIN_BUFFER_SIZE;
int ret, header_bits;
if (avctx->pix_fmt == AV_PIX_FMT_BGR24)
max_pkt_size += width * height * 3 * 3;
else {
max_pkt_size += mb_width * mb_height * 3 * 4
* s->hsample[0] * s->vsample[0];
}
if ((ret = ff_alloc_packet(pkt, max_pkt_size)) < 0) {
av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", max_pkt_size);
return ret;
}
init_put_bits(&pb, pkt->data, pkt->size);
ff_mjpeg_encode_picture_header(avctx, &pb, &s->scantable,
s->matrix);
header_bits = put_bits_count(&pb);
if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
ret = ljpeg_encode_bgr(avctx, &pb, pict);
if (ret < 0)
return ret;
}else{
int mb_x, mb_y, i;

Loading…
Cancel
Save