2007-05-14 02:16:56 +03:00
|
|
|
/*
|
|
|
|
* JPEG-LS encoder
|
|
|
|
* Copyright (c) 2003 Michael Niedermayer
|
|
|
|
* Copyright (c) 2006 Konstantin Shishkov
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2007-05-14 02:16:56 +03:00
|
|
|
* JPEG-LS encoder.
|
|
|
|
*/
|
|
|
|
|
2021-04-29 15:31:46 +02:00
|
|
|
#define UNCHECKED_BITSTREAM_READER 1
|
2021-04-29 20:34:00 +02:00
|
|
|
#include "libavutil/opt.h"
|
2007-05-14 02:16:56 +03:00
|
|
|
#include "avcodec.h"
|
2020-09-03 12:42:09 +02:00
|
|
|
#include "bytestream.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2021-04-29 17:44:22 +02:00
|
|
|
#include "encode.h"
|
2009-04-13 19:20:26 +03:00
|
|
|
#include "get_bits.h"
|
2013-01-22 23:35:04 +03:00
|
|
|
#include "put_bits.h"
|
2022-01-03 13:35:39 +02:00
|
|
|
#include "put_golomb.h"
|
2009-01-19 00:57:40 +02:00
|
|
|
#include "mathops.h"
|
2007-05-14 02:16:56 +03:00
|
|
|
#include "mjpeg.h"
|
|
|
|
#include "jpegls.h"
|
|
|
|
|
2015-11-09 04:15:06 +02:00
|
|
|
typedef struct JPEGLSContext {
|
|
|
|
AVClass *class;
|
|
|
|
|
|
|
|
int pred;
|
2021-04-29 17:27:00 +02:00
|
|
|
int comps;
|
|
|
|
|
|
|
|
size_t size;
|
|
|
|
uint8_t *buf;
|
2015-11-09 04:15:06 +02:00
|
|
|
} JPEGLSContext;
|
|
|
|
|
2020-09-03 12:42:09 +02:00
|
|
|
static inline void put_marker_byteu(PutByteContext *pb, enum JpegMarker code)
|
|
|
|
{
|
|
|
|
bytestream2_put_byteu(pb, 0xff);
|
|
|
|
bytestream2_put_byteu(pb, code);
|
|
|
|
}
|
|
|
|
|
2007-05-14 02:16:56 +03:00
|
|
|
/**
|
|
|
|
* Encode error from regular symbol
|
|
|
|
*/
|
2013-05-15 20:03:53 +03:00
|
|
|
static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q,
|
|
|
|
int err)
|
|
|
|
{
|
2007-05-14 02:16:56 +03:00
|
|
|
int k;
|
|
|
|
int val;
|
|
|
|
int map;
|
|
|
|
|
2013-05-15 20:03:53 +03:00
|
|
|
for (k = 0; (state->N[Q] << k) < state->A[Q]; k++)
|
|
|
|
;
|
2007-05-14 02:16:56 +03:00
|
|
|
|
|
|
|
map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
|
|
|
|
|
2013-05-15 20:03:53 +03:00
|
|
|
if (err < 0)
|
2007-05-14 02:16:56 +03:00
|
|
|
err += state->range;
|
2013-05-15 20:55:09 +03:00
|
|
|
if (err >= (state->range + 1 >> 1)) {
|
2007-05-14 02:16:56 +03:00
|
|
|
err -= state->range;
|
2013-05-15 20:03:53 +03:00
|
|
|
val = 2 * FFABS(err) - 1 - map;
|
2007-05-14 02:16:56 +03:00
|
|
|
} else
|
|
|
|
val = 2 * err + map;
|
|
|
|
|
|
|
|
set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
|
|
|
|
|
|
|
|
ff_jpegls_update_state_regular(state, Q, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode error from run termination
|
|
|
|
*/
|
2013-05-15 20:03:53 +03:00
|
|
|
static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb,
|
|
|
|
int RItype, int err, int limit_add)
|
|
|
|
{
|
2007-05-14 02:16:56 +03:00
|
|
|
int k;
|
|
|
|
int val, map;
|
|
|
|
int Q = 365 + RItype;
|
|
|
|
int temp;
|
|
|
|
|
|
|
|
temp = state->A[Q];
|
2013-05-15 20:03:53 +03:00
|
|
|
if (RItype)
|
2007-05-14 02:16:56 +03:00
|
|
|
temp += state->N[Q] >> 1;
|
2013-05-15 20:03:53 +03:00
|
|
|
for (k = 0; (state->N[Q] << k) < temp; k++)
|
|
|
|
;
|
2007-05-14 02:16:56 +03:00
|
|
|
map = 0;
|
2013-05-15 20:03:53 +03:00
|
|
|
if (!k && err && (2 * state->B[Q] < state->N[Q]))
|
2007-05-14 02:16:56 +03:00
|
|
|
map = 1;
|
|
|
|
|
2013-05-15 20:03:53 +03:00
|
|
|
if (err < 0)
|
|
|
|
val = -(2 * err) - 1 - RItype + map;
|
2007-05-14 02:16:56 +03:00
|
|
|
else
|
|
|
|
val = 2 * err - RItype - map;
|
|
|
|
set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
|
|
|
|
|
2013-05-15 20:03:53 +03:00
|
|
|
if (err < 0)
|
2007-05-14 02:16:56 +03:00
|
|
|
state->B[Q]++;
|
|
|
|
state->A[Q] += (val + 1 - RItype) >> 1;
|
|
|
|
|
|
|
|
ff_jpegls_downscale_state(state, Q);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode run value as specified by JPEG-LS standard
|
|
|
|
*/
|
2013-05-15 20:03:53 +03:00
|
|
|
static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run,
|
|
|
|
int comp, int trail)
|
|
|
|
{
|
|
|
|
while (run >= (1 << ff_log2_run[state->run_index[comp]])) {
|
2007-05-14 02:16:56 +03:00
|
|
|
put_bits(pb, 1, 1);
|
|
|
|
run -= 1 << ff_log2_run[state->run_index[comp]];
|
2013-05-15 20:03:53 +03:00
|
|
|
if (state->run_index[comp] < 31)
|
2007-05-14 02:16:56 +03:00
|
|
|
state->run_index[comp]++;
|
|
|
|
}
|
|
|
|
/* if hit EOL, encode another full run, else encode aborted run */
|
2013-05-15 20:03:53 +03:00
|
|
|
if (!trail && run) {
|
2007-05-14 02:16:56 +03:00
|
|
|
put_bits(pb, 1, 1);
|
2013-05-15 20:03:53 +03:00
|
|
|
} else if (trail) {
|
2007-05-14 02:16:56 +03:00
|
|
|
put_bits(pb, 1, 0);
|
2013-05-15 20:03:53 +03:00
|
|
|
if (ff_log2_run[state->run_index[comp]])
|
2007-05-14 02:16:56 +03:00
|
|
|
put_bits(pb, ff_log2_run[state->run_index[comp]], run);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode one line of image
|
|
|
|
*/
|
2013-05-15 20:03:53 +03:00
|
|
|
static inline void ls_encode_line(JLSState *state, PutBitContext *pb,
|
2020-09-03 19:02:59 +02:00
|
|
|
void *tmp, const void *in, int last2, int w,
|
2013-05-15 20:03:53 +03:00
|
|
|
int stride, int comp, int bits)
|
|
|
|
{
|
2007-05-14 02:16:56 +03:00
|
|
|
int x = 0;
|
2020-09-03 19:02:59 +02:00
|
|
|
int Ra = R(tmp, 0), Rb, Rc = last2, Rd;
|
2007-05-14 02:16:56 +03:00
|
|
|
int D0, D1, D2;
|
|
|
|
|
2013-05-15 20:03:53 +03:00
|
|
|
while (x < w) {
|
2007-05-14 02:16:56 +03:00
|
|
|
int err, pred, sign;
|
|
|
|
|
|
|
|
/* compute gradients */
|
2020-09-03 19:02:59 +02:00
|
|
|
Rb = R(tmp, x);
|
|
|
|
Rd = (x >= w - stride) ? R(tmp, x) : R(tmp, x + stride);
|
2007-05-14 02:16:56 +03:00
|
|
|
D0 = Rd - Rb;
|
|
|
|
D1 = Rb - Rc;
|
|
|
|
D2 = Rc - Ra;
|
|
|
|
|
|
|
|
/* run mode */
|
2013-05-15 20:03:53 +03:00
|
|
|
if ((FFABS(D0) <= state->near) &&
|
|
|
|
(FFABS(D1) <= state->near) &&
|
|
|
|
(FFABS(D2) <= state->near)) {
|
2007-05-14 02:16:56 +03:00
|
|
|
int RUNval, RItype, run;
|
|
|
|
|
2013-05-15 20:03:53 +03:00
|
|
|
run = 0;
|
2007-05-14 02:16:56 +03:00
|
|
|
RUNval = Ra;
|
2020-09-03 16:25:24 +02:00
|
|
|
while (x < w && (FFABS(R(in, x) - RUNval) <= state->near)) {
|
2007-05-14 02:16:56 +03:00
|
|
|
run++;
|
2020-09-03 19:02:59 +02:00
|
|
|
W(tmp, x, Ra);
|
2007-05-14 02:16:56 +03:00
|
|
|
x += stride;
|
|
|
|
}
|
|
|
|
ls_encode_run(state, pb, run, comp, x < w);
|
2013-05-15 20:03:53 +03:00
|
|
|
if (x >= w)
|
2007-05-14 02:16:56 +03:00
|
|
|
return;
|
2020-09-03 19:02:59 +02:00
|
|
|
Rb = R(tmp, x);
|
2013-05-15 20:55:09 +03:00
|
|
|
RItype = FFABS(Ra - Rb) <= state->near;
|
2013-05-15 20:03:53 +03:00
|
|
|
pred = RItype ? Ra : Rb;
|
2020-09-03 16:25:24 +02:00
|
|
|
err = R(in, x) - pred;
|
2007-05-14 02:16:56 +03:00
|
|
|
|
2013-05-15 20:03:53 +03:00
|
|
|
if (!RItype && Ra > Rb)
|
2007-05-14 02:16:56 +03:00
|
|
|
err = -err;
|
|
|
|
|
2013-05-15 20:03:53 +03:00
|
|
|
if (state->near) {
|
|
|
|
if (err > 0)
|
|
|
|
err = (state->near + err) / state->twonear;
|
2007-05-14 02:16:56 +03:00
|
|
|
else
|
|
|
|
err = -(state->near - err) / state->twonear;
|
|
|
|
|
2013-05-15 20:03:53 +03:00
|
|
|
if (RItype || (Rb >= Ra))
|
2007-05-14 02:16:56 +03:00
|
|
|
Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
|
|
|
|
else
|
|
|
|
Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
|
2020-09-03 16:25:24 +02:00
|
|
|
} else
|
2020-09-03 18:39:43 +02:00
|
|
|
Ra = R(in, x);
|
2020-09-03 19:02:59 +02:00
|
|
|
W(tmp, x, Ra);
|
2020-09-03 16:25:24 +02:00
|
|
|
|
2013-05-15 20:03:53 +03:00
|
|
|
if (err < 0)
|
2007-05-14 02:16:56 +03:00
|
|
|
err += state->range;
|
2013-05-15 20:55:09 +03:00
|
|
|
if (err >= state->range + 1 >> 1)
|
2007-05-14 02:16:56 +03:00
|
|
|
err -= state->range;
|
|
|
|
|
2013-05-15 20:03:53 +03:00
|
|
|
ls_encode_runterm(state, pb, RItype, err,
|
|
|
|
ff_log2_run[state->run_index[comp]]);
|
2007-05-14 02:16:56 +03:00
|
|
|
|
2013-05-15 20:03:53 +03:00
|
|
|
if (state->run_index[comp] > 0)
|
2007-05-14 02:16:56 +03:00
|
|
|
state->run_index[comp]--;
|
|
|
|
} else { /* regular mode */
|
|
|
|
int context;
|
|
|
|
|
2013-05-15 20:03:53 +03:00
|
|
|
context = ff_jpegls_quantize(state, D0) * 81 +
|
|
|
|
ff_jpegls_quantize(state, D1) * 9 +
|
|
|
|
ff_jpegls_quantize(state, D2);
|
|
|
|
pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
|
2007-05-14 02:16:56 +03:00
|
|
|
|
2013-05-15 20:03:53 +03:00
|
|
|
if (context < 0) {
|
2007-05-14 02:16:56 +03:00
|
|
|
context = -context;
|
2013-05-15 20:03:53 +03:00
|
|
|
sign = 1;
|
|
|
|
pred = av_clip(pred - state->C[context], 0, state->maxval);
|
2020-09-03 16:25:24 +02:00
|
|
|
err = pred - R(in, x);
|
2013-05-15 20:03:53 +03:00
|
|
|
} else {
|
2007-05-14 02:16:56 +03:00
|
|
|
sign = 0;
|
|
|
|
pred = av_clip(pred + state->C[context], 0, state->maxval);
|
2020-09-03 16:25:24 +02:00
|
|
|
err = R(in, x) - pred;
|
2007-05-14 02:16:56 +03:00
|
|
|
}
|
|
|
|
|
2013-05-15 20:03:53 +03:00
|
|
|
if (state->near) {
|
|
|
|
if (err > 0)
|
|
|
|
err = (state->near + err) / state->twonear;
|
2007-05-14 02:16:56 +03:00
|
|
|
else
|
|
|
|
err = -(state->near - err) / state->twonear;
|
2013-05-15 20:03:53 +03:00
|
|
|
if (!sign)
|
2007-05-14 02:16:56 +03:00
|
|
|
Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
|
|
|
|
else
|
|
|
|
Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
|
2020-09-03 16:25:24 +02:00
|
|
|
} else
|
2020-09-03 18:39:43 +02:00
|
|
|
Ra = R(in, x);
|
2020-09-03 19:02:59 +02:00
|
|
|
W(tmp, x, Ra);
|
2007-05-14 02:16:56 +03:00
|
|
|
|
|
|
|
ls_encode_regular(state, pb, context, err);
|
|
|
|
}
|
2020-09-03 18:39:43 +02:00
|
|
|
Rc = Rb;
|
2007-05-14 02:16:56 +03:00
|
|
|
x += stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-03 12:42:09 +02:00
|
|
|
static void ls_store_lse(JLSState *state, PutByteContext *pb)
|
2013-05-15 20:03:53 +03:00
|
|
|
{
|
2007-05-14 02:16:56 +03:00
|
|
|
/* Test if we have default params and don't need to store LSE */
|
2011-06-06 15:13:02 +03:00
|
|
|
JLSState state2 = { 0 };
|
2013-05-15 20:03:53 +03:00
|
|
|
state2.bpp = state->bpp;
|
2007-05-14 02:16:56 +03:00
|
|
|
state2.near = state->near;
|
|
|
|
ff_jpegls_reset_coding_parameters(&state2, 1);
|
2013-05-15 20:03:53 +03:00
|
|
|
if (state->T1 == state2.T1 &&
|
|
|
|
state->T2 == state2.T2 &&
|
|
|
|
state->T3 == state2.T3 &&
|
|
|
|
state->reset == state2.reset)
|
2007-05-14 02:16:56 +03:00
|
|
|
return;
|
|
|
|
/* store LSE type 1 */
|
2020-09-03 12:42:09 +02:00
|
|
|
put_marker_byteu(pb, LSE);
|
|
|
|
bytestream2_put_be16u(pb, 13);
|
|
|
|
bytestream2_put_byteu(pb, 1);
|
|
|
|
bytestream2_put_be16u(pb, state->maxval);
|
|
|
|
bytestream2_put_be16u(pb, state->T1);
|
|
|
|
bytestream2_put_be16u(pb, state->T2);
|
|
|
|
bytestream2_put_be16u(pb, state->T3);
|
|
|
|
bytestream2_put_be16u(pb, state->reset);
|
2007-05-14 02:16:56 +03:00
|
|
|
}
|
|
|
|
|
2012-02-22 09:38:13 +03:00
|
|
|
static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,
|
|
|
|
const AVFrame *pict, int *got_packet)
|
|
|
|
{
|
2015-11-09 04:15:06 +02:00
|
|
|
JPEGLSContext *ctx = avctx->priv_data;
|
2013-11-09 12:14:46 +03:00
|
|
|
const AVFrame *const p = pict;
|
2020-09-03 12:42:09 +02:00
|
|
|
PutByteContext pb;
|
|
|
|
PutBitContext pb2;
|
2007-05-14 02:16:56 +03:00
|
|
|
GetBitContext gb;
|
2020-09-03 16:25:24 +02:00
|
|
|
const uint8_t *in;
|
2020-09-03 19:02:59 +02:00
|
|
|
uint8_t *last = NULL;
|
2020-09-03 19:48:10 +02:00
|
|
|
JLSState state = { 0 };
|
2021-04-29 17:44:22 +02:00
|
|
|
size_t size;
|
|
|
|
int i, ret, size_in_bits;
|
2007-05-14 02:16:56 +03:00
|
|
|
int comps;
|
|
|
|
|
2021-04-29 17:27:00 +02:00
|
|
|
last = av_mallocz(FFABS(p->linesize[0]));
|
2020-09-03 19:50:03 +02:00
|
|
|
if (!last)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-02-22 09:38:13 +03:00
|
|
|
|
2021-04-29 17:27:00 +02:00
|
|
|
init_put_bits(&pb2, ctx->buf, ctx->size);
|
2012-02-22 09:38:13 +03:00
|
|
|
|
2021-04-29 17:27:00 +02:00
|
|
|
comps = ctx->comps;
|
2007-05-14 02:16:56 +03:00
|
|
|
/* initialize JPEG-LS state from JPEG parameters */
|
2020-09-03 19:48:10 +02:00
|
|
|
state.near = ctx->pred;
|
|
|
|
state.bpp = (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8;
|
|
|
|
ff_jpegls_reset_coding_parameters(&state, 0);
|
|
|
|
ff_jpegls_init_state(&state);
|
2007-05-14 02:16:56 +03:00
|
|
|
|
2020-09-03 16:25:24 +02:00
|
|
|
in = p->data[0];
|
2013-05-15 20:03:53 +03:00
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
|
2007-05-14 02:16:56 +03:00
|
|
|
int t = 0;
|
|
|
|
|
2013-05-15 20:03:53 +03:00
|
|
|
for (i = 0; i < avctx->height; i++) {
|
2020-09-03 19:02:59 +02:00
|
|
|
int last0 = last[0];
|
2020-09-03 19:48:10 +02:00
|
|
|
ls_encode_line(&state, &pb2, last, in, t, avctx->width, 1, 0, 8);
|
2020-09-03 19:02:59 +02:00
|
|
|
t = last0;
|
2020-09-03 16:25:24 +02:00
|
|
|
in += p->linesize[0];
|
2007-05-14 02:16:56 +03:00
|
|
|
}
|
2013-05-15 20:03:53 +03:00
|
|
|
} else if (avctx->pix_fmt == AV_PIX_FMT_GRAY16) {
|
2007-05-14 02:16:56 +03:00
|
|
|
int t = 0;
|
|
|
|
|
2013-05-15 20:03:53 +03:00
|
|
|
for (i = 0; i < avctx->height; i++) {
|
2020-09-03 19:02:59 +02:00
|
|
|
int last0 = *((uint16_t *)last);
|
2020-09-03 19:48:10 +02:00
|
|
|
ls_encode_line(&state, &pb2, last, in, t, avctx->width, 1, 0, 16);
|
2020-09-03 19:02:59 +02:00
|
|
|
t = last0;
|
2020-09-03 16:25:24 +02:00
|
|
|
in += p->linesize[0];
|
2007-05-14 02:16:56 +03:00
|
|
|
}
|
2013-05-15 20:03:53 +03:00
|
|
|
} else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
|
2007-05-14 02:16:56 +03:00
|
|
|
int j, width;
|
2013-05-15 20:03:53 +03:00
|
|
|
int Rc[3] = { 0, 0, 0 };
|
2007-05-14 02:16:56 +03:00
|
|
|
|
|
|
|
width = avctx->width * 3;
|
2013-05-15 20:03:53 +03:00
|
|
|
for (i = 0; i < avctx->height; i++) {
|
|
|
|
for (j = 0; j < 3; j++) {
|
2020-09-03 19:02:59 +02:00
|
|
|
int last0 = last[j];
|
2020-09-03 19:48:10 +02:00
|
|
|
ls_encode_line(&state, &pb2, last + j, in + j, Rc[j],
|
2013-05-15 20:03:53 +03:00
|
|
|
width, 3, j, 8);
|
2020-09-03 19:02:59 +02:00
|
|
|
Rc[j] = last0;
|
2007-05-14 02:16:56 +03:00
|
|
|
}
|
2020-09-03 16:25:24 +02:00
|
|
|
in += p->linesize[0];
|
2007-05-14 02:16:56 +03:00
|
|
|
}
|
2013-05-15 20:03:53 +03:00
|
|
|
} else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
|
2007-05-14 02:16:56 +03:00
|
|
|
int j, width;
|
2013-05-15 20:03:53 +03:00
|
|
|
int Rc[3] = { 0, 0, 0 };
|
2007-05-14 02:16:56 +03:00
|
|
|
|
|
|
|
width = avctx->width * 3;
|
2013-05-15 20:03:53 +03:00
|
|
|
for (i = 0; i < avctx->height; i++) {
|
|
|
|
for (j = 2; j >= 0; j--) {
|
2020-09-03 19:02:59 +02:00
|
|
|
int last0 = last[j];
|
2020-09-03 19:48:10 +02:00
|
|
|
ls_encode_line(&state, &pb2, last + j, in + j, Rc[j],
|
2013-05-15 20:03:53 +03:00
|
|
|
width, 3, j, 8);
|
2020-09-03 19:02:59 +02:00
|
|
|
Rc[j] = last0;
|
2007-05-14 02:16:56 +03:00
|
|
|
}
|
2020-09-03 16:25:24 +02:00
|
|
|
in += p->linesize[0];
|
2007-05-14 02:16:56 +03:00
|
|
|
}
|
|
|
|
}
|
2021-04-29 17:44:22 +02:00
|
|
|
av_free(last);
|
|
|
|
/* Now the actual image data has been written, which enables us to estimate
|
|
|
|
* the needed packet size: For every 15 input bits, an escape bit might be
|
|
|
|
* added below; and if put_bits_count % 15 is >= 8, then another bit might
|
|
|
|
* be added.
|
|
|
|
* Furthermore the specification says that after doing 0xff escaping unused
|
|
|
|
* bits in the last byte must be set to 0, so just append 7 "optional" zero
|
|
|
|
* bits to avoid special-casing. This also simplifies the size calculation:
|
|
|
|
* Properly rounding up is now automatically baked-in. */
|
2007-05-14 02:16:56 +03:00
|
|
|
put_bits(&pb2, 7, 0);
|
2021-04-29 17:44:22 +02:00
|
|
|
/* Make sure that the bit count + padding is representable in an int;
|
|
|
|
necessary for put_bits_count() as well as for using a GetBitContext. */
|
|
|
|
if (put_bytes_count(&pb2, 0) > INT_MAX / 8 - AV_INPUT_BUFFER_PADDING_SIZE)
|
|
|
|
return AVERROR(ERANGE);
|
|
|
|
size_in_bits = put_bits_count(&pb2);
|
2007-05-14 02:16:56 +03:00
|
|
|
flush_put_bits(&pb2);
|
2021-04-29 17:44:22 +02:00
|
|
|
size = size_in_bits * 2U / 15;
|
|
|
|
size += 2 + 2 + 2 + 1 + 2 + 2 + 1 + comps * (1 + 1 + 1) + 2 + 2 + 1
|
|
|
|
+ comps * (1 + 1) + 1 + 1 + 1; /* Header */
|
|
|
|
size += 2 + 2 + 1 + 2 + 2 + 2 + 2 + 2; /* LSE */
|
|
|
|
size += 2; /* EOI */
|
|
|
|
if ((ret = ff_get_encode_buffer(avctx, pkt, size, 0)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
bytestream2_init_writer(&pb, pkt->data, pkt->size);
|
|
|
|
|
|
|
|
/* write our own JPEG header, can't use mjpeg_picture_header */
|
|
|
|
put_marker_byteu(&pb, SOI);
|
|
|
|
put_marker_byteu(&pb, SOF48);
|
|
|
|
bytestream2_put_be16u(&pb, 8 + comps * 3); // header size depends on components
|
|
|
|
bytestream2_put_byteu(&pb, (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8); // bpp
|
|
|
|
bytestream2_put_be16u(&pb, avctx->height);
|
|
|
|
bytestream2_put_be16u(&pb, avctx->width);
|
|
|
|
bytestream2_put_byteu(&pb, comps); // components
|
|
|
|
for (i = 1; i <= comps; i++) {
|
|
|
|
bytestream2_put_byteu(&pb, i); // component ID
|
|
|
|
bytestream2_put_byteu(&pb, 0x11); // subsampling: none
|
|
|
|
bytestream2_put_byteu(&pb, 0); // Tiq, used by JPEG-LS ext
|
|
|
|
}
|
|
|
|
|
|
|
|
put_marker_byteu(&pb, SOS);
|
|
|
|
bytestream2_put_be16u(&pb, 6 + comps * 2);
|
|
|
|
bytestream2_put_byteu(&pb, comps);
|
|
|
|
for (i = 1; i <= comps; i++) {
|
|
|
|
bytestream2_put_byteu(&pb, i); // component ID
|
|
|
|
bytestream2_put_byteu(&pb, 0); // mapping index: none
|
|
|
|
}
|
|
|
|
bytestream2_put_byteu(&pb, ctx->pred);
|
|
|
|
bytestream2_put_byteu(&pb, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
|
|
|
|
bytestream2_put_byteu(&pb, 0); // point transform: none
|
|
|
|
|
|
|
|
ls_store_lse(&state, &pb);
|
|
|
|
|
2007-05-14 02:16:56 +03:00
|
|
|
/* do escape coding */
|
2021-04-29 17:44:22 +02:00
|
|
|
init_get_bits(&gb, pb2.buf, size_in_bits);
|
|
|
|
size_in_bits -= 7;
|
|
|
|
while (get_bits_count(&gb) < size_in_bits) {
|
2007-05-14 02:16:56 +03:00
|
|
|
int v;
|
|
|
|
v = get_bits(&gb, 8);
|
2021-04-29 15:31:46 +02:00
|
|
|
bytestream2_put_byteu(&pb, v);
|
2013-05-15 20:03:53 +03:00
|
|
|
if (v == 0xFF) {
|
2007-05-14 02:16:56 +03:00
|
|
|
v = get_bits(&gb, 7);
|
2021-04-29 15:31:46 +02:00
|
|
|
bytestream2_put_byteu(&pb, v);
|
2007-05-14 02:16:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End of image */
|
2021-04-29 15:31:46 +02:00
|
|
|
put_marker_byteu(&pb, EOI);
|
2007-05-14 02:16:56 +03:00
|
|
|
|
2021-04-29 17:44:22 +02:00
|
|
|
av_shrink_packet(pkt, bytestream2_tell_p(&pb));
|
2012-02-22 09:38:13 +03:00
|
|
|
*got_packet = 1;
|
|
|
|
return 0;
|
2007-05-14 02:16:56 +03:00
|
|
|
}
|
|
|
|
|
2021-04-29 13:38:16 +02:00
|
|
|
static av_cold int encode_jpegls_init(AVCodecContext *avctx)
|
|
|
|
{
|
2021-04-29 17:27:00 +02:00
|
|
|
JPEGLSContext *ctx = avctx->priv_data;
|
|
|
|
size_t size;
|
|
|
|
|
2021-04-29 13:38:16 +02:00
|
|
|
if ((avctx->width | avctx->height) > UINT16_MAX) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Dimensions exceeding 65535x65535\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2021-04-29 17:27:00 +02:00
|
|
|
if (avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
|
|
|
|
avctx->pix_fmt == AV_PIX_FMT_GRAY16)
|
|
|
|
ctx->comps = 1;
|
|
|
|
else
|
|
|
|
ctx->comps = 3;
|
|
|
|
size = AV_INPUT_BUFFER_MIN_SIZE;
|
|
|
|
/* INT_MAX due to PutBit-API. */
|
|
|
|
if (avctx->width * (unsigned)avctx->height > (INT_MAX - size) / 4 / ctx->comps)
|
|
|
|
return AVERROR(ERANGE);
|
|
|
|
size += 4 * ctx->comps * avctx->width * avctx->height;
|
|
|
|
ctx->size = size;
|
|
|
|
ctx->buf = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
if (!ctx->buf)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int encode_jpegls_close(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
JPEGLSContext *ctx = avctx->priv_data;
|
|
|
|
|
|
|
|
av_freep(&ctx->buf);
|
2021-04-29 13:38:16 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-09 04:15:06 +02:00
|
|
|
#define OFFSET(x) offsetof(JPEGLSContext, x)
|
|
|
|
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
|
|
|
|
static const AVOption options[] = {
|
|
|
|
{ "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, VE, "pred" },
|
|
|
|
{ "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
|
|
|
|
{ "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
|
|
|
|
{ "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "pred" },
|
|
|
|
|
|
|
|
{ NULL},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass jpegls_class = {
|
|
|
|
.class_name = "jpegls",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_jpegls_encoder = {
|
|
|
|
.p.name = "jpegls",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
|
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_JPEGLS,
|
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
|
2015-11-09 04:15:06 +02:00
|
|
|
.priv_data_size = sizeof(JPEGLSContext),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.priv_class = &jpegls_class,
|
2021-04-29 13:38:16 +02:00
|
|
|
.init = encode_jpegls_init,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_ENCODE_CB(encode_picture_ls),
|
2021-04-29 17:27:00 +02:00
|
|
|
.close = encode_jpegls_close,
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.pix_fmts = (const enum AVPixelFormat[]) {
|
2013-05-15 20:03:53 +03:00
|
|
|
AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
|
|
|
|
AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
|
2012-10-06 13:10:34 +03:00
|
|
|
AV_PIX_FMT_NONE
|
2012-04-06 19:19:39 +03:00
|
|
|
},
|
2022-07-10 00:05:45 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
|
2007-05-14 02:16:56 +03:00
|
|
|
};
|