Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libavcodec/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ OBJS-$(CONFIG_JACOSUB_DECODER) += jacosubdec.o ass.o
OBJS-$(CONFIG_JPEG2000_ENCODER) += j2kenc.o mqcenc.o mqc.o jpeg2000.o \
jpeg2000dwt.o
OBJS-$(CONFIG_JPEG2000_DECODER) += jpeg2000dec.o jpeg2000.o jpeg2000dsp.o \
jpeg2000dwt.o mqcdec.o mqc.o
jpeg2000dwt.o mqcdec.o mqc.o jpeg2000htdec.o
OBJS-$(CONFIG_JPEGLS_DECODER) += jpeglsdec.o jpegls.o
OBJS-$(CONFIG_JPEGLS_ENCODER) += jpeglsenc.o jpegls.o
OBJS-$(CONFIG_JV_DECODER) += jvdec.o
Expand Down
5 changes: 4 additions & 1 deletion libavcodec/jpeg2000.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ enum Jpeg2000Quantsty { // quantization style
#define JPEG2000_CSTY_SOP 0x02 // SOP marker present
#define JPEG2000_CSTY_EPH 0x04 // EPH marker present
#define JPEG2000_CTSY_HTJ2K_F 0x40 // Only HT code-blocks (Rec. ITU-T T.814 | ISO/IEC 15444-15) are present
#define JPEG2000_CTSY_HTJ2K_M 0xC0 // HT code blocks (Rec. ITU-T T.814 | ISO/IEC 15444-15) can be present
#define JPEG2000_CTSY_HTJ2K_M 0xC0 // HT code-blocks (Rec. ITU-T T.814 | ISO/IEC 15444-15) can be present

// Progression orders
#define JPEG2000_PGOD_LRCP 0x00 // Layer-resolution level-component-position progression
Expand Down Expand Up @@ -189,6 +189,9 @@ typedef struct Jpeg2000Cblk {
Jpeg2000Pass *passes;
Jpeg2000Layer *layers;
int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
/* specific to HT code-blocks */
int zbp;
int pass_lengths[2];
} Jpeg2000Cblk; // code block

typedef struct Jpeg2000Prec {
Expand Down
155 changes: 51 additions & 104 deletions libavcodec/jpeg2000dec.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
#include "jpeg2000.h"
#include "jpeg2000dsp.h"
#include "profiles.h"
#include "jpeg2000dec.h"
#include "jpeg2000htdec.h"

#define JP2_SIG_TYPE 0x6A502020
#define JP2_SIG_VALUE 0x0D0A870A
Expand All @@ -51,93 +53,6 @@
#define HAD_COC 0x01
#define HAD_QCC 0x02

#define MAX_POCS 32

typedef struct Jpeg2000POCEntry {
uint16_t LYEpoc;
uint16_t CSpoc;
uint16_t CEpoc;
uint8_t RSpoc;
uint8_t REpoc;
uint8_t Ppoc;
} Jpeg2000POCEntry;

typedef struct Jpeg2000POC {
Jpeg2000POCEntry poc[MAX_POCS];
int nb_poc;
int is_default;
} Jpeg2000POC;

typedef struct Jpeg2000TilePart {
uint8_t tile_index; // Tile index who refers the tile-part
const uint8_t *tp_end;
GetByteContext header_tpg; // bit stream of header if PPM header is used
GetByteContext tpg; // bit stream in tile-part
} Jpeg2000TilePart;

/* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
* one per component, so tile_part elements have a size of 3 */
typedef struct Jpeg2000Tile {
Jpeg2000Component *comp;
uint8_t properties[4];
Jpeg2000CodingStyle codsty[4];
Jpeg2000QuantStyle qntsty[4];
Jpeg2000POC poc;
Jpeg2000TilePart tile_part[32];
uint8_t has_ppt; // whether this tile has a ppt marker
uint8_t *packed_headers; // contains packed headers. Used only along with PPT marker
int packed_headers_size; // size in bytes of the packed headers
GetByteContext packed_headers_stream; // byte context corresponding to packed headers
uint16_t tp_idx; // Tile-part index
int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
} Jpeg2000Tile;

typedef struct Jpeg2000DecoderContext {
AVClass *class;
AVCodecContext *avctx;
GetByteContext g;

int width, height;
int image_offset_x, image_offset_y;
int tile_offset_x, tile_offset_y;
uint8_t cbps[4]; // bits per sample in particular components
uint8_t sgnd[4]; // if a component is signed
uint8_t properties[4];

uint8_t has_ppm;
uint8_t *packed_headers; // contains packed headers. Used only along with PPM marker
int packed_headers_size;
GetByteContext packed_headers_stream;
uint8_t in_tile_headers;

int cdx[4], cdy[4];
int precision;
int ncomponents;
int colour_space;
uint32_t palette[256];
int8_t pal8;
int cdef[4];
int tile_width, tile_height;
unsigned numXtiles, numYtiles;
int maxtilelen;
AVRational sar;

Jpeg2000CodingStyle codsty[4];
Jpeg2000QuantStyle qntsty[4];
Jpeg2000POC poc;
uint8_t roi_shift[4];

int bit_index;

int curtileno;

Jpeg2000Tile *tile;
Jpeg2000DSPContext dsp;

/*options parameters*/
int reduction_factor;
} Jpeg2000DecoderContext;

/* get_bits functions for JPEG2000 packet bitstream
* It is a get_bit function with a bit-stuffing routine. If the value of the
* byte is 0xFF, the next byte includes an extra zero bit stuffed into the MSB.
Expand Down Expand Up @@ -522,12 +437,13 @@ static int get_cox(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *c)
c->cblk_style = bytestream2_get_byteu(&s->g);
if (c->cblk_style != 0) { // cblk style
if (c->cblk_style & JPEG2000_CTSY_HTJ2K_M || c->cblk_style & JPEG2000_CTSY_HTJ2K_F) {
av_log(s->avctx, AV_LOG_ERROR, "Support for High throughput JPEG 2000 is not yet available\n");
return AVERROR_PATCHWELCOME;
av_log(s->avctx,AV_LOG_TRACE,"High Throughput jpeg 2000 codestream.\n");
s->is_htj2k = 1;
} else {
av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
if (c->cblk_style & JPEG2000_CBLK_BYPASS)
av_log(s->avctx, AV_LOG_WARNING, "Selective arithmetic coding bypass\n");
}
av_log(s->avctx, AV_LOG_WARNING, "extra cblk styles %X\n", c->cblk_style);
if (c->cblk_style & JPEG2000_CBLK_BYPASS)
av_log(s->avctx, AV_LOG_WARNING, "Selective arithmetic coding bypass\n");
}
c->transform = bytestream2_get_byteu(&s->g); // DWT transformation type
/* set integer 9/7 DWT in case of BITEXACT flag */
Expand Down Expand Up @@ -1152,13 +1068,15 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
return incl;

if (!cblk->npasses) {
int v = expn[bandno] + numgbits - 1 -
tag_tree_decode(s, prec->zerobits + cblkno, 100);
int zbp = tag_tree_decode(s, prec->zerobits + cblkno, 100);
int v = expn[bandno] + numgbits - 1 - zbp;

if (v < 0 || v > 30) {
av_log(s->avctx, AV_LOG_ERROR,
"nonzerobits %d invalid or unsupported\n", v);
return AVERROR_INVALIDDATA;
}
cblk->zbp = zbp;
cblk->nonzerobits = v;
}
if ((newpasses = getnpasses(s)) < 0)
Expand Down Expand Up @@ -1199,8 +1117,23 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
}
}

if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)
return ret;
if (newpasses > 1 && s->is_htj2k) {
// Retrieve pass lengths for each pass
int href_passes = (cblk->npasses + newpasses - 1) % 3;
int eb = av_log2(newpasses - href_passes);
int extra_bit = newpasses > 2 ? 1 : 0;
if ((ret = get_bits(s, llen + eb + 3)) < 0)
return ret;
cblk->pass_lengths[0] = ret;
if ((ret = get_bits(s, llen + 3 + extra_bit)) < 0)
return ret;
cblk->pass_lengths[1] = ret;
ret = cblk->pass_lengths[0] + cblk->pass_lengths[1];
} else {
if ((ret = get_bits(s, av_log2(newpasses1) + cblk->lblock)) < 0)
return ret;
cblk->pass_lengths[0] = ret;
}
if (ret > cblk->data_allocated) {
size_t new_size = FFMAX(2*cblk->data_allocated, ret);
void *new = av_realloc(cblk->data, new_size);
Expand Down Expand Up @@ -1949,17 +1882,20 @@ static inline void tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile

/* Loop on tile components */
for (compno = 0; compno < s->ncomponents; compno++) {
Jpeg2000Component *comp = tile->comp + compno;
Jpeg2000CodingStyle *codsty = tile->codsty + compno;
Jpeg2000Component *comp = tile->comp + compno;
Jpeg2000CodingStyle *codsty = tile->codsty + compno;
Jpeg2000QuantStyle *quantsty = tile->qntsty + compno;

int coded = 0;
int subbandno = 0;

t1.stride = (1<<codsty->log2_cblk_width) + 2;

/* Loop on resolution levels */
for (reslevelno = 0; reslevelno < codsty->nreslevels2decode; reslevelno++) {
Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
/* Loop on bands */
for (bandno = 0; bandno < rlevel->nbands; bandno++) {
for (bandno = 0; bandno < rlevel->nbands; bandno++, subbandno++) {
int nb_precincts, precno;
Jpeg2000Band *band = rlevel->band + bandno;
int cblkno = 0, bandpos;
Expand All @@ -1979,12 +1915,23 @@ static inline void tile_codeblocks(const Jpeg2000DecoderContext *s, Jpeg2000Tile
for (cblkno = 0;
cblkno < prec->nb_codeblocks_width * prec->nb_codeblocks_height;
cblkno++) {
int x, y;
int x, y, ret;
/* See Rec. ITU-T T.800, Equation E-2 */
int magp = quantsty->expn[subbandno] + quantsty->nguardbits - 1;

Jpeg2000Cblk *cblk = prec->cblk + cblkno;
int ret = decode_cblk(s, codsty, &t1, cblk,
cblk->coord[0][1] - cblk->coord[0][0],
cblk->coord[1][1] - cblk->coord[1][0],
bandpos, comp->roi_shift);

if (codsty->cblk_style & JPEG2000_CTSY_HTJ2K_F)
ret = ff_jpeg2000_decode_htj2k(s, codsty, &t1, cblk,
cblk->coord[0][1] - cblk->coord[0][0],
cblk->coord[1][1] - cblk->coord[1][0],
magp, comp->roi_shift);
else
ret = decode_cblk(s, codsty, &t1, cblk,
cblk->coord[0][1] - cblk->coord[0][0],
cblk->coord[1][1] - cblk->coord[1][0],
bandpos, comp->roi_shift);

if (ret)
coded = 1;
else
Expand Down
121 changes: 121 additions & 0 deletions libavcodec/jpeg2000dec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* JPEG 2000 image decoder
* Copyright (c) 2007 Kamil Nowosad
* Copyright (c) 2013 Nicolas Bertrand <nicoinattendu@gmail.com>
* Copyright (c) 2022 Caleb Etemesi <etemesicaleb@gmail.com>
*
* 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
*/

#ifndef AVCODEC_JPEG2000DEC_H
#define AVCODEC_JPEG2000DEC_H

#include "bytestream.h"
#include "jpeg2000.h"
#include "jpeg2000dsp.h"


#define MAX_POCS 32

typedef struct Jpeg2000POCEntry {
uint16_t LYEpoc;
uint16_t CSpoc;
uint16_t CEpoc;
uint8_t RSpoc;
uint8_t REpoc;
uint8_t Ppoc;
} Jpeg2000POCEntry;

typedef struct Jpeg2000POC {
Jpeg2000POCEntry poc[MAX_POCS];
int nb_poc;
int is_default;
} Jpeg2000POC;

typedef struct Jpeg2000TilePart {
uint8_t tile_index; // Tile index who refers the tile-part
const uint8_t *tp_end;
GetByteContext header_tpg; // bit stream of header if PPM header is used
GetByteContext tpg; // bit stream in tile-part
} Jpeg2000TilePart;

/* RMK: For JPEG2000 DCINEMA 3 tile-parts in a tile
* one per component, so tile_part elements have a size of 3 */
typedef struct Jpeg2000Tile {
Jpeg2000Component *comp;
uint8_t properties[4];
Jpeg2000CodingStyle codsty[4];
Jpeg2000QuantStyle qntsty[4];
Jpeg2000POC poc;
Jpeg2000TilePart tile_part[32];
uint8_t has_ppt; // whether this tile has a ppt marker
uint8_t *packed_headers; // contains packed headers. Used only along with PPT marker
int packed_headers_size; // size in bytes of the packed headers
GetByteContext packed_headers_stream; // byte context corresponding to packed headers
uint16_t tp_idx; // Tile-part index
int coord[2][2]; // border coordinates {{x0, x1}, {y0, y1}}
} Jpeg2000Tile;

typedef struct Jpeg2000DecoderContext {
AVClass *class;
AVCodecContext *avctx;
GetByteContext g;

int width, height;
int image_offset_x, image_offset_y;
int tile_offset_x, tile_offset_y;
uint8_t cbps[4]; // bits per sample in particular components
uint8_t sgnd[4]; // if a component is signed
uint8_t properties[4];

uint8_t has_ppm;
uint8_t *packed_headers; // contains packed headers. Used only along with PPM marker
int packed_headers_size;
GetByteContext packed_headers_stream;
uint8_t in_tile_headers;

int cdx[4], cdy[4];
int precision;
int ncomponents;
int colour_space;
uint32_t palette[256];
int8_t pal8;
int cdef[4];
int tile_width, tile_height;
unsigned numXtiles, numYtiles;
int maxtilelen;
AVRational sar;

Jpeg2000CodingStyle codsty[4];
Jpeg2000QuantStyle qntsty[4];
Jpeg2000POC poc;
uint8_t roi_shift[4];

int bit_index;

int curtileno;

Jpeg2000Tile *tile;
Jpeg2000DSPContext dsp;

/*options parameters*/
int reduction_factor;
/*HTJ2K params*/
uint8_t is_htj2k;
} Jpeg2000DecoderContext;

#endif //AVCODEC_JPEG2000DEC_H
Loading