[FFmpeg-devel] [RFC PATCH v7 4/4] libavcodec/jpeg2000: fix tag tree reset

2020-08-27 Thread gautamramk
From: Gautam Ramakrishnan 

The implementation of the tag tree did not
set the correct reset value for the encoder.
This lead to inefficent tag tree being encoded.
This patch fixes the implementation of the
ff_tag_tree_zero() function.
---
 libavcodec/j2kenc.c  | 4 ++--
 libavcodec/jpeg2000.c| 8 
 libavcodec/jpeg2000.h| 2 +-
 tests/ref/vsynth/vsynth1-jpeg2000| 4 ++--
 tests/ref/vsynth/vsynth1-jpeg2000-97 | 4 ++--
 tests/ref/vsynth/vsynth2-jpeg2000| 4 ++--
 tests/ref/vsynth/vsynth2-jpeg2000-97 | 4 ++--
 tests/ref/vsynth/vsynth3-jpeg2000| 4 ++--
 tests/ref/vsynth/vsynth3-jpeg2000-97 | 4 ++--
 tests/ref/vsynth/vsynth_lena-jpeg2000| 4 ++--
 tests/ref/vsynth/vsynth_lena-jpeg2000-97 | 4 ++--
 11 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 8b9f6464af..4cefe6d7fb 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -802,8 +802,8 @@ static int encode_packet(Jpeg2000EncoderContext *s, 
Jpeg2000ResLevel *rlevel, in
 Jpeg2000Prec *prec = band->prec + precno;
 int nb_cblks = prec->nb_codeblocks_height * 
prec->nb_codeblocks_width;
 int pos;
-ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
-ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height, 99);
+ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height, 99);
 for (pos = 0; pos < nb_cblks; pos++) {
 Jpeg2000Cblk *cblk = >cblk[pos];
 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - 
cblk->nonzerobits;
diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 6501de0d04..56d98c8a89 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -82,12 +82,12 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, 
int h)
 return res;
 }
 
-void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h, int val)
 {
 int i, siz = ff_tag_tree_size(w, h);
 
 for (i = 0; i < siz; i++) {
-t[i].val = 0;
+t[i].val = val;
 t[i].temp_val = 0;
 t[i].vis = 0;
 }
@@ -575,8 +575,8 @@ void ff_jpeg2000_reinit(Jpeg2000Component *comp, 
Jpeg2000CodingStyle *codsty)
 Jpeg2000Band *band = rlevel->band + bandno;
 for(precno = 0; precno < rlevel->num_precincts_x * 
rlevel->num_precincts_y; precno++) {
 Jpeg2000Prec *prec = band->prec + precno;
-ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
-ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height, 0);
+ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height, 0);
 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * 
prec->nb_codeblocks_height; cblkno++) {
 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
 cblk->length = 0;
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index a7ba8aa7f3..612832c872 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -302,6 +302,6 @@ static inline int needs_termination(int style, int passno) {
 }
 
 int32_t ff_tag_tree_size(int w, int h);
-void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h);
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h, int val);
 
 #endif /* AVCODEC_JPEG2000_H */
diff --git a/tests/ref/vsynth/vsynth1-jpeg2000 
b/tests/ref/vsynth/vsynth1-jpeg2000
index 5081507459..f8e987f95d 100644
--- a/tests/ref/vsynth/vsynth1-jpeg2000
+++ b/tests/ref/vsynth/vsynth1-jpeg2000
@@ -1,4 +1,4 @@
-dd66b25f2ebc965eae4c29cfacdd960f *tests/data/fate/vsynth1-jpeg2000.avi
-2274950 tests/data/fate/vsynth1-jpeg2000.avi
+95add005faf68fcf8f16e86eab079ca2 *tests/data/fate/vsynth1-jpeg2000.avi
+2263192 tests/data/fate/vsynth1-jpeg2000.avi
 b7f48a8965f78011c76483277befc6fc *tests/data/fate/vsynth1-jpeg2000.out.rawvideo
 stddev:5.35 PSNR: 33.56 MAXDIFF:   59 bytes:  7603200/  7603200
diff --git a/tests/ref/vsynth/vsynth1-jpeg2000-97 
b/tests/ref/vsynth/vsynth1-jpeg2000-97
index 7e18f20e25..6ab5aa4237 100644
--- a/tests/ref/vsynth/vsynth1-jpeg2000-97
+++ b/tests/ref/vsynth/vsynth1-jpeg2000-97
@@ -1,4 +1,4 @@
-f22ad99de77f8f4382c6cf10d5af42b5 *tests/data/fate/vsynth1-jpeg2000-97.avi
-3661422 tests/data/fate/vsynth1-jpeg2000-97.avi
+e4d03b2e3c03e56c7f831b1e662c4031 *tests/data/fate/vsynth1-jpeg2000-97.avi
+3643928 tests/data/fate/vsynth1-jpeg2000-97.avi
 

[FFmpeg-devel] [RFC PATCH v7 3/4] libavcodec/j2kenc: Support for multiple layers

2020-08-27 Thread gautamramk
From: Gautam Ramakrishnan 

This patch allows setting a compression ratio and to
set multiple layers. The user has to input a compression
ratio for each layer.
The per layer compression ration can be set as follows:
-layer_rates "r1,r2,...rn"
for to create 'n' layers.
---
 doc/encoders.texi|  16 +
 libavcodec/j2kenc.c  | 472 +++
 libavcodec/jpeg2000.c|  12 +-
 libavcodec/jpeg2000.h|  10 +
 libavcodec/version.h |   2 +-
 tests/ref/vsynth/vsynth1-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth1-jpeg2000-97 |   6 +-
 tests/ref/vsynth/vsynth2-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth2-jpeg2000-97 |   8 +-
 tests/ref/vsynth/vsynth3-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth3-jpeg2000-97 |   8 +-
 tests/ref/vsynth/vsynth_lena-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth_lena-jpeg2000-97 |   8 +-
 13 files changed, 471 insertions(+), 103 deletions(-)

diff --git a/doc/encoders.texi b/doc/encoders.texi
index fd9235a05a..69bf742c2d 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -1401,6 +1401,22 @@ Possible values are:
 @end table
 Set to @code{lrcp} by default.
 
+@item layer_rates @var{string}
+By default, when this option is not used, compression is done using the 
quality metric.
+This option allows for compression using compression ratio. The compression 
ratio for each
+level could be specified. The compression ratio of a layer @code{l} species 
the what ratio of
+total file size is contained in the first @code{l} layers.
+
+Example usage:
+
+@example
+ffmpeg -i input.bmp -c:v jpeg2000 -layer_rates "100,10,1" output.j2k
+@end example
+
+This would compress the image to contain 3 layers, where the data contained in 
the
+first layer would be compressed by 1000 times, compressed by 100 in the first 
two layers,
+and shall contain all data while using all 3 layers.
+
 @end table
 
 @section librav1e
diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 87acd2d5c9..8b9f6464af 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -32,6 +32,7 @@
  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  * Copyright (c) 2005, Herve Drolon, FreeImage Team
  * Copyright (c) 2007, Callum Lerwick 
+ * Copyright (c) 2020, Gautam Ramakrishnan 
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -72,6 +73,7 @@
 #include "libavutil/pixdesc.h"
 #include "libavutil/opt.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/avstring.h"
 
 #define NMSEDEC_BITS 7
 #define NMSEDEC_FRACBITS (NMSEDEC_BITS-1)
@@ -100,6 +102,7 @@ static const int dwt_norms[2][4][10] = { // 
[dwt_type][band][rlevel] (multiplied
 
 typedef struct {
Jpeg2000Component *comp;
+   double *layer_rates;
 } Jpeg2000Tile;
 
 typedef struct {
@@ -126,12 +129,16 @@ typedef struct {
 Jpeg2000QuantStyle  qntsty;
 
 Jpeg2000Tile *tile;
+int layer_rates[100];
+uint8_t compression_rate_enc; ///< Is compression done using compression 
ratio?
 
 int format;
 int pred;
 int sop;
 int eph;
 int prog;
+int nlayers;
+char *lr_str;
 } Jpeg2000EncoderContext;
 
 
@@ -334,7 +341,7 @@ static int put_cod(Jpeg2000EncoderContext *s)
 bytestream_put_byte(>buf, scod);  // Scod
 // SGcod
 bytestream_put_byte(>buf, s->prog); // progression level
-bytestream_put_be16(>buf, 1); // num of layers
+bytestream_put_be16(>buf, s->nlayers); // num of layers
 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
 bytestream_put_byte(>buf, 0); // unspecified
 }else{
@@ -413,6 +420,31 @@ static uint8_t *put_sot(Jpeg2000EncoderContext *s, int 
tileno)
 return psotptr;
 }
 
+static void compute_rates(Jpeg2000EncoderContext* s)
+{
+int i, j;
+int layno, compno;
+for (i = 0; i < s->numYtiles; i++) {
+for (j = 0; j < s->numXtiles; j++) {
+Jpeg2000Tile *tile = >tile[s->numXtiles * i + j];
+for (compno = 0; compno < s->ncomponents; compno++) {
+int tilew = tile->comp[compno].coord[0][1] - 
tile->comp[compno].coord[0][0];
+int tileh = tile->comp[compno].coord[1][1] - 
tile->comp[compno].coord[1][0];
+int scale = (compno?1 << s->chroma_shift[0]:1) * (compno?1 << 
s->chroma_shift[1]:1);
+for (layno = 0; layno < s->nlayers; layno++) {
+if (s->layer_rates[layno] > 0) {
+tile->layer_rates[layno] += (double)(tilew * tileh) * 
s->ncomponents * s->cbps[compno] /
+
(double)(s->layer_rates[layno] * 8 * scale);
+} else {
+tile->layer_rates[layno] = 0.0;
+}
+}
+}
+}
+}
+
+}
+
 /**
  * compute the sizes of tiles, resolution levels, bands, etc.
  * allocate memory for them
@@ -437,6 

[FFmpeg-devel] [RFC PATCH v7 1/4] libavcodec/jpeg2000: Make tag tree functions non static

2020-08-27 Thread gautamramk
From: Gautam Ramakrishnan 

This patch makes the tag_tree_zero() and tag_tree_size()
functions non static and callable from other files.
---
 libavcodec/jpeg2000.c | 12 ++--
 libavcodec/jpeg2000.h |  3 +++
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 1aca31ffa4..26e09fbe38 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -39,7 +39,7 @@
 /* tag tree routines */
 
 /* allocate the memory for tag tree */
-static int32_t tag_tree_size(int w, int h)
+int32_t ff_tag_tree_size(int w, int h)
 {
 int64_t res = 0;
 while (w > 1 || h > 1) {
@@ -57,7 +57,7 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int 
h)
 Jpeg2000TgtNode *res, *t, *t2;
 int32_t tt_size;
 
-tt_size = tag_tree_size(w, h);
+tt_size = ff_tag_tree_size(w, h);
 
 t = res = av_mallocz_array(tt_size, sizeof(*t));
 if (!res)
@@ -82,9 +82,9 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int 
h)
 return res;
 }
 
-static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
 {
-int i, siz = tag_tree_size(w, h);
+int i, siz = ff_tag_tree_size(w, h);
 
 for (i = 0; i < siz; i++) {
 t[i].val = 0;
@@ -567,8 +567,8 @@ void ff_jpeg2000_reinit(Jpeg2000Component *comp, 
Jpeg2000CodingStyle *codsty)
 Jpeg2000Band *band = rlevel->band + bandno;
 for(precno = 0; precno < rlevel->num_precincts_x * 
rlevel->num_precincts_y; precno++) {
 Jpeg2000Prec *prec = band->prec + precno;
-tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
-tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * 
prec->nb_codeblocks_height; cblkno++) {
 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
 cblk->length = 0;
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index 5b0627c3dc..c3437b02fe 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -290,4 +290,7 @@ static inline int needs_termination(int style, int passno) {
 return 0;
 }
 
+int32_t ff_tag_tree_size(int w, int h);
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h);
+
 #endif /* AVCODEC_JPEG2000_H */
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH v7 2/4] libavcodec/j2kenc: Fix tag tree coding

2020-08-27 Thread gautamramk
From: Gautam Ramakrishnan 

The implementation of tag tree encoding was incorrect.
However, this error was not visible as the current j2k
encoder encodes only 1 layer.
This patch fixes tag tree coding for JPEG2000 such tag
tree coding would work for multi layer encoding.
---
 libavcodec/j2kenc.c   | 41 +
 libavcodec/jpeg2000.c |  1 +
 libavcodec/jpeg2000.h |  1 +
 3 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 16863f8e8c..87acd2d5c9 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -242,27 +242,36 @@ static void j2k_flush(Jpeg2000EncoderContext *s)
 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, 
int threshold)
 {
 Jpeg2000TgtNode *stack[30];
-int sp = 1, curval = 0;
-stack[0] = node;
+int sp = -1, curval = 0;
 
-node = node->parent;
-while(node){
-if (node->vis){
-curval = node->val;
-break;
-}
-node->vis++;
-stack[sp++] = node;
+while(node->parent){
+stack[++sp] = node;
 node = node->parent;
 }
-while(--sp >= 0){
-if (stack[sp]->val >= threshold){
+
+while (1) {
+if (curval > node->temp_val)
+node->temp_val = curval;
+else {
+curval = node->temp_val;
+}
+
+if (node->val >= threshold) {
 put_bits(s, 0, threshold - curval);
-break;
+curval = threshold;
+} else {
+put_bits(s, 0, node->val - curval);
+curval = node->val;
+if (!node->vis) {
+put_bits(s, 1, 1);
+node->vis = 1;
+}
 }
-put_bits(s, 0, stack[sp]->val - curval);
-put_bits(s, 1, 1);
-curval = stack[sp]->val;
+
+node->temp_val = curval;
+if (sp < 0)
+break;
+node = stack[sp--];
 }
 }
 
diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 26e09fbe38..3d3e7ec313 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -88,6 +88,7 @@ void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
 
 for (i = 0; i < siz; i++) {
 t[i].val = 0;
+t[i].temp_val = 0;
 t[i].vis = 0;
 }
 }
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index c3437b02fe..ad58b1ae88 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -127,6 +127,7 @@ typedef struct Jpeg2000T1Context {
 
 typedef struct Jpeg2000TgtNode {
 uint8_t val;
+uint8_t temp_val;
 uint8_t vis;
 struct Jpeg2000TgtNode *parent;
 } Jpeg2000TgtNode;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH v6 1/4] libavcodec/jpeg2000: Make tag tree functions non static

2020-08-26 Thread gautamramk
From: Gautam Ramakrishnan 

This patch makes the tag_tree_zero() and tag_tree_size()
functions non static and callable from other files.
---
 libavcodec/jpeg2000.c | 12 ++--
 libavcodec/jpeg2000.h |  3 +++
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 1aca31ffa4..26e09fbe38 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -39,7 +39,7 @@
 /* tag tree routines */
 
 /* allocate the memory for tag tree */
-static int32_t tag_tree_size(int w, int h)
+int32_t ff_tag_tree_size(int w, int h)
 {
 int64_t res = 0;
 while (w > 1 || h > 1) {
@@ -57,7 +57,7 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int 
h)
 Jpeg2000TgtNode *res, *t, *t2;
 int32_t tt_size;
 
-tt_size = tag_tree_size(w, h);
+tt_size = ff_tag_tree_size(w, h);
 
 t = res = av_mallocz_array(tt_size, sizeof(*t));
 if (!res)
@@ -82,9 +82,9 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int 
h)
 return res;
 }
 
-static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
 {
-int i, siz = tag_tree_size(w, h);
+int i, siz = ff_tag_tree_size(w, h);
 
 for (i = 0; i < siz; i++) {
 t[i].val = 0;
@@ -567,8 +567,8 @@ void ff_jpeg2000_reinit(Jpeg2000Component *comp, 
Jpeg2000CodingStyle *codsty)
 Jpeg2000Band *band = rlevel->band + bandno;
 for(precno = 0; precno < rlevel->num_precincts_x * 
rlevel->num_precincts_y; precno++) {
 Jpeg2000Prec *prec = band->prec + precno;
-tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
-tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * 
prec->nb_codeblocks_height; cblkno++) {
 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
 cblk->length = 0;
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index 5b0627c3dc..c3437b02fe 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -290,4 +290,7 @@ static inline int needs_termination(int style, int passno) {
 return 0;
 }
 
+int32_t ff_tag_tree_size(int w, int h);
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h);
+
 #endif /* AVCODEC_JPEG2000_H */
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH v6 4/4] libavcodec/jpeg2000: fix tag tree reset

2020-08-26 Thread gautamramk
From: Gautam Ramakrishnan 

The implementation of the tag tree did not
set the correct reset value for the encoder.
This lead to inefficent tag tree being encoded.
This patch fixes the implementation of the
ff_tag_tree_zero() function.
---
 libavcodec/j2kenc.c  | 4 ++--
 libavcodec/jpeg2000.c| 8 
 libavcodec/jpeg2000.h| 2 +-
 tests/ref/vsynth/vsynth1-jpeg2000| 4 ++--
 tests/ref/vsynth/vsynth1-jpeg2000-97 | 4 ++--
 tests/ref/vsynth/vsynth2-jpeg2000| 4 ++--
 tests/ref/vsynth/vsynth2-jpeg2000-97 | 4 ++--
 tests/ref/vsynth/vsynth3-jpeg2000| 4 ++--
 tests/ref/vsynth/vsynth3-jpeg2000-97 | 4 ++--
 tests/ref/vsynth/vsynth_lena-jpeg2000| 4 ++--
 tests/ref/vsynth/vsynth_lena-jpeg2000-97 | 4 ++--
 11 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 8dab18cdd6..ed3ad73c8e 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -801,8 +801,8 @@ static int encode_packet(Jpeg2000EncoderContext *s, 
Jpeg2000ResLevel *rlevel, in
 Jpeg2000Prec *prec = band->prec + precno;
 int nb_cblks = prec->nb_codeblocks_height * 
prec->nb_codeblocks_width;
 int pos;
-ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
-ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height, 99);
+ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height, 99);
 for (pos = 0; pos < nb_cblks; pos++) {
 Jpeg2000Cblk *cblk = >cblk[pos];
 prec->zerobits[pos].val = expn[bandno] + numgbits - 1 - 
cblk->nonzerobits;
diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 6501de0d04..56d98c8a89 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -82,12 +82,12 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, 
int h)
 return res;
 }
 
-void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h, int val)
 {
 int i, siz = ff_tag_tree_size(w, h);
 
 for (i = 0; i < siz; i++) {
-t[i].val = 0;
+t[i].val = val;
 t[i].temp_val = 0;
 t[i].vis = 0;
 }
@@ -575,8 +575,8 @@ void ff_jpeg2000_reinit(Jpeg2000Component *comp, 
Jpeg2000CodingStyle *codsty)
 Jpeg2000Band *band = rlevel->band + bandno;
 for(precno = 0; precno < rlevel->num_precincts_x * 
rlevel->num_precincts_y; precno++) {
 Jpeg2000Prec *prec = band->prec + precno;
-ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
-ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height, 0);
+ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height, 0);
 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * 
prec->nb_codeblocks_height; cblkno++) {
 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
 cblk->length = 0;
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index a7ba8aa7f3..612832c872 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -302,6 +302,6 @@ static inline int needs_termination(int style, int passno) {
 }
 
 int32_t ff_tag_tree_size(int w, int h);
-void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h);
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h, int val);
 
 #endif /* AVCODEC_JPEG2000_H */
diff --git a/tests/ref/vsynth/vsynth1-jpeg2000 
b/tests/ref/vsynth/vsynth1-jpeg2000
index 5081507459..f8e987f95d 100644
--- a/tests/ref/vsynth/vsynth1-jpeg2000
+++ b/tests/ref/vsynth/vsynth1-jpeg2000
@@ -1,4 +1,4 @@
-dd66b25f2ebc965eae4c29cfacdd960f *tests/data/fate/vsynth1-jpeg2000.avi
-2274950 tests/data/fate/vsynth1-jpeg2000.avi
+95add005faf68fcf8f16e86eab079ca2 *tests/data/fate/vsynth1-jpeg2000.avi
+2263192 tests/data/fate/vsynth1-jpeg2000.avi
 b7f48a8965f78011c76483277befc6fc *tests/data/fate/vsynth1-jpeg2000.out.rawvideo
 stddev:5.35 PSNR: 33.56 MAXDIFF:   59 bytes:  7603200/  7603200
diff --git a/tests/ref/vsynth/vsynth1-jpeg2000-97 
b/tests/ref/vsynth/vsynth1-jpeg2000-97
index 7e18f20e25..6ab5aa4237 100644
--- a/tests/ref/vsynth/vsynth1-jpeg2000-97
+++ b/tests/ref/vsynth/vsynth1-jpeg2000-97
@@ -1,4 +1,4 @@
-f22ad99de77f8f4382c6cf10d5af42b5 *tests/data/fate/vsynth1-jpeg2000-97.avi
-3661422 tests/data/fate/vsynth1-jpeg2000-97.avi
+e4d03b2e3c03e56c7f831b1e662c4031 *tests/data/fate/vsynth1-jpeg2000-97.avi
+3643928 tests/data/fate/vsynth1-jpeg2000-97.avi
 

[FFmpeg-devel] [RFC PATCH v6 2/4] libavcodec/j2kenc: Fix tag tree coding

2020-08-26 Thread gautamramk
From: Gautam Ramakrishnan 

The implementation of tag tree encoding was incorrect.
However, this error was not visible as the current j2k
encoder encodes only 1 layer.
This patch fixes tag tree coding for JPEG2000 such tag
tree coding would work for multi layer encoding.
---
 libavcodec/j2kenc.c   | 41 +
 libavcodec/jpeg2000.c |  1 +
 libavcodec/jpeg2000.h |  1 +
 3 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 16863f8e8c..87acd2d5c9 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -242,27 +242,36 @@ static void j2k_flush(Jpeg2000EncoderContext *s)
 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, 
int threshold)
 {
 Jpeg2000TgtNode *stack[30];
-int sp = 1, curval = 0;
-stack[0] = node;
+int sp = -1, curval = 0;
 
-node = node->parent;
-while(node){
-if (node->vis){
-curval = node->val;
-break;
-}
-node->vis++;
-stack[sp++] = node;
+while(node->parent){
+stack[++sp] = node;
 node = node->parent;
 }
-while(--sp >= 0){
-if (stack[sp]->val >= threshold){
+
+while (1) {
+if (curval > node->temp_val)
+node->temp_val = curval;
+else {
+curval = node->temp_val;
+}
+
+if (node->val >= threshold) {
 put_bits(s, 0, threshold - curval);
-break;
+curval = threshold;
+} else {
+put_bits(s, 0, node->val - curval);
+curval = node->val;
+if (!node->vis) {
+put_bits(s, 1, 1);
+node->vis = 1;
+}
 }
-put_bits(s, 0, stack[sp]->val - curval);
-put_bits(s, 1, 1);
-curval = stack[sp]->val;
+
+node->temp_val = curval;
+if (sp < 0)
+break;
+node = stack[sp--];
 }
 }
 
diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 26e09fbe38..3d3e7ec313 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -88,6 +88,7 @@ void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
 
 for (i = 0; i < siz; i++) {
 t[i].val = 0;
+t[i].temp_val = 0;
 t[i].vis = 0;
 }
 }
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index c3437b02fe..ad58b1ae88 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -127,6 +127,7 @@ typedef struct Jpeg2000T1Context {
 
 typedef struct Jpeg2000TgtNode {
 uint8_t val;
+uint8_t temp_val;
 uint8_t vis;
 struct Jpeg2000TgtNode *parent;
 } Jpeg2000TgtNode;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH v6 3/4] libavcodec/j2kenc: Support for multiple layers

2020-08-26 Thread gautamramk
From: Gautam Ramakrishnan 

This patch allows setting a compression ratio and to
set multiple layers. The user has to input a compression
ratio for each layer.
The per layer compression ration can be set as follows:
-layer_rates "r1,r2,...rn"
for to create 'n' layers.
---
 doc/encoders.texi|  16 +
 libavcodec/j2kenc.c  | 492 +++
 libavcodec/jpeg2000.c|  12 +-
 libavcodec/jpeg2000.h|  10 +
 tests/ref/vsynth/vsynth1-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth1-jpeg2000-97 |   6 +-
 tests/ref/vsynth/vsynth2-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth2-jpeg2000-97 |   8 +-
 tests/ref/vsynth/vsynth3-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth3-jpeg2000-97 |   8 +-
 tests/ref/vsynth/vsynth_lena-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth_lena-jpeg2000-97 |   8 +-
 12 files changed, 490 insertions(+), 102 deletions(-)

diff --git a/doc/encoders.texi b/doc/encoders.texi
index fd9235a05a..481ad2fb3d 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -1401,6 +1401,22 @@ Possible values are:
 @end table
 Set to @code{lrcp} by default.
 
+@item layer_rates @var{string}
+By default, compression is done using the quality metric. This option allows 
for
+compression using compression ratio. The compression ratio for each level could
+be specified. The compression ratio of a layer @code{l} species the what ratio 
of
+total file size is contained in the first @code{l} layers.
+
+Example usage:
+
+@example
+ffmpeg -i input.bmp -c:v jpeg2000 -layer_rates "100,10,1" output.j2k
+@end example
+
+This would compress the image to contain 3 layers, where the data contained in 
the
+first layer would be compressed by 1000 times, compressed by 100 in the first 
two layers,
+and shall contain all data while using all 3 layers.
+
 @end table
 
 @section librav1e
diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 87acd2d5c9..8dab18cdd6 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -32,6 +32,7 @@
  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  * Copyright (c) 2005, Herve Drolon, FreeImage Team
  * Copyright (c) 2007, Callum Lerwick 
+ * Copyright (c) 2020, Gautam Ramakrishnan 
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -100,6 +101,7 @@ static const int dwt_norms[2][4][10] = { // 
[dwt_type][band][rlevel] (multiplied
 
 typedef struct {
Jpeg2000Component *comp;
+   double *layer_rates;
 } Jpeg2000Tile;
 
 typedef struct {
@@ -126,12 +128,16 @@ typedef struct {
 Jpeg2000QuantStyle  qntsty;
 
 Jpeg2000Tile *tile;
+int layer_rates[100];
+uint8_t compression_rate_enc; ///< Is compression done using compression 
ratio?
 
 int format;
 int pred;
 int sop;
 int eph;
 int prog;
+int nlayers;
+char *lr_str;
 } Jpeg2000EncoderContext;
 
 
@@ -334,7 +340,7 @@ static int put_cod(Jpeg2000EncoderContext *s)
 bytestream_put_byte(>buf, scod);  // Scod
 // SGcod
 bytestream_put_byte(>buf, s->prog); // progression level
-bytestream_put_be16(>buf, 1); // num of layers
+bytestream_put_be16(>buf, s->nlayers); // num of layers
 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
 bytestream_put_byte(>buf, 0); // unspecified
 }else{
@@ -413,6 +419,31 @@ static uint8_t *put_sot(Jpeg2000EncoderContext *s, int 
tileno)
 return psotptr;
 }
 
+static void compute_rates(Jpeg2000EncoderContext* s)
+{
+int i, j;
+int layno, compno;
+for (i = 0; i < s->numYtiles; i++) {
+for (j = 0; j < s->numXtiles; j++) {
+Jpeg2000Tile *tile = >tile[s->numXtiles * i + j];
+for (compno = 0; compno < s->ncomponents; compno++) {
+int tilew = tile->comp[compno].coord[0][1] - 
tile->comp[compno].coord[0][0];
+int tileh = tile->comp[compno].coord[1][1] - 
tile->comp[compno].coord[1][0];
+int scale = (compno?1 << s->chroma_shift[0]:1) * (compno?1 << 
s->chroma_shift[1]:1);
+for (layno = 0; layno < s->nlayers; layno++) {
+if (s->layer_rates[layno] > 0) {
+tile->layer_rates[layno] += (double)(tilew * tileh) * 
s->ncomponents * s->cbps[compno] /
+
(double)(s->layer_rates[layno] * 8 * scale);
+} else {
+tile->layer_rates[layno] = 0.0;
+}
+}
+}
+}
+}
+
+}
+
 /**
  * compute the sizes of tiles, resolution levels, bands, etc.
  * allocate memory for them
@@ -437,6 +468,11 @@ static int init_tiles(Jpeg2000EncoderContext *s)
 tile->comp = av_mallocz_array(s->ncomponents, 
sizeof(Jpeg2000Component));
 if (!tile->comp)
 return AVERROR(ENOMEM);
+
+tile->layer_rates = av_mallocz_array(s->nlayers, 

[FFmpeg-devel] [RFC PATCH v5 2/3] libavcodec/j2kenc: Fix tag tree coding

2020-08-25 Thread gautamramk
From: Gautam Ramakrishnan 

The implementation of tag tree encoding was incorrect.
However, this error was not visible as the current j2k
encoder encodes only 1 layer.
This patch fixes tag tree coding for JPEG2000 such tag
tree coding would work for multi layer encoding.
---
 libavcodec/j2kenc.c   | 41 +
 libavcodec/jpeg2000.c |  9 +
 libavcodec/jpeg2000.h |  3 ++-
 3 files changed, 32 insertions(+), 21 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 16863f8e8c..87acd2d5c9 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -242,27 +242,36 @@ static void j2k_flush(Jpeg2000EncoderContext *s)
 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, 
int threshold)
 {
 Jpeg2000TgtNode *stack[30];
-int sp = 1, curval = 0;
-stack[0] = node;
+int sp = -1, curval = 0;
 
-node = node->parent;
-while(node){
-if (node->vis){
-curval = node->val;
-break;
-}
-node->vis++;
-stack[sp++] = node;
+while(node->parent){
+stack[++sp] = node;
 node = node->parent;
 }
-while(--sp >= 0){
-if (stack[sp]->val >= threshold){
+
+while (1) {
+if (curval > node->temp_val)
+node->temp_val = curval;
+else {
+curval = node->temp_val;
+}
+
+if (node->val >= threshold) {
 put_bits(s, 0, threshold - curval);
-break;
+curval = threshold;
+} else {
+put_bits(s, 0, node->val - curval);
+curval = node->val;
+if (!node->vis) {
+put_bits(s, 1, 1);
+node->vis = 1;
+}
 }
-put_bits(s, 0, stack[sp]->val - curval);
-put_bits(s, 1, 1);
-curval = stack[sp]->val;
+
+node->temp_val = curval;
+if (sp < 0)
+break;
+node = stack[sp--];
 }
 }
 
diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 26e09fbe38..2e26bc5b00 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -82,12 +82,13 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, 
int h)
 return res;
 }
 
-void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h, int val)
 {
 int i, siz = ff_tag_tree_size(w, h);
 
 for (i = 0; i < siz; i++) {
-t[i].val = 0;
+t[i].val = val;
+t[i].temp_val = 0;
 t[i].vis = 0;
 }
 }
@@ -567,8 +568,8 @@ void ff_jpeg2000_reinit(Jpeg2000Component *comp, 
Jpeg2000CodingStyle *codsty)
 Jpeg2000Band *band = rlevel->band + bandno;
 for(precno = 0; precno < rlevel->num_precincts_x * 
rlevel->num_precincts_y; precno++) {
 Jpeg2000Prec *prec = band->prec + precno;
-ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
-ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height, 0);
+ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height, 0);
 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * 
prec->nb_codeblocks_height; cblkno++) {
 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
 cblk->length = 0;
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index c3437b02fe..a9f2e01632 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -127,6 +127,7 @@ typedef struct Jpeg2000T1Context {
 
 typedef struct Jpeg2000TgtNode {
 uint8_t val;
+uint8_t temp_val;
 uint8_t vis;
 struct Jpeg2000TgtNode *parent;
 } Jpeg2000TgtNode;
@@ -291,6 +292,6 @@ static inline int needs_termination(int style, int passno) {
 }
 
 int32_t ff_tag_tree_size(int w, int h);
-void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h);
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h, int val);
 
 #endif /* AVCODEC_JPEG2000_H */
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH v5 3/3] libavcodec/j2kenc: Support for multiple layers

2020-08-25 Thread gautamramk
From: Gautam Ramakrishnan 

This patch allows setting a compression ratio and to
set multiple layers. The user has to input a compression
ratio for each layer.
The per layer compression ration can be set as follows:
-layer_rates "r1,r2,...rn"
for to create 'n' layers.
---
 doc/encoders.texi|  16 +
 libavcodec/j2kenc.c  | 492 +++
 libavcodec/jpeg2000.c|  12 +-
 libavcodec/jpeg2000.h|  10 +
 tests/ref/vsynth/vsynth1-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth1-jpeg2000-97 |   6 +-
 tests/ref/vsynth/vsynth2-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth2-jpeg2000-97 |   8 +-
 tests/ref/vsynth/vsynth3-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth3-jpeg2000-97 |   8 +-
 tests/ref/vsynth/vsynth_lena-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth_lena-jpeg2000-97 |   8 +-
 12 files changed, 490 insertions(+), 102 deletions(-)

diff --git a/doc/encoders.texi b/doc/encoders.texi
index fd9235a05a..481ad2fb3d 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -1401,6 +1401,22 @@ Possible values are:
 @end table
 Set to @code{lrcp} by default.
 
+@item layer_rates @var{string}
+By default, compression is done using the quality metric. This option allows 
for
+compression using compression ratio. The compression ratio for each level could
+be specified. The compression ratio of a layer @code{l} species the what ratio 
of
+total file size is contained in the first @code{l} layers.
+
+Example usage:
+
+@example
+ffmpeg -i input.bmp -c:v jpeg2000 -layer_rates "100,10,1" output.j2k
+@end example
+
+This would compress the image to contain 3 layers, where the data contained in 
the
+first layer would be compressed by 1000 times, compressed by 100 in the first 
two layers,
+and shall contain all data while using all 3 layers.
+
 @end table
 
 @section librav1e
diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 87acd2d5c9..ed3ad73c8e 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -32,6 +32,7 @@
  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  * Copyright (c) 2005, Herve Drolon, FreeImage Team
  * Copyright (c) 2007, Callum Lerwick 
+ * Copyright (c) 2020, Gautam Ramakrishnan 
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -100,6 +101,7 @@ static const int dwt_norms[2][4][10] = { // 
[dwt_type][band][rlevel] (multiplied
 
 typedef struct {
Jpeg2000Component *comp;
+   double *layer_rates;
 } Jpeg2000Tile;
 
 typedef struct {
@@ -126,12 +128,16 @@ typedef struct {
 Jpeg2000QuantStyle  qntsty;
 
 Jpeg2000Tile *tile;
+int layer_rates[100];
+uint8_t compression_rate_enc; ///< Is compression done using compression 
ratio?
 
 int format;
 int pred;
 int sop;
 int eph;
 int prog;
+int nlayers;
+char *lr_str;
 } Jpeg2000EncoderContext;
 
 
@@ -334,7 +340,7 @@ static int put_cod(Jpeg2000EncoderContext *s)
 bytestream_put_byte(>buf, scod);  // Scod
 // SGcod
 bytestream_put_byte(>buf, s->prog); // progression level
-bytestream_put_be16(>buf, 1); // num of layers
+bytestream_put_be16(>buf, s->nlayers); // num of layers
 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
 bytestream_put_byte(>buf, 0); // unspecified
 }else{
@@ -413,6 +419,31 @@ static uint8_t *put_sot(Jpeg2000EncoderContext *s, int 
tileno)
 return psotptr;
 }
 
+static void compute_rates(Jpeg2000EncoderContext* s)
+{
+int i, j;
+int layno, compno;
+for (i = 0; i < s->numYtiles; i++) {
+for (j = 0; j < s->numXtiles; j++) {
+Jpeg2000Tile *tile = >tile[s->numXtiles * i + j];
+for (compno = 0; compno < s->ncomponents; compno++) {
+int tilew = tile->comp[compno].coord[0][1] - 
tile->comp[compno].coord[0][0];
+int tileh = tile->comp[compno].coord[1][1] - 
tile->comp[compno].coord[1][0];
+int scale = (compno?1 << s->chroma_shift[0]:1) * (compno?1 << 
s->chroma_shift[1]:1);
+for (layno = 0; layno < s->nlayers; layno++) {
+if (s->layer_rates[layno] > 0) {
+tile->layer_rates[layno] += (double)(tilew * tileh) * 
s->ncomponents * s->cbps[compno] /
+
(double)(s->layer_rates[layno] * 8 * scale);
+} else {
+tile->layer_rates[layno] = 0.0;
+}
+}
+}
+}
+}
+
+}
+
 /**
  * compute the sizes of tiles, resolution levels, bands, etc.
  * allocate memory for them
@@ -437,6 +468,11 @@ static int init_tiles(Jpeg2000EncoderContext *s)
 tile->comp = av_mallocz_array(s->ncomponents, 
sizeof(Jpeg2000Component));
 if (!tile->comp)
 return AVERROR(ENOMEM);
+
+tile->layer_rates = av_mallocz_array(s->nlayers, 

[FFmpeg-devel] [RFC PATCH v5 1/3] libavcodec/jpeg2000: Make tag tree functions non static

2020-08-25 Thread gautamramk
From: Gautam Ramakrishnan 

This patch makes the tag_tree_zero() and tag_tree_size()
functions non static and callable from other files.
---
 libavcodec/jpeg2000.c | 12 ++--
 libavcodec/jpeg2000.h |  3 +++
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 1aca31ffa4..26e09fbe38 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -39,7 +39,7 @@
 /* tag tree routines */
 
 /* allocate the memory for tag tree */
-static int32_t tag_tree_size(int w, int h)
+int32_t ff_tag_tree_size(int w, int h)
 {
 int64_t res = 0;
 while (w > 1 || h > 1) {
@@ -57,7 +57,7 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int 
h)
 Jpeg2000TgtNode *res, *t, *t2;
 int32_t tt_size;
 
-tt_size = tag_tree_size(w, h);
+tt_size = ff_tag_tree_size(w, h);
 
 t = res = av_mallocz_array(tt_size, sizeof(*t));
 if (!res)
@@ -82,9 +82,9 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int 
h)
 return res;
 }
 
-static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
 {
-int i, siz = tag_tree_size(w, h);
+int i, siz = ff_tag_tree_size(w, h);
 
 for (i = 0; i < siz; i++) {
 t[i].val = 0;
@@ -567,8 +567,8 @@ void ff_jpeg2000_reinit(Jpeg2000Component *comp, 
Jpeg2000CodingStyle *codsty)
 Jpeg2000Band *band = rlevel->band + bandno;
 for(precno = 0; precno < rlevel->num_precincts_x * 
rlevel->num_precincts_y; precno++) {
 Jpeg2000Prec *prec = band->prec + precno;
-tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
-tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * 
prec->nb_codeblocks_height; cblkno++) {
 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
 cblk->length = 0;
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index 5b0627c3dc..c3437b02fe 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -290,4 +290,7 @@ static inline int needs_termination(int style, int passno) {
 return 0;
 }
 
+int32_t ff_tag_tree_size(int w, int h);
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h);
+
 #endif /* AVCODEC_JPEG2000_H */
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH v4 3/5] libavcodec/jpeg2000: Modify cleanup

2020-08-24 Thread gautamramk
From: Gautam Ramakrishnan 

This patch makes the ff_jpeg2000_cleanup
function take in an extra parameter which
indicates whether it is called from the
encoder or decoder.
---
 libavcodec/j2kenc.c  | 2 +-
 libavcodec/jpeg2000.c| 2 +-
 libavcodec/jpeg2000.h| 2 +-
 libavcodec/jpeg2000dec.c | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 87acd2d5c9..711655f1d6 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -1220,7 +1220,7 @@ static void cleanup(Jpeg2000EncoderContext *s)
 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
 for (compno = 0; compno < s->ncomponents; compno++){
 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
-ff_jpeg2000_cleanup(comp, codsty);
+ff_jpeg2000_cleanup(comp, codsty, 1);
 }
 av_freep(>tile[tileno].comp);
 }
diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 3d3e7ec313..70c25a0ca2 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -580,7 +580,7 @@ void ff_jpeg2000_reinit(Jpeg2000Component *comp, 
Jpeg2000CodingStyle *codsty)
 }
 }
 
-void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
+void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, 
int isencoder)
 {
 int reslevelno, bandno, precno;
 for (reslevelno = 0;
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index ad58b1ae88..fee9607e86 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -272,7 +272,7 @@ int ff_jpeg2000_init_component(Jpeg2000Component *comp,
 
 void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
 
-void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
+void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, 
int isencoder);
 
 static inline int needs_termination(int style, int passno) {
 if (style & JPEG2000_CBLK_BYPASS) {
diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 624542c2f8..c5192d007f 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -2123,7 +2123,7 @@ static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext 
*s)
 Jpeg2000Component *comp = s->tile[tileno].comp   + compno;
 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
 
-ff_jpeg2000_cleanup(comp, codsty);
+ff_jpeg2000_cleanup(comp, codsty, 0);
 }
 av_freep(>tile[tileno].comp);
 av_freep(>tile[tileno].packed_headers);
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH v4 5/5] doc/encoders.texi: Add documentation for JPEG2000 layers

2020-08-24 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds documentation for the utility and usage
of "layer_rates" option used in the JPEG2000 encoder.
---
 doc/encoders.texi | 16 
 1 file changed, 16 insertions(+)

diff --git a/doc/encoders.texi b/doc/encoders.texi
index fd9235a05a..481ad2fb3d 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -1401,6 +1401,22 @@ Possible values are:
 @end table
 Set to @code{lrcp} by default.
 
+@item layer_rates @var{string}
+By default, compression is done using the quality metric. This option allows 
for
+compression using compression ratio. The compression ratio for each level could
+be specified. The compression ratio of a layer @code{l} species the what ratio 
of
+total file size is contained in the first @code{l} layers.
+
+Example usage:
+
+@example
+ffmpeg -i input.bmp -c:v jpeg2000 -layer_rates "100,10,1" output.j2k
+@end example
+
+This would compress the image to contain 3 layers, where the data contained in 
the
+first layer would be compressed by 1000 times, compressed by 100 in the first 
two layers,
+and shall contain all data while using all 3 layers.
+
 @end table
 
 @section librav1e
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH v4 1/5] libavcodec/jpeg2000: Make tag tree functions non static

2020-08-24 Thread gautamramk
From: Gautam Ramakrishnan 

This patch makes the tag_tree_zero() and tag_tree_size()
functions non static and callable from other files.
---
 libavcodec/jpeg2000.c | 12 ++--
 libavcodec/jpeg2000.h |  3 +++
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 1aca31ffa4..26e09fbe38 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -39,7 +39,7 @@
 /* tag tree routines */
 
 /* allocate the memory for tag tree */
-static int32_t tag_tree_size(int w, int h)
+int32_t ff_tag_tree_size(int w, int h)
 {
 int64_t res = 0;
 while (w > 1 || h > 1) {
@@ -57,7 +57,7 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int 
h)
 Jpeg2000TgtNode *res, *t, *t2;
 int32_t tt_size;
 
-tt_size = tag_tree_size(w, h);
+tt_size = ff_tag_tree_size(w, h);
 
 t = res = av_mallocz_array(tt_size, sizeof(*t));
 if (!res)
@@ -82,9 +82,9 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int 
h)
 return res;
 }
 
-static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
 {
-int i, siz = tag_tree_size(w, h);
+int i, siz = ff_tag_tree_size(w, h);
 
 for (i = 0; i < siz; i++) {
 t[i].val = 0;
@@ -567,8 +567,8 @@ void ff_jpeg2000_reinit(Jpeg2000Component *comp, 
Jpeg2000CodingStyle *codsty)
 Jpeg2000Band *band = rlevel->band + bandno;
 for(precno = 0; precno < rlevel->num_precincts_x * 
rlevel->num_precincts_y; precno++) {
 Jpeg2000Prec *prec = band->prec + precno;
-tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
-tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * 
prec->nb_codeblocks_height; cblkno++) {
 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
 cblk->length = 0;
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index 5b0627c3dc..c3437b02fe 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -290,4 +290,7 @@ static inline int needs_termination(int style, int passno) {
 return 0;
 }
 
+int32_t ff_tag_tree_size(int w, int h);
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h);
+
 #endif /* AVCODEC_JPEG2000_H */
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH v4 2/5] libavcodec/j2kenc: Fix tag tree coding

2020-08-24 Thread gautamramk
From: Gautam Ramakrishnan 

The implementation of tag tree encoding was incorrect.
However, this error was not visible as the current j2k
encoder encodes only 1 layer.
This patch fixes tag tree coding for JPEG2000 such tag
tree coding would work for multi layer encoding.
---
 libavcodec/j2kenc.c   | 41 +
 libavcodec/jpeg2000.c |  1 +
 libavcodec/jpeg2000.h |  1 +
 3 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 16863f8e8c..87acd2d5c9 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -242,27 +242,36 @@ static void j2k_flush(Jpeg2000EncoderContext *s)
 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, 
int threshold)
 {
 Jpeg2000TgtNode *stack[30];
-int sp = 1, curval = 0;
-stack[0] = node;
+int sp = -1, curval = 0;
 
-node = node->parent;
-while(node){
-if (node->vis){
-curval = node->val;
-break;
-}
-node->vis++;
-stack[sp++] = node;
+while(node->parent){
+stack[++sp] = node;
 node = node->parent;
 }
-while(--sp >= 0){
-if (stack[sp]->val >= threshold){
+
+while (1) {
+if (curval > node->temp_val)
+node->temp_val = curval;
+else {
+curval = node->temp_val;
+}
+
+if (node->val >= threshold) {
 put_bits(s, 0, threshold - curval);
-break;
+curval = threshold;
+} else {
+put_bits(s, 0, node->val - curval);
+curval = node->val;
+if (!node->vis) {
+put_bits(s, 1, 1);
+node->vis = 1;
+}
 }
-put_bits(s, 0, stack[sp]->val - curval);
-put_bits(s, 1, 1);
-curval = stack[sp]->val;
+
+node->temp_val = curval;
+if (sp < 0)
+break;
+node = stack[sp--];
 }
 }
 
diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 26e09fbe38..3d3e7ec313 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -88,6 +88,7 @@ void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
 
 for (i = 0; i < siz; i++) {
 t[i].val = 0;
+t[i].temp_val = 0;
 t[i].vis = 0;
 }
 }
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index c3437b02fe..ad58b1ae88 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -127,6 +127,7 @@ typedef struct Jpeg2000T1Context {
 
 typedef struct Jpeg2000TgtNode {
 uint8_t val;
+uint8_t temp_val;
 uint8_t vis;
 struct Jpeg2000TgtNode *parent;
 } Jpeg2000TgtNode;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH v4 4/5] libavcodec/j2kenc: Support for multiple layers

2020-08-24 Thread gautamramk
From: Gautam Ramakrishnan 

This patch allows setting a compression ratio and to
set multiple layers. The user has to input a compression
ratio for each layer.
The per layer compression ration can be set as follows:
-layer_rates "r1,r2,...rn"
for to create 'n' layers.
---
 libavcodec/j2kenc.c  | 492 +++
 libavcodec/jpeg2000.c|  13 +-
 libavcodec/jpeg2000.h|  10 +
 tests/ref/vsynth/vsynth1-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth1-jpeg2000-97 |   6 +-
 tests/ref/vsynth/vsynth2-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth2-jpeg2000-97 |   8 +-
 tests/ref/vsynth/vsynth3-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth3-jpeg2000-97 |   8 +-
 tests/ref/vsynth/vsynth_lena-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth_lena-jpeg2000-97 |   8 +-
 11 files changed, 475 insertions(+), 102 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 711655f1d6..0e65adf445 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -32,6 +32,7 @@
  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  * Copyright (c) 2005, Herve Drolon, FreeImage Team
  * Copyright (c) 2007, Callum Lerwick 
+ * Copyright (c) 2020, Gautam Ramakrishnan 
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -100,6 +101,7 @@ static const int dwt_norms[2][4][10] = { // 
[dwt_type][band][rlevel] (multiplied
 
 typedef struct {
Jpeg2000Component *comp;
+   double *layer_rates;
 } Jpeg2000Tile;
 
 typedef struct {
@@ -126,12 +128,16 @@ typedef struct {
 Jpeg2000QuantStyle  qntsty;
 
 Jpeg2000Tile *tile;
+int layer_rates[100];
+uint8_t compression_rate_enc; ///< Is compression done using compression 
ratio?
 
 int format;
 int pred;
 int sop;
 int eph;
 int prog;
+int nlayers;
+char *lr_str;
 } Jpeg2000EncoderContext;
 
 
@@ -334,7 +340,7 @@ static int put_cod(Jpeg2000EncoderContext *s)
 bytestream_put_byte(>buf, scod);  // Scod
 // SGcod
 bytestream_put_byte(>buf, s->prog); // progression level
-bytestream_put_be16(>buf, 1); // num of layers
+bytestream_put_be16(>buf, s->nlayers); // num of layers
 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
 bytestream_put_byte(>buf, 0); // unspecified
 }else{
@@ -413,6 +419,31 @@ static uint8_t *put_sot(Jpeg2000EncoderContext *s, int 
tileno)
 return psotptr;
 }
 
+static void compute_rates(Jpeg2000EncoderContext* s)
+{
+int i, j;
+int layno, compno;
+for (i = 0; i < s->numYtiles; i++) {
+for (j = 0; j < s->numXtiles; j++) {
+Jpeg2000Tile *tile = >tile[s->numXtiles * i + j];
+for (compno = 0; compno < s->ncomponents; compno++) {
+int tilew = tile->comp[compno].coord[0][1] - 
tile->comp[compno].coord[0][0];
+int tileh = tile->comp[compno].coord[1][1] - 
tile->comp[compno].coord[1][0];
+int scale = (compno?1 << s->chroma_shift[0]:1) * (compno?1 << 
s->chroma_shift[1]:1);
+for (layno = 0; layno < s->nlayers; layno++) {
+if (s->layer_rates[layno] > 0) {
+tile->layer_rates[layno] += (double)(tilew * tileh) * 
s->ncomponents * s->cbps[compno] /
+
(double)(s->layer_rates[layno] * 8 * scale);
+} else {
+tile->layer_rates[layno] = 0.0;
+}
+}
+}
+}
+}
+
+}
+
 /**
  * compute the sizes of tiles, resolution levels, bands, etc.
  * allocate memory for them
@@ -437,6 +468,11 @@ static int init_tiles(Jpeg2000EncoderContext *s)
 tile->comp = av_mallocz_array(s->ncomponents, 
sizeof(Jpeg2000Component));
 if (!tile->comp)
 return AVERROR(ENOMEM);
+
+tile->layer_rates = av_mallocz_array(s->nlayers, 
sizeof(*tile->layer_rates));
+if (!tile->layer_rates)
+return AVERROR(ENOMEM);
+
 for (compno = 0; compno < s->ncomponents; compno++){
 Jpeg2000Component *comp = tile->comp + compno;
 int ret, i, j;
@@ -461,6 +497,7 @@ static int init_tiles(Jpeg2000EncoderContext *s)
 return ret;
 }
 }
+compute_rates(s);
 return 0;
 }
 
@@ -703,6 +740,8 @@ static void encode_cblk(Jpeg2000EncoderContext *s, 
Jpeg2000T1Context *t1, Jpeg20
 }
 
 cblk->passes[passno].rate = ff_mqc_flush_to(>mqc, 
cblk->passes[passno].flushed, >passes[passno].flushed_len);
+cblk->passes[passno].rate -= cblk->passes[passno].flushed_len;
+
 wmsedec += (int64_t)nmsedec << (2*bpno);
 cblk->passes[passno].disto = wmsedec;
 
@@ -714,8 +753,10 @@ static void encode_cblk(Jpeg2000EncoderContext *s, 
Jpeg2000T1Context *t1, Jpeg20
 cblk->npasses = passno;
 cblk->ninclpasses = passno;
 

[FFmpeg-devel] [RFC PATCH v3 5/5] doc/encoders.texi: Add documentation for JPEG2000 layers

2020-08-24 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds documentation for the utility and usage
of "layer_rates" option used in the JPEG2000 encoder.
---
 doc/encoders.texi | 16 
 1 file changed, 16 insertions(+)

diff --git a/doc/encoders.texi b/doc/encoders.texi
index fd9235a05a..481ad2fb3d 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -1401,6 +1401,22 @@ Possible values are:
 @end table
 Set to @code{lrcp} by default.
 
+@item layer_rates @var{string}
+By default, compression is done using the quality metric. This option allows 
for
+compression using compression ratio. The compression ratio for each level could
+be specified. The compression ratio of a layer @code{l} species the what ratio 
of
+total file size is contained in the first @code{l} layers.
+
+Example usage:
+
+@example
+ffmpeg -i input.bmp -c:v jpeg2000 -layer_rates "100,10,1" output.j2k
+@end example
+
+This would compress the image to contain 3 layers, where the data contained in 
the
+first layer would be compressed by 1000 times, compressed by 100 in the first 
two layers,
+and shall contain all data while using all 3 layers.
+
 @end table
 
 @section librav1e
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH v3 1/5] libavcodec/jpeg2000: Make tag tree functions non static

2020-08-24 Thread gautamramk
From: Gautam Ramakrishnan 

This patch makes the tag_tree_zero() and tag_tree_size()
functions non static and callable from other files.
---
 libavcodec/jpeg2000.c | 12 ++--
 libavcodec/jpeg2000.h |  3 +++
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 1aca31ffa4..26e09fbe38 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -39,7 +39,7 @@
 /* tag tree routines */
 
 /* allocate the memory for tag tree */
-static int32_t tag_tree_size(int w, int h)
+int32_t ff_tag_tree_size(int w, int h)
 {
 int64_t res = 0;
 while (w > 1 || h > 1) {
@@ -57,7 +57,7 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int 
h)
 Jpeg2000TgtNode *res, *t, *t2;
 int32_t tt_size;
 
-tt_size = tag_tree_size(w, h);
+tt_size = ff_tag_tree_size(w, h);
 
 t = res = av_mallocz_array(tt_size, sizeof(*t));
 if (!res)
@@ -82,9 +82,9 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int 
h)
 return res;
 }
 
-static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
 {
-int i, siz = tag_tree_size(w, h);
+int i, siz = ff_tag_tree_size(w, h);
 
 for (i = 0; i < siz; i++) {
 t[i].val = 0;
@@ -567,8 +567,8 @@ void ff_jpeg2000_reinit(Jpeg2000Component *comp, 
Jpeg2000CodingStyle *codsty)
 Jpeg2000Band *band = rlevel->band + bandno;
 for(precno = 0; precno < rlevel->num_precincts_x * 
rlevel->num_precincts_y; precno++) {
 Jpeg2000Prec *prec = band->prec + precno;
-tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
-tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * 
prec->nb_codeblocks_height; cblkno++) {
 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
 cblk->length = 0;
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index 5b0627c3dc..c3437b02fe 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -290,4 +290,7 @@ static inline int needs_termination(int style, int passno) {
 return 0;
 }
 
+int32_t ff_tag_tree_size(int w, int h);
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h);
+
 #endif /* AVCODEC_JPEG2000_H */
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH v3 3/5] libavcodec/jpeg2000: Modify cleanup

2020-08-24 Thread gautamramk
From: Gautam Ramakrishnan 

This patch makes the ff_jpeg2000_cleanup
function take in an extra parameter which
indicates whether it is called from the
encoder or decoder.
---
 libavcodec/j2kenc.c  | 2 +-
 libavcodec/jpeg2000.c| 2 +-
 libavcodec/jpeg2000.h| 2 +-
 libavcodec/jpeg2000dec.c | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index d9777fe07f..8699296434 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -1218,7 +1218,7 @@ static void cleanup(Jpeg2000EncoderContext *s)
 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
 for (compno = 0; compno < s->ncomponents; compno++){
 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
-ff_jpeg2000_cleanup(comp, codsty);
+ff_jpeg2000_cleanup(comp, codsty, 1);
 }
 av_freep(>tile[tileno].comp);
 }
diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 3d3e7ec313..70c25a0ca2 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -580,7 +580,7 @@ void ff_jpeg2000_reinit(Jpeg2000Component *comp, 
Jpeg2000CodingStyle *codsty)
 }
 }
 
-void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
+void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, 
int isencoder)
 {
 int reslevelno, bandno, precno;
 for (reslevelno = 0;
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index ad58b1ae88..fee9607e86 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -272,7 +272,7 @@ int ff_jpeg2000_init_component(Jpeg2000Component *comp,
 
 void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
 
-void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
+void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, 
int isencoder);
 
 static inline int needs_termination(int style, int passno) {
 if (style & JPEG2000_CBLK_BYPASS) {
diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 624542c2f8..c5192d007f 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -2123,7 +2123,7 @@ static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext 
*s)
 Jpeg2000Component *comp = s->tile[tileno].comp   + compno;
 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
 
-ff_jpeg2000_cleanup(comp, codsty);
+ff_jpeg2000_cleanup(comp, codsty, 0);
 }
 av_freep(>tile[tileno].comp);
 av_freep(>tile[tileno].packed_headers);
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH v3 4/5] libavcodec/j2kenc: Support for multiple layers

2020-08-24 Thread gautamramk
From: Gautam Ramakrishnan 

This patch allows setting a compression ratio and to
set multiple layers. The user has to input a compression
ratio for each layer.
The per layer compression ration can be set as follows:
-layer_rates "r1,r2,...rn"
for to create 'n' layers.
---
 libavcodec/j2kenc.c  | 492 +++
 libavcodec/jpeg2000.c|  13 +-
 libavcodec/jpeg2000.h|  10 +
 tests/ref/vsynth/vsynth1-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth1-jpeg2000-97 |   6 +-
 tests/ref/vsynth/vsynth2-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth2-jpeg2000-97 |   8 +-
 tests/ref/vsynth/vsynth3-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth3-jpeg2000-97 |   8 +-
 tests/ref/vsynth/vsynth_lena-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth_lena-jpeg2000-97 |   8 +-
 11 files changed, 475 insertions(+), 102 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 8699296434..5f72d924d9 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -32,6 +32,7 @@
  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  * Copyright (c) 2005, Herve Drolon, FreeImage Team
  * Copyright (c) 2007, Callum Lerwick 
+ * Copyright (c) 2020, Gautam Ramakrishnan 
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -100,6 +101,7 @@ static const int dwt_norms[2][4][10] = { // 
[dwt_type][band][rlevel] (multiplied
 
 typedef struct {
Jpeg2000Component *comp;
+   double *layer_rates;
 } Jpeg2000Tile;
 
 typedef struct {
@@ -126,12 +128,16 @@ typedef struct {
 Jpeg2000QuantStyle  qntsty;
 
 Jpeg2000Tile *tile;
+int layer_rates[100];
+uint8_t compression_rate_enc; ///< Is compression done using compression 
ratio?
 
 int format;
 int pred;
 int sop;
 int eph;
 int prog;
+int nlayers;
+char *lr_str;
 } Jpeg2000EncoderContext;
 
 
@@ -332,7 +338,7 @@ static int put_cod(Jpeg2000EncoderContext *s)
 bytestream_put_byte(>buf, scod);  // Scod
 // SGcod
 bytestream_put_byte(>buf, s->prog); // progression level
-bytestream_put_be16(>buf, 1); // num of layers
+bytestream_put_be16(>buf, s->nlayers); // num of layers
 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
 bytestream_put_byte(>buf, 0); // unspecified
 }else{
@@ -411,6 +417,31 @@ static uint8_t *put_sot(Jpeg2000EncoderContext *s, int 
tileno)
 return psotptr;
 }
 
+static void compute_rates(Jpeg2000EncoderContext* s)
+{
+int i, j;
+int layno, compno;
+for (i = 0; i < s->numYtiles; i++) {
+for (j = 0; j < s->numXtiles; j++) {
+Jpeg2000Tile *tile = >tile[s->numXtiles * i + j];
+for (compno = 0; compno < s->ncomponents; compno++) {
+int tilew = tile->comp[compno].coord[0][1] - 
tile->comp[compno].coord[0][0];
+int tileh = tile->comp[compno].coord[1][1] - 
tile->comp[compno].coord[1][0];
+int scale = (compno?1 << s->chroma_shift[0]:1) * (compno?1 << 
s->chroma_shift[1]:1);
+for (layno = 0; layno < s->nlayers; layno++) {
+if (s->layer_rates[layno] > 0) {
+tile->layer_rates[layno] += (double)(tilew * tileh) * 
s->ncomponents * s->cbps[compno] /
+
(double)(s->layer_rates[layno] * 8 * scale);
+} else {
+tile->layer_rates[layno] = 0.0;
+}
+}
+}
+}
+}
+
+}
+
 /**
  * compute the sizes of tiles, resolution levels, bands, etc.
  * allocate memory for them
@@ -435,6 +466,11 @@ static int init_tiles(Jpeg2000EncoderContext *s)
 tile->comp = av_mallocz_array(s->ncomponents, 
sizeof(Jpeg2000Component));
 if (!tile->comp)
 return AVERROR(ENOMEM);
+
+tile->layer_rates = av_mallocz_array(s->nlayers, 
sizeof(*tile->layer_rates));
+if (!tile->layer_rates)
+return AVERROR(ENOMEM);
+
 for (compno = 0; compno < s->ncomponents; compno++){
 Jpeg2000Component *comp = tile->comp + compno;
 int ret, i, j;
@@ -459,6 +495,7 @@ static int init_tiles(Jpeg2000EncoderContext *s)
 return ret;
 }
 }
+compute_rates(s);
 return 0;
 }
 
@@ -701,6 +738,8 @@ static void encode_cblk(Jpeg2000EncoderContext *s, 
Jpeg2000T1Context *t1, Jpeg20
 }
 
 cblk->passes[passno].rate = ff_mqc_flush_to(>mqc, 
cblk->passes[passno].flushed, >passes[passno].flushed_len);
+cblk->passes[passno].rate -= cblk->passes[passno].flushed_len;
+
 wmsedec += (int64_t)nmsedec << (2*bpno);
 cblk->passes[passno].disto = wmsedec;
 
@@ -712,8 +751,10 @@ static void encode_cblk(Jpeg2000EncoderContext *s, 
Jpeg2000T1Context *t1, Jpeg20
 cblk->npasses = passno;
 cblk->ninclpasses = passno;
 

[FFmpeg-devel] [RFC PATCH v3 2/5] libavcodec/j2kenc: Fix tag tree coding

2020-08-24 Thread gautamramk
From: Gautam Ramakrishnan 

The implementation of tag tree encoding was incorrect.
However, this error was not visible as the current j2k
encoder encodes only 1 layer.
This patch fixes tag tree coding for JPEG2000 such tag
tree coding would work for multi layer encoding.
---
 libavcodec/j2kenc.c   | 41 -
 libavcodec/jpeg2000.c |  1 +
 libavcodec/jpeg2000.h |  1 +
 3 files changed, 26 insertions(+), 17 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 16863f8e8c..d9777fe07f 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -242,27 +242,34 @@ static void j2k_flush(Jpeg2000EncoderContext *s)
 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, 
int threshold)
 {
 Jpeg2000TgtNode *stack[30];
-int sp = 1, curval = 0;
-stack[0] = node;
+int sp = -1, curval = 0;
 
-node = node->parent;
-while(node){
-if (node->vis){
-curval = node->val;
-break;
-}
-node->vis++;
-stack[sp++] = node;
+while(node->parent){
+stack[++sp] = node;
 node = node->parent;
 }
-while(--sp >= 0){
-if (stack[sp]->val >= threshold){
-put_bits(s, 0, threshold - curval);
-break;
+
+while (1) {
+if (curval > node->temp_val)
+node->temp_val = curval;
+else {
+curval = node->temp_val;
 }
-put_bits(s, 0, stack[sp]->val - curval);
-put_bits(s, 1, 1);
-curval = stack[sp]->val;
+while (curval < threshold) {
+if (curval >= node->val) {
+if (!node->vis) {
+node->vis = 1;
+put_bits(s, 1, 1);
+}
+break;
+}
+put_bits(s, 0, 1);
+curval++;
+}
+node->temp_val = curval;
+if (sp < 0)
+break;
+node = stack[sp--];
 }
 }
 
diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 26e09fbe38..3d3e7ec313 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -88,6 +88,7 @@ void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
 
 for (i = 0; i < siz; i++) {
 t[i].val = 0;
+t[i].temp_val = 0;
 t[i].vis = 0;
 }
 }
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index c3437b02fe..ad58b1ae88 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -127,6 +127,7 @@ typedef struct Jpeg2000T1Context {
 
 typedef struct Jpeg2000TgtNode {
 uint8_t val;
+uint8_t temp_val;
 uint8_t vis;
 struct Jpeg2000TgtNode *parent;
 } Jpeg2000TgtNode;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH v2 2/4] libavcodec/j2kenc: Fix tag tree coding

2020-08-23 Thread gautamramk
From: Gautam Ramakrishnan 

This patch fixes tag tree coding for JPEG2000
encoder.
---
 libavcodec/j2kenc.c   | 41 -
 libavcodec/jpeg2000.c |  1 +
 libavcodec/jpeg2000.h |  1 +
 3 files changed, 26 insertions(+), 17 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 16863f8e8c..d9777fe07f 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -242,27 +242,34 @@ static void j2k_flush(Jpeg2000EncoderContext *s)
 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, 
int threshold)
 {
 Jpeg2000TgtNode *stack[30];
-int sp = 1, curval = 0;
-stack[0] = node;
+int sp = -1, curval = 0;
 
-node = node->parent;
-while(node){
-if (node->vis){
-curval = node->val;
-break;
-}
-node->vis++;
-stack[sp++] = node;
+while(node->parent){
+stack[++sp] = node;
 node = node->parent;
 }
-while(--sp >= 0){
-if (stack[sp]->val >= threshold){
-put_bits(s, 0, threshold - curval);
-break;
+
+while (1) {
+if (curval > node->temp_val)
+node->temp_val = curval;
+else {
+curval = node->temp_val;
 }
-put_bits(s, 0, stack[sp]->val - curval);
-put_bits(s, 1, 1);
-curval = stack[sp]->val;
+while (curval < threshold) {
+if (curval >= node->val) {
+if (!node->vis) {
+node->vis = 1;
+put_bits(s, 1, 1);
+}
+break;
+}
+put_bits(s, 0, 1);
+curval++;
+}
+node->temp_val = curval;
+if (sp < 0)
+break;
+node = stack[sp--];
 }
 }
 
diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 26e09fbe38..3d3e7ec313 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -88,6 +88,7 @@ void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
 
 for (i = 0; i < siz; i++) {
 t[i].val = 0;
+t[i].temp_val = 0;
 t[i].vis = 0;
 }
 }
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index c3437b02fe..ad58b1ae88 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -127,6 +127,7 @@ typedef struct Jpeg2000T1Context {
 
 typedef struct Jpeg2000TgtNode {
 uint8_t val;
+uint8_t temp_val;
 uint8_t vis;
 struct Jpeg2000TgtNode *parent;
 } Jpeg2000TgtNode;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH v2 3/4] libavcodec/jpeg2000: Modify cleanup

2020-08-23 Thread gautamramk
From: Gautam Ramakrishnan 

This patch makes the ff_jpeg2000_cleanup
function take in an extra parameter which
indicates whether it is called from the
encoder or decoder.
---
 libavcodec/j2kenc.c  | 2 +-
 libavcodec/jpeg2000.c| 2 +-
 libavcodec/jpeg2000.h| 2 +-
 libavcodec/jpeg2000dec.c | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index d9777fe07f..8699296434 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -1218,7 +1218,7 @@ static void cleanup(Jpeg2000EncoderContext *s)
 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
 for (compno = 0; compno < s->ncomponents; compno++){
 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
-ff_jpeg2000_cleanup(comp, codsty);
+ff_jpeg2000_cleanup(comp, codsty, 1);
 }
 av_freep(>tile[tileno].comp);
 }
diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 3d3e7ec313..70c25a0ca2 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -580,7 +580,7 @@ void ff_jpeg2000_reinit(Jpeg2000Component *comp, 
Jpeg2000CodingStyle *codsty)
 }
 }
 
-void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
+void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, 
int isencoder)
 {
 int reslevelno, bandno, precno;
 for (reslevelno = 0;
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index ad58b1ae88..fee9607e86 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -272,7 +272,7 @@ int ff_jpeg2000_init_component(Jpeg2000Component *comp,
 
 void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
 
-void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
+void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, 
int isencoder);
 
 static inline int needs_termination(int style, int passno) {
 if (style & JPEG2000_CBLK_BYPASS) {
diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 624542c2f8..c5192d007f 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -2123,7 +2123,7 @@ static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext 
*s)
 Jpeg2000Component *comp = s->tile[tileno].comp   + compno;
 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
 
-ff_jpeg2000_cleanup(comp, codsty);
+ff_jpeg2000_cleanup(comp, codsty, 0);
 }
 av_freep(>tile[tileno].comp);
 av_freep(>tile[tileno].packed_headers);
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH v2 1/4] libavcodec/jpeg2000: Make tag tree functions non static

2020-08-23 Thread gautamramk
From: Gautam Ramakrishnan 

This patch makes the tag_tree_zero() and tag_tree_size()
functions non static and callable from other files.
---
 libavcodec/jpeg2000.c | 12 ++--
 libavcodec/jpeg2000.h |  3 +++
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 1aca31ffa4..26e09fbe38 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -39,7 +39,7 @@
 /* tag tree routines */
 
 /* allocate the memory for tag tree */
-static int32_t tag_tree_size(int w, int h)
+int32_t ff_tag_tree_size(int w, int h)
 {
 int64_t res = 0;
 while (w > 1 || h > 1) {
@@ -57,7 +57,7 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int 
h)
 Jpeg2000TgtNode *res, *t, *t2;
 int32_t tt_size;
 
-tt_size = tag_tree_size(w, h);
+tt_size = ff_tag_tree_size(w, h);
 
 t = res = av_mallocz_array(tt_size, sizeof(*t));
 if (!res)
@@ -82,9 +82,9 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int 
h)
 return res;
 }
 
-static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
 {
-int i, siz = tag_tree_size(w, h);
+int i, siz = ff_tag_tree_size(w, h);
 
 for (i = 0; i < siz; i++) {
 t[i].val = 0;
@@ -567,8 +567,8 @@ void ff_jpeg2000_reinit(Jpeg2000Component *comp, 
Jpeg2000CodingStyle *codsty)
 Jpeg2000Band *band = rlevel->band + bandno;
 for(precno = 0; precno < rlevel->num_precincts_x * 
rlevel->num_precincts_y; precno++) {
 Jpeg2000Prec *prec = band->prec + precno;
-tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
-tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * 
prec->nb_codeblocks_height; cblkno++) {
 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
 cblk->length = 0;
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index 5b0627c3dc..c3437b02fe 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -290,4 +290,7 @@ static inline int needs_termination(int style, int passno) {
 return 0;
 }
 
+int32_t ff_tag_tree_size(int w, int h);
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h);
+
 #endif /* AVCODEC_JPEG2000_H */
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH v2 4/4] libavcodec/j2kenc: Support for multiple layers

2020-08-23 Thread gautamramk
From: Gautam Ramakrishnan 

This patch allows setting a compression ratio and to
set multiple layers. The user has to input a compression
ratio for each layer.
The per layer compression ration can be set as follows:
-layer_rates "r1,r2,...rn"
for to create 'n' layers.
---
 libavcodec/j2kenc.c  | 492 +++
 libavcodec/jpeg2000.c|  13 +-
 libavcodec/jpeg2000.h|  10 +
 tests/ref/vsynth/vsynth1-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth1-jpeg2000-97 |   6 +-
 tests/ref/vsynth/vsynth2-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth2-jpeg2000-97 |   8 +-
 tests/ref/vsynth/vsynth3-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth3-jpeg2000-97 |   8 +-
 tests/ref/vsynth/vsynth_lena-jpeg2000|   8 +-
 tests/ref/vsynth/vsynth_lena-jpeg2000-97 |   8 +-
 11 files changed, 475 insertions(+), 102 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 8699296434..5f72d924d9 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -32,6 +32,7 @@
  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  * Copyright (c) 2005, Herve Drolon, FreeImage Team
  * Copyright (c) 2007, Callum Lerwick 
+ * Copyright (c) 2020, Gautam Ramakrishnan 
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -100,6 +101,7 @@ static const int dwt_norms[2][4][10] = { // 
[dwt_type][band][rlevel] (multiplied
 
 typedef struct {
Jpeg2000Component *comp;
+   double *layer_rates;
 } Jpeg2000Tile;
 
 typedef struct {
@@ -126,12 +128,16 @@ typedef struct {
 Jpeg2000QuantStyle  qntsty;
 
 Jpeg2000Tile *tile;
+int layer_rates[100];
+uint8_t compression_rate_enc; ///< Is compression done using compression 
ratio?
 
 int format;
 int pred;
 int sop;
 int eph;
 int prog;
+int nlayers;
+char *lr_str;
 } Jpeg2000EncoderContext;
 
 
@@ -332,7 +338,7 @@ static int put_cod(Jpeg2000EncoderContext *s)
 bytestream_put_byte(>buf, scod);  // Scod
 // SGcod
 bytestream_put_byte(>buf, s->prog); // progression level
-bytestream_put_be16(>buf, 1); // num of layers
+bytestream_put_be16(>buf, s->nlayers); // num of layers
 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
 bytestream_put_byte(>buf, 0); // unspecified
 }else{
@@ -411,6 +417,31 @@ static uint8_t *put_sot(Jpeg2000EncoderContext *s, int 
tileno)
 return psotptr;
 }
 
+static void compute_rates(Jpeg2000EncoderContext* s)
+{
+int i, j;
+int layno, compno;
+for (i = 0; i < s->numYtiles; i++) {
+for (j = 0; j < s->numXtiles; j++) {
+Jpeg2000Tile *tile = >tile[s->numXtiles * i + j];
+for (compno = 0; compno < s->ncomponents; compno++) {
+int tilew = tile->comp[compno].coord[0][1] - 
tile->comp[compno].coord[0][0];
+int tileh = tile->comp[compno].coord[1][1] - 
tile->comp[compno].coord[1][0];
+int scale = (compno?1 << s->chroma_shift[0]:1) * (compno?1 << 
s->chroma_shift[1]:1);
+for (layno = 0; layno < s->nlayers; layno++) {
+if (s->layer_rates[layno] > 0) {
+tile->layer_rates[layno] += (double)(tilew * tileh) * 
s->ncomponents * s->cbps[compno] /
+
(double)(s->layer_rates[layno] * 8 * scale);
+} else {
+tile->layer_rates[layno] = 0.0;
+}
+}
+}
+}
+}
+
+}
+
 /**
  * compute the sizes of tiles, resolution levels, bands, etc.
  * allocate memory for them
@@ -435,6 +466,11 @@ static int init_tiles(Jpeg2000EncoderContext *s)
 tile->comp = av_mallocz_array(s->ncomponents, 
sizeof(Jpeg2000Component));
 if (!tile->comp)
 return AVERROR(ENOMEM);
+
+tile->layer_rates = av_mallocz_array(s->nlayers, 
sizeof(*tile->layer_rates));
+if (!tile->layer_rates)
+return AVERROR(ENOMEM);
+
 for (compno = 0; compno < s->ncomponents; compno++){
 Jpeg2000Component *comp = tile->comp + compno;
 int ret, i, j;
@@ -459,6 +495,7 @@ static int init_tiles(Jpeg2000EncoderContext *s)
 return ret;
 }
 }
+compute_rates(s);
 return 0;
 }
 
@@ -701,6 +738,8 @@ static void encode_cblk(Jpeg2000EncoderContext *s, 
Jpeg2000T1Context *t1, Jpeg20
 }
 
 cblk->passes[passno].rate = ff_mqc_flush_to(>mqc, 
cblk->passes[passno].flushed, >passes[passno].flushed_len);
+cblk->passes[passno].rate -= cblk->passes[passno].flushed_len;
+
 wmsedec += (int64_t)nmsedec << (2*bpno);
 cblk->passes[passno].disto = wmsedec;
 
@@ -712,8 +751,10 @@ static void encode_cblk(Jpeg2000EncoderContext *s, 
Jpeg2000T1Context *t1, Jpeg20
 cblk->npasses = passno;
 cblk->ninclpasses = passno;
 

[FFmpeg-devel] [RFC PATCH 4/4] libavcodec/j2kenc: Support for multiple layers

2020-08-19 Thread gautamramk
From: Gautam Ramakrishnan 

This patch allows setting a compression ratio and to
set multiple layers. The user has to input a compression
ratio for each layer.
The per layer compression ration can be set as follows:
-layer_rates "r1,r2,...rn"
for to create 'n' layers.
---
 libavcodec/j2kenc.c   | 443 ++
 libavcodec/jpeg2000.c |  13 +-
 libavcodec/jpeg2000.h |  10 +
 3 files changed, 384 insertions(+), 82 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 8699296434..b09db36c14 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -32,6 +32,7 @@
  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  * Copyright (c) 2005, Herve Drolon, FreeImage Team
  * Copyright (c) 2007, Callum Lerwick 
+ * Copyright (c) 2020, Gautam Ramakrishnan 
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -100,6 +101,7 @@ static const int dwt_norms[2][4][10] = { // 
[dwt_type][band][rlevel] (multiplied
 
 typedef struct {
Jpeg2000Component *comp;
+   double *layer_rates;
 } Jpeg2000Tile;
 
 typedef struct {
@@ -126,12 +128,15 @@ typedef struct {
 Jpeg2000QuantStyle  qntsty;
 
 Jpeg2000Tile *tile;
+int layer_rates[100];
 
 int format;
 int pred;
 int sop;
 int eph;
 int prog;
+int nlayers;
+char *lr_str;
 } Jpeg2000EncoderContext;
 
 
@@ -332,7 +337,7 @@ static int put_cod(Jpeg2000EncoderContext *s)
 bytestream_put_byte(>buf, scod);  // Scod
 // SGcod
 bytestream_put_byte(>buf, s->prog); // progression level
-bytestream_put_be16(>buf, 1); // num of layers
+bytestream_put_be16(>buf, s->nlayers); // num of layers
 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
 bytestream_put_byte(>buf, 0); // unspecified
 }else{
@@ -411,6 +416,31 @@ static uint8_t *put_sot(Jpeg2000EncoderContext *s, int 
tileno)
 return psotptr;
 }
 
+static void compute_rates(Jpeg2000EncoderContext* s)
+{
+int i, j;
+int layno, compno;
+for (i = 0; i < s->numYtiles; i++) {
+for (j = 0; j < s->numXtiles; j++) {
+Jpeg2000Tile *tile = >tile[s->numXtiles * i + j];
+for (compno = 0; compno < s->ncomponents; compno++) {
+int tilew = tile->comp[compno].coord[0][1] - 
tile->comp[compno].coord[0][0];
+int tileh = tile->comp[compno].coord[1][1] - 
tile->comp[compno].coord[1][0];
+int scale = (compno?1 << s->chroma_shift[0]:1) * (compno?1 << 
s->chroma_shift[1]:1);
+for (layno = 0; layno < s->nlayers; layno++) {
+if (s->layer_rates[layno] > 0.0f) {
+tile->layer_rates[layno] += (double)(tilew * tileh) * 
s->ncomponents * s->cbps[compno] /
+
(double)(s->layer_rates[layno] * 8 * scale);
+} else {
+tile->layer_rates[layno] = 0.0f;
+}
+}
+}
+}
+}
+
+}
+
 /**
  * compute the sizes of tiles, resolution levels, bands, etc.
  * allocate memory for them
@@ -448,6 +478,10 @@ static int init_tiles(Jpeg2000EncoderContext *s)
 for (j = 0; j < 2; j++)
 comp->coord[i][j] = comp->coord_o[i][j] = 
ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
 
+tile->layer_rates = av_mallocz_array(s->ncomponents, 
sizeof(*tile->layer_rates));
+if (!tile->layer_rates)
+return AVERROR(ENOMEM);
+
 if ((ret = ff_jpeg2000_init_component(comp,
 codsty,
 qntsty,
@@ -459,6 +493,7 @@ static int init_tiles(Jpeg2000EncoderContext *s)
 return ret;
 }
 }
+compute_rates(s);
 return 0;
 }
 
@@ -701,6 +736,8 @@ static void encode_cblk(Jpeg2000EncoderContext *s, 
Jpeg2000T1Context *t1, Jpeg20
 }
 
 cblk->passes[passno].rate = ff_mqc_flush_to(>mqc, 
cblk->passes[passno].flushed, >passes[passno].flushed_len);
+cblk->passes[passno].rate -= cblk->passes[passno].flushed_len;
+
 wmsedec += (int64_t)nmsedec << (2*bpno);
 cblk->passes[passno].disto = wmsedec;
 
@@ -733,10 +770,12 @@ static void putnumpasses(Jpeg2000EncoderContext *s, int n)
 }
 
 
-static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, 
int precno,
-  uint8_t *expn, int numgbits, int packetno)
+static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, 
int layno,
+ int precno, uint8_t *expn, int numgbits, int packetno,
+ int nlayers)
 {
 int bandno, empty = 1;
+int i;
 // init bitstream
 *s->buf = 0;
 s->bit_index = 0;
@@ -748,18 +787,61 @@ static int encode_packet(Jpeg2000EncoderContext *s, 

[FFmpeg-devel] [RFC PATCH 1/4] libavcodec/jpeg2000: Make tag tree functions non static

2020-08-19 Thread gautamramk
From: Gautam Ramakrishnan 

This patch makes the tag_tree_zero() and tag_tree_size()
functions non static and callable from other files.
---
 libavcodec/jpeg2000.c | 12 ++--
 libavcodec/jpeg2000.h |  3 +++
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 1aca31ffa4..26e09fbe38 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -39,7 +39,7 @@
 /* tag tree routines */
 
 /* allocate the memory for tag tree */
-static int32_t tag_tree_size(int w, int h)
+int32_t ff_tag_tree_size(int w, int h)
 {
 int64_t res = 0;
 while (w > 1 || h > 1) {
@@ -57,7 +57,7 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int 
h)
 Jpeg2000TgtNode *res, *t, *t2;
 int32_t tt_size;
 
-tt_size = tag_tree_size(w, h);
+tt_size = ff_tag_tree_size(w, h);
 
 t = res = av_mallocz_array(tt_size, sizeof(*t));
 if (!res)
@@ -82,9 +82,9 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int 
h)
 return res;
 }
 
-static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
 {
-int i, siz = tag_tree_size(w, h);
+int i, siz = ff_tag_tree_size(w, h);
 
 for (i = 0; i < siz; i++) {
 t[i].val = 0;
@@ -567,8 +567,8 @@ void ff_jpeg2000_reinit(Jpeg2000Component *comp, 
Jpeg2000CodingStyle *codsty)
 Jpeg2000Band *band = rlevel->band + bandno;
 for(precno = 0; precno < rlevel->num_precincts_x * 
rlevel->num_precincts_y; precno++) {
 Jpeg2000Prec *prec = band->prec + precno;
-tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
-tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->zerobits, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
+ff_tag_tree_zero(prec->cblkincl, prec->nb_codeblocks_width, 
prec->nb_codeblocks_height);
 for (cblkno = 0; cblkno < prec->nb_codeblocks_width * 
prec->nb_codeblocks_height; cblkno++) {
 Jpeg2000Cblk *cblk = prec->cblk + cblkno;
 cblk->length = 0;
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index 5b0627c3dc..c3437b02fe 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -290,4 +290,7 @@ static inline int needs_termination(int style, int passno) {
 return 0;
 }
 
+int32_t ff_tag_tree_size(int w, int h);
+void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h);
+
 #endif /* AVCODEC_JPEG2000_H */
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH 2/4] libavcodec/j2kenc: Fix tag tree coding

2020-08-19 Thread gautamramk
From: Gautam Ramakrishnan 

This patch fixes tag tree coding for JPEG2000
encoder.
---
 libavcodec/j2kenc.c   | 41 -
 libavcodec/jpeg2000.c |  1 +
 libavcodec/jpeg2000.h |  1 +
 3 files changed, 26 insertions(+), 17 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 16863f8e8c..d9777fe07f 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -242,27 +242,34 @@ static void j2k_flush(Jpeg2000EncoderContext *s)
 static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, 
int threshold)
 {
 Jpeg2000TgtNode *stack[30];
-int sp = 1, curval = 0;
-stack[0] = node;
+int sp = -1, curval = 0;
 
-node = node->parent;
-while(node){
-if (node->vis){
-curval = node->val;
-break;
-}
-node->vis++;
-stack[sp++] = node;
+while(node->parent){
+stack[++sp] = node;
 node = node->parent;
 }
-while(--sp >= 0){
-if (stack[sp]->val >= threshold){
-put_bits(s, 0, threshold - curval);
-break;
+
+while (1) {
+if (curval > node->temp_val)
+node->temp_val = curval;
+else {
+curval = node->temp_val;
 }
-put_bits(s, 0, stack[sp]->val - curval);
-put_bits(s, 1, 1);
-curval = stack[sp]->val;
+while (curval < threshold) {
+if (curval >= node->val) {
+if (!node->vis) {
+node->vis = 1;
+put_bits(s, 1, 1);
+}
+break;
+}
+put_bits(s, 0, 1);
+curval++;
+}
+node->temp_val = curval;
+if (sp < 0)
+break;
+node = stack[sp--];
 }
 }
 
diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 26e09fbe38..3d3e7ec313 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -88,6 +88,7 @@ void ff_tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
 
 for (i = 0; i < siz; i++) {
 t[i].val = 0;
+t[i].temp_val = 0;
 t[i].vis = 0;
 }
 }
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index c3437b02fe..ad58b1ae88 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -127,6 +127,7 @@ typedef struct Jpeg2000T1Context {
 
 typedef struct Jpeg2000TgtNode {
 uint8_t val;
+uint8_t temp_val;
 uint8_t vis;
 struct Jpeg2000TgtNode *parent;
 } Jpeg2000TgtNode;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH 3/4] libavcodec/jpeg2000: Modify cleanup

2020-08-19 Thread gautamramk
From: Gautam Ramakrishnan 

This patch makes the ff_jpeg2000_cleanup
function take in an extra parameter which
indicates whether it is called from the
encoder or decoder.
---
 libavcodec/j2kenc.c  | 2 +-
 libavcodec/jpeg2000.c| 2 +-
 libavcodec/jpeg2000.h| 2 +-
 libavcodec/jpeg2000dec.c | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index d9777fe07f..8699296434 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -1218,7 +1218,7 @@ static void cleanup(Jpeg2000EncoderContext *s)
 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
 for (compno = 0; compno < s->ncomponents; compno++){
 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
-ff_jpeg2000_cleanup(comp, codsty);
+ff_jpeg2000_cleanup(comp, codsty, 1);
 }
 av_freep(>tile[tileno].comp);
 }
diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 3d3e7ec313..70c25a0ca2 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -580,7 +580,7 @@ void ff_jpeg2000_reinit(Jpeg2000Component *comp, 
Jpeg2000CodingStyle *codsty)
 }
 }
 
-void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
+void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, 
int isencoder)
 {
 int reslevelno, bandno, precno;
 for (reslevelno = 0;
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index ad58b1ae88..fee9607e86 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -272,7 +272,7 @@ int ff_jpeg2000_init_component(Jpeg2000Component *comp,
 
 void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
 
-void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
+void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, 
int isencoder);
 
 static inline int needs_termination(int style, int passno) {
 if (style & JPEG2000_CBLK_BYPASS) {
diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 624542c2f8..c5192d007f 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -2123,7 +2123,7 @@ static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext 
*s)
 Jpeg2000Component *comp = s->tile[tileno].comp   + compno;
 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
 
-ff_jpeg2000_cleanup(comp, codsty);
+ff_jpeg2000_cleanup(comp, codsty, 0);
 }
 av_freep(>tile[tileno].comp);
 av_freep(>tile[tileno].packed_headers);
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 3/4] libavcodec/jpeg2000: Modify cleanup

2020-08-18 Thread gautamramk
From: Gautam Ramakrishnan 

This patch makes the ff_jpeg2000_cleanup
function take in an extra parameter which
indicates whether it is called from the
encoder or decoder.
---
 libavcodec/j2kenc.c  | 2 +-
 libavcodec/jpeg2000.c| 2 +-
 libavcodec/jpeg2000.h| 2 +-
 libavcodec/jpeg2000dec.c | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 1c31e48d61..264bae3f73 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -1218,7 +1218,7 @@ static void cleanup(Jpeg2000EncoderContext *s)
 for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){
 for (compno = 0; compno < s->ncomponents; compno++){
 Jpeg2000Component *comp = s->tile[tileno].comp + compno;
-ff_jpeg2000_cleanup(comp, codsty);
+ff_jpeg2000_cleanup(comp, codsty, 1);
 }
 av_freep(>tile[tileno].comp);
 }
diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 7a34a36121..88e6ebc8b3 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -580,7 +580,7 @@ void ff_jpeg2000_reinit(Jpeg2000Component *comp, 
Jpeg2000CodingStyle *codsty)
 }
 }
 
-void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty)
+void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, 
int isencoder)
 {
 int reslevelno, bandno, precno;
 for (reslevelno = 0;
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index e9f4a51261..611aea1665 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -272,7 +272,7 @@ int ff_jpeg2000_init_component(Jpeg2000Component *comp,
 
 void ff_jpeg2000_reinit(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
 
-void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty);
+void ff_jpeg2000_cleanup(Jpeg2000Component *comp, Jpeg2000CodingStyle *codsty, 
int isencoder);
 
 static inline int needs_termination(int style, int passno) {
 if (style & JPEG2000_CBLK_BYPASS) {
diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 624542c2f8..c5192d007f 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -2123,7 +2123,7 @@ static void jpeg2000_dec_cleanup(Jpeg2000DecoderContext 
*s)
 Jpeg2000Component *comp = s->tile[tileno].comp   + compno;
 Jpeg2000CodingStyle *codsty = s->tile[tileno].codsty + compno;
 
-ff_jpeg2000_cleanup(comp, codsty);
+ff_jpeg2000_cleanup(comp, codsty, 0);
 }
 av_freep(>tile[tileno].comp);
 av_freep(>tile[tileno].packed_headers);
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 4/4] libavcodec/j2kenc: Support for multiple layers

2020-08-18 Thread gautamramk
From: Gautam Ramakrishnan 

This patch allows setting a compression ratio and to
set multiple layers. The user has to input a compression
ratio for each layer.
The per layer compression ration can be set as follows:
-layer_rates "r1,r2,...rn"
for to create 'n' layers.
---
 libavcodec/j2kenc.c   | 441 ++
 libavcodec/jpeg2000.c |  14 +-
 libavcodec/jpeg2000.h |  10 +
 3 files changed, 382 insertions(+), 83 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 264bae3f73..3882fa8474 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -100,6 +100,7 @@ static const int dwt_norms[2][4][10] = { // 
[dwt_type][band][rlevel] (multiplied
 
 typedef struct {
Jpeg2000Component *comp;
+   double *layer_rates;
 } Jpeg2000Tile;
 
 typedef struct {
@@ -126,12 +127,15 @@ typedef struct {
 Jpeg2000QuantStyle  qntsty;
 
 Jpeg2000Tile *tile;
+int layer_rates[100];
 
 int format;
 int pred;
 int sop;
 int eph;
 int prog;
+int nlayers;
+char *lr_str;
 } Jpeg2000EncoderContext;
 
 
@@ -239,7 +243,7 @@ static void j2k_flush(Jpeg2000EncoderContext *s)
 /* tag tree routines */
 
 /** code the value stored in node */
-static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, 
int threshold, int log)
+static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, 
int threshold)
 {
 Jpeg2000TgtNode *stack[30];
 int sp = -1, curval = 0;
@@ -332,7 +336,7 @@ static int put_cod(Jpeg2000EncoderContext *s)
 bytestream_put_byte(>buf, scod);  // Scod
 // SGcod
 bytestream_put_byte(>buf, s->prog); // progression level
-bytestream_put_be16(>buf, 1); // num of layers
+bytestream_put_be16(>buf, s->nlayers); // num of layers
 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
 bytestream_put_byte(>buf, 0); // unspecified
 }else{
@@ -411,6 +415,28 @@ static uint8_t *put_sot(Jpeg2000EncoderContext *s, int 
tileno)
 return psotptr;
 }
 
+static void compute_rates(Jpeg2000EncoderContext* s)
+{
+int i, j;
+int layno;
+for (i = 0; i < s->numYtiles; i++) {
+for (j = 0; j < s->numXtiles; j++) {
+Jpeg2000Tile *tile = >tile[s->numXtiles * i + j];
+int tilew = tile->comp->coord[0][1] - tile->comp->coord[0][0];
+int tileh = tile->comp->coord[1][1] - tile->comp->coord[1][0];
+for (layno = 0; layno < s->nlayers; layno++) {
+if (s->layer_rates[layno] > 0.0f) {
+tile->layer_rates[layno] = (double)(tilew * tileh) * 
s->ncomponents * s->cbps[0] / (s->layer_rates[layno] * 8);
+//av_log(s, AV_LOG_WARNING, "Layer %d rate is %lf\n", 
layno, tile->layer_rates[layno]);
+} else {
+tile->layer_rates[layno] = 0;
+}
+}
+}
+}
+
+}
+
 /**
  * compute the sizes of tiles, resolution levels, bands, etc.
  * allocate memory for them
@@ -448,6 +474,10 @@ static int init_tiles(Jpeg2000EncoderContext *s)
 for (j = 0; j < 2; j++)
 comp->coord[i][j] = comp->coord_o[i][j] = 
ff_jpeg2000_ceildivpow2(comp->coord[i][j], s->chroma_shift[i]);
 
+tile->layer_rates = av_mallocz_array(s->ncomponents, 
sizeof(*tile->layer_rates));
+if (!tile->layer_rates)
+return AVERROR(ENOMEM);
+
 if ((ret = ff_jpeg2000_init_component(comp,
 codsty,
 qntsty,
@@ -459,6 +489,7 @@ static int init_tiles(Jpeg2000EncoderContext *s)
 return ret;
 }
 }
+compute_rates(s);
 return 0;
 }
 
@@ -701,6 +732,8 @@ static void encode_cblk(Jpeg2000EncoderContext *s, 
Jpeg2000T1Context *t1, Jpeg20
 }
 
 cblk->passes[passno].rate = ff_mqc_flush_to(>mqc, 
cblk->passes[passno].flushed, >passes[passno].flushed_len);
+cblk->passes[passno].rate -= cblk->passes[passno].flushed_len;
+
 wmsedec += (int64_t)nmsedec << (2*bpno);
 cblk->passes[passno].disto = wmsedec;
 
@@ -733,10 +766,12 @@ static void putnumpasses(Jpeg2000EncoderContext *s, int n)
 }
 
 
-static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, 
int precno,
-  uint8_t *expn, int numgbits, int packetno)
+static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, 
int layno,
+ int precno, uint8_t *expn, int numgbits, int packetno,
+ int nlayers)
 {
 int bandno, empty = 1;
+int i;
 // init bitstream
 *s->buf = 0;
 s->bit_index = 0;
@@ -748,18 +783,61 @@ static int encode_packet(Jpeg2000EncoderContext *s, 
Jpeg2000ResLevel *rlevel, in
 }
 // header
 
+if (!layno) {
+for (bandno = 0; bandno < rlevel->nbands; bandno++){
+Jpeg2000Band 

[FFmpeg-devel] [PATCH 2/4] libavcodec/j2kenc: Fix tag tree coding

2020-08-18 Thread gautamramk
From: Gautam Ramakrishnan 

This patch fixes tag tree coding for JPEG2000
encoder.
---
 libavcodec/j2kenc.c   | 43 +--
 libavcodec/jpeg2000.c |  1 +
 libavcodec/jpeg2000.h |  1 +
 3 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 16863f8e8c..1c31e48d61 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -239,30 +239,37 @@ static void j2k_flush(Jpeg2000EncoderContext *s)
 /* tag tree routines */
 
 /** code the value stored in node */
-static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, 
int threshold)
+static void tag_tree_code(Jpeg2000EncoderContext *s, Jpeg2000TgtNode *node, 
int threshold, int log)
 {
 Jpeg2000TgtNode *stack[30];
-int sp = 1, curval = 0;
-stack[0] = node;
+int sp = -1, curval = 0;
 
-node = node->parent;
-while(node){
-if (node->vis){
-curval = node->val;
-break;
-}
-node->vis++;
-stack[sp++] = node;
+while(node->parent){
+stack[++sp] = node;
 node = node->parent;
 }
-while(--sp >= 0){
-if (stack[sp]->val >= threshold){
-put_bits(s, 0, threshold - curval);
-break;
+
+while (1) {
+if (curval > node->temp_val)
+node->temp_val = curval;
+else {
+curval = node->temp_val;
 }
-put_bits(s, 0, stack[sp]->val - curval);
-put_bits(s, 1, 1);
-curval = stack[sp]->val;
+while (curval < threshold) {
+if (curval >= node->val) {
+if (!node->vis) {
+node->vis = 1;
+put_bits(s, 1, 1);
+}
+break;
+}
+put_bits(s, 0, 1);
+curval++;
+}
+node->temp_val = curval;
+if (sp < 0)
+break;
+node = stack[sp--];
 }
 }
 
diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 35e21f54a4..7a34a36121 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -88,6 +88,7 @@ void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
 
 for (i = 0; i < siz; i++) {
 t[i].val = 0;
+t[i].temp_val = 0;
 t[i].vis = 0;
 }
 }
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index 4ea7daa5da..e9f4a51261 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -127,6 +127,7 @@ typedef struct Jpeg2000T1Context {
 
 typedef struct Jpeg2000TgtNode {
 uint8_t val;
+uint8_t temp_val;
 uint8_t vis;
 struct Jpeg2000TgtNode *parent;
 } Jpeg2000TgtNode;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 1/4] libavcodec/jpeg2000: Make tag tree functions non static

2020-08-18 Thread gautamramk
From: Gautam Ramakrishnan 

This patch makes the tag_tree_zero() and tag_tree_size()
functions non static and callable from other files.
---
 libavcodec/jpeg2000.c | 4 ++--
 libavcodec/jpeg2000.h | 3 +++
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 1aca31ffa4..35e21f54a4 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -39,7 +39,7 @@
 /* tag tree routines */
 
 /* allocate the memory for tag tree */
-static int32_t tag_tree_size(int w, int h)
+int32_t tag_tree_size(int w, int h)
 {
 int64_t res = 0;
 while (w > 1 || h > 1) {
@@ -82,7 +82,7 @@ static Jpeg2000TgtNode *ff_jpeg2000_tag_tree_init(int w, int 
h)
 return res;
 }
 
-static void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
+void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h)
 {
 int i, siz = tag_tree_size(w, h);
 
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index 5b0627c3dc..4ea7daa5da 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -290,4 +290,7 @@ static inline int needs_termination(int style, int passno) {
 return 0;
 }
 
+int32_t tag_tree_size(int w, int h);
+void tag_tree_zero(Jpeg2000TgtNode *t, int w, int h);
+
 #endif /* AVCODEC_JPEG2000_H */
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v2] doc/encoders: Add all options for JPEG2000 encoder

2020-08-10 Thread gautamramk
From: Gautam Ramakrishnan 

This patch updates the documentation by adding all options
for JPEG2000 encoder.
---
 doc/encoders.texi | 36 +++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/doc/encoders.texi b/doc/encoders.texi
index de0472e225..426baa8033 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -1365,10 +1365,44 @@ can be selected with @code{-pred 1}.
 @subsection Options
 
 @table @option
-@item format
+@item format @var{integer}
 Can be set to either @code{j2k} or @code{jp2} (the default) that
 makes it possible to store non-rgb pix_fmts.
 
+@item tile_width @var{integer}
+Sets tile width. Default is 256.
+
+@item tile_height @var{integer}
+Sets tile height. Default is 256.
+
+@item pred @var{integer}
+Allows setting the DWT type
+@table @option
+@item dwt97int (Lossy)
+@item dwt53 (Lossless)
+@end table
+Default is @code{dwt97int}
+
+@item sop @var{boolean}
+Setting this to 1 will make the encoder to use SOP marker at the
+start of every packet. Set to 0 by default.
+
+@item eph @var{boolean}
+Setting this to 1 will make the encoder to use the EPH flag at the
+end of every packet header. Set to 0 by default.
+
+@item prog @var{integer}
+Sets the progression order to be used by the encoder.
+Possible values are:
+@table @option
+@item lrcp
+@item rlcp
+@item rpcl
+@item pcrl
+@item cprl
+@end table
+Set to @code{lrcp} by default.
+
 @end table
 
 @section librav1e
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH] doc/encoders: Add all options for JPEG2000 encoder

2020-08-09 Thread gautamramk
From: Gautam Ramakrishnan 

This patch updates the documentation by adding all options
for JPEG2000 encoder.
---
 doc/encoders.texi | 20 
 1 file changed, 20 insertions(+)

diff --git a/doc/encoders.texi b/doc/encoders.texi
index de0472e225..aaac026709 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -1369,6 +1369,26 @@ can be selected with @code{-pred 1}.
 Can be set to either @code{j2k} or @code{jp2} (the default) that
 makes it possible to store non-rgb pix_fmts.
 
+@item tile_width/tile_height
+Allows setting of tile width and height.
+
+@item pred
+Allows setting the DWT type, i.e lossy
+DWT 97 (@code{dwt97int}) or lossless DWT 53 (@code{dwt53}).
+
+@item sop
+Setting this to 1 will make the encoder to use SOP marker at the
+start of every packet. Set to 0 by default.
+
+@item eph
+Setting this to 1 will make the encoder to use the EPH flag at the
+end of every packet header. Set to 0 by default.
+
+@item prog
+Sets the progression order to be used by the encoder.
+Possible values are @code{lrcp}, @code{rlcp}, @code{rpcl},
+@code{pcrl} and @code{cprl}. Set to @code{lrcp} by default.
+
 @end table
 
 @section librav1e
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH] libavcodec/j2kenc: Support for all Progression orders

2020-08-06 Thread gautamramk
From: Gautam Ramakrishnan 

This patch allows for selecting the progression order
in the j2k encoder. However, all components and resolution
levels will use the same progression order and will not
feature the use of progression order change markers.
---
 libavcodec/j2kenc.c | 223 +++-
 1 file changed, 222 insertions(+), 1 deletion(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index fad013521b..16863f8e8c 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -131,6 +131,7 @@ typedef struct {
 int pred;
 int sop;
 int eph;
+int prog;
 } Jpeg2000EncoderContext;
 
 
@@ -323,7 +324,7 @@ static int put_cod(Jpeg2000EncoderContext *s)
 scod |= JPEG2000_CSTY_EPH;
 bytestream_put_byte(>buf, scod);  // Scod
 // SGcod
-bytestream_put_byte(>buf, 0); // progression level
+bytestream_put_byte(>buf, s->prog); // progression level
 bytestream_put_be16(>buf, 1); // num of layers
 if(s->avctx->pix_fmt == AV_PIX_FMT_YUV444P){
 bytestream_put_byte(>buf, 0); // unspecified
@@ -837,8 +838,21 @@ static int encode_packets(Jpeg2000EncoderContext *s, 
Jpeg2000Tile *tile, int til
 Jpeg2000CodingStyle *codsty = >codsty;
 Jpeg2000QuantStyle  *qntsty = >qntsty;
 int packetno = 0;
+int step_x, step_y;
+int x, y;
+int tile_coord[2][2];
+int col = tileno % s->numXtiles;
+int row = tileno / s->numXtiles;
+
+tile_coord[0][0] = col * s->tile_width;
+tile_coord[0][1] = FFMIN(tile_coord[0][0] + s->tile_width, s->width);
+tile_coord[1][0] = row * s->tile_height;
+tile_coord[1][1] = FFMIN(tile_coord[1][0] + s->tile_height, s->height);
+
 av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
 // lay-rlevel-comp-pos progression
+switch (s->prog) {
+case JPEG2000_PGOD_LRCP:
 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
 for (compno = 0; compno < s->ncomponents; compno++){
 int precno;
@@ -850,6 +864,207 @@ static int encode_packets(Jpeg2000EncoderContext *s, 
Jpeg2000Tile *tile, int til
 }
 }
 }
+break;
+case JPEG2000_PGOD_RLCP:
+for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
+for (compno = 0; compno < s->ncomponents; compno++){
+int precno;
+Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel 
+ reslevelno;
+for (precno = 0; precno < reslevel->num_precincts_x * 
reslevel->num_precincts_y; precno++){
+if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + 
(reslevelno ? 3*reslevelno-2 : 0),
+  qntsty->nguardbits, packetno++)) < 0)
+return ret;
+}
+}
+}
+break;
+case JPEG2000_PGOD_RPCL:
+for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++) {
+int precno;
+step_x = 30;
+step_y = 30;
+for (compno = 0; compno < s->ncomponents; compno++) {
+Jpeg2000Component *comp = tile->comp + compno;
+if (reslevelno < codsty->nreslevels) {
+uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; //  
==> N_L - r
+Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
+step_x = FFMIN(step_x, rlevel->log2_prec_width  + 
reducedresno);
+step_y = FFMIN(step_y, rlevel->log2_prec_height + 
reducedresno);
+}
+}
+
+step_x = 1comp + compno;
+uint8_t reducedresno = codsty->nreslevels - 1 -reslevelno; 
//  ==> N_L - r
+Jpeg2000ResLevel *reslevel = comp->reslevel + reslevelno;
+int log_subsampling[2] = { compno?s->chroma_shift[0]:0, 
compno?s->chroma_shift[1]:0};
+unsigned prcx, prcy;
+int trx0, try0;
+
+trx0 = ff_jpeg2000_ceildivpow2(tile_coord[0][0], 
log_subsampling[0] + reducedresno);
+try0 = ff_jpeg2000_ceildivpow2(tile_coord[1][0], 
log_subsampling[1] + reducedresno);
+
+if (!(y % ((uint64_t)1 << (reslevel->log2_prec_height + 
reducedresno + log_subsampling[1])) == 0 ||
+(y == tile_coord[1][0] && (try0 << reducedresno) % (1U 
<< (reducedresno + reslevel->log2_prec_height)
+continue;
+
+if (!(x % ((uint64_t)1 << (reslevel->log2_prec_width + 
reducedresno + log_subsampling[0])) == 0 ||
+(x == tile_coord[0][0] && (trx0 << reducedresno) % (1U 
<< (reducedresno + reslevel->log2_prec_width)
+continue;
+
+// check if a precinct exists
+prcx   = ff_jpeg2000_ceildivpow2(x, log_subsampling[0] + 
reducedresno) >> reslevel->log2_prec_width;
+prcy   = 

[FFmpeg-devel] [PATCH] libavcodec/j2kenc: Allow Encoder to use SOP and EPH markers

2020-08-04 Thread gautamramk
From: Gautam Ramakrishnan 

This patch allows the encoder to use SOP and EPH
markers. This would be useful as these markers
provide better error detection mechanisms.
---
 libavcodec/j2kenc.c | 28 ++--
 1 file changed, 22 insertions(+), 6 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 5ebc1f3a99..fad013521b 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -129,6 +129,8 @@ typedef struct {
 
 int format;
 int pred;
+int sop;
+int eph;
 } Jpeg2000EncoderContext;
 
 
@@ -308,13 +310,18 @@ static int put_siz(Jpeg2000EncoderContext *s)
 static int put_cod(Jpeg2000EncoderContext *s)
 {
 Jpeg2000CodingStyle *codsty = >codsty;
+uint8_t scod = 0;
 
 if (s->buf_end - s->buf < 14)
 return -1;
 
 bytestream_put_be16(>buf, JPEG2000_COD);
 bytestream_put_be16(>buf, 12); // Lcod
-bytestream_put_byte(>buf, 0);  // Scod
+if (s->sop)
+scod |= JPEG2000_CSTY_SOP;
+if (s->eph)
+scod |= JPEG2000_CSTY_EPH;
+bytestream_put_byte(>buf, scod);  // Scod
 // SGcod
 bytestream_put_byte(>buf, 0); // progression level
 bytestream_put_be16(>buf, 1); // num of layers
@@ -719,14 +726,18 @@ static void putnumpasses(Jpeg2000EncoderContext *s, int n)
 
 
 static int encode_packet(Jpeg2000EncoderContext *s, Jpeg2000ResLevel *rlevel, 
int precno,
-  uint8_t *expn, int numgbits)
+  uint8_t *expn, int numgbits, int packetno)
 {
 int bandno, empty = 1;
-
 // init bitstream
 *s->buf = 0;
 s->bit_index = 0;
 
+if (s->sop) {
+bytestream_put_be16(>buf, JPEG2000_SOP);
+bytestream_put_be16(>buf, 4);
+bytestream_put_be16(>buf, packetno);
+}
 // header
 
 // is the packet empty?
@@ -794,6 +805,10 @@ static int encode_packet(Jpeg2000EncoderContext *s, 
Jpeg2000ResLevel *rlevel, in
 }
 }
 j2k_flush(s);
+if (s->eph) {
+bytestream_put_be16(>buf, JPEG2000_EPH);
+}
+
 for (bandno = 0; bandno < rlevel->nbands; bandno++){
 Jpeg2000Band *band = rlevel->band + bandno;
 Jpeg2000Prec *prec = band->prec + precno;
@@ -821,7 +836,7 @@ static int encode_packets(Jpeg2000EncoderContext *s, 
Jpeg2000Tile *tile, int til
 int compno, reslevelno, ret;
 Jpeg2000CodingStyle *codsty = >codsty;
 Jpeg2000QuantStyle  *qntsty = >qntsty;
-
+int packetno = 0;
 av_log(s->avctx, AV_LOG_DEBUG, "tier2\n");
 // lay-rlevel-comp-pos progression
 for (reslevelno = 0; reslevelno < codsty->nreslevels; reslevelno++){
@@ -830,7 +845,7 @@ static int encode_packets(Jpeg2000EncoderContext *s, 
Jpeg2000Tile *tile, int til
 Jpeg2000ResLevel *reslevel = s->tile[tileno].comp[compno].reslevel 
+ reslevelno;
 for (precno = 0; precno < reslevel->num_precincts_x * 
reslevel->num_precincts_y; precno++){
 if ((ret = encode_packet(s, reslevel, precno, qntsty->expn + 
(reslevelno ? 3*reslevelno-2 : 0),
-  qntsty->nguardbits)) < 0)
+  qntsty->nguardbits, packetno++)) < 0)
 return ret;
 }
 }
@@ -1244,7 +1259,8 @@ static const AVOption options[] = {
 { "pred",  "DWT Type",  OFFSET(pred),  
AV_OPT_TYPE_INT,   { .i64 = 0   }, 0, 1,   VE, "pred"   
 },
 { "dwt97int",  NULL,0, 
AV_OPT_TYPE_CONST, { .i64 = 0   }, INT_MIN, INT_MAX,   VE, "pred"   
 },
 { "dwt53", NULL,0, 
AV_OPT_TYPE_CONST, { .i64 = 0   }, INT_MIN, INT_MAX,   VE, "pred"   
 },
-
+{ "sop",   "SOP marker",OFFSET(sop),   
AV_OPT_TYPE_INT,   { .i64 = 0   }, 0, 1,   VE, },
+{ "eph",   "EPH marker",OFFSET(eph),   
AV_OPT_TYPE_INT,   { .i64 = 0   }, 0, 1,   VE, },
 { NULL }
 };
 
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 1/2] libavcodec/j2kenc: Encoding up to 16 bits

2020-07-29 Thread gautamramk
From: Gautam Ramakrishnan 

This patch allows the JPEG2000 encoder to
encode images to up to 16 bits.
---
 libavcodec/j2kenc.c | 99 ++---
 1 file changed, 57 insertions(+), 42 deletions(-)

diff --git a/libavcodec/j2kenc.c b/libavcodec/j2kenc.c
index 38643c9a28..5ebc1f3a99 100644
--- a/libavcodec/j2kenc.c
+++ b/libavcodec/j2kenc.c
@@ -298,7 +298,7 @@ static int put_siz(Jpeg2000EncoderContext *s)
 bytestream_put_be16(>buf, s->ncomponents); // CSiz
 
 for (i = 0; i < s->ncomponents; i++){ // Ssiz_i XRsiz_i, YRsiz_i
-bytestream_put_byte(>buf, 7);
+bytestream_put_byte(>buf, s->cbps[i] - 1);
 bytestream_put_byte(>buf, i?1buf, i?1numXtiles * s->numYtiles; tileno++){
-Jpeg2000Tile *tile = s->tile + tileno;
-if (s->planar){
-for (compno = 0; compno < s->ncomponents; compno++){
-Jpeg2000Component *comp = tile->comp + compno;
-int *dst = comp->i_data;
-line = s->picture->data[compno]
-   + comp->coord[1][0] * s->picture->linesize[compno]
-   + comp->coord[0][0];
-for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){
-uint8_t *ptr = line;
-for (x = comp->coord[0][0]; x < comp->coord[0][1]; x++)
-*dst++ = *ptr++ - (1 << 7);
-line += s->picture->linesize[compno];
-}
-}
-} else{
-line = s->picture->data[0] + tile->comp[0].coord[1][0] * 
s->picture->linesize[0]
-   + tile->comp[0].coord[0][0] * s->ncomponents;
-
-i = 0;
-for (y = tile->comp[0].coord[1][0]; y < tile->comp[0].coord[1][1]; 
y++){
-uint8_t *ptr = line;
-for (x = tile->comp[0].coord[0][0]; x < 
tile->comp[0].coord[0][1]; x++, i++){
-for (compno = 0; compno < s->ncomponents; compno++){
-tile->comp[compno].i_data[i] = *ptr++  - (1 << 7);
-}
-}
-line += s->picture->linesize[0];
-}
-}
+#define COPY_FRAME(D, PIXEL)   
 \
+static void copy_frame_ ##D(Jpeg2000EncoderContext *s) 
 \
+{  
 \
+int tileno, compno, i, y, x;   
 \
+PIXEL *line;   
 \
+for (tileno = 0; tileno < s->numXtiles * s->numYtiles; tileno++){  
 \
+Jpeg2000Tile *tile = s->tile + tileno; 
 \
+if (s->planar){
 \
+for (compno = 0; compno < s->ncomponents; compno++){   
 \
+Jpeg2000Component *comp = tile->comp + compno; 
 \
+int *dst = comp->i_data;   
 \
+int cbps = s->cbps[compno];
 \
+line = (PIXEL*)s->picture->data[compno]
 \
+   + comp->coord[1][0] * (s->picture->linesize[compno] 
/ sizeof(PIXEL)) \
+   + comp->coord[0][0];
 \
+for (y = comp->coord[1][0]; y < comp->coord[1][1]; y++){   
 \
+PIXEL *ptr = line; 
 \
+for (x = comp->coord[0][0]; x < comp->coord[0][1]; 
x++) \
+*dst++ = *ptr++ - (1 << (cbps - 1));   
 \
+line += s->picture->linesize[compno] / 

[FFmpeg-devel] [PATCH 2/2] libavcodec/pgxdec Fix pix format selection error

2020-07-29 Thread gautamramk
From: Gautam Ramakrishnan 

This patch makes the pgx decoder select the correct
byte order instead of selecting big endian format for
16 bit images.
---
 libavcodec/pgxdec.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/pgxdec.c b/libavcodec/pgxdec.c
index 93b9f4e7a0..a72d6fe6fe 100644
--- a/libavcodec/pgxdec.c
+++ b/libavcodec/pgxdec.c
@@ -137,7 +137,7 @@ static int pgx_decode_frame(AVCodecContext *avctx, void 
*data,
 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
 bpp = 8;
 } else if (depth <= 16) {
-avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
+avctx->pix_fmt = AV_PIX_FMT_GRAY16;
 bpp = 16;
 } else {
 av_log(avctx, AV_LOG_ERROR, "Maximum depth of 16 bits supported.\n");
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v5] libavcodec/jpeg2000dec: Support for PPM marker

2020-07-25 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support for PPM marker for JPEG2000
decoder. It allows the samples p1_03.j2k and p1_05.j2k
to be decoded.
---
 libavcodec/jpeg2000dec.c | 106 +++
 1 file changed, 96 insertions(+), 10 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index a470cf47da..9cc416f66b 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -71,6 +71,7 @@ typedef struct 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;
 
@@ -102,7 +103,13 @@ typedef struct Jpeg2000DecoderContext {
 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;
@@ -929,6 +936,30 @@ static int get_plt(Jpeg2000DecoderContext *s, int n)
 return 0;
 }
 
+static int get_ppm(Jpeg2000DecoderContext *s, int n)
+{
+void *new;
+
+if (n < 3) {
+av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPM data.\n");
+return AVERROR_INVALIDDATA;
+}
+bytestream2_get_byte(>g); //Zppm is skipped and not used
+new = av_realloc(s->packed_headers,
+ s->packed_headers_size + n - 3);
+if (new) {
+s->packed_headers = new;
+} else
+return AVERROR(ENOMEM);
+s->has_ppm = 1;
+memset(>packed_headers_stream, 0, sizeof(s->packed_headers_stream));
+bytestream_get_buffer(>g.buffer, s->packed_headers + 
s->packed_headers_size,
+  n - 3);
+s->packed_headers_size += n - 3;
+
+return 0;
+}
+
 static int get_ppt(Jpeg2000DecoderContext *s, int n)
 {
 Jpeg2000Tile *tile;
@@ -1040,8 +1071,19 @@ static int getlblockinc(Jpeg2000DecoderContext *s)
 return res;
 }
 
-static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+static inline void select_header(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
  int *tp_index)
+{
+s->g = tile->tile_part[*tp_index].header_tpg;
+if (bytestream2_get_bytes_left(>g) == 0 && s->bit_index == 8) {
+if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
+s->g = tile->tile_part[++(*tp_index)].tpg;
+}
+}
+}
+
+static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+ int *tp_index, Jpeg2000CodingStyle *codsty)
 {
 s->g = tile->tile_part[*tp_index].tpg;
 if (bytestream2_get_bytes_left(>g) == 0 && s->bit_index == 8) {
@@ -1049,8 +1091,12 @@ static inline void select_stream(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile,
 s->g = tile->tile_part[++(*tp_index)].tpg;
 }
 }
-if (bytestream2_peek_be32(>g) == JPEG2000_SOP_FIXED_BYTES)
-bytestream2_skip(>g, JPEG2000_SOP_BYTE_LENGTH);
+if (codsty->csty & JPEG2000_CSTY_SOP) {
+if (bytestream2_peek_be32(>g) == JPEG2000_SOP_FIXED_BYTES)
+bytestream2_skip(>g, JPEG2000_SOP_BYTE_LENGTH);
+else
+av_log(s->avctx, AV_LOG_ERROR, "SOP marker not found. instead 
%X\n", bytestream2_peek_be32(>g));
+}
 }
 
 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile 
*tile, int *tp_index,
@@ -1065,10 +,12 @@ static int 
jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
 return 0;
 rlevel->band[0].prec[precno].decoded_layers = layno + 1;
 // Select stream to read from
-if (tile->has_ppt)
+if (s->has_ppm)
+select_header(s, tile, tp_index);
+else if (tile->has_ppt)
 s->g = tile->packed_headers_stream;
 else
-select_stream(s, tile, tp_index);
+select_stream(s, tile, tp_index, codsty);
 
 if (!(ret = get_bits(s, 1))) {
 jpeg2000_flush(s);
@@ -1179,9 +1227,12 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile,
 }
 
 // Save state of stream
-if (tile->has_ppt) {
+if (s->has_ppm) {
+tile->tile_part[*tp_index].header_tpg = s->g;
+select_stream(s, tile, tp_index, codsty);
+} else if (tile->has_ppt) {
 tile->packed_headers_stream = s->g;
-select_stream(s, tile, tp_index);
+select_stream(s, tile, tp_index, codsty);
 }
 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
 Jpeg2000Band *band = 

[FFmpeg-devel] [PATCH v4 2/2] libavcodec/jpeg2000dec: Support for PPM marker

2020-07-23 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support for PPM marker for JPEG2000
decoder. It allows the samples p1_03.j2k and p1_05.j2k
to be decoded.
---
 libavcodec/jpeg2000dec.c | 107 +++
 1 file changed, 97 insertions(+), 10 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index a470cf47da..558cc6b4de 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -71,6 +71,7 @@ typedef struct 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;
 
@@ -102,7 +103,13 @@ typedef struct Jpeg2000DecoderContext {
 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;
@@ -929,6 +936,31 @@ static int get_plt(Jpeg2000DecoderContext *s, int n)
 return 0;
 }
 
+static int get_ppm(Jpeg2000DecoderContext *s, int n)
+{
+void *new;
+
+if (n < 3) {
+av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPM data.\n");
+return AVERROR_INVALIDDATA;
+}
+bytestream2_get_byte(>g); //Zppm is skipped and not used
+new = av_realloc(s->packed_headers,
+ s->packed_headers_size + n - 3);
+if (new) {
+s->packed_headers = new;
+} else
+return AVERROR(ENOMEM);
+s->has_ppm = 1;
+memset(>packed_headers_stream, 0, sizeof(s->packed_headers_stream));
+memcpy(s->packed_headers + s->packed_headers_size,
+   s->g.buffer, n - 3);
+s->packed_headers_size += n - 3;
+bytestream2_skip(>g, n - 3);
+
+return 0;
+}
+
 static int get_ppt(Jpeg2000DecoderContext *s, int n)
 {
 Jpeg2000Tile *tile;
@@ -1040,8 +1072,19 @@ static int getlblockinc(Jpeg2000DecoderContext *s)
 return res;
 }
 
-static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+static inline void select_header(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
  int *tp_index)
+{
+s->g = tile->tile_part[*tp_index].header_tpg;
+if (bytestream2_get_bytes_left(>g) == 0 && s->bit_index == 8) {
+if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
+s->g = tile->tile_part[++(*tp_index)].tpg;
+}
+}
+}
+
+static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+ int *tp_index, Jpeg2000CodingStyle *codsty)
 {
 s->g = tile->tile_part[*tp_index].tpg;
 if (bytestream2_get_bytes_left(>g) == 0 && s->bit_index == 8) {
@@ -1049,8 +1092,12 @@ static inline void select_stream(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile,
 s->g = tile->tile_part[++(*tp_index)].tpg;
 }
 }
-if (bytestream2_peek_be32(>g) == JPEG2000_SOP_FIXED_BYTES)
-bytestream2_skip(>g, JPEG2000_SOP_BYTE_LENGTH);
+if (codsty->csty & JPEG2000_CSTY_SOP) {
+if (bytestream2_peek_be32(>g) == JPEG2000_SOP_FIXED_BYTES)
+bytestream2_skip(>g, JPEG2000_SOP_BYTE_LENGTH);
+else
+av_log(s->avctx, AV_LOG_ERROR, "SOP marker not found. instead 
%X\n", bytestream2_peek_be32(>g));
+}
 }
 
 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile 
*tile, int *tp_index,
@@ -1065,10 +1112,12 @@ static int 
jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
 return 0;
 rlevel->band[0].prec[precno].decoded_layers = layno + 1;
 // Select stream to read from
-if (tile->has_ppt)
+if (s->has_ppm)
+select_header(s, tile, tp_index);
+else if (tile->has_ppt)
 s->g = tile->packed_headers_stream;
 else
-select_stream(s, tile, tp_index);
+select_stream(s, tile, tp_index, codsty);
 
 if (!(ret = get_bits(s, 1))) {
 jpeg2000_flush(s);
@@ -1179,9 +1228,12 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile,
 }
 
 // Save state of stream
-if (tile->has_ppt) {
+if (s->has_ppm) {
+tile->tile_part[*tp_index].header_tpg = s->g;
+select_stream(s, tile, tp_index, codsty);
+} else if (tile->has_ppt) {
 tile->packed_headers_stream = s->g;
-select_stream(s, tile, tp_index);
+select_stream(s, tile, tp_index, codsty);
 }
 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
 Jpeg2000Band *band 

[FFmpeg-devel] [PATCH v4 1/2] libavcodec/jpeg2000dec: Fix codeblock decode check

2020-07-23 Thread gautamramk
From: Gautam Ramakrishnan 

The codeblock decoder checks whether the mqc decoder
has decoded the right number of bytes. However, this
check does not account for the fact that the mqc encoder's
flush routine adds 2 bytes of data which does not have to be
read by the decoder. The check is modified to account for
this. This patch solves issue #4827
---
 libavcodec/jpeg2000dec.c | 8 ++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index e941ebb5d0..a470cf47da 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -1754,9 +1754,13 @@ static int decode_cblk(Jpeg2000DecoderContext *s, 
Jpeg2000CodingStyle *codsty,
 pass_cnt ++;
 }
 
-if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != 
t1->mqc.bp) {
+if (cblk->data + cblk->length - 2 > t1->mqc.bp) {
 av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
-   cblk->data + cblk->length - 2*(term_cnt < 
cblk->nb_terminations) - t1->mqc.bp);
+   cblk->data + cblk->length - 2 - t1->mqc.bp);
+}
+
+if (cblk->data + cblk->length < t1->mqc.bp) {
+av_log(s->avctx, AV_LOG_WARNING, "Synthetic End of Stream Marker 
Read.\n");
 }
 
 return 1;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC Patch 2/2] libavformat/rtpenc_jpeg2000 JPEG2000 RTP Muxer

2020-07-22 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support to mux JPEG2000 streams over
RTP.
---
 libavformat/Makefile  |   1 +
 libavformat/rtpenc.c  |   4 ++
 libavformat/rtpenc.h  |   1 +
 libavformat/rtpenc_jpeg2000.c | 121 ++
 libavformat/sdp.c |  32 +
 5 files changed, 159 insertions(+)
 create mode 100644 libavformat/rtpenc_jpeg2000.c

diff --git a/libavformat/Makefile b/libavformat/Makefile
index 4495047e3a..f2d260fada 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -459,6 +459,7 @@ OBJS-$(CONFIG_RTP_MUXER) += rtp.o \
 rtpenc_h263_rfc2190.o \
 rtpenc_h264_hevc.o\
 rtpenc_jpeg.o \
+rtpenc_jpeg2000.o \
 rtpenc_mpv.o \
 rtpenc.o  \
 rtpenc_vc2hq.o  \
diff --git a/libavformat/rtpenc.c b/libavformat/rtpenc.c
index 9ef7e9094d..1af9e3f455 100644
--- a/libavformat/rtpenc.c
+++ b/libavformat/rtpenc.c
@@ -84,6 +84,7 @@ static int is_supported(enum AVCodecID id)
 case AV_CODEC_ID_MJPEG:
 case AV_CODEC_ID_SPEEX:
 case AV_CODEC_ID_OPUS:
+case AV_CODEC_ID_JPEG2000:
 return 1;
 default:
 return 0;
@@ -619,6 +620,9 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket 
*pkt)
 case AV_CODEC_ID_MJPEG:
 ff_rtp_send_jpeg(s1, pkt->data, size);
 break;
+case AV_CODEC_ID_JPEG2000:
+ff_rtp_send_jpeg2000(s1, pkt->data, size);
+break;
 case AV_CODEC_ID_OPUS:
 if (size > s->max_payload_size) {
 av_log(s1, AV_LOG_ERROR,
diff --git a/libavformat/rtpenc.h b/libavformat/rtpenc.h
index 62dc9ab10a..0db339f2ee 100644
--- a/libavformat/rtpenc.h
+++ b/libavformat/rtpenc.h
@@ -95,6 +95,7 @@ void ff_rtp_send_vc2hq(AVFormatContext *s1, const uint8_t 
*buf, int size, int in
 void ff_rtp_send_vp8(AVFormatContext *s1, const uint8_t *buff, int size);
 void ff_rtp_send_vp9(AVFormatContext *s1, const uint8_t *buff, int size);
 void ff_rtp_send_jpeg(AVFormatContext *s1, const uint8_t *buff, int size);
+void ff_rtp_send_jpeg2000(AVFormatContext *s1, const uint8_t *buff, int size);
 
 const uint8_t *ff_h263_find_resync_marker_reverse(const uint8_t *av_restrict 
start,
   const uint8_t *av_restrict 
end);
diff --git a/libavformat/rtpenc_jpeg2000.c b/libavformat/rtpenc_jpeg2000.c
new file mode 100644
index 00..699bc2e1b9
--- /dev/null
+++ b/libavformat/rtpenc_jpeg2000.c
@@ -0,0 +1,121 @@
+/*
+ * RTP JPEG2000 video Packetizer, RFC 5371
+ * Copyright (c) 2020 Gautam Ramakrishnan
+ *
+ * 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
+ */
+
+#include "libavcodec/bytestream.h"
+#include "libavutil/intreadwrite.h"
+#include "rtpenc.h"
+
+#define PAYLOAD_HDR_SIZ 8
+
+static void send_packet(AVFormatContext *s1, const uint8_t *buf, int start, 
int end,
+   int header, int tileno, int cstream_start, int last)
+{
+RTPMuxContext *s = s1->priv_data;
+int unit_len = end - start;
+int send_left = end - start;
+while (send_left > 0) {
+int len = FFMIN(send_left, s->max_payload_size - PAYLOAD_HDR_SIZ);
+uint8_t flags = 0;
+if (unit_len <= s->max_payload_size - PAYLOAD_HDR_SIZ)
+flags |= 3 << 3;
+else if (header && (send_left - len))
+flags |= 1 << 3;
+else if (header && !(send_left - len))
+flags |= 2 << 3;
+if (header)
+flags |= 1;
+bytestream_put_byte(>buf_ptr, flags);
+bytestream_put_byte(>buf_ptr, 255);
+bytestream_put_be16(>buf_ptr, tileno);
+bytestream_put_byte(>buf_ptr, 0);
+bytestream_put_be24(>buf_ptr, start - cstream_start);
+memcpy(s->buf_ptr, buf + start, len);
+ff_rtp_send_data(s1, s->buf, len + PAYLOAD_HDR_SIZ, last && send_left 
<= len);
+send_left -= len;
+s->buf_ptr = s->buf;
+start += len;
+}
+}
+
+void 

[FFmpeg-devel] [RFC Patch 1/2] libavformat/rtpdec_jpeg2000: RTP Demuxing for JPEG2000

2020-07-22 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support to receive JPEG2000 RTP streams.
---
 libavformat/Makefile  |   1 +
 libavformat/rtpdec.c  |   1 +
 libavformat/rtpdec_formats.h  |   1 +
 libavformat/rtpdec_jpeg2000.c | 116 ++
 4 files changed, 119 insertions(+)
 create mode 100644 libavformat/rtpdec_jpeg2000.c

diff --git a/libavformat/Makefile b/libavformat/Makefile
index 62d8cbb54e..4495047e3a 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -46,6 +46,7 @@ OBJS-$(CONFIG_RTPDEC)+= rdt.o 
  \
 rtpdec_hevc.o   \
 rtpdec_ilbc.o   \
 rtpdec_jpeg.o   \
+rtpdec_jpeg2000.o   \
 rtpdec_latm.o   \
 rtpdec_mpa_robust.o \
 rtpdec_mpeg12.o \
diff --git a/libavformat/rtpdec.c b/libavformat/rtpdec.c
index 3d5b200099..b47dfdfebc 100644
--- a/libavformat/rtpdec.c
+++ b/libavformat/rtpdec.c
@@ -118,6 +118,7 @@ static const RTPDynamicProtocolHandler 
*rtp_dynamic_protocol_handler_list[] = {
 _vorbis_dynamic_handler,
 _vp8_dynamic_handler,
 _vp9_dynamic_handler,
+_jpeg2000_dynamic_handler,
 _dynamic_handler,
 _dynamic_handler,
 _dynamic_handler,
diff --git a/libavformat/rtpdec_formats.h b/libavformat/rtpdec_formats.h
index dad2b8ac1b..78ea4fb384 100644
--- a/libavformat/rtpdec_formats.h
+++ b/libavformat/rtpdec_formats.h
@@ -89,5 +89,6 @@ extern const RTPDynamicProtocolHandler 
ff_vc2hq_dynamic_handler;
 extern const RTPDynamicProtocolHandler ff_vorbis_dynamic_handler;
 extern const RTPDynamicProtocolHandler ff_vp8_dynamic_handler;
 extern const RTPDynamicProtocolHandler ff_vp9_dynamic_handler;
+extern const RTPDynamicProtocolHandler ff_jpeg2000_dynamic_handler;
 
 #endif /* AVFORMAT_RTPDEC_FORMATS_H */
diff --git a/libavformat/rtpdec_jpeg2000.c b/libavformat/rtpdec_jpeg2000.c
new file mode 100644
index 00..b5337a9cdb
--- /dev/null
+++ b/libavformat/rtpdec_jpeg2000.c
@@ -0,0 +1,116 @@
+/*
+ * Code for the RTP depacketization of JPEG2000.
+ * Copyright (c) 2020 Gautam Ramakrishnan
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * @brief JPEG2000 / RTP Code
+ * @author Gautam Ramakrishnan
+ */
+
+#include "rtpdec_formats.h"
+#include "avio_internal.h"
+#include "internal.h"
+#include "libavutil/attributes.h"
+#include "libavutil/avstring.h"
+#include "libavcodec/get_bits.h"
+
+#define PAYLOAD_HDR_SIZ 8
+
+/**
+ * RTP/JPEG specific private data.
+ */
+struct PayloadContext {
+AVIOContext *frame; // current frame buffer
+uint32_ttimestamp;  // current frame timestamp
+};
+
+static void jpeg2000_close_context(PayloadContext *data)
+{
+ffio_free_dyn_buf(>frame);
+}
+
+static int jpeg2000_parse_packet(AVFormatContext *ctx, PayloadContext *data,
+AVStream *st, AVPacket *pkt, uint32_t 
*timestamp,
+const uint8_t *buf, int len, uint16_t seq,
+int flags)
+{
+int ret;
+int off;
+
+if (len < 8) {
+av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
+return AVERROR_INVALIDDATA;
+}
+off = (uint64_t)AV_RB64(buf) & 0xFF;
+buf += 8;
+len -= 8;
+if (!off) {
+/* Skip the current frame in case of the end packet
+ * has been lost somewhere. */
+ffio_free_dyn_buf(>frame);
+
+if ((ret = avio_open_dyn_buf(>frame)) < 0)
+return ret;
+data->timestamp = *timestamp;
+}
+if (!data->frame) {
+av_log(ctx, AV_LOG_ERROR,
+   "Received packet without a start chunk; dropping frame.\n");
+return AVERROR(EAGAIN);
+}
+
+if (data->timestamp != *timestamp) {
+/* Skip the current frame if timestamp is incorrect.
+ * A start packet has been lost somewhere. */
+ffio_free_dyn_buf(>frame);
+av_log(ctx, 

[FFmpeg-devel] [PATCH v3 2/3] libavcodec/jpeg2000dec: Add check when done with main header markers

2020-07-21 Thread gautamramk
From: Gautam Ramakrishnan 

This patch sets a flag when the processing of the
main header is complete.
---
 libavcodec/jpeg2000dec.c | 4 
 1 file changed, 4 insertions(+)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 5e9e97eb6a..23792c15b2 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -102,6 +102,7 @@ typedef struct Jpeg2000DecoderContext {
 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 in_tile_headers;
 int cdx[4], cdy[4];
 int precision;
 int ncomponents;
@@ -2165,6 +2166,9 @@ static int 
jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
 ret = get_poc(s, len, poc);
 break;
 case JPEG2000_SOT:
+if (!s->in_tile_headers) {
+s->in_tile_headers = 1;
+}
 if (!(ret = get_sot(s, len))) {
 av_assert1(s->curtileno >= 0);
 codsty = s->tile[s->curtileno].codsty;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v3 1/3] libavcodec/jpeg2000dec: Fix codeblock decode check

2020-07-21 Thread gautamramk
From: Gautam Ramakrishnan 

The codeblock decoder checks whether the mqc decoder
has decoded the right number of bytes. However, this
check does not account for the fact that the mqc encoder's
flush routine adds 2 bytes of data which does not have to be
read by the decoder. The check is modified to account for
this. This patch solves issue #4827
---
 libavcodec/jpeg2000dec.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index f5ad8402fb..5e9e97eb6a 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -1753,9 +1753,9 @@ static int decode_cblk(Jpeg2000DecoderContext *s, 
Jpeg2000CodingStyle *codsty,
 pass_cnt ++;
 }
 
-if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != 
t1->mqc.bp) {
+if (cblk->data + cblk->length - 2 > t1->mqc.bp) {
 av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
-   cblk->data + cblk->length - 2*(term_cnt < 
cblk->nb_terminations) - t1->mqc.bp);
+   cblk->data + cblk->length - 2 - t1->mqc.bp);
 }
 
 return 1;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v3 3/3] libavcodec/jpeg2000dec: Support for PPM marker

2020-07-21 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support for PPM marker for JPEG2000
decoder. It allows the samples p1_03.j2k and p1_05.j2k
to be decoded.
---
 libavcodec/jpeg2000dec.c | 107 +++
 1 file changed, 97 insertions(+), 10 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 23792c15b2..0bc29633ff 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -71,6 +71,7 @@ typedef struct 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;
 
@@ -102,7 +103,13 @@ typedef struct Jpeg2000DecoderContext {
 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;
@@ -929,6 +936,31 @@ static int get_plt(Jpeg2000DecoderContext *s, int n)
 return 0;
 }
 
+static int get_ppm(Jpeg2000DecoderContext *s, int n)
+{
+void *new;
+
+if (n < 3) {
+av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPM data.\n");
+return AVERROR_INVALIDDATA;
+}
+bytestream2_get_byte(>g); //Zppm is skipped and not used
+new = av_realloc(s->packed_headers,
+ s->packed_headers_size + n - 3);
+if (new) {
+s->packed_headers = new;
+} else
+return AVERROR(ENOMEM);
+s->has_ppm = 1;
+memset(>packed_headers_stream, 0, sizeof(s->packed_headers_stream));
+memcpy(s->packed_headers + s->packed_headers_size,
+   s->g.buffer, n - 3);
+s->packed_headers_size += n - 3;
+bytestream2_skip(>g, n - 3);
+
+return 0;
+}
+
 static int get_ppt(Jpeg2000DecoderContext *s, int n)
 {
 Jpeg2000Tile *tile;
@@ -1040,8 +1072,19 @@ static int getlblockinc(Jpeg2000DecoderContext *s)
 return res;
 }
 
-static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+static inline void select_header(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
  int *tp_index)
+{
+s->g = tile->tile_part[*tp_index].header_tpg;
+if (bytestream2_get_bytes_left(>g) == 0 && s->bit_index == 8) {
+if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
+s->g = tile->tile_part[++(*tp_index)].tpg;
+}
+}
+}
+
+static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+ int *tp_index, Jpeg2000CodingStyle *codsty)
 {
 s->g = tile->tile_part[*tp_index].tpg;
 if (bytestream2_get_bytes_left(>g) == 0 && s->bit_index == 8) {
@@ -1049,8 +1092,12 @@ static inline void select_stream(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile,
 s->g = tile->tile_part[++(*tp_index)].tpg;
 }
 }
-if (bytestream2_peek_be32(>g) == JPEG2000_SOP_FIXED_BYTES)
-bytestream2_skip(>g, JPEG2000_SOP_BYTE_LENGTH);
+if (codsty->csty & JPEG2000_CSTY_SOP) {
+if (bytestream2_peek_be32(>g) == JPEG2000_SOP_FIXED_BYTES)
+bytestream2_skip(>g, JPEG2000_SOP_BYTE_LENGTH);
+else
+av_log(s->avctx, AV_LOG_ERROR, "SOP marker not found. instead 
%X\n", bytestream2_peek_be32(>g));
+}
 }
 
 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile 
*tile, int *tp_index,
@@ -1065,10 +1112,12 @@ static int 
jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
 return 0;
 rlevel->band[0].prec[precno].decoded_layers = layno + 1;
 // Select stream to read from
-if (tile->has_ppt)
+if (s->has_ppm)
+select_header(s, tile, tp_index);
+else if (tile->has_ppt)
 s->g = tile->packed_headers_stream;
 else
-select_stream(s, tile, tp_index);
+select_stream(s, tile, tp_index, codsty);
 
 if (!(ret = get_bits(s, 1))) {
 jpeg2000_flush(s);
@@ -1179,9 +1228,12 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile,
 }
 
 // Save state of stream
-if (tile->has_ppt) {
+if (s->has_ppm) {
+tile->tile_part[*tp_index].header_tpg = s->g;
+select_stream(s, tile, tp_index, codsty);
+} else if (tile->has_ppt) {
 tile->packed_headers_stream = s->g;
-select_stream(s, tile, tp_index);
+select_stream(s, tile, tp_index, codsty);
 }
 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
 Jpeg2000Band *band 

[FFmpeg-devel] [RFC PATCH] libavformat/rtpdec_jpeg2000: RTP Demuxing for JPEG2000

2020-07-21 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support to receive JPEG2000 RTP streams.
---
 libavformat/Makefile  |   1 +
 libavformat/rtpdec.c  |   1 +
 libavformat/rtpdec_formats.h  |   1 +
 libavformat/rtpdec_jpeg2000.c | 116 ++
 4 files changed, 119 insertions(+)
 create mode 100644 libavformat/rtpdec_jpeg2000.c

diff --git a/libavformat/Makefile b/libavformat/Makefile
index 62d8cbb54e..4495047e3a 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -46,6 +46,7 @@ OBJS-$(CONFIG_RTPDEC)+= rdt.o 
  \
 rtpdec_hevc.o   \
 rtpdec_ilbc.o   \
 rtpdec_jpeg.o   \
+rtpdec_jpeg2000.o   \
 rtpdec_latm.o   \
 rtpdec_mpa_robust.o \
 rtpdec_mpeg12.o \
diff --git a/libavformat/rtpdec.c b/libavformat/rtpdec.c
index 3d5b200099..b47dfdfebc 100644
--- a/libavformat/rtpdec.c
+++ b/libavformat/rtpdec.c
@@ -118,6 +118,7 @@ static const RTPDynamicProtocolHandler 
*rtp_dynamic_protocol_handler_list[] = {
 _vorbis_dynamic_handler,
 _vp8_dynamic_handler,
 _vp9_dynamic_handler,
+_jpeg2000_dynamic_handler,
 _dynamic_handler,
 _dynamic_handler,
 _dynamic_handler,
diff --git a/libavformat/rtpdec_formats.h b/libavformat/rtpdec_formats.h
index dad2b8ac1b..78ea4fb384 100644
--- a/libavformat/rtpdec_formats.h
+++ b/libavformat/rtpdec_formats.h
@@ -89,5 +89,6 @@ extern const RTPDynamicProtocolHandler 
ff_vc2hq_dynamic_handler;
 extern const RTPDynamicProtocolHandler ff_vorbis_dynamic_handler;
 extern const RTPDynamicProtocolHandler ff_vp8_dynamic_handler;
 extern const RTPDynamicProtocolHandler ff_vp9_dynamic_handler;
+extern const RTPDynamicProtocolHandler ff_jpeg2000_dynamic_handler;
 
 #endif /* AVFORMAT_RTPDEC_FORMATS_H */
diff --git a/libavformat/rtpdec_jpeg2000.c b/libavformat/rtpdec_jpeg2000.c
new file mode 100644
index 00..b5337a9cdb
--- /dev/null
+++ b/libavformat/rtpdec_jpeg2000.c
@@ -0,0 +1,116 @@
+/*
+ * Code for the RTP depacketization of JPEG2000.
+ * Copyright (c) 2020 Gautam Ramakrishnan
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * @brief JPEG2000 / RTP Code
+ * @author Gautam Ramakrishnan
+ */
+
+#include "rtpdec_formats.h"
+#include "avio_internal.h"
+#include "internal.h"
+#include "libavutil/attributes.h"
+#include "libavutil/avstring.h"
+#include "libavcodec/get_bits.h"
+
+#define PAYLOAD_HDR_SIZ 8
+
+/**
+ * RTP/JPEG specific private data.
+ */
+struct PayloadContext {
+AVIOContext *frame; // current frame buffer
+uint32_ttimestamp;  // current frame timestamp
+};
+
+static void jpeg2000_close_context(PayloadContext *data)
+{
+ffio_free_dyn_buf(>frame);
+}
+
+static int jpeg2000_parse_packet(AVFormatContext *ctx, PayloadContext *data,
+AVStream *st, AVPacket *pkt, uint32_t 
*timestamp,
+const uint8_t *buf, int len, uint16_t seq,
+int flags)
+{
+int ret;
+int off;
+
+if (len < 8) {
+av_log(ctx, AV_LOG_ERROR, "Too short RTP/JPEG packet.\n");
+return AVERROR_INVALIDDATA;
+}
+off = (uint64_t)AV_RB64(buf) & 0xFF;
+buf += 8;
+len -= 8;
+if (!off) {
+/* Skip the current frame in case of the end packet
+ * has been lost somewhere. */
+ffio_free_dyn_buf(>frame);
+
+if ((ret = avio_open_dyn_buf(>frame)) < 0)
+return ret;
+data->timestamp = *timestamp;
+}
+if (!data->frame) {
+av_log(ctx, AV_LOG_ERROR,
+   "Received packet without a start chunk; dropping frame.\n");
+return AVERROR(EAGAIN);
+}
+
+if (data->timestamp != *timestamp) {
+/* Skip the current frame if timestamp is incorrect.
+ * A start packet has been lost somewhere. */
+ffio_free_dyn_buf(>frame);
+av_log(ctx, 

[FFmpeg-devel] [PATCH v2 3/3] libavcodec/jpeg2000dec: Support for PPM marker

2020-07-18 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support for PPM marker for JPEG2000
decoder. It allows the samples p1_03.j2k and p1_05.j2k
to be decoded.
---
 libavcodec/jpeg2000dec.c | 111 +++
 1 file changed, 101 insertions(+), 10 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 5e9e97eb6a..e37f834afe 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -71,6 +71,7 @@ typedef struct 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;
 
@@ -102,6 +103,13 @@ typedef struct Jpeg2000DecoderContext {
 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;
@@ -928,6 +936,31 @@ static int get_plt(Jpeg2000DecoderContext *s, int n)
 return 0;
 }
 
+static int get_ppm(Jpeg2000DecoderContext *s, int n)
+{
+void *new;
+
+if (n < 3) {
+av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPM data.\n");
+return AVERROR_INVALIDDATA;
+}
+s->has_ppm = 1;
+bytestream2_get_byte(>g); //Zppm is skipped and not used
+new = av_realloc(s->packed_headers,
+ s->packed_headers_size + n - 3);
+if (new) {
+s->packed_headers = new;
+} else
+return AVERROR(ENOMEM);
+memset(>packed_headers_stream, 0, sizeof(s->packed_headers_stream));
+memcpy(s->packed_headers + s->packed_headers_size,
+   s->g.buffer, n - 3);
+s->packed_headers_size += n - 3;
+bytestream2_skip(>g, n - 3);
+
+return 0;
+}
+
 static int get_ppt(Jpeg2000DecoderContext *s, int n)
 {
 Jpeg2000Tile *tile;
@@ -1039,8 +1072,19 @@ static int getlblockinc(Jpeg2000DecoderContext *s)
 return res;
 }
 
-static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+static inline void select_header(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
  int *tp_index)
+{
+s->g = tile->tile_part[*tp_index].header_tpg;
+if (bytestream2_get_bytes_left(>g) == 0 && s->bit_index == 8) {
+if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
+s->g = tile->tile_part[++(*tp_index)].tpg;
+}
+}
+}
+
+static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+ int *tp_index, Jpeg2000CodingStyle *codsty)
 {
 s->g = tile->tile_part[*tp_index].tpg;
 if (bytestream2_get_bytes_left(>g) == 0 && s->bit_index == 8) {
@@ -1048,8 +1092,12 @@ static inline void select_stream(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile,
 s->g = tile->tile_part[++(*tp_index)].tpg;
 }
 }
-if (bytestream2_peek_be32(>g) == JPEG2000_SOP_FIXED_BYTES)
-bytestream2_skip(>g, JPEG2000_SOP_BYTE_LENGTH);
+if (codsty->csty & JPEG2000_CSTY_SOP) {
+if (bytestream2_peek_be32(>g) == JPEG2000_SOP_FIXED_BYTES)
+bytestream2_skip(>g, JPEG2000_SOP_BYTE_LENGTH);
+else
+av_log(s->avctx, AV_LOG_ERROR, "SOP marker not found. instead 
%X\n", bytestream2_peek_be32(>g));
+}
 }
 
 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile 
*tile, int *tp_index,
@@ -1064,10 +1112,12 @@ static int 
jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
 return 0;
 rlevel->band[0].prec[precno].decoded_layers = layno + 1;
 // Select stream to read from
-if (tile->has_ppt)
+if (s->has_ppm)
+select_header(s, tile, tp_index);
+else if (tile->has_ppt)
 s->g = tile->packed_headers_stream;
 else
-select_stream(s, tile, tp_index);
+select_stream(s, tile, tp_index, codsty);
 
 if (!(ret = get_bits(s, 1))) {
 jpeg2000_flush(s);
@@ -1178,9 +1228,12 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile,
 }
 
 // Save state of stream
-if (tile->has_ppt) {
+if (s->has_ppm) {
+tile->tile_part[*tp_index].header_tpg = s->g;
+select_stream(s, tile, tp_index, codsty);
+} else if (tile->has_ppt) {
 tile->packed_headers_stream = s->g;
-select_stream(s, tile, tp_index);
+select_stream(s, tile, tp_index, codsty);
 }
 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
 Jpeg2000Band *band 

[FFmpeg-devel] [PATCH v2 1/3] libavcodec/jpeg2000dec: Fix codeblock decode check

2020-07-18 Thread gautamramk
From: Gautam Ramakrishnan 

The codeblock decoder checks whether the mqc decoder
has decoded the right number of bytes. However, this
check does not account for the fact that the mqc encoder's
flush routine adds 2 bytes of data which does not have to be
read by the decoder. The check is modified to account for
this. This patch solves issue #4827
---
 libavcodec/jpeg2000dec.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 5ea6fd0b9a..5043125fe3 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -1753,9 +1753,9 @@ static int decode_cblk(Jpeg2000DecoderContext *s, 
Jpeg2000CodingStyle *codsty,
 pass_cnt ++;
 }
 
-if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != 
t1->mqc.bp) {
+if (cblk->data + cblk->length - 2 > t1->mqc.bp) {
 av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
-   cblk->data + cblk->length - 2*(term_cnt < 
cblk->nb_terminations) - t1->mqc.bp);
+   cblk->data + cblk->length - 2 - t1->mqc.bp);
 }
 
 return 1;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v2 2/3] libavcodec/jpeg2000dec: Support for Parameterless Markers

2020-07-18 Thread gautamramk
From: Gautam Ramakrishnan 

The JPEG2000 standard reserves marker values 0xFF30
to 0xFF3F to be used as parameterless markers. This
patch adds support to decode codestream with such
markers. This allows decoding of p0_02.j2k.
---
 libavcodec/jpeg2000dec.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 5043125fe3..5e9e97eb6a 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -2092,7 +2092,8 @@ static int 
jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
 
 marker = bytestream2_get_be16u(>g);
 oldpos = bytestream2_tell(>g);
-
+if (marker >= 0xFF30 && marker <= 0xFF3F)
+continue;
 if (marker == JPEG2000_SOD) {
 Jpeg2000Tile *tile;
 Jpeg2000TilePart *tp;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 1/2] libavcodec/jpeg2000dec: Fix codeblock decode check

2020-07-18 Thread gautamramk
From: Gautam Ramakrishnan 

The codeblock decoder checks whether the mqc decoder
has decoded the right number of bytes. However, this
check does not account for the fact that the mqc encoder's
flush routine adds 2 bytes of data which does not have to be
read by the decoder. The check is modified to account for
this. This patch solves issue #4827
---
 libavcodec/jpeg2000dec.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 5ea6fd0b9a..5043125fe3 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -1753,9 +1753,9 @@ static int decode_cblk(Jpeg2000DecoderContext *s, 
Jpeg2000CodingStyle *codsty,
 pass_cnt ++;
 }
 
-if (cblk->data + cblk->length - 2*(term_cnt < cblk->nb_terminations) != 
t1->mqc.bp) {
+if (cblk->data + cblk->length - 2 > t1->mqc.bp) {
 av_log(s->avctx, AV_LOG_WARNING, "End mismatch %"PTRDIFF_SPECIFIER"\n",
-   cblk->data + cblk->length - 2*(term_cnt < 
cblk->nb_terminations) - t1->mqc.bp);
+   cblk->data + cblk->length - 2 - t1->mqc.bp);
 }
 
 return 1;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 2/2] libavcodec/jpeg2000dec: Support for PPM marker

2020-07-18 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support for PPM marker for JPEG2000
decoder. It allows the samples p1_03.j2k and p1_05.j2k
to be decoded.
---
 libavcodec/jpeg2000dec.c | 111 +++
 1 file changed, 101 insertions(+), 10 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 5043125fe3..4f380eebe8 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -71,6 +71,7 @@ typedef struct 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;
 
@@ -102,6 +103,13 @@ typedef struct Jpeg2000DecoderContext {
 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;
@@ -928,6 +936,31 @@ static int get_plt(Jpeg2000DecoderContext *s, int n)
 return 0;
 }
 
+static int get_ppm(Jpeg2000DecoderContext *s, int n)
+{
+void *new;
+
+if (n < 3) {
+av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPM data.\n");
+return AVERROR_INVALIDDATA;
+}
+s->has_ppm = 1;
+bytestream2_get_byte(>g); //Zppm is skipped and not used
+new = av_realloc(s->packed_headers,
+ s->packed_headers_size + n - 3);
+if (new) {
+s->packed_headers = new;
+} else
+return AVERROR(ENOMEM);
+memset(>packed_headers_stream, 0, sizeof(s->packed_headers_stream));
+memcpy(s->packed_headers + s->packed_headers_size,
+   s->g.buffer, n - 3);
+s->packed_headers_size += n - 3;
+bytestream2_skip(>g, n - 3);
+
+return 0;
+}
+
 static int get_ppt(Jpeg2000DecoderContext *s, int n)
 {
 Jpeg2000Tile *tile;
@@ -1039,8 +1072,19 @@ static int getlblockinc(Jpeg2000DecoderContext *s)
 return res;
 }
 
-static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+static inline void select_header(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
  int *tp_index)
+{
+s->g = tile->tile_part[*tp_index].header_tpg;
+if (bytestream2_get_bytes_left(>g) == 0 && s->bit_index == 8) {
+if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
+s->g = tile->tile_part[++(*tp_index)].tpg;
+}
+}
+}
+
+static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+ int *tp_index, Jpeg2000CodingStyle *codsty)
 {
 s->g = tile->tile_part[*tp_index].tpg;
 if (bytestream2_get_bytes_left(>g) == 0 && s->bit_index == 8) {
@@ -1048,8 +1092,12 @@ static inline void select_stream(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile,
 s->g = tile->tile_part[++(*tp_index)].tpg;
 }
 }
-if (bytestream2_peek_be32(>g) == JPEG2000_SOP_FIXED_BYTES)
-bytestream2_skip(>g, JPEG2000_SOP_BYTE_LENGTH);
+if (codsty->csty & JPEG2000_CSTY_SOP) {
+if (bytestream2_peek_be32(>g) == JPEG2000_SOP_FIXED_BYTES)
+bytestream2_skip(>g, JPEG2000_SOP_BYTE_LENGTH);
+else
+av_log(s->avctx, AV_LOG_ERROR, "SOP marker not found. instead 
%X\n", bytestream2_peek_be32(>g));
+}
 }
 
 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile 
*tile, int *tp_index,
@@ -1064,10 +1112,12 @@ static int 
jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
 return 0;
 rlevel->band[0].prec[precno].decoded_layers = layno + 1;
 // Select stream to read from
-if (tile->has_ppt)
+if (s->has_ppm)
+select_header(s, tile, tp_index);
+else if (tile->has_ppt)
 s->g = tile->packed_headers_stream;
 else
-select_stream(s, tile, tp_index);
+select_stream(s, tile, tp_index, codsty);
 
 if (!(ret = get_bits(s, 1))) {
 jpeg2000_flush(s);
@@ -1178,9 +1228,12 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile,
 }
 
 // Save state of stream
-if (tile->has_ppt) {
+if (s->has_ppm) {
+tile->tile_part[*tp_index].header_tpg = s->g;
+select_stream(s, tile, tp_index, codsty);
+} else if (tile->has_ppt) {
 tile->packed_headers_stream = s->g;
-select_stream(s, tile, tp_index);
+select_stream(s, tile, tp_index, codsty);
 }
 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
 Jpeg2000Band *band 

[FFmpeg-devel] [PATCH v2 4/4] libavcodec/jpeg2000dec: Support for PPM marker

2020-07-14 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support for PPM marker for JPEG2000
decoder. It allows the samples p1_03.j2k and p1_05.j2k
to be decoded.
---
 libavcodec/jpeg2000dec.c | 111 +++
 1 file changed, 101 insertions(+), 10 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 5ea6fd0b9a..4f3a5af20e 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -71,6 +71,7 @@ typedef struct 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;
 
@@ -102,6 +103,13 @@ typedef struct Jpeg2000DecoderContext {
 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;
@@ -928,6 +936,31 @@ static int get_plt(Jpeg2000DecoderContext *s, int n)
 return 0;
 }
 
+static int get_ppm(Jpeg2000DecoderContext *s, int n)
+{
+void *new;
+
+if (n < 3) {
+av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPM data.\n");
+return AVERROR_INVALIDDATA;
+}
+s->has_ppm = 1;
+bytestream2_get_byte(>g); //Zppm is skipped and not used
+new = av_realloc(s->packed_headers,
+ s->packed_headers_size + n - 3);
+if (new) {
+s->packed_headers = new;
+} else
+return AVERROR(ENOMEM);
+memset(>packed_headers_stream, 0, sizeof(s->packed_headers_stream));
+memcpy(s->packed_headers + s->packed_headers_size,
+   s->g.buffer, n - 3);
+s->packed_headers_size += n - 3;
+bytestream2_skip(>g, n - 3);
+
+return 0;
+}
+
 static int get_ppt(Jpeg2000DecoderContext *s, int n)
 {
 Jpeg2000Tile *tile;
@@ -1039,8 +1072,19 @@ static int getlblockinc(Jpeg2000DecoderContext *s)
 return res;
 }
 
-static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+static inline void select_header(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
  int *tp_index)
+{
+s->g = tile->tile_part[*tp_index].header_tpg;
+if (bytestream2_get_bytes_left(>g) == 0 && s->bit_index == 8) {
+if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
+s->g = tile->tile_part[++(*tp_index)].tpg;
+}
+}
+}
+
+static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+ int *tp_index, Jpeg2000CodingStyle *codsty)
 {
 s->g = tile->tile_part[*tp_index].tpg;
 if (bytestream2_get_bytes_left(>g) == 0 && s->bit_index == 8) {
@@ -1048,8 +1092,12 @@ static inline void select_stream(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile,
 s->g = tile->tile_part[++(*tp_index)].tpg;
 }
 }
-if (bytestream2_peek_be32(>g) == JPEG2000_SOP_FIXED_BYTES)
-bytestream2_skip(>g, JPEG2000_SOP_BYTE_LENGTH);
+if (codsty->csty & JPEG2000_CSTY_SOP) {
+if (bytestream2_peek_be32(>g) == JPEG2000_SOP_FIXED_BYTES)
+bytestream2_skip(>g, JPEG2000_SOP_BYTE_LENGTH);
+else
+av_log(s->avctx, AV_LOG_ERROR, "SOP marker not found. instead 
%X\n", bytestream2_peek_be32(>g));
+}
 }
 
 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile 
*tile, int *tp_index,
@@ -1064,10 +1112,12 @@ static int 
jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
 return 0;
 rlevel->band[0].prec[precno].decoded_layers = layno + 1;
 // Select stream to read from
-if (tile->has_ppt)
+if (s->has_ppm)
+select_header(s, tile, tp_index);
+else if (tile->has_ppt)
 s->g = tile->packed_headers_stream;
 else
-select_stream(s, tile, tp_index);
+select_stream(s, tile, tp_index, codsty);
 
 if (!(ret = get_bits(s, 1))) {
 jpeg2000_flush(s);
@@ -1178,9 +1228,12 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile,
 }
 
 // Save state of stream
-if (tile->has_ppt) {
+if (s->has_ppm) {
+tile->tile_part[*tp_index].header_tpg = s->g;
+select_stream(s, tile, tp_index, codsty);
+} else if (tile->has_ppt) {
 tile->packed_headers_stream = s->g;
-select_stream(s, tile, tp_index);
+select_stream(s, tile, tp_index, codsty);
 }
 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
 Jpeg2000Band *band 

[FFmpeg-devel] [PATCH v2 1/4] libavcodec/jpeg2000dec : Prevent overriding SOP marker bit

2020-07-14 Thread gautamramk
From: Gautam Ramakrishnan 

Currently, the COC marker overrides the SOP marker bit.
However, only the COD marker may set this value. This
patch fixes this bug.
---
 libavcodec/jpeg2000dec.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 18a933077e..48ca1c37a5 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -588,7 +588,7 @@ static int get_coc(Jpeg2000DecoderContext *s, 
Jpeg2000CodingStyle *c,
uint8_t *properties)
 {
 int compno, ret;
-uint8_t has_eph;
+uint8_t has_eph, has_sop;
 
 if (bytestream2_get_bytes_left(>g) < 2) {
 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
@@ -606,8 +606,10 @@ static int get_coc(Jpeg2000DecoderContext *s, 
Jpeg2000CodingStyle *c,
 
 c  += compno;
 has_eph = c->csty & JPEG2000_CSTY_EPH;
+has_sop = c->csty & JPEG2000_CSTY_SOP;
 c->csty = bytestream2_get_byteu(>g);
 c->csty |= has_eph; //do not override eph present bits from COD
+c->csty |= has_sop; //do not override sop present bits from COD
 
 if ((ret = get_cox(s, c)) < 0)
 return ret;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v2 2/4] libavcodec/jpeg2000 Fix PCRL Progression Order check

2020-07-14 Thread gautamramk
From: Gautam Ramakrishnan 

The PCRL progression checks were incomplete. This patch
modifes completes the check. Tested on p1_05.j2k.
---
 libavcodec/jpeg2000dec.c | 23 +++
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 48ca1c37a5..adaac192e9 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -1465,23 +1465,30 @@ static int 
jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2
 Jpeg2000Component *comp = tile->comp + compno;
 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
 Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
-int xc = x / s->cdx[compno];
-int yc = y / s->cdy[compno];
+
+if (!s->cdx[compno] || !s->cdy[compno])
+return AVERROR_INVALIDDATA;
 
 for (reslevelno = RSpoc; reslevelno < 
FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
 unsigned prcx, prcy;
 uint8_t reducedresno = codsty->nreslevels - 1 
-reslevelno; //  ==> N_L - r
 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
+int trx0, try0;
 
-if (yc % (1LL << (rlevel->log2_prec_height + 
reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
-continue;
+trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], 
s->cdx[compno] << reducedresno);
+try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], 
s->cdy[compno] << reducedresno);
 
-if (xc % (1LL << (rlevel->log2_prec_width + 
reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
-continue;
+if (!(y % ((uint64_t)s->cdy[compno] << 
(rlevel->log2_prec_height + reducedresno)) == 0 ||
+ (y == tile->coord[1][0] && (try0 << reducedresno) 
% (1U << (reducedresno + rlevel->log2_prec_height)
+ continue;
+
+if (!(x % ((uint64_t)s->cdx[compno] << 
(rlevel->log2_prec_width + reducedresno)) == 0 ||
+ (x == tile->coord[0][0] && (trx0 << reducedresno) 
% (1U << (reducedresno + rlevel->log2_prec_width)
+ continue;
 
 // check if a precinct exists
-prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> 
rlevel->log2_prec_width;
-prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> 
rlevel->log2_prec_height;
+prcx   = ff_jpeg2000_ceildiv(x, s->cdx[compno] << 
reducedresno) >> rlevel->log2_prec_width;
+prcy   = ff_jpeg2000_ceildiv(y, s->cdy[compno] << 
reducedresno) >> rlevel->log2_prec_height;
 prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], 
reducedresno) >> rlevel->log2_prec_width;
 prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], 
reducedresno) >> rlevel->log2_prec_height;
 
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v2 3/4] libavcodec/jpeg2000 Fix RPCL Progression order check

2020-07-14 Thread gautamramk
From: Gautam Ramakrishnan 

The RPCL progression order check was incomplete. This
patch completes the check. Tested on p1_07.j2k.
---
 libavcodec/jpeg2000dec.c | 18 --
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index adaac192e9..5ea6fd0b9a 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -1395,22 +1395,28 @@ static int 
jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2
 uint8_t reducedresno = codsty->nreslevels - 1 
-reslevelno; //  ==> N_L - r
 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
 unsigned prcx, prcy;
+int trx0, try0;
 
-int xc = x / s->cdx[compno];
-int yc = y / s->cdy[compno];
+if (!s->cdx[compno] || !s->cdy[compno])
+return AVERROR_INVALIDDATA;
+
+trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], 
s->cdx[compno] << reducedresno);
+try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], 
s->cdy[compno] << reducedresno);
 
 if (reslevelno >= codsty->nreslevels)
 continue;
 
-if (yc % (1LL << (rlevel->log2_prec_height + 
reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
+if (!(y % ((uint64_t)s->cdy[compno] << 
(rlevel->log2_prec_height + reducedresno)) == 0 ||
+ (y == tile->coord[1][0] && (try0 << reducedresno) 
% (1U << (reducedresno + rlevel->log2_prec_height)
 continue;
 
-if (xc % (1LL << (rlevel->log2_prec_width + 
reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
+if (!(x % ((uint64_t)s->cdx[compno] << 
(rlevel->log2_prec_width + reducedresno)) == 0 ||
+ (x == tile->coord[0][0] && (trx0 << reducedresno) 
% (1U << (reducedresno + rlevel->log2_prec_width)
 continue;
 
 // check if a precinct exists
-prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> 
rlevel->log2_prec_width;
-prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> 
rlevel->log2_prec_height;
+prcx   = ff_jpeg2000_ceildiv(x, s->cdx[compno] << 
reducedresno) >> rlevel->log2_prec_width;
+prcy   = ff_jpeg2000_ceildiv(y, s->cdy[compno] << 
reducedresno) >> rlevel->log2_prec_height;
 prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], 
reducedresno) >> rlevel->log2_prec_width;
 prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], 
reducedresno) >> rlevel->log2_prec_height;
 
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 3/4] libavcodec/jpeg2000 Fix RPCL Progression order check

2020-07-13 Thread gautamramk
From: Gautam Ramakrishnan 

The RPCL progression order check was incomplete. This
patch completes the check. Tested on p1_07.j2k.
---
 libavcodec/jpeg2000dec.c | 18 --
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index adaac192e9..5ea6fd0b9a 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -1395,22 +1395,28 @@ static int 
jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2
 uint8_t reducedresno = codsty->nreslevels - 1 
-reslevelno; //  ==> N_L - r
 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
 unsigned prcx, prcy;
+int trx0, try0;
 
-int xc = x / s->cdx[compno];
-int yc = y / s->cdy[compno];
+if (!s->cdx[compno] || !s->cdy[compno])
+return AVERROR_INVALIDDATA;
+
+trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], 
s->cdx[compno] << reducedresno);
+try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], 
s->cdy[compno] << reducedresno);
 
 if (reslevelno >= codsty->nreslevels)
 continue;
 
-if (yc % (1LL << (rlevel->log2_prec_height + 
reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
+if (!(y % ((uint64_t)s->cdy[compno] << 
(rlevel->log2_prec_height + reducedresno)) == 0 ||
+ (y == tile->coord[1][0] && (try0 << reducedresno) 
% (1U << (reducedresno + rlevel->log2_prec_height)
 continue;
 
-if (xc % (1LL << (rlevel->log2_prec_width + 
reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
+if (!(x % ((uint64_t)s->cdx[compno] << 
(rlevel->log2_prec_width + reducedresno)) == 0 ||
+ (x == tile->coord[0][0] && (trx0 << reducedresno) 
% (1U << (reducedresno + rlevel->log2_prec_width)
 continue;
 
 // check if a precinct exists
-prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> 
rlevel->log2_prec_width;
-prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> 
rlevel->log2_prec_height;
+prcx   = ff_jpeg2000_ceildiv(x, s->cdx[compno] << 
reducedresno) >> rlevel->log2_prec_width;
+prcy   = ff_jpeg2000_ceildiv(y, s->cdy[compno] << 
reducedresno) >> rlevel->log2_prec_height;
 prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], 
reducedresno) >> rlevel->log2_prec_width;
 prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], 
reducedresno) >> rlevel->log2_prec_height;
 
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 2/4] libavcodec/jpeg2000 Fix PCRL Progression Order check

2020-07-13 Thread gautamramk
From: Gautam Ramakrishnan 

The PCRL progression checks were incomplete. This patch
modifes completes the check. Tested on p1_05.j2k.
---
 libavcodec/jpeg2000dec.c | 23 +++
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 48ca1c37a5..adaac192e9 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -1465,23 +1465,30 @@ static int 
jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2
 Jpeg2000Component *comp = tile->comp + compno;
 Jpeg2000CodingStyle *codsty = tile->codsty + compno;
 Jpeg2000QuantStyle *qntsty  = tile->qntsty + compno;
-int xc = x / s->cdx[compno];
-int yc = y / s->cdy[compno];
+
+if (!s->cdx[compno] || !s->cdy[compno])
+return AVERROR_INVALIDDATA;
 
 for (reslevelno = RSpoc; reslevelno < 
FFMIN(codsty->nreslevels, REpoc); reslevelno++) {
 unsigned prcx, prcy;
 uint8_t reducedresno = codsty->nreslevels - 1 
-reslevelno; //  ==> N_L - r
 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
+int trx0, try0;
 
-if (yc % (1LL << (rlevel->log2_prec_height + 
reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
-continue;
+trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], 
s->cdx[compno] << reducedresno);
+try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], 
s->cdy[compno] << reducedresno);
 
-if (xc % (1LL << (rlevel->log2_prec_width + 
reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
-continue;
+if (!(y % ((uint64_t)s->cdy[compno] << 
(rlevel->log2_prec_height + reducedresno)) == 0 ||
+ (y == tile->coord[1][0] && (try0 << reducedresno) 
% (1U << (reducedresno + rlevel->log2_prec_height)
+ continue;
+
+if (!(x % ((uint64_t)s->cdx[compno] << 
(rlevel->log2_prec_width + reducedresno)) == 0 ||
+ (x == tile->coord[0][0] && (trx0 << reducedresno) 
% (1U << (reducedresno + rlevel->log2_prec_width)
+ continue;
 
 // check if a precinct exists
-prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> 
rlevel->log2_prec_width;
-prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> 
rlevel->log2_prec_height;
+prcx   = ff_jpeg2000_ceildiv(x, s->cdx[compno] << 
reducedresno) >> rlevel->log2_prec_width;
+prcy   = ff_jpeg2000_ceildiv(y, s->cdy[compno] << 
reducedresno) >> rlevel->log2_prec_height;
 prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], 
reducedresno) >> rlevel->log2_prec_width;
 prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], 
reducedresno) >> rlevel->log2_prec_height;
 
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 4/4] libavcodec/jpeg2000dec: Support for PPM marker

2020-07-13 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support for PPM marker for JPEG2000
decoder. It allows the samples p1_03.j2k and p1_05.j2k
to be decoded.
---
 libavcodec/jpeg2000dec.c | 107 +++
 1 file changed, 97 insertions(+), 10 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 5ea6fd0b9a..f6200a81ed 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -71,6 +71,7 @@ typedef struct 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;
 
@@ -102,6 +103,13 @@ typedef struct Jpeg2000DecoderContext {
 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;
@@ -928,6 +936,31 @@ static int get_plt(Jpeg2000DecoderContext *s, int n)
 return 0;
 }
 
+static int get_ppm(Jpeg2000DecoderContext *s, int n)
+{
+void *new;
+
+if (n < 3) {
+av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPM data.\n");
+return AVERROR_INVALIDDATA;
+}
+s->has_ppm = 1;
+bytestream2_get_byte(>g); //Zppm is skipped and not used
+new = av_realloc(s->packed_headers,
+ s->packed_headers_size + n - 3);
+if (new) {
+s->packed_headers = new;
+} else
+return AVERROR(ENOMEM);
+memset(>packed_headers_stream, 0, sizeof(s->packed_headers_stream));
+memcpy(s->packed_headers + s->packed_headers_size,
+   s->g.buffer, n - 3);
+s->packed_headers_size += n - 3;
+bytestream2_skip(>g, n - 3);
+
+return 0;
+}
+
 static int get_ppt(Jpeg2000DecoderContext *s, int n)
 {
 Jpeg2000Tile *tile;
@@ -1039,8 +1072,19 @@ static int getlblockinc(Jpeg2000DecoderContext *s)
 return res;
 }
 
-static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+static inline void select_header(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
  int *tp_index)
+{
+s->g = tile->tile_part[*tp_index].header_tpg;
+if (bytestream2_get_bytes_left(>g) == 0 && s->bit_index == 8) {
+if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
+s->g = tile->tile_part[++(*tp_index)].tpg;
+}
+}
+}
+
+static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+ int *tp_index, Jpeg2000CodingStyle *codsty)
 {
 s->g = tile->tile_part[*tp_index].tpg;
 if (bytestream2_get_bytes_left(>g) == 0 && s->bit_index == 8) {
@@ -1048,8 +1092,12 @@ static inline void select_stream(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile,
 s->g = tile->tile_part[++(*tp_index)].tpg;
 }
 }
-if (bytestream2_peek_be32(>g) == JPEG2000_SOP_FIXED_BYTES)
-bytestream2_skip(>g, JPEG2000_SOP_BYTE_LENGTH);
+if (codsty->csty & JPEG2000_CSTY_SOP) {
+if (bytestream2_peek_be32(>g) == JPEG2000_SOP_FIXED_BYTES)
+bytestream2_skip(>g, JPEG2000_SOP_BYTE_LENGTH);
+else
+av_log(s->avctx, AV_LOG_ERROR, "SOP marker not found. instead 
%X\n", bytestream2_peek_be32(>g));
+}
 }
 
 static int jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile 
*tile, int *tp_index,
@@ -1064,10 +1112,12 @@ static int 
jpeg2000_decode_packet(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
 return 0;
 rlevel->band[0].prec[precno].decoded_layers = layno + 1;
 // Select stream to read from
-if (tile->has_ppt)
+if (s->has_ppm)
+select_header(s, tile, tp_index);
+else if (tile->has_ppt)
 s->g = tile->packed_headers_stream;
 else
-select_stream(s, tile, tp_index);
+select_stream(s, tile, tp_index, codsty);
 
 if (!(ret = get_bits(s, 1))) {
 jpeg2000_flush(s);
@@ -1178,9 +1228,12 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile,
 }
 
 // Save state of stream
-if (tile->has_ppt) {
+if (s->has_ppm) {
+tile->tile_part[*tp_index].header_tpg = s->g;
+select_stream(s, tile, tp_index, codsty);
+} else if (tile->has_ppt) {
 tile->packed_headers_stream = s->g;
-select_stream(s, tile, tp_index);
+select_stream(s, tile, tp_index, codsty);
 }
 for (bandno = 0; bandno < rlevel->nbands; bandno++) {
 Jpeg2000Band *band 

[FFmpeg-devel] [PATCH 1/4] libavcodec/jpeg2000dec : Prevent overriding SOP marker bit

2020-07-13 Thread gautamramk
From: Gautam Ramakrishnan 

Currently, the COC marker overrides the SOP marker bit.
However, only the COD marker may set this value. This
patch fixes this bug.
---
 libavcodec/jpeg2000dec.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 18a933077e..48ca1c37a5 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -588,7 +588,7 @@ static int get_coc(Jpeg2000DecoderContext *s, 
Jpeg2000CodingStyle *c,
uint8_t *properties)
 {
 int compno, ret;
-uint8_t has_eph;
+uint8_t has_eph, has_sop;
 
 if (bytestream2_get_bytes_left(>g) < 2) {
 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for COC\n");
@@ -606,8 +606,10 @@ static int get_coc(Jpeg2000DecoderContext *s, 
Jpeg2000CodingStyle *c,
 
 c  += compno;
 has_eph = c->csty & JPEG2000_CSTY_EPH;
+has_sop = c->csty & JPEG2000_CSTY_SOP;
 c->csty = bytestream2_get_byteu(>g);
 c->csty |= has_eph; //do not override eph present bits from COD
+c->csty |= has_sop; //do not override sop present bits from COD
 
 if ((ret = get_cox(s, c)) < 0)
 return ret;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH] libavcodec/jpeg2000dec: Fix RPCL progression order

2020-07-09 Thread gautamramk
From: Gautam Ramakrishnan 

This patch fixes a check in the RPCL progression order,
tested on p1_07.j2k.
---
 libavcodec/jpeg2000dec.c | 19 ---
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 18a933077e..3ea939f7d9 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -1393,22 +1393,28 @@ static int 
jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2
 uint8_t reducedresno = codsty->nreslevels - 1 
-reslevelno; //  ==> N_L - r
 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
 unsigned prcx, prcy;
+int trx0, try0;
 
-int xc = x / s->cdx[compno];
-int yc = y / s->cdy[compno];
+if (!s->cdx[compno] || !s->cdy[compno])
+return AVERROR_INVALIDDATA;
+
+trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], 
s->cdx[compno] << reslevelno);
+try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], 
s->cdy[compno] << reslevelno);
 
 if (reslevelno >= codsty->nreslevels)
 continue;
 
-if (yc % (1LL << (rlevel->log2_prec_height + 
reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
+if (!(y % ((uint64_t)s->cdy[compno] << 
(rlevel->log2_prec_height + reducedresno)) == 0 ||
+ (y == tile->coord[1][0] && (try0 << reducedresno) 
% (1U << (reducedresno + rlevel->log2_prec_height)
 continue;
 
-if (xc % (1LL << (rlevel->log2_prec_width + 
reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
+if (!(x % ((uint64_t)s->cdx[compno] << 
(rlevel->log2_prec_width + reducedresno)) == 0 ||
+ (x == tile->coord[0][0] && (trx0 << reducedresno) 
% (1U << (reducedresno + rlevel->log2_prec_width)
 continue;
 
 // check if a precinct exists
-prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> 
rlevel->log2_prec_width;
-prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> 
rlevel->log2_prec_height;
+prcx   = ff_jpeg2000_ceildiv(x, s->cdx[compno] << 
reducedresno) >> rlevel->log2_prec_width;
+prcy   = ff_jpeg2000_ceildiv(y, s->cdy[compno] << 
reducedresno) >> rlevel->log2_prec_height;
 prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], 
reducedresno) >> rlevel->log2_prec_width;
 prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], 
reducedresno) >> rlevel->log2_prec_height;
 
@@ -1906,7 +1912,6 @@ static inline void tile_codeblocks(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile
 continue;
 x = cblk->coord[0][0] - band->coord[0][0];
 y = cblk->coord[1][0] - band->coord[1][0];
-
 if (comp->roi_shift)
 roi_scale_cblk(cblk, comp, );
 if (codsty->transform == FF_DWT97)
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH] libavcodec/jpeg2000dec: Fix RPCL progression order

2020-07-08 Thread gautamramk
From: Gautam Ramakrishnan 

This patch fixes a check in the RPCL progression order,
making it similar to the openjpeg implementation.
---
 libavcodec/jpeg2000dec.c | 19 ---
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 18a933077e..3ea939f7d9 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -1393,22 +1393,28 @@ static int 
jpeg2000_decode_packets_po_iteration(Jpeg2000DecoderContext *s, Jpeg2
 uint8_t reducedresno = codsty->nreslevels - 1 
-reslevelno; //  ==> N_L - r
 Jpeg2000ResLevel *rlevel = comp->reslevel + reslevelno;
 unsigned prcx, prcy;
+int trx0, try0;
 
-int xc = x / s->cdx[compno];
-int yc = y / s->cdy[compno];
+if (!s->cdx[compno] || !s->cdy[compno])
+return AVERROR_INVALIDDATA;
+
+trx0 = ff_jpeg2000_ceildiv(tile->coord[0][0], 
s->cdx[compno] << reslevelno);
+try0 = ff_jpeg2000_ceildiv(tile->coord[1][0], 
s->cdy[compno] << reslevelno);
 
 if (reslevelno >= codsty->nreslevels)
 continue;
 
-if (yc % (1LL << (rlevel->log2_prec_height + 
reducedresno)) && y != tile->coord[1][0]) //FIXME this is a subset of the check
+if (!(y % ((uint64_t)s->cdy[compno] << 
(rlevel->log2_prec_height + reducedresno)) == 0 ||
+ (y == tile->coord[1][0] && (try0 << reducedresno) 
% (1U << (reducedresno + rlevel->log2_prec_height)
 continue;
 
-if (xc % (1LL << (rlevel->log2_prec_width + 
reducedresno)) && x != tile->coord[0][0]) //FIXME this is a subset of the check
+if (!(x % ((uint64_t)s->cdx[compno] << 
(rlevel->log2_prec_width + reducedresno)) == 0 ||
+ (x == tile->coord[0][0] && (trx0 << reducedresno) 
% (1U << (reducedresno + rlevel->log2_prec_width)
 continue;
 
 // check if a precinct exists
-prcx   = ff_jpeg2000_ceildivpow2(xc, reducedresno) >> 
rlevel->log2_prec_width;
-prcy   = ff_jpeg2000_ceildivpow2(yc, reducedresno) >> 
rlevel->log2_prec_height;
+prcx   = ff_jpeg2000_ceildiv(x, s->cdx[compno] << 
reducedresno) >> rlevel->log2_prec_width;
+prcy   = ff_jpeg2000_ceildiv(y, s->cdy[compno] << 
reducedresno) >> rlevel->log2_prec_height;
 prcx  -= ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], 
reducedresno) >> rlevel->log2_prec_width;
 prcy  -= ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], 
reducedresno) >> rlevel->log2_prec_height;
 
@@ -1906,7 +1912,6 @@ static inline void tile_codeblocks(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile
 continue;
 x = cblk->coord[0][0] - band->coord[0][0];
 y = cblk->coord[1][0] - band->coord[1][0];
-
 if (comp->roi_shift)
 roi_scale_cblk(cblk, comp, );
 if (codsty->transform == FF_DWT97)
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 1/2] libavcodec/jpeg2000dec: Enhance pix fmt selection

2020-07-04 Thread gautamramk
From: Gautam Ramakrishnan 

This patch assigns default pix format values when
a match does not take place.
---
 libavcodec/jpeg2000dec.c | 12 
 1 file changed, 12 insertions(+)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 3f4a9ef96c..f62553c874 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -436,6 +436,18 @@ static int get_siz(Jpeg2000DecoderContext *s)
 s->cdef[3] = 3;
 i = 0;
 }
+} else if (ncomponents == 3 && s->precision == 8 &&
+   s->cdx[0] == s->cdx[1] && s->cdx[0] == s->cdx[2] &&
+   s->cdy[0] == s->cdy[1] && s->cdy[0] == s->cdy[2]) {
+s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
+i = 0;
+} else if (ncomponents == 2 && s->precision == 8 &&
+   s->cdx[0] == s->cdx[1] && s->cdy[0] == s->cdy[1]) {
+s->avctx->pix_fmt = AV_PIX_FMT_YA8;
+i = 0;
+} else if (ncomponents == 1 && s->precision == 8) {
+s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
+i = 0;
 }
 }
 
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 2/2] libavcodec/jpeg2000dec.c: Enable image offsets

2020-07-04 Thread gautamramk
From: Gautam Ramakrishnan 

This patch enables support for image offsets.
---
 libavcodec/jpeg2000dec.c | 4 
 1 file changed, 4 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index f62553c874..18a933077e 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -288,10 +288,6 @@ static int get_siz(Jpeg2000DecoderContext *s)
 s->tile_offset_y  = bytestream2_get_be32u(>g); // YT0Siz
 ncomponents   = bytestream2_get_be16u(>g); // CSiz
 
-if (s->image_offset_x || s->image_offset_y) {
-avpriv_request_sample(s->avctx, "Support for image offsets");
-return AVERROR_PATCHWELCOME;
-}
 if (av_image_check_size2(s->width, s->height, s->avctx->max_pixels, 
AV_PIX_FMT_NONE, 0, s->avctx)) {
 avpriv_request_sample(s->avctx, "Large Dimensions");
 return AVERROR_PATCHWELCOME;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v7 1/2] libavcodec/pgxdec: Add PGX decoder

2020-07-02 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds a pgx decoder.
---
 Changelog   |   1 +
 doc/general.texi|   2 +
 libavcodec/Makefile |   1 +
 libavcodec/allcodecs.c  |   1 +
 libavcodec/codec_desc.c |   7 ++
 libavcodec/codec_id.h   |   1 +
 libavcodec/pgxdec.c | 169 
 libavcodec/version.h|   4 +-
 8 files changed, 184 insertions(+), 2 deletions(-)
 create mode 100644 libavcodec/pgxdec.c

diff --git a/Changelog b/Changelog
index a60e7d2eb8..1bb9931c0d 100644
--- a/Changelog
+++ b/Changelog
@@ -4,6 +4,7 @@ releases are sorted from youngest to oldest.
 version :
 - AudioToolbox output device
 - MacCaption demuxer
+- PGX decoder
 
 
 version 4.3:
diff --git a/doc/general.texi b/doc/general.texi
index ea34b963b5..53d556c56c 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -751,6 +751,8 @@ following image formats are supported:
 @tab Portable GrayMap image
 @item PGMYUV   @tab X @tab X
 @tab PGM with U and V components in YUV 4:2:0
+@item PGX  @tab   @tab X
+@tab PGX file decoder
 @item PIC  @tab @tab X
 @tab Pictor/PC Paint
 @item PNG  @tab X @tab X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 5a6ea59715..12aa43fe51 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -536,6 +536,7 @@ OBJS-$(CONFIG_PGM_ENCODER) += pnmenc.o
 OBJS-$(CONFIG_PGMYUV_DECODER)  += pnmdec.o pnm.o
 OBJS-$(CONFIG_PGMYUV_ENCODER)  += pnmenc.o
 OBJS-$(CONFIG_PGSSUB_DECODER)  += pgssubdec.o
+OBJS-$(CONFIG_PGX_DECODER) += pgxdec.o
 OBJS-$(CONFIG_PICTOR_DECODER)  += pictordec.o cga_data.o
 OBJS-$(CONFIG_PIXLET_DECODER)  += pixlet.o
 OBJS-$(CONFIG_PJS_DECODER) += textdec.o ass.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index fa0c08d42e..a5048290f7 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -238,6 +238,7 @@ extern AVCodec ff_pgm_encoder;
 extern AVCodec ff_pgm_decoder;
 extern AVCodec ff_pgmyuv_encoder;
 extern AVCodec ff_pgmyuv_decoder;
+extern AVCodec ff_pgx_decoder;
 extern AVCodec ff_pictor_decoder;
 extern AVCodec ff_pixlet_decoder;
 extern AVCodec ff_png_encoder;
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 9f8847544f..67e0a3055c 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1405,6 +1405,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
 .long_name = NULL_IF_CONFIG_SMALL("AVS2-P2/IEEE1857.4"),
 .props = AV_CODEC_PROP_LOSSY,
 },
+{
+.id= AV_CODEC_ID_PGX,
+.type  = AVMEDIA_TYPE_VIDEO,
+.name  = "pgx",
+.long_name = NULL_IF_CONFIG_SMALL("PGX (JPEG2000 Test Format)"),
+.props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
+},
 {
 .id= AV_CODEC_ID_Y41P,
 .type  = AVMEDIA_TYPE_VIDEO,
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index d885962c9c..896ecb0ce0 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -241,6 +241,7 @@ enum AVCodecID {
 AV_CODEC_ID_SCREENPRESSO,
 AV_CODEC_ID_RSCC,
 AV_CODEC_ID_AVS2,
+AV_CODEC_ID_PGX,
 
 AV_CODEC_ID_Y41P = 0x8000,
 AV_CODEC_ID_AVRP,
diff --git a/libavcodec/pgxdec.c b/libavcodec/pgxdec.c
new file mode 100644
index 00..ee43278250
--- /dev/null
+++ b/libavcodec/pgxdec.c
@@ -0,0 +1,169 @@
+/*
+ * PGX image format
+ * Copyright (c) 2020 Gautam Ramakrishnan
+ *
+ * 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
+ */
+
+#include "avcodec.h"
+#include "internal.h"
+#include "bytestream.h"
+#include "libavutil/imgutils.h"
+
+static int pgx_get_number(AVCodecContext *avctx, GetByteContext *g, int 
*number) {
+int ret = AVERROR_INVALIDDATA;
+char digit;
+
+*number = 0;
+while (1) {
+uint64_t temp;
+if (!bytestream2_get_bytes_left(g))
+return AVERROR_INVALIDDATA;
+digit = bytestream2_get_byte(g);
+if (digit == ' ' || digit == 0xA || digit == 0xD)
+break;
+else if (digit < '0' || digit > '9')
+return AVERROR_INVALIDDATA;
+
+temp = (uint64_t)10 * (*number) + (digit - '0');
+if (temp > INT_MAX)
+   

[FFmpeg-devel] [PATCH v7 2/2] libavformat/img2dec: Added pgx demuxer

2020-07-02 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support to demux pgx file
format.
---
 libavformat/allformats.c | 1 +
 libavformat/img2dec.c| 9 +
 libavformat/version.h| 2 +-
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 97fd06debb..f8527b1fd4 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -488,6 +488,7 @@ extern AVInputFormat  ff_image_pbm_pipe_demuxer;
 extern AVInputFormat  ff_image_pcx_pipe_demuxer;
 extern AVInputFormat  ff_image_pgmyuv_pipe_demuxer;
 extern AVInputFormat  ff_image_pgm_pipe_demuxer;
+extern AVInputFormat  ff_image_pgx_pipe_demuxer;
 extern AVInputFormat  ff_image_pictor_pipe_demuxer;
 extern AVInputFormat  ff_image_png_pipe_demuxer;
 extern AVInputFormat  ff_image_ppm_pipe_demuxer;
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index ee7ceed08f..a7e89cd056 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -1000,6 +1000,14 @@ static int pgmyuv_probe(const AVProbeData *p) // custom 
FFmpeg format recognized
 return ret && av_match_ext(p->filename, "pgmyuv") ? ret : 0;
 }
 
+static int pgx_probe(const AVProbeData *p)
+{
+const uint8_t *b = p->buf;
+if (!memcmp(b, "PG ML ", 6))
+return AVPROBE_SCORE_EXTENSION + 1;
+return 0;
+}
+
 static int ppm_probe(const AVProbeData *p)
 {
 return pnm_magic_check(p, 3) || pnm_magic_check(p, 6) ? pnm_probe(p) : 0;
@@ -1094,6 +1102,7 @@ IMAGEAUTO_DEMUXER(pbm, AV_CODEC_ID_PBM)
 IMAGEAUTO_DEMUXER(pcx, AV_CODEC_ID_PCX)
 IMAGEAUTO_DEMUXER(pgm, AV_CODEC_ID_PGM)
 IMAGEAUTO_DEMUXER(pgmyuv,  AV_CODEC_ID_PGMYUV)
+IMAGEAUTO_DEMUXER(pgx, AV_CODEC_ID_PGX)
 IMAGEAUTO_DEMUXER(pictor,  AV_CODEC_ID_PICTOR)
 IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
 IMAGEAUTO_DEMUXER(ppm, AV_CODEC_ID_PPM)
diff --git a/libavformat/version.h b/libavformat/version.h
index 3c1957b00c..75c03fde0a 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,7 +32,7 @@
 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  58
-#define LIBAVFORMAT_VERSION_MINOR  47
+#define LIBAVFORMAT_VERSION_MINOR  48
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 2/2] libavcodec/jpeg2000dec.c: Enable image offsets

2020-07-01 Thread gautamramk
From: Gautam Ramakrishnan 

This patch enables support for image offsets.
---
 libavcodec/jpeg2000dec.c | 4 
 1 file changed, 4 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 86f9170723..f91406ab9e 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -288,10 +288,6 @@ static int get_siz(Jpeg2000DecoderContext *s)
 s->tile_offset_y  = bytestream2_get_be32u(>g); // YT0Siz
 ncomponents   = bytestream2_get_be16u(>g); // CSiz
 
-if (s->image_offset_x || s->image_offset_y) {
-avpriv_request_sample(s->avctx, "Support for image offsets");
-return AVERROR_PATCHWELCOME;
-}
 if (av_image_check_size2(s->width, s->height, s->avctx->max_pixels, 
AV_PIX_FMT_NONE, 0, s->avctx)) {
 avpriv_request_sample(s->avctx, "Large Dimensions");
 return AVERROR_PATCHWELCOME;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 1/2] libavcodec/jpeg2000dec: Enhance pix fmt selection

2020-07-01 Thread gautamramk
From: Gautam Ramakrishnan 

This patch assigns default pix format values when
a match does not take place.
---
 libavcodec/jpeg2000dec.c | 9 +
 1 file changed, 9 insertions(+)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 3f4a9ef96c..86f9170723 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -436,6 +436,15 @@ static int get_siz(Jpeg2000DecoderContext *s)
 s->cdef[3] = 3;
 i = 0;
 }
+} else if (ncomponents == 3 && s->precision == 8) {
+s->avctx->pix_fmt = AV_PIX_FMT_RGB24;
+i = 0;
+} else if (ncomponents == 2 && s->precision == 8) {
+s->avctx->pix_fmt = AV_PIX_FMT_YA8;
+i = 0;
+} else if (ncomponents == 1 && s->precision == 8) {
+s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
+i = 0;
 }
 }
 
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v6 1/2] libavcodec/pgxdec: Add PGX decoder

2020-06-30 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds a pgx decoder.
---
 Changelog   |   1 +
 doc/general.texi|   2 +
 libavcodec/Makefile |   1 +
 libavcodec/allcodecs.c  |   1 +
 libavcodec/codec_desc.c |   7 ++
 libavcodec/codec_id.h   |   1 +
 libavcodec/pgxdec.c | 199 
 libavcodec/version.h|   2 +-
 8 files changed, 213 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/pgxdec.c

diff --git a/Changelog b/Changelog
index a60e7d2eb8..1bb9931c0d 100644
--- a/Changelog
+++ b/Changelog
@@ -4,6 +4,7 @@ releases are sorted from youngest to oldest.
 version :
 - AudioToolbox output device
 - MacCaption demuxer
+- PGX decoder
 
 
 version 4.3:
diff --git a/doc/general.texi b/doc/general.texi
index ea34b963b5..53d556c56c 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -751,6 +751,8 @@ following image formats are supported:
 @tab Portable GrayMap image
 @item PGMYUV   @tab X @tab X
 @tab PGM with U and V components in YUV 4:2:0
+@item PGX  @tab   @tab X
+@tab PGX file decoder
 @item PIC  @tab @tab X
 @tab Pictor/PC Paint
 @item PNG  @tab X @tab X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 5a6ea59715..12aa43fe51 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -536,6 +536,7 @@ OBJS-$(CONFIG_PGM_ENCODER) += pnmenc.o
 OBJS-$(CONFIG_PGMYUV_DECODER)  += pnmdec.o pnm.o
 OBJS-$(CONFIG_PGMYUV_ENCODER)  += pnmenc.o
 OBJS-$(CONFIG_PGSSUB_DECODER)  += pgssubdec.o
+OBJS-$(CONFIG_PGX_DECODER) += pgxdec.o
 OBJS-$(CONFIG_PICTOR_DECODER)  += pictordec.o cga_data.o
 OBJS-$(CONFIG_PIXLET_DECODER)  += pixlet.o
 OBJS-$(CONFIG_PJS_DECODER) += textdec.o ass.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index fa0c08d42e..a5048290f7 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -238,6 +238,7 @@ extern AVCodec ff_pgm_encoder;
 extern AVCodec ff_pgm_decoder;
 extern AVCodec ff_pgmyuv_encoder;
 extern AVCodec ff_pgmyuv_decoder;
+extern AVCodec ff_pgx_decoder;
 extern AVCodec ff_pictor_decoder;
 extern AVCodec ff_pixlet_decoder;
 extern AVCodec ff_png_encoder;
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 9f8847544f..67e0a3055c 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1405,6 +1405,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
 .long_name = NULL_IF_CONFIG_SMALL("AVS2-P2/IEEE1857.4"),
 .props = AV_CODEC_PROP_LOSSY,
 },
+{
+.id= AV_CODEC_ID_PGX,
+.type  = AVMEDIA_TYPE_VIDEO,
+.name  = "pgx",
+.long_name = NULL_IF_CONFIG_SMALL("PGX (JPEG2000 Test Format)"),
+.props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
+},
 {
 .id= AV_CODEC_ID_Y41P,
 .type  = AVMEDIA_TYPE_VIDEO,
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index d885962c9c..896ecb0ce0 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -241,6 +241,7 @@ enum AVCodecID {
 AV_CODEC_ID_SCREENPRESSO,
 AV_CODEC_ID_RSCC,
 AV_CODEC_ID_AVS2,
+AV_CODEC_ID_PGX,
 
 AV_CODEC_ID_Y41P = 0x8000,
 AV_CODEC_ID_AVRP,
diff --git a/libavcodec/pgxdec.c b/libavcodec/pgxdec.c
new file mode 100644
index 00..c9e07c1e55
--- /dev/null
+++ b/libavcodec/pgxdec.c
@@ -0,0 +1,199 @@
+/*
+ * PGX image format
+ * Copyright (c) 2020 Gautam Ramakrishnan
+ *
+ * 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
+ */
+
+#include "avcodec.h"
+#include "internal.h"
+#include "bytestream.h"
+#include "libavutil/imgutils.h"
+
+typedef struct PGXContext {
+GetByteContext  g;
+} PGXContext;
+
+static int pgx_get_number(AVCodecContext *avctx, PGXContext * const s) {
+int ret = -1;
+char digit;
+
+while (1) {
+uint64_t temp;
+if (!bytestream2_get_bytes_left(>g))
+return -1;
+digit = bytestream2_get_byte(>g);
+if (digit == ' ' || digit == 0xA || digit == 0xD)
+break;
+else if (digit < '0' || digit > '9')
+return -1;
+
+if (ret < 0)
+ret = 0;
+
+temp = 10L * ret + (digit - '0');
+if 

[FFmpeg-devel] [PATCH v6 2/2] libavformat/img2dec: Added pgx demuxer

2020-06-30 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support to demux pgx file
format.
---
 libavformat/allformats.c | 1 +
 libavformat/img2dec.c| 9 +
 libavformat/version.h| 2 +-
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 97fd06debb..f8527b1fd4 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -488,6 +488,7 @@ extern AVInputFormat  ff_image_pbm_pipe_demuxer;
 extern AVInputFormat  ff_image_pcx_pipe_demuxer;
 extern AVInputFormat  ff_image_pgmyuv_pipe_demuxer;
 extern AVInputFormat  ff_image_pgm_pipe_demuxer;
+extern AVInputFormat  ff_image_pgx_pipe_demuxer;
 extern AVInputFormat  ff_image_pictor_pipe_demuxer;
 extern AVInputFormat  ff_image_png_pipe_demuxer;
 extern AVInputFormat  ff_image_ppm_pipe_demuxer;
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index ee7ceed08f..a7e89cd056 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -1000,6 +1000,14 @@ static int pgmyuv_probe(const AVProbeData *p) // custom 
FFmpeg format recognized
 return ret && av_match_ext(p->filename, "pgmyuv") ? ret : 0;
 }
 
+static int pgx_probe(const AVProbeData *p)
+{
+const uint8_t *b = p->buf;
+if (!memcmp(b, "PG ML ", 6))
+return AVPROBE_SCORE_EXTENSION + 1;
+return 0;
+}
+
 static int ppm_probe(const AVProbeData *p)
 {
 return pnm_magic_check(p, 3) || pnm_magic_check(p, 6) ? pnm_probe(p) : 0;
@@ -1094,6 +1102,7 @@ IMAGEAUTO_DEMUXER(pbm, AV_CODEC_ID_PBM)
 IMAGEAUTO_DEMUXER(pcx, AV_CODEC_ID_PCX)
 IMAGEAUTO_DEMUXER(pgm, AV_CODEC_ID_PGM)
 IMAGEAUTO_DEMUXER(pgmyuv,  AV_CODEC_ID_PGMYUV)
+IMAGEAUTO_DEMUXER(pgx, AV_CODEC_ID_PGX)
 IMAGEAUTO_DEMUXER(pictor,  AV_CODEC_ID_PICTOR)
 IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
 IMAGEAUTO_DEMUXER(ppm, AV_CODEC_ID_PPM)
diff --git a/libavformat/version.h b/libavformat/version.h
index 3c1957b00c..75c03fde0a 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,7 +32,7 @@
 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  58
-#define LIBAVFORMAT_VERSION_MINOR  47
+#define LIBAVFORMAT_VERSION_MINOR  48
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v4 1/2] libavcodec/pgxdec: Add PGX decoder

2020-06-29 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds a pgx decoder.
---
 Changelog   |   1 +
 doc/general.texi|   2 +
 libavcodec/Makefile |   1 +
 libavcodec/allcodecs.c  |   1 +
 libavcodec/codec_desc.c |   7 ++
 libavcodec/codec_id.h   |   1 +
 libavcodec/pgxdec.c | 199 
 libavcodec/version.h|   2 +-
 8 files changed, 213 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/pgxdec.c

diff --git a/Changelog b/Changelog
index a60e7d2eb8..1bb9931c0d 100644
--- a/Changelog
+++ b/Changelog
@@ -4,6 +4,7 @@ releases are sorted from youngest to oldest.
 version :
 - AudioToolbox output device
 - MacCaption demuxer
+- PGX decoder
 
 
 version 4.3:
diff --git a/doc/general.texi b/doc/general.texi
index ea34b963b5..53d556c56c 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -751,6 +751,8 @@ following image formats are supported:
 @tab Portable GrayMap image
 @item PGMYUV   @tab X @tab X
 @tab PGM with U and V components in YUV 4:2:0
+@item PGX  @tab   @tab X
+@tab PGX file decoder
 @item PIC  @tab @tab X
 @tab Pictor/PC Paint
 @item PNG  @tab X @tab X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 5a6ea59715..12aa43fe51 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -536,6 +536,7 @@ OBJS-$(CONFIG_PGM_ENCODER) += pnmenc.o
 OBJS-$(CONFIG_PGMYUV_DECODER)  += pnmdec.o pnm.o
 OBJS-$(CONFIG_PGMYUV_ENCODER)  += pnmenc.o
 OBJS-$(CONFIG_PGSSUB_DECODER)  += pgssubdec.o
+OBJS-$(CONFIG_PGX_DECODER) += pgxdec.o
 OBJS-$(CONFIG_PICTOR_DECODER)  += pictordec.o cga_data.o
 OBJS-$(CONFIG_PIXLET_DECODER)  += pixlet.o
 OBJS-$(CONFIG_PJS_DECODER) += textdec.o ass.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index fa0c08d42e..a5048290f7 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -238,6 +238,7 @@ extern AVCodec ff_pgm_encoder;
 extern AVCodec ff_pgm_decoder;
 extern AVCodec ff_pgmyuv_encoder;
 extern AVCodec ff_pgmyuv_decoder;
+extern AVCodec ff_pgx_decoder;
 extern AVCodec ff_pictor_decoder;
 extern AVCodec ff_pixlet_decoder;
 extern AVCodec ff_png_encoder;
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 9f8847544f..67e0a3055c 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1405,6 +1405,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
 .long_name = NULL_IF_CONFIG_SMALL("AVS2-P2/IEEE1857.4"),
 .props = AV_CODEC_PROP_LOSSY,
 },
+{
+.id= AV_CODEC_ID_PGX,
+.type  = AVMEDIA_TYPE_VIDEO,
+.name  = "pgx",
+.long_name = NULL_IF_CONFIG_SMALL("PGX (JPEG2000 Test Format)"),
+.props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
+},
 {
 .id= AV_CODEC_ID_Y41P,
 .type  = AVMEDIA_TYPE_VIDEO,
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index d885962c9c..896ecb0ce0 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -241,6 +241,7 @@ enum AVCodecID {
 AV_CODEC_ID_SCREENPRESSO,
 AV_CODEC_ID_RSCC,
 AV_CODEC_ID_AVS2,
+AV_CODEC_ID_PGX,
 
 AV_CODEC_ID_Y41P = 0x8000,
 AV_CODEC_ID_AVRP,
diff --git a/libavcodec/pgxdec.c b/libavcodec/pgxdec.c
new file mode 100644
index 00..9c3b18f87d
--- /dev/null
+++ b/libavcodec/pgxdec.c
@@ -0,0 +1,199 @@
+/*
+ * PGX image format
+ * Copyright (c) 2020 Gautam Ramakrishnan
+ *
+ * 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
+ */
+
+#include "avcodec.h"
+#include "internal.h"
+#include "bytestream.h"
+#include "libavutil/imgutils.h"
+
+typedef struct PGXContext {
+GetByteContext  g;
+} PGXContext;
+
+static int pgx_get_number(AVCodecContext *avctx, PGXContext * const s) {
+int ret = -1;
+char digit;
+
+while (1) {
+uint64_t temp;
+if (!bytestream2_get_bytes_left(>g))
+return -1;
+digit = bytestream2_get_byte(>g);
+if (digit == ' ' || digit == 0xA || digit == 0xD)
+break;
+else if (digit < '0' || digit > '9')
+return -1;
+
+if (ret < 0)
+ret = 0;
+
+temp = 10 * ret + (digit - '0');
+if 

[FFmpeg-devel] [PATCH v5] libavformat/img2dec: Added pgx demuxer

2020-06-29 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support to demux pgx file
format.
---
 libavformat/allformats.c | 1 +
 libavformat/img2dec.c| 9 +
 libavformat/version.h| 2 +-
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 97fd06debb..f8527b1fd4 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -488,6 +488,7 @@ extern AVInputFormat  ff_image_pbm_pipe_demuxer;
 extern AVInputFormat  ff_image_pcx_pipe_demuxer;
 extern AVInputFormat  ff_image_pgmyuv_pipe_demuxer;
 extern AVInputFormat  ff_image_pgm_pipe_demuxer;
+extern AVInputFormat  ff_image_pgx_pipe_demuxer;
 extern AVInputFormat  ff_image_pictor_pipe_demuxer;
 extern AVInputFormat  ff_image_png_pipe_demuxer;
 extern AVInputFormat  ff_image_ppm_pipe_demuxer;
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index ee7ceed08f..a7e89cd056 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -1000,6 +1000,14 @@ static int pgmyuv_probe(const AVProbeData *p) // custom 
FFmpeg format recognized
 return ret && av_match_ext(p->filename, "pgmyuv") ? ret : 0;
 }
 
+static int pgx_probe(const AVProbeData *p)
+{
+const uint8_t *b = p->buf;
+if (!memcmp(b, "PG ML ", 6))
+return AVPROBE_SCORE_EXTENSION + 1;
+return 0;
+}
+
 static int ppm_probe(const AVProbeData *p)
 {
 return pnm_magic_check(p, 3) || pnm_magic_check(p, 6) ? pnm_probe(p) : 0;
@@ -1094,6 +1102,7 @@ IMAGEAUTO_DEMUXER(pbm, AV_CODEC_ID_PBM)
 IMAGEAUTO_DEMUXER(pcx, AV_CODEC_ID_PCX)
 IMAGEAUTO_DEMUXER(pgm, AV_CODEC_ID_PGM)
 IMAGEAUTO_DEMUXER(pgmyuv,  AV_CODEC_ID_PGMYUV)
+IMAGEAUTO_DEMUXER(pgx, AV_CODEC_ID_PGX)
 IMAGEAUTO_DEMUXER(pictor,  AV_CODEC_ID_PICTOR)
 IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
 IMAGEAUTO_DEMUXER(ppm, AV_CODEC_ID_PPM)
diff --git a/libavformat/version.h b/libavformat/version.h
index 3c1957b00c..75c03fde0a 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,7 +32,7 @@
 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  58
-#define LIBAVFORMAT_VERSION_MINOR  47
+#define LIBAVFORMAT_VERSION_MINOR  48
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v4 2/2] libavformat/img2dec: Added pgx demuxer

2020-06-29 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support to demux pgx file
format.
---
 libavformat/allformats.c | 1 +
 libavformat/img2dec.c| 9 +
 libavformat/version.h| 2 +-
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 97fd06debb..f8527b1fd4 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -488,6 +488,7 @@ extern AVInputFormat  ff_image_pbm_pipe_demuxer;
 extern AVInputFormat  ff_image_pcx_pipe_demuxer;
 extern AVInputFormat  ff_image_pgmyuv_pipe_demuxer;
 extern AVInputFormat  ff_image_pgm_pipe_demuxer;
+extern AVInputFormat  ff_image_pgx_pipe_demuxer;
 extern AVInputFormat  ff_image_pictor_pipe_demuxer;
 extern AVInputFormat  ff_image_png_pipe_demuxer;
 extern AVInputFormat  ff_image_ppm_pipe_demuxer;
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index ee7ceed08f..b6063bb685 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -1000,6 +1000,14 @@ static int pgmyuv_probe(const AVProbeData *p) // custom 
FFmpeg format recognized
 return ret && av_match_ext(p->filename, "pgmyuv") ? ret : 0;
 }
 
+static int pgx_probe(const AVProbeData *p)
+{
+const uint8_t *b = p->buf;
+if ((AV_RB64(b) & 0x) == 0x5047204D4C20)
+return AVPROBE_SCORE_EXTENSION + 1;
+return 0;
+}
+
 static int ppm_probe(const AVProbeData *p)
 {
 return pnm_magic_check(p, 3) || pnm_magic_check(p, 6) ? pnm_probe(p) : 0;
@@ -1094,6 +1102,7 @@ IMAGEAUTO_DEMUXER(pbm, AV_CODEC_ID_PBM)
 IMAGEAUTO_DEMUXER(pcx, AV_CODEC_ID_PCX)
 IMAGEAUTO_DEMUXER(pgm, AV_CODEC_ID_PGM)
 IMAGEAUTO_DEMUXER(pgmyuv,  AV_CODEC_ID_PGMYUV)
+IMAGEAUTO_DEMUXER(pgx, AV_CODEC_ID_PGX)
 IMAGEAUTO_DEMUXER(pictor,  AV_CODEC_ID_PICTOR)
 IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
 IMAGEAUTO_DEMUXER(ppm, AV_CODEC_ID_PPM)
diff --git a/libavformat/version.h b/libavformat/version.h
index 3c1957b00c..75c03fde0a 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,7 +32,7 @@
 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  58
-#define LIBAVFORMAT_VERSION_MINOR  47
+#define LIBAVFORMAT_VERSION_MINOR  48
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH] libavcodec/jpeg2000.c: Precinct size check removed

2020-06-29 Thread gautamramk
From: Gautam Ramakrishnan 

This patch removes a check which throws an error if
the log2 precinct width/height is 0. The standard allows
the first component to have 0 as the log2 width/height.
However, to ensure proper intialization of coding style,
an extra check has been added.
---
 libavcodec/jpeg2000.c| 3 ---
 libavcodec/jpeg2000.h| 1 +
 libavcodec/jpeg2000dec.c | 6 --
 3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 73206d17f3..1aca31ffa4 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -509,9 +509,6 @@ int ff_jpeg2000_init_component(Jpeg2000Component *comp,
 // update precincts size: 2^n value
 reslevel->log2_prec_width  = codsty->log2_prec_widths[reslevelno];
 reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
-if (!reslevel->log2_prec_width || !reslevel->log2_prec_height) {
-return AVERROR_INVALIDDATA;
-}
 
 /* Number of bands for each resolution level */
 if (reslevelno == 0)
diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index 0f82716981..27df43786d 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -144,6 +144,7 @@ typedef struct Jpeg2000CodingStyle {
 uint8_t prog_order;   // progression order
 uint8_t log2_prec_widths[JPEG2000_MAX_RESLEVELS];  // precincts size 
according resolution levels
 uint8_t log2_prec_heights[JPEG2000_MAX_RESLEVELS]; // TODO: initialize 
prec_size array with 0?
+uint8_t init;
 } Jpeg2000CodingStyle;
 
 typedef struct Jpeg2000QuantStyle {
diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 546a646668..3f4a9ef96c 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -567,7 +567,7 @@ static int get_cod(Jpeg2000DecoderContext *s, 
Jpeg2000CodingStyle *c,
 
 if ((ret = get_cox(s, )) < 0)
 return ret;
-
+tmp.init = 1;
 for (compno = 0; compno < s->ncomponents; compno++)
 if (!(properties[compno] & HAD_COC))
 memcpy(c + compno, , sizeof(tmp));
@@ -605,6 +605,7 @@ static int get_coc(Jpeg2000DecoderContext *s, 
Jpeg2000CodingStyle *c,
 return ret;
 
 properties[compno] |= HAD_COC;
+c->init = 1;
 return 0;
 }
 
@@ -991,7 +992,8 @@ static int init_tile(Jpeg2000DecoderContext *s, int tileno)
 
 if (!comp->roi_shift)
 comp->roi_shift = s->roi_shift[compno];
-
+if (!codsty->init)
+return AVERROR_INVALIDDATA;
 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
  s->cbps[compno], s->cdx[compno],
  s->cdy[compno], s->avctx))
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v3 1/2] libavcodec/pgxdec: Add PGX decoder

2020-06-28 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds a pgx decoder.
---
 Changelog   |   1 +
 doc/general.texi|   2 +
 libavcodec/Makefile |   1 +
 libavcodec/allcodecs.c  |   1 +
 libavcodec/codec_desc.c |   7 ++
 libavcodec/codec_id.h   |   1 +
 libavcodec/pgxdec.c | 205 
 libavcodec/version.h|   2 +-
 8 files changed, 219 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/pgxdec.c

diff --git a/Changelog b/Changelog
index a60e7d2eb8..1bb9931c0d 100644
--- a/Changelog
+++ b/Changelog
@@ -4,6 +4,7 @@ releases are sorted from youngest to oldest.
 version :
 - AudioToolbox output device
 - MacCaption demuxer
+- PGX decoder
 
 
 version 4.3:
diff --git a/doc/general.texi b/doc/general.texi
index ea34b963b5..53d556c56c 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -751,6 +751,8 @@ following image formats are supported:
 @tab Portable GrayMap image
 @item PGMYUV   @tab X @tab X
 @tab PGM with U and V components in YUV 4:2:0
+@item PGX  @tab   @tab X
+@tab PGX file decoder
 @item PIC  @tab @tab X
 @tab Pictor/PC Paint
 @item PNG  @tab X @tab X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 5a6ea59715..12aa43fe51 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -536,6 +536,7 @@ OBJS-$(CONFIG_PGM_ENCODER) += pnmenc.o
 OBJS-$(CONFIG_PGMYUV_DECODER)  += pnmdec.o pnm.o
 OBJS-$(CONFIG_PGMYUV_ENCODER)  += pnmenc.o
 OBJS-$(CONFIG_PGSSUB_DECODER)  += pgssubdec.o
+OBJS-$(CONFIG_PGX_DECODER) += pgxdec.o
 OBJS-$(CONFIG_PICTOR_DECODER)  += pictordec.o cga_data.o
 OBJS-$(CONFIG_PIXLET_DECODER)  += pixlet.o
 OBJS-$(CONFIG_PJS_DECODER) += textdec.o ass.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index fa0c08d42e..a5048290f7 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -238,6 +238,7 @@ extern AVCodec ff_pgm_encoder;
 extern AVCodec ff_pgm_decoder;
 extern AVCodec ff_pgmyuv_encoder;
 extern AVCodec ff_pgmyuv_decoder;
+extern AVCodec ff_pgx_decoder;
 extern AVCodec ff_pictor_decoder;
 extern AVCodec ff_pixlet_decoder;
 extern AVCodec ff_png_encoder;
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 9f8847544f..67e0a3055c 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1405,6 +1405,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
 .long_name = NULL_IF_CONFIG_SMALL("AVS2-P2/IEEE1857.4"),
 .props = AV_CODEC_PROP_LOSSY,
 },
+{
+.id= AV_CODEC_ID_PGX,
+.type  = AVMEDIA_TYPE_VIDEO,
+.name  = "pgx",
+.long_name = NULL_IF_CONFIG_SMALL("PGX (JPEG2000 Test Format)"),
+.props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
+},
 {
 .id= AV_CODEC_ID_Y41P,
 .type  = AVMEDIA_TYPE_VIDEO,
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index d885962c9c..896ecb0ce0 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -241,6 +241,7 @@ enum AVCodecID {
 AV_CODEC_ID_SCREENPRESSO,
 AV_CODEC_ID_RSCC,
 AV_CODEC_ID_AVS2,
+AV_CODEC_ID_PGX,
 
 AV_CODEC_ID_Y41P = 0x8000,
 AV_CODEC_ID_AVRP,
diff --git a/libavcodec/pgxdec.c b/libavcodec/pgxdec.c
new file mode 100644
index 00..688846f797
--- /dev/null
+++ b/libavcodec/pgxdec.c
@@ -0,0 +1,205 @@
+/*
+ * PGX image format
+ * Copyright (c) 2020 Gautam Ramakrishnan
+ *
+ * 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
+ */
+
+#include "avcodec.h"
+#include "internal.h"
+#include "bytestream.h"
+#include "libavutil/imgutils.h"
+
+typedef struct PGXContext {
+GetByteContext  g;
+} PGXContext;
+
+static int pgx_get_number(AVCodecContext *avctx, PGXContext * const s) {
+int ret = -1;
+char digit;
+int numdigits = 0;
+
+while (1) {
+if (numdigits > 10) {
+return -1;
+}
+if (!bytestream2_get_bytes_left(>g))
+return -1;
+digit = bytestream2_get_byte(>g);
+if (digit == ' ' || digit == 0xA || digit == 0xD)
+break;
+else if (digit < '0' || digit > '9')
+return -1;
+
+if (ret < 0)
+

[FFmpeg-devel] [PATCH v3 2/2] libavformat/img2dec: Added pgx demuxer

2020-06-28 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support to demux pgx file
format.
---
 libavformat/allformats.c |  1 +
 libavformat/img2dec.c| 10 ++
 libavformat/version.h|  2 +-
 3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 97fd06debb..f8527b1fd4 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -488,6 +488,7 @@ extern AVInputFormat  ff_image_pbm_pipe_demuxer;
 extern AVInputFormat  ff_image_pcx_pipe_demuxer;
 extern AVInputFormat  ff_image_pgmyuv_pipe_demuxer;
 extern AVInputFormat  ff_image_pgm_pipe_demuxer;
+extern AVInputFormat  ff_image_pgx_pipe_demuxer;
 extern AVInputFormat  ff_image_pictor_pipe_demuxer;
 extern AVInputFormat  ff_image_png_pipe_demuxer;
 extern AVInputFormat  ff_image_ppm_pipe_demuxer;
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index ee7ceed08f..ec27e04a30 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -1000,6 +1000,15 @@ static int pgmyuv_probe(const AVProbeData *p) // custom 
FFmpeg format recognized
 return ret && av_match_ext(p->filename, "pgmyuv") ? ret : 0;
 }
 
+static int pgx_probe(const AVProbeData *p)
+{
+const uint8_t *b = p->buf;
+int ret = (AV_RB64(b) & 0x) == 0x5047204D4C20;
+if (ret)
+return AVPROBE_SCORE_EXTENSION + 1;
+return 0;
+}
+
 static int ppm_probe(const AVProbeData *p)
 {
 return pnm_magic_check(p, 3) || pnm_magic_check(p, 6) ? pnm_probe(p) : 0;
@@ -1094,6 +1103,7 @@ IMAGEAUTO_DEMUXER(pbm, AV_CODEC_ID_PBM)
 IMAGEAUTO_DEMUXER(pcx, AV_CODEC_ID_PCX)
 IMAGEAUTO_DEMUXER(pgm, AV_CODEC_ID_PGM)
 IMAGEAUTO_DEMUXER(pgmyuv,  AV_CODEC_ID_PGMYUV)
+IMAGEAUTO_DEMUXER(pgx, AV_CODEC_ID_PGX)
 IMAGEAUTO_DEMUXER(pictor,  AV_CODEC_ID_PICTOR)
 IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
 IMAGEAUTO_DEMUXER(ppm, AV_CODEC_ID_PPM)
diff --git a/libavformat/version.h b/libavformat/version.h
index 3c1957b00c..75c03fde0a 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,7 +32,7 @@
 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  58
-#define LIBAVFORMAT_VERSION_MINOR  47
+#define LIBAVFORMAT_VERSION_MINOR  48
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v2 1/2] libavcodec/pgxdec: Add PGX decoder

2020-06-27 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds a pgx decoder.
---
 Changelog   |   1 +
 doc/general.texi|   2 +
 libavcodec/Makefile |   1 +
 libavcodec/allcodecs.c  |   1 +
 libavcodec/codec_desc.c |   7 ++
 libavcodec/codec_id.h   |   1 +
 libavcodec/pgxdec.c | 199 
 libavcodec/version.h|   2 +-
 8 files changed, 213 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/pgxdec.c

diff --git a/Changelog b/Changelog
index a60e7d2eb8..1bb9931c0d 100644
--- a/Changelog
+++ b/Changelog
@@ -4,6 +4,7 @@ releases are sorted from youngest to oldest.
 version :
 - AudioToolbox output device
 - MacCaption demuxer
+- PGX decoder
 
 
 version 4.3:
diff --git a/doc/general.texi b/doc/general.texi
index ea34b963b5..53d556c56c 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -751,6 +751,8 @@ following image formats are supported:
 @tab Portable GrayMap image
 @item PGMYUV   @tab X @tab X
 @tab PGM with U and V components in YUV 4:2:0
+@item PGX  @tab   @tab X
+@tab PGX file decoder
 @item PIC  @tab @tab X
 @tab Pictor/PC Paint
 @item PNG  @tab X @tab X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 5a6ea59715..12aa43fe51 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -536,6 +536,7 @@ OBJS-$(CONFIG_PGM_ENCODER) += pnmenc.o
 OBJS-$(CONFIG_PGMYUV_DECODER)  += pnmdec.o pnm.o
 OBJS-$(CONFIG_PGMYUV_ENCODER)  += pnmenc.o
 OBJS-$(CONFIG_PGSSUB_DECODER)  += pgssubdec.o
+OBJS-$(CONFIG_PGX_DECODER) += pgxdec.o
 OBJS-$(CONFIG_PICTOR_DECODER)  += pictordec.o cga_data.o
 OBJS-$(CONFIG_PIXLET_DECODER)  += pixlet.o
 OBJS-$(CONFIG_PJS_DECODER) += textdec.o ass.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index fa0c08d42e..a5048290f7 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -238,6 +238,7 @@ extern AVCodec ff_pgm_encoder;
 extern AVCodec ff_pgm_decoder;
 extern AVCodec ff_pgmyuv_encoder;
 extern AVCodec ff_pgmyuv_decoder;
+extern AVCodec ff_pgx_decoder;
 extern AVCodec ff_pictor_decoder;
 extern AVCodec ff_pixlet_decoder;
 extern AVCodec ff_png_encoder;
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 9f8847544f..67e0a3055c 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1405,6 +1405,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
 .long_name = NULL_IF_CONFIG_SMALL("AVS2-P2/IEEE1857.4"),
 .props = AV_CODEC_PROP_LOSSY,
 },
+{
+.id= AV_CODEC_ID_PGX,
+.type  = AVMEDIA_TYPE_VIDEO,
+.name  = "pgx",
+.long_name = NULL_IF_CONFIG_SMALL("PGX (JPEG2000 Test Format)"),
+.props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
+},
 {
 .id= AV_CODEC_ID_Y41P,
 .type  = AVMEDIA_TYPE_VIDEO,
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index d885962c9c..896ecb0ce0 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -241,6 +241,7 @@ enum AVCodecID {
 AV_CODEC_ID_SCREENPRESSO,
 AV_CODEC_ID_RSCC,
 AV_CODEC_ID_AVS2,
+AV_CODEC_ID_PGX,
 
 AV_CODEC_ID_Y41P = 0x8000,
 AV_CODEC_ID_AVRP,
diff --git a/libavcodec/pgxdec.c b/libavcodec/pgxdec.c
new file mode 100644
index 00..5f4704f31a
--- /dev/null
+++ b/libavcodec/pgxdec.c
@@ -0,0 +1,199 @@
+/*
+ * PGX image format
+ * Copyright (c) 2020 Gautam Ramakrishnan
+ *
+ * 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
+ */
+
+#include "avcodec.h"
+#include "internal.h"
+#include "bytestream.h"
+
+typedef struct PGXContext {
+GetByteContext  g;
+} PGXContext;
+
+static int pgx_get_number(AVCodecContext *avctx, PGXContext * const s) {
+int ret = -1;
+char digit;
+int numdigits = 0;
+
+while (1) {
+if (numdigits > 10) {
+return -1;
+}
+if (!bytestream2_get_bytes_left(>g))
+return -1;
+digit = bytestream2_get_byte(>g);
+if (digit == ' ' || digit == 0xA || digit == 0xD)
+break;
+else if (digit < '0' || digit > '9')
+return -1;
+
+if (ret < 0)
+ret = 0;
+ret = 10 * ret + 

[FFmpeg-devel] [PATCH v2 2/2] libavformat/img2dec: Added pgx demuxer

2020-06-27 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support to demux pgx file
format.
---
 libavformat/allformats.c |  1 +
 libavformat/img2dec.c| 10 ++
 libavformat/version.h|  2 +-
 3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 97fd06debb..f8527b1fd4 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -488,6 +488,7 @@ extern AVInputFormat  ff_image_pbm_pipe_demuxer;
 extern AVInputFormat  ff_image_pcx_pipe_demuxer;
 extern AVInputFormat  ff_image_pgmyuv_pipe_demuxer;
 extern AVInputFormat  ff_image_pgm_pipe_demuxer;
+extern AVInputFormat  ff_image_pgx_pipe_demuxer;
 extern AVInputFormat  ff_image_pictor_pipe_demuxer;
 extern AVInputFormat  ff_image_png_pipe_demuxer;
 extern AVInputFormat  ff_image_ppm_pipe_demuxer;
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index ee7ceed08f..ec27e04a30 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -1000,6 +1000,15 @@ static int pgmyuv_probe(const AVProbeData *p) // custom 
FFmpeg format recognized
 return ret && av_match_ext(p->filename, "pgmyuv") ? ret : 0;
 }
 
+static int pgx_probe(const AVProbeData *p)
+{
+const uint8_t *b = p->buf;
+int ret = (AV_RB64(b) & 0x) == 0x5047204D4C20;
+if (ret)
+return AVPROBE_SCORE_EXTENSION + 1;
+return 0;
+}
+
 static int ppm_probe(const AVProbeData *p)
 {
 return pnm_magic_check(p, 3) || pnm_magic_check(p, 6) ? pnm_probe(p) : 0;
@@ -1094,6 +1103,7 @@ IMAGEAUTO_DEMUXER(pbm, AV_CODEC_ID_PBM)
 IMAGEAUTO_DEMUXER(pcx, AV_CODEC_ID_PCX)
 IMAGEAUTO_DEMUXER(pgm, AV_CODEC_ID_PGM)
 IMAGEAUTO_DEMUXER(pgmyuv,  AV_CODEC_ID_PGMYUV)
+IMAGEAUTO_DEMUXER(pgx, AV_CODEC_ID_PGX)
 IMAGEAUTO_DEMUXER(pictor,  AV_CODEC_ID_PICTOR)
 IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
 IMAGEAUTO_DEMUXER(ppm, AV_CODEC_ID_PPM)
diff --git a/libavformat/version.h b/libavformat/version.h
index 3c1957b00c..75c03fde0a 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,7 +32,7 @@
 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  58
-#define LIBAVFORMAT_VERSION_MINOR  47
+#define LIBAVFORMAT_VERSION_MINOR  48
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 2/2] libavformat/img2dec: Added pgx demuxer

2020-06-27 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support to demux pgx file
format.
---
 libavformat/allformats.c |  1 +
 libavformat/img2dec.c| 10 ++
 libavformat/version.h|  2 +-
 3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 97fd06debb..f8527b1fd4 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -488,6 +488,7 @@ extern AVInputFormat  ff_image_pbm_pipe_demuxer;
 extern AVInputFormat  ff_image_pcx_pipe_demuxer;
 extern AVInputFormat  ff_image_pgmyuv_pipe_demuxer;
 extern AVInputFormat  ff_image_pgm_pipe_demuxer;
+extern AVInputFormat  ff_image_pgx_pipe_demuxer;
 extern AVInputFormat  ff_image_pictor_pipe_demuxer;
 extern AVInputFormat  ff_image_png_pipe_demuxer;
 extern AVInputFormat  ff_image_ppm_pipe_demuxer;
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index ee7ceed08f..ec27e04a30 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -1000,6 +1000,15 @@ static int pgmyuv_probe(const AVProbeData *p) // custom 
FFmpeg format recognized
 return ret && av_match_ext(p->filename, "pgmyuv") ? ret : 0;
 }
 
+static int pgx_probe(const AVProbeData *p)
+{
+const uint8_t *b = p->buf;
+int ret = (AV_RB64(b) & 0x) == 0x5047204D4C20;
+if (ret)
+return AVPROBE_SCORE_EXTENSION + 1;
+return 0;
+}
+
 static int ppm_probe(const AVProbeData *p)
 {
 return pnm_magic_check(p, 3) || pnm_magic_check(p, 6) ? pnm_probe(p) : 0;
@@ -1094,6 +1103,7 @@ IMAGEAUTO_DEMUXER(pbm, AV_CODEC_ID_PBM)
 IMAGEAUTO_DEMUXER(pcx, AV_CODEC_ID_PCX)
 IMAGEAUTO_DEMUXER(pgm, AV_CODEC_ID_PGM)
 IMAGEAUTO_DEMUXER(pgmyuv,  AV_CODEC_ID_PGMYUV)
+IMAGEAUTO_DEMUXER(pgx, AV_CODEC_ID_PGX)
 IMAGEAUTO_DEMUXER(pictor,  AV_CODEC_ID_PICTOR)
 IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
 IMAGEAUTO_DEMUXER(ppm, AV_CODEC_ID_PPM)
diff --git a/libavformat/version.h b/libavformat/version.h
index 3c1957b00c..75c03fde0a 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,7 +32,7 @@
 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  58
-#define LIBAVFORMAT_VERSION_MINOR  47
+#define LIBAVFORMAT_VERSION_MINOR  48
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 1/2] libavcodec/pgxdec: Add PGX decoder

2020-06-27 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds a pgx decoder.
---
 Changelog   |   1 +
 doc/general.texi|   2 +
 libavcodec/Makefile |   1 +
 libavcodec/allcodecs.c  |   1 +
 libavcodec/codec_desc.c |   7 ++
 libavcodec/codec_id.h   |   1 +
 libavcodec/pgxdec.c | 184 
 libavcodec/version.h|   2 +-
 8 files changed, 198 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/pgxdec.c

diff --git a/Changelog b/Changelog
index a60e7d2eb8..1bb9931c0d 100644
--- a/Changelog
+++ b/Changelog
@@ -4,6 +4,7 @@ releases are sorted from youngest to oldest.
 version :
 - AudioToolbox output device
 - MacCaption demuxer
+- PGX decoder
 
 
 version 4.3:
diff --git a/doc/general.texi b/doc/general.texi
index ea34b963b5..53d556c56c 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -751,6 +751,8 @@ following image formats are supported:
 @tab Portable GrayMap image
 @item PGMYUV   @tab X @tab X
 @tab PGM with U and V components in YUV 4:2:0
+@item PGX  @tab   @tab X
+@tab PGX file decoder
 @item PIC  @tab @tab X
 @tab Pictor/PC Paint
 @item PNG  @tab X @tab X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 5a6ea59715..12aa43fe51 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -536,6 +536,7 @@ OBJS-$(CONFIG_PGM_ENCODER) += pnmenc.o
 OBJS-$(CONFIG_PGMYUV_DECODER)  += pnmdec.o pnm.o
 OBJS-$(CONFIG_PGMYUV_ENCODER)  += pnmenc.o
 OBJS-$(CONFIG_PGSSUB_DECODER)  += pgssubdec.o
+OBJS-$(CONFIG_PGX_DECODER) += pgxdec.o
 OBJS-$(CONFIG_PICTOR_DECODER)  += pictordec.o cga_data.o
 OBJS-$(CONFIG_PIXLET_DECODER)  += pixlet.o
 OBJS-$(CONFIG_PJS_DECODER) += textdec.o ass.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index fa0c08d42e..a5048290f7 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -238,6 +238,7 @@ extern AVCodec ff_pgm_encoder;
 extern AVCodec ff_pgm_decoder;
 extern AVCodec ff_pgmyuv_encoder;
 extern AVCodec ff_pgmyuv_decoder;
+extern AVCodec ff_pgx_decoder;
 extern AVCodec ff_pictor_decoder;
 extern AVCodec ff_pixlet_decoder;
 extern AVCodec ff_png_encoder;
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 9f8847544f..67e0a3055c 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1405,6 +1405,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
 .long_name = NULL_IF_CONFIG_SMALL("AVS2-P2/IEEE1857.4"),
 .props = AV_CODEC_PROP_LOSSY,
 },
+{
+.id= AV_CODEC_ID_PGX,
+.type  = AVMEDIA_TYPE_VIDEO,
+.name  = "pgx",
+.long_name = NULL_IF_CONFIG_SMALL("PGX (JPEG2000 Test Format)"),
+.props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
+},
 {
 .id= AV_CODEC_ID_Y41P,
 .type  = AVMEDIA_TYPE_VIDEO,
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index d885962c9c..896ecb0ce0 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -241,6 +241,7 @@ enum AVCodecID {
 AV_CODEC_ID_SCREENPRESSO,
 AV_CODEC_ID_RSCC,
 AV_CODEC_ID_AVS2,
+AV_CODEC_ID_PGX,
 
 AV_CODEC_ID_Y41P = 0x8000,
 AV_CODEC_ID_AVRP,
diff --git a/libavcodec/pgxdec.c b/libavcodec/pgxdec.c
new file mode 100644
index 00..df5f5028cd
--- /dev/null
+++ b/libavcodec/pgxdec.c
@@ -0,0 +1,184 @@
+/*
+ * PGX image format
+ * Copyright (c) 2020 Gautam Ramakrishnan
+ *
+ * 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
+ */
+
+#include "avcodec.h"
+#include "internal.h"
+#include "bytestream.h"
+
+typedef struct PGXContext {
+GetByteContext  g;
+int depth;
+int sign;
+int width;
+int height;
+} PGXContext;
+
+static int pgx_get_number(AVCodecContext *avctx, PGXContext * const s) {
+int ret = -1;
+char digit;
+
+while (1) {
+if (!bytestream2_get_bytes_left(>g))
+return AVERROR_INVALIDDATA;
+digit = bytestream2_get_byte(>g);
+if (digit == ' ' || digit == 0xA || digit == 0xD) {
+break;
+} else if (digit < '0' || digit > '9') {
+return AVERROR_INVALIDDATA;
+}
+if (ret < 0)
+ret = 0;
+   

[FFmpeg-devel] [RFC PATCH 1/2] libavcodec/pgxdec: Add PGX decoder

2020-06-26 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds a pgx decoder.
---
 Changelog   |   1 +
 doc/general.texi|   2 +
 libavcodec/Makefile |   1 +
 libavcodec/allcodecs.c  |   1 +
 libavcodec/codec_desc.c |   7 ++
 libavcodec/codec_id.h   |   1 +
 libavcodec/pgxdec.c | 184 
 libavcodec/version.h|   2 +-
 8 files changed, 198 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/pgxdec.c

diff --git a/Changelog b/Changelog
index a60e7d2eb8..1bb9931c0d 100644
--- a/Changelog
+++ b/Changelog
@@ -4,6 +4,7 @@ releases are sorted from youngest to oldest.
 version :
 - AudioToolbox output device
 - MacCaption demuxer
+- PGX decoder
 
 
 version 4.3:
diff --git a/doc/general.texi b/doc/general.texi
index ea34b963b5..53d556c56c 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -751,6 +751,8 @@ following image formats are supported:
 @tab Portable GrayMap image
 @item PGMYUV   @tab X @tab X
 @tab PGM with U and V components in YUV 4:2:0
+@item PGX  @tab   @tab X
+@tab PGX file decoder
 @item PIC  @tab @tab X
 @tab Pictor/PC Paint
 @item PNG  @tab X @tab X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 5a6ea59715..12aa43fe51 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -536,6 +536,7 @@ OBJS-$(CONFIG_PGM_ENCODER) += pnmenc.o
 OBJS-$(CONFIG_PGMYUV_DECODER)  += pnmdec.o pnm.o
 OBJS-$(CONFIG_PGMYUV_ENCODER)  += pnmenc.o
 OBJS-$(CONFIG_PGSSUB_DECODER)  += pgssubdec.o
+OBJS-$(CONFIG_PGX_DECODER) += pgxdec.o
 OBJS-$(CONFIG_PICTOR_DECODER)  += pictordec.o cga_data.o
 OBJS-$(CONFIG_PIXLET_DECODER)  += pixlet.o
 OBJS-$(CONFIG_PJS_DECODER) += textdec.o ass.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index fa0c08d42e..a5048290f7 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -238,6 +238,7 @@ extern AVCodec ff_pgm_encoder;
 extern AVCodec ff_pgm_decoder;
 extern AVCodec ff_pgmyuv_encoder;
 extern AVCodec ff_pgmyuv_decoder;
+extern AVCodec ff_pgx_decoder;
 extern AVCodec ff_pictor_decoder;
 extern AVCodec ff_pixlet_decoder;
 extern AVCodec ff_png_encoder;
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 9f8847544f..16d995147f 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -3381,6 +3381,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
 .long_name = NULL_IF_CONFIG_SMALL("AVFrame to AVPacket passthrough"),
 .props = AV_CODEC_PROP_LOSSLESS,
 },
+{
+.id= AV_CODEC_ID_PGX,
+.type  = AVMEDIA_TYPE_VIDEO,
+.name  = "pgx",
+.long_name = NULL_IF_CONFIG_SMALL("AVFrame to AVPacket passthrough"),
+.props = AV_CODEC_PROP_LOSSLESS,
+},
 };
 
 static int descriptor_compare(const void *key, const void *member)
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index d885962c9c..896ecb0ce0 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -241,6 +241,7 @@ enum AVCodecID {
 AV_CODEC_ID_SCREENPRESSO,
 AV_CODEC_ID_RSCC,
 AV_CODEC_ID_AVS2,
+AV_CODEC_ID_PGX,
 
 AV_CODEC_ID_Y41P = 0x8000,
 AV_CODEC_ID_AVRP,
diff --git a/libavcodec/pgxdec.c b/libavcodec/pgxdec.c
new file mode 100644
index 00..1be5a91eac
--- /dev/null
+++ b/libavcodec/pgxdec.c
@@ -0,0 +1,184 @@
+/*
+ * PGX image format
+ * Copyright (c) 2020 Gautam Ramakrishnan
+ *
+ * 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
+ */
+
+#include "avcodec.h"
+#include "internal.h"
+#include "bytestream.h"
+
+typedef struct PGXContext {
+GetByteContext  g;
+int depth;
+int sign;
+int width;
+int height;
+} PGXContext;
+
+static int pgx_get_number(AVCodecContext *avctx, PGXContext * const s) {
+int ret = -1;
+char digit;
+
+while (1) {
+if (!bytestream2_get_bytes_left(>g))
+return AVERROR_INVALIDDATA;
+digit = bytestream2_get_byte(>g);
+if (digit == ' ' || digit == 0xA || digit == 0xD) {
+break;
+} else if (digit < '0' || digit > '9') {
+return AVERROR_INVALIDDATA;
+}
+if (ret < 0)
+ret = 0;
+ret = 10 * ret 

[FFmpeg-devel] [RFC PATCH 2/2] ilibavformat/img2dec: Added pgx demuxer

2020-06-26 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support to demux the pgx
file format.
---
 libavformat/allformats.c |  1 +
 libavformat/img2dec.c| 10 ++
 libavformat/version.h|  2 +-
 3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 97fd06debb..f8527b1fd4 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -488,6 +488,7 @@ extern AVInputFormat  ff_image_pbm_pipe_demuxer;
 extern AVInputFormat  ff_image_pcx_pipe_demuxer;
 extern AVInputFormat  ff_image_pgmyuv_pipe_demuxer;
 extern AVInputFormat  ff_image_pgm_pipe_demuxer;
+extern AVInputFormat  ff_image_pgx_pipe_demuxer;
 extern AVInputFormat  ff_image_pictor_pipe_demuxer;
 extern AVInputFormat  ff_image_png_pipe_demuxer;
 extern AVInputFormat  ff_image_ppm_pipe_demuxer;
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index ee7ceed08f..ec27e04a30 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -1000,6 +1000,15 @@ static int pgmyuv_probe(const AVProbeData *p) // custom 
FFmpeg format recognized
 return ret && av_match_ext(p->filename, "pgmyuv") ? ret : 0;
 }
 
+static int pgx_probe(const AVProbeData *p)
+{
+const uint8_t *b = p->buf;
+int ret = (AV_RB64(b) & 0x) == 0x5047204D4C20;
+if (ret)
+return AVPROBE_SCORE_EXTENSION + 1;
+return 0;
+}
+
 static int ppm_probe(const AVProbeData *p)
 {
 return pnm_magic_check(p, 3) || pnm_magic_check(p, 6) ? pnm_probe(p) : 0;
@@ -1094,6 +1103,7 @@ IMAGEAUTO_DEMUXER(pbm, AV_CODEC_ID_PBM)
 IMAGEAUTO_DEMUXER(pcx, AV_CODEC_ID_PCX)
 IMAGEAUTO_DEMUXER(pgm, AV_CODEC_ID_PGM)
 IMAGEAUTO_DEMUXER(pgmyuv,  AV_CODEC_ID_PGMYUV)
+IMAGEAUTO_DEMUXER(pgx, AV_CODEC_ID_PGX)
 IMAGEAUTO_DEMUXER(pictor,  AV_CODEC_ID_PICTOR)
 IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
 IMAGEAUTO_DEMUXER(ppm, AV_CODEC_ID_PPM)
diff --git a/libavformat/version.h b/libavformat/version.h
index 3c1957b00c..75c03fde0a 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,7 +32,7 @@
 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  58
-#define LIBAVFORMAT_VERSION_MINOR  47
+#define LIBAVFORMAT_VERSION_MINOR  48
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH] libavcodec/pgx: Added pgx decoder

2020-06-24 Thread gautamramk
From: Gautam Ramakrishnan 

This patch support to read and decode
pgx files.
---
 libavcodec/Makefile  |   1 +
 libavcodec/allcodecs.c   |   1 +
 libavcodec/codec_id.h|   1 +
 libavcodec/pgx.h |  38 +
 libavcodec/pgxdec.c  | 176 +++
 libavformat/allformats.c |   1 +
 libavformat/img2dec.c|  11 +++
 7 files changed, 229 insertions(+)
 create mode 100644 libavcodec/pgx.h
 create mode 100644 libavcodec/pgxdec.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 5a6ea59715..0198c244e0 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -533,6 +533,7 @@ OBJS-$(CONFIG_PCX_ENCODER) += pcxenc.o
 OBJS-$(CONFIG_PFM_DECODER) += pnmdec.o pnm.o
 OBJS-$(CONFIG_PGM_DECODER) += pnmdec.o pnm.o
 OBJS-$(CONFIG_PGM_ENCODER) += pnmenc.o
+OBJS-$(CONFIG_PGX_DECODER) += pgxdec.o
 OBJS-$(CONFIG_PGMYUV_DECODER)  += pnmdec.o pnm.o
 OBJS-$(CONFIG_PGMYUV_ENCODER)  += pnmenc.o
 OBJS-$(CONFIG_PGSSUB_DECODER)  += pgssubdec.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index fa0c08d42e..b0217e6d6a 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -236,6 +236,7 @@ extern AVCodec ff_pcx_decoder;
 extern AVCodec ff_pfm_decoder;
 extern AVCodec ff_pgm_encoder;
 extern AVCodec ff_pgm_decoder;
+extern AVCodec ff_pgx_decoder;
 extern AVCodec ff_pgmyuv_encoder;
 extern AVCodec ff_pgmyuv_decoder;
 extern AVCodec ff_pictor_decoder;
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index d885962c9c..027ef21c62 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -111,6 +111,7 @@ enum AVCodecID {
 AV_CODEC_ID_PPM,
 AV_CODEC_ID_PBM,
 AV_CODEC_ID_PGM,
+AV_CODEC_ID_PGX,
 AV_CODEC_ID_PGMYUV,
 AV_CODEC_ID_PAM,
 AV_CODEC_ID_FFVHUFF,
diff --git a/libavcodec/pgx.h b/libavcodec/pgx.h
new file mode 100644
index 00..bbe93fafe7
--- /dev/null
+++ b/libavcodec/pgx.h
@@ -0,0 +1,38 @@
+/*
+ * PGX image format
+ * Copyright (c) 2020 Gautam Ramakrishnan
+ *
+ * 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_PGX_H
+#define AVCODEC_PGX_H
+
+#include "avcodec.h"
+#include "bytestream.h"
+
+typedef struct PGXContext {
+GetByteContext  g;
+int depth;
+int sgnd;
+int width;
+int height;
+} PGXContext;
+
+int ff_pgx_decode_header(AVCodecContext *avctx, PGXContext * const s);
+
+#endif /* AVCODEC_PNM_H */
\ No newline at end of file
diff --git a/libavcodec/pgxdec.c b/libavcodec/pgxdec.c
new file mode 100644
index 00..233bf34717
--- /dev/null
+++ b/libavcodec/pgxdec.c
@@ -0,0 +1,176 @@
+/*
+ * PGX image format
+ * Copyright (c) 2020 Gautam Ramakrishnan
+ *
+ * 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
+ */
+
+#include "avcodec.h"
+#include "internal.h"
+#include "pgx.h"
+
+static int pgx_get_number(AVCodecContext *avctx, PGXContext * const s) {
+char number[10];
+int i;
+int ret;
+number[9] = '\0';
+for (i = 0; i < 9; i++) {
+if (!bytestream2_get_bytes_left(>g))
+return AVERROR_INVALIDDATA;
+number[i] = (char)bytestream2_get_byteu(>g);
+if (number[i] == ' ' || number[i] == 0xA || number[i] == 0xD) {
+number[i] = '\0';
+break;
+} else if (number[i] < '0' || number[i] > '9') {
+return AVERROR_INVALIDDATA;
+}
+}
+if (i > 9)
+return AVERROR_INVALIDDATA;
+ret = (int)strtol(number, NULL, 10);
+if (ret >= 0)
+return 

[FFmpeg-devel] [PATCH] libavcodec/pgx: Added pgx decoder

2020-06-24 Thread gautamramk
From: Gautam Ramakrishnan 

This patch support to read and decode
pgx files.
---
 libavcodec/Makefile  |   1 +
 libavcodec/allcodecs.c   |   1 +
 libavcodec/codec_id.h|   1 +
 libavcodec/pgx.h |  38 +
 libavcodec/pgxdec.c  | 177 +++
 libavformat/allformats.c |   1 +
 libavformat/img2dec.c|  11 +++
 7 files changed, 230 insertions(+)
 create mode 100644 libavcodec/pgx.h
 create mode 100644 libavcodec/pgxdec.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 5a6ea59715..0198c244e0 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -533,6 +533,7 @@ OBJS-$(CONFIG_PCX_ENCODER) += pcxenc.o
 OBJS-$(CONFIG_PFM_DECODER) += pnmdec.o pnm.o
 OBJS-$(CONFIG_PGM_DECODER) += pnmdec.o pnm.o
 OBJS-$(CONFIG_PGM_ENCODER) += pnmenc.o
+OBJS-$(CONFIG_PGX_DECODER) += pgxdec.o
 OBJS-$(CONFIG_PGMYUV_DECODER)  += pnmdec.o pnm.o
 OBJS-$(CONFIG_PGMYUV_ENCODER)  += pnmenc.o
 OBJS-$(CONFIG_PGSSUB_DECODER)  += pgssubdec.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index fa0c08d42e..b0217e6d6a 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -236,6 +236,7 @@ extern AVCodec ff_pcx_decoder;
 extern AVCodec ff_pfm_decoder;
 extern AVCodec ff_pgm_encoder;
 extern AVCodec ff_pgm_decoder;
+extern AVCodec ff_pgx_decoder;
 extern AVCodec ff_pgmyuv_encoder;
 extern AVCodec ff_pgmyuv_decoder;
 extern AVCodec ff_pictor_decoder;
diff --git a/libavcodec/codec_id.h b/libavcodec/codec_id.h
index d885962c9c..027ef21c62 100644
--- a/libavcodec/codec_id.h
+++ b/libavcodec/codec_id.h
@@ -111,6 +111,7 @@ enum AVCodecID {
 AV_CODEC_ID_PPM,
 AV_CODEC_ID_PBM,
 AV_CODEC_ID_PGM,
+AV_CODEC_ID_PGX,
 AV_CODEC_ID_PGMYUV,
 AV_CODEC_ID_PAM,
 AV_CODEC_ID_FFVHUFF,
diff --git a/libavcodec/pgx.h b/libavcodec/pgx.h
new file mode 100644
index 00..bbe93fafe7
--- /dev/null
+++ b/libavcodec/pgx.h
@@ -0,0 +1,38 @@
+/*
+ * PGX image format
+ * Copyright (c) 2020 Gautam Ramakrishnan
+ *
+ * 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_PGX_H
+#define AVCODEC_PGX_H
+
+#include "avcodec.h"
+#include "bytestream.h"
+
+typedef struct PGXContext {
+GetByteContext  g;
+int depth;
+int sgnd;
+int width;
+int height;
+} PGXContext;
+
+int ff_pgx_decode_header(AVCodecContext *avctx, PGXContext * const s);
+
+#endif /* AVCODEC_PNM_H */
\ No newline at end of file
diff --git a/libavcodec/pgxdec.c b/libavcodec/pgxdec.c
new file mode 100644
index 00..f972853bf9
--- /dev/null
+++ b/libavcodec/pgxdec.c
@@ -0,0 +1,177 @@
+/*
+ * PGX image format
+ * Copyright (c) 2020 Gautam Ramakrishnan
+ *
+ * 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
+ */
+
+#include "avcodec.h"
+#include "internal.h"
+#include "pgx.h"
+
+static int pgx_get_number(AVCodecContext *avctx, PGXContext * const s) {
+char number[10];
+int i;
+int ret;
+number[9] = '\0';
+for (i = 0; i < 9; i++) {
+if (!bytestream2_get_bytes_left(>g))
+return AVERROR_INVALIDDATA;
+number[i] = (char)bytestream2_get_byteu(>g);
+if (number[i] == ' ' || number[i] == 0xA || number[i] == 0xD) {
+number[i] = '\0';
+break;
+} else if (number[i] < '0' || number[i] > '9') {
+return AVERROR_INVALIDDATA;
+}
+}
+if (i > 9)
+return AVERROR_INVALIDDATA;
+ret = (int)strtol(number, NULL, 10);
+if (ret >= 0)
+return 

[FFmpeg-devel] [PATCH 5/5] libavcodec/jpeg2000dec.c: Remove log2_chroma check in pixel format selection

2020-06-21 Thread gautamramk
From: Gautam Ramakrishnan 

The log2_chroma_wh is derived from the sample separations of the
codestream if the file is a j2k codestream. Not sure if sample
separation is same is subsampling and whether using sample
separation values from the codestream to determine pixel format.
---
 libavcodec/jpeg2000dec.c | 2 --
 1 file changed, 2 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index c8c89803ac..2b9659bf96 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -225,8 +225,6 @@ static int pix_fmt_match(enum AVPixelFormat pix_fmt, int 
components,
 
 case 1:
 match = match && desc->comp[0].depth >= bpc &&
- (log2_chroma_wh >>  2 & 3) == 0 &&
- (log2_chroma_wh   & 3) == 0 &&
  (desc->flags & AV_PIX_FMT_FLAG_PAL) == pal8 * 
AV_PIX_FMT_FLAG_PAL;
 }
 return match;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 3/5] libavcodec/jpeg2000dec.c Fixed WRITE_FRAME and tile co-ordinates:

2020-06-21 Thread gautamramk
From: Gautam Ramakrishnan 

libopenjpeg2000 uses ceiling division while dividing tile
co-ordinates with the sample separation. Also, corrections
were made to the WRITE_FRAME macro.
---
 libavcodec/jpeg2000dec.c | 24 ++--
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 05e85f4317..546a646668 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -978,12 +978,11 @@ static int init_tile(Jpeg2000DecoderContext *s, int 
tileno)
 comp->coord_o[0][1] = tile->coord[0][1];
 comp->coord_o[1][0] = tile->coord[1][0];
 comp->coord_o[1][1] = tile->coord[1][1];
-if (compno) {
-comp->coord_o[0][0] /= s->cdx[compno];
-comp->coord_o[0][1] /= s->cdx[compno];
-comp->coord_o[1][0] /= s->cdy[compno];
-comp->coord_o[1][1] /= s->cdy[compno];
-}
+
+comp->coord_o[0][0] = ff_jpeg2000_ceildiv(comp->coord_o[0][0], 
s->cdx[compno]);
+comp->coord_o[0][1] = ff_jpeg2000_ceildiv(comp->coord_o[0][1], 
s->cdx[compno]);
+comp->coord_o[1][0] = ff_jpeg2000_ceildiv(comp->coord_o[1][0], 
s->cdy[compno]);
+comp->coord_o[1][1] = ff_jpeg2000_ceildiv(comp->coord_o[1][1], 
s->cdy[compno]);
 
 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], 
s->reduction_factor);
 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], 
s->reduction_factor);
@@ -1936,18 +1935,23 @@ static inline void 
tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile
 float *datap = comp->f_data;   
   \
 int32_t *i_datap = comp->i_data;   
   \
 int cbps = s->cbps[compno];
   \
-int w= tile->comp[compno].coord[0][1] - 
s->image_offset_x;\
+int w= tile->comp[compno].coord[0][1] -
   \
+   ff_jpeg2000_ceildiv(s->image_offset_x, 
s->cdx[compno]);\
+int h= tile->comp[compno].coord[1][1] -
   \
+   ff_jpeg2000_ceildiv(s->image_offset_y, 
s->cdy[compno]);\
 int plane= 0;  
   \

   \
 if (planar)
   \
 plane = s->cdef[compno] ? s->cdef[compno]-1 : 
(s->ncomponents-1); \

   \
-y= tile->comp[compno].coord[1][0] - s->image_offset_y / 
s->cdy[compno];   \
+y= tile->comp[compno].coord[1][0] -
   \
+   ff_jpeg2000_ceildiv(s->image_offset_y, s->cdy[compno]); 
   \
 line = (PIXEL *)picture->data[plane] + y * 
(picture->linesize[plane] / sizeof(PIXEL));\
-for (; y < tile->comp[compno].coord[1][1] - s->image_offset_y; 
y++) { \
+for (; y < h; y++) {   
   \
 PIXEL *dst;
   \

   \
-x   = tile->comp[compno].coord[0][0] - s->image_offset_x / 
s->cdx[compno];\
+x   = tile->comp[compno].coord[0][0] - 
   \
+  ff_jpeg2000_ceildiv(s->image_offset_x, s->cdx[compno]);  
   \
 dst = line + x * pixelsize + compno*!planar;   
   \

   \
 if (codsty->transform == FF_DWT97) {   
   \
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 4/5] libavcodec/jpeg2000dec.c: Enable image offsets

2020-06-21 Thread gautamramk
From: Gautam Ramakrishnan 

This patch enables support for image offsets.
---
 libavcodec/jpeg2000dec.c | 4 
 1 file changed, 4 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 546a646668..c8c89803ac 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -288,10 +288,6 @@ static int get_siz(Jpeg2000DecoderContext *s)
 s->tile_offset_y  = bytestream2_get_be32u(>g); // YT0Siz
 ncomponents   = bytestream2_get_be16u(>g); // CSiz
 
-if (s->image_offset_x || s->image_offset_y) {
-avpriv_request_sample(s->avctx, "Support for image offsets");
-return AVERROR_PATCHWELCOME;
-}
 if (av_image_check_size2(s->width, s->height, s->avctx->max_pixels, 
AV_PIX_FMT_NONE, 0, s->avctx)) {
 avpriv_request_sample(s->avctx, "Large Dimensions");
 return AVERROR_PATCHWELCOME;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 2/5] libavcodec/jpeg2000dec.c: Modify image dimensions

2020-06-21 Thread gautamramk
From: Gautam Ramakrishnan 

Reduce image size of the image if all components have
a non zero sample separation. This is to replicate the
output of opj_decompress.
---
 libavcodec/jpeg2000dec.c | 19 ++-
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index ab36009a2d..05e85f4317 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -269,6 +269,8 @@ static int get_siz(Jpeg2000DecoderContext *s)
 const enum AVPixelFormat *possible_fmts = NULL;
 int possible_fmts_nb = 0;
 int ret;
+int o_dimx, o_dimy; //original image dimensions.
+int dimx, dimy;
 
 if (bytestream2_get_bytes_left(>g) < 36) {
 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for SIZ\n");
@@ -371,11 +373,18 @@ static int get_siz(Jpeg2000DecoderContext *s)
 }
 
 /* compute image size with reduction factor */
-ret = ff_set_dimensions(s->avctx,
-ff_jpeg2000_ceildivpow2(s->width  - s->image_offset_x,
-   s->reduction_factor),
-ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
-   s->reduction_factor));
+o_dimx = ff_jpeg2000_ceildivpow2(s->width  - s->image_offset_x,
+   s->reduction_factor);
+o_dimy = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
+   s->reduction_factor);
+dimx = ff_jpeg2000_ceildiv(o_dimx, s->cdx[0]);
+dimy = ff_jpeg2000_ceildiv(o_dimy, s->cdy[0]);
+for (i = 1; i < s->ncomponents; i++) {
+dimx = FFMAX(dimx, ff_jpeg2000_ceildiv(o_dimx, s->cdx[i]));
+dimy = FFMAX(dimy, ff_jpeg2000_ceildiv(o_dimy, s->cdy[i]));
+}
+
+ret = ff_set_dimensions(s->avctx, dimx, dimy);
 if (ret < 0)
 return ret;
 
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH 1/5] libavcodec/jpeg2000.c: Precinct size check removed

2020-06-21 Thread gautamramk
From: Gautam Ramakrishnan 

This patch removes a check which throws an error if
the log2 precinct width/height is 0. The standard allows
the first component to have 0 as the log2 width/height.
---
 libavcodec/jpeg2000.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 73206d17f3..1aca31ffa4 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -509,9 +509,6 @@ int ff_jpeg2000_init_component(Jpeg2000Component *comp,
 // update precincts size: 2^n value
 reslevel->log2_prec_width  = codsty->log2_prec_widths[reslevelno];
 reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
-if (!reslevel->log2_prec_width || !reslevel->log2_prec_height) {
-return AVERROR_INVALIDDATA;
-}
 
 /* Number of bands for each resolution level */
 if (reslevelno == 0)
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH] libavcodec/jpeg2000: Make corrections jpeg2000 decoder

2020-06-18 Thread gautamramk
From: Gautam Ramakrishnan 

This is with reference to my previous email on the mailing list
with subject: "query on pixel formats".
I wish to cleanup some errors in the decoder code. These changes
would allow the samples p1_01.j2k and p1_07.j2k to be decoded.
However, I am facing issues with pixel format selection and have
currently forced the pixel formats to demonstrate the changes made.
Would be grateful if anyone could suggest modifications to the pix
format selection.
---
 libavcodec/jpeg2000.c|  3 ---
 libavcodec/jpeg2000dec.c | 53 ++--
 2 files changed, 34 insertions(+), 22 deletions(-)

diff --git a/libavcodec/jpeg2000.c b/libavcodec/jpeg2000.c
index 73206d17f3..1aca31ffa4 100644
--- a/libavcodec/jpeg2000.c
+++ b/libavcodec/jpeg2000.c
@@ -509,9 +509,6 @@ int ff_jpeg2000_init_component(Jpeg2000Component *comp,
 // update precincts size: 2^n value
 reslevel->log2_prec_width  = codsty->log2_prec_widths[reslevelno];
 reslevel->log2_prec_height = codsty->log2_prec_heights[reslevelno];
-if (!reslevel->log2_prec_width || !reslevel->log2_prec_height) {
-return AVERROR_INVALIDDATA;
-}
 
 /* Number of bands for each resolution level */
 if (reslevelno == 0)
diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index ab36009a2d..ae63c68ca8 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -269,6 +269,7 @@ static int get_siz(Jpeg2000DecoderContext *s)
 const enum AVPixelFormat *possible_fmts = NULL;
 int possible_fmts_nb = 0;
 int ret;
+int dimx, dimy;
 
 if (bytestream2_get_bytes_left(>g) < 36) {
 av_log(s->avctx, AV_LOG_ERROR, "Insufficient space for SIZ\n");
@@ -286,10 +287,6 @@ static int get_siz(Jpeg2000DecoderContext *s)
 s->tile_offset_y  = bytestream2_get_be32u(>g); // YT0Siz
 ncomponents   = bytestream2_get_be16u(>g); // CSiz
 
-if (s->image_offset_x || s->image_offset_y) {
-avpriv_request_sample(s->avctx, "Support for image offsets");
-return AVERROR_PATCHWELCOME;
-}
 if (av_image_check_size2(s->width, s->height, s->avctx->max_pixels, 
AV_PIX_FMT_NONE, 0, s->avctx)) {
 avpriv_request_sample(s->avctx, "Large Dimensions");
 return AVERROR_PATCHWELCOME;
@@ -371,11 +368,18 @@ static int get_siz(Jpeg2000DecoderContext *s)
 }
 
 /* compute image size with reduction factor */
-ret = ff_set_dimensions(s->avctx,
-ff_jpeg2000_ceildivpow2(s->width  - s->image_offset_x,
-   s->reduction_factor),
-ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
-   s->reduction_factor));
+dimx = ff_jpeg2000_ceildivpow2(s->width  - s->image_offset_x,
+   s->reduction_factor);
+dimy = ff_jpeg2000_ceildivpow2(s->height - s->image_offset_y,
+   s->reduction_factor);
+dimx = ff_jpeg2000_ceildiv(dimx, s->cdx[0]);
+dimy = ff_jpeg2000_ceildiv(dimy, s->cdy[0]);
+for (i = 1; i < s->ncomponents; i++) {
+dimx = FFMAX(dimx, ff_jpeg2000_ceildiv(dimx, s->cdx[i]));
+dimy = FFMAX(dimy, ff_jpeg2000_ceildiv(dimy, s->cdy[i]));
+}
+
+ret = ff_set_dimensions(s->avctx, dimx, dimy);
 if (ret < 0)
 return ret;
 
@@ -427,6 +431,13 @@ static int get_siz(Jpeg2000DecoderContext *s)
 s->cdef[3] = 3;
 i = 0;
 }
+} else if (ncomponents == 2) {
+s->avctx->pix_fmt = AV_PIX_FMT_YA8;
+i = 0;
+} else if (ncomponents == 1) {
+s->avctx->pix_fmt = AV_PIX_FMT_GRAY8;
+i = 0;
+s->cdef[0] = 0;
 }
 }
 
@@ -969,12 +980,11 @@ static int init_tile(Jpeg2000DecoderContext *s, int 
tileno)
 comp->coord_o[0][1] = tile->coord[0][1];
 comp->coord_o[1][0] = tile->coord[1][0];
 comp->coord_o[1][1] = tile->coord[1][1];
-if (compno) {
-comp->coord_o[0][0] /= s->cdx[compno];
-comp->coord_o[0][1] /= s->cdx[compno];
-comp->coord_o[1][0] /= s->cdy[compno];
-comp->coord_o[1][1] /= s->cdy[compno];
-}
+
+comp->coord_o[0][0] = ff_jpeg2000_ceildiv(comp->coord_o[0][0], 
s->cdx[compno]);
+comp->coord_o[0][1] = ff_jpeg2000_ceildiv(comp->coord_o[0][1], 
s->cdx[compno]);
+comp->coord_o[1][0] = ff_jpeg2000_ceildiv(comp->coord_o[1][0], 
s->cdy[compno]);
+comp->coord_o[1][1] = ff_jpeg2000_ceildiv(comp->coord_o[1][1], 
s->cdy[compno]);
 
 comp->coord[0][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][0], 
s->reduction_factor);
 comp->coord[0][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[0][1], 
s->reduction_factor);
@@ -1927,18 +1937,23 @@ static inline void 
tile_codeblocks(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile
 float *datap = 

[FFmpeg-devel] [RFC PATCH] libavcodec/libopenjpeg: pix fmt selection change

2020-06-09 Thread gautamramk
From: Gautam Ramakrishnan 

This patch makes selection of pix_fmt similar to
that in the native decoder. This makes samples such
as p0_05.j2k and p1_03.j2k decodable by libopenjpeg.
---
 libavcodec/libopenjpegdec.c | 21 +++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/libavcodec/libopenjpegdec.c b/libavcodec/libopenjpegdec.c
index 344c5ba5a3..f5f208784e 100644
--- a/libavcodec/libopenjpegdec.c
+++ b/libavcodec/libopenjpegdec.c
@@ -272,11 +272,11 @@ static inline void libopenjpeg_copyto8(AVFrame *picture, 
opj_image_t *image) {
 int *comp_data;
 uint8_t *img_ptr;
 int index, x, y;
-
 for (index = 0; index < image->numcomps; index++) {
+int plane = index?index-1:image->numcomps-1;
 comp_data = image->comps[index].data;
 for (y = 0; y < image->comps[index].h; y++) {
-img_ptr = picture->data[index] + y * picture->linesize[index];
+img_ptr = picture->data[plane] + y * picture->linesize[plane];
 for (x = 0; x < image->comps[index].w; x++) {
 *img_ptr = 0x80 * image->comps[index].sgnd + *comp_data;
 img_ptr++;
@@ -408,6 +408,23 @@ static int libopenjpeg_decode_frame(AVCodecContext *avctx,
 if (avctx->pix_fmt == AV_PIX_FMT_NONE)
 avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
 
+if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
+if (image->numcomps == 4 &&
+image->comps[0].dx == 1 && image->comps[0].dy == 1 &&
+image->comps[1].dx == 1 && image->comps[1].dy == 1 &&
+image->comps[2].dx == image->comps[3].dx &&
+image->comps[2].dy == image->comps[3].dy) {
+int maxprec = 0;
+for (int i = 0; i < 4; i++)
+maxprec = FFMAX(maxprec, image->comps[i].prec);
+if (maxprec == 8 &&
+image->comps[2].dx == 2 &&
+image->comps[2].dy == 2) {
+avctx->pix_fmt = AV_PIX_FMT_YUVA420P;
+}
+}
+}
+
 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
 av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format.\n");
 ret = AVERROR_UNKNOWN;
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH] libavcodec/jpeg2000dec: Support for PPM marker

2020-06-08 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support for PPM marker for JPEG2000
decoder. It allows the sample p1_03.j2k to be decoded.
---
 libavcodec/jpeg2000dec.c | 78 ++--
 1 file changed, 75 insertions(+), 3 deletions(-)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index b7766459c4..4e66ebf7c6 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -71,6 +71,7 @@ typedef struct 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;
 
@@ -102,6 +103,13 @@ typedef struct Jpeg2000DecoderContext {
 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;
@@ -901,6 +909,31 @@ static int get_plt(Jpeg2000DecoderContext *s, int n)
 return 0;
 }
 
+static int get_ppm(Jpeg2000DecoderContext *s, int n)
+{
+void *new;
+
+if (n < 3) {
+av_log(s->avctx, AV_LOG_ERROR, "Invalid length for PPM data.\n");
+return AVERROR_INVALIDDATA;
+}
+s->has_ppm = 1;
+bytestream2_get_byte(>g); //Zppm is skipped and not used
+new = av_realloc(s->packed_headers,
+ s->packed_headers_size + n - 3);
+if (new) {
+s->packed_headers = new;
+} else
+return AVERROR(ENOMEM);
+memset(>packed_headers_stream, 0, sizeof(s->packed_headers_stream));
+memcpy(s->packed_headers + s->packed_headers_size,
+   s->g.buffer, n - 3);
+s->packed_headers_size += n - 3;
+bytestream2_skip(>g, n - 3);
+
+return 0;
+}
+
 static int get_ppt(Jpeg2000DecoderContext *s, int n)
 {
 Jpeg2000Tile *tile;
@@ -1012,6 +1045,17 @@ static int getlblockinc(Jpeg2000DecoderContext *s)
 return res;
 }
 
+static inline void select_header(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
+ int *tp_index)
+{
+s->g = tile->tile_part[*tp_index].header_tpg;
+if (bytestream2_get_bytes_left(>g) == 0 && s->bit_index == 8) {
+if (*tp_index < FF_ARRAY_ELEMS(tile->tile_part) - 1) {
+s->g = tile->tile_part[++(*tp_index)].tpg;
+}
+}
+}
+
 static inline void select_stream(Jpeg2000DecoderContext *s, Jpeg2000Tile *tile,
  int *tp_index)
 {
@@ -1037,7 +1081,9 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile,
 return 0;
 rlevel->band[0].prec[precno].decoded_layers = layno + 1;
 // Select stream to read from
-if (tile->has_ppt)
+if (s->has_ppm)
+select_header(s, tile, tp_index);
+else if (tile->has_ppt)
 s->g = tile->packed_headers_stream;
 else
 select_stream(s, tile, tp_index);
@@ -1151,7 +1197,10 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile,
 }
 
 // Save state of stream
-if (tile->has_ppt) {
+if (s->has_ppm) {
+tile->tile_part[*tp_index].header_tpg = s->g;
+select_stream(s, tile, tp_index);
+} else if (tile->has_ppt) {
 tile->packed_headers_stream = s->g;
 select_stream(s, tile, tp_index);
 }
@@ -1201,7 +1250,9 @@ static int jpeg2000_decode_packet(Jpeg2000DecoderContext 
*s, Jpeg2000Tile *tile,
 return 0;
 
 skip_data:
-if (tile->has_ppt)
+if (s->has_ppm)
+tile->tile_part[*tp_index].header_tpg = s->g;
+else if (tile->has_ppt)
 tile->packed_headers_stream = s->g;
 else
 tile->tile_part[*tp_index].tpg = s->g;
@@ -2068,6 +2119,11 @@ static int 
jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
 return AVERROR_INVALIDDATA;
 }
 
+if (s->has_ppm) {
+uint32_t tp_header_size = 
bytestream2_get_be32u(>packed_headers_stream);
+bytestream2_init(>header_tpg, 
s->packed_headers_stream.buffer, tp_header_size);
+bytestream2_skip(>packed_headers_stream, tp_header_size);
+}
 if (tile->has_ppt && tile->tp_idx == 0) {
 bytestream2_init(>packed_headers_stream, 
tile->packed_headers, tile->packed_headers_size);
 }
@@ -2119,6 +2175,12 @@ static int 
jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
 ret = get_poc(s, len, poc);
 break;
 case JPEG2000_SOT:
+if 

[FFmpeg-devel] [PATCH] libavcodec/jpeg2000_parser: Add jpeg2000 parser

2020-06-06 Thread gautamramk
From: Gautam Ramakrishnan 

I have attempted to write a JPEG2000 Parser. Have tested
by generating a file containing 14 frames, as mentioned
by Micheal. Have also tried testing with various packet
sizes by setting -frame_size option. Additionally,
fixed a few formatting issues as pointed out by Micheal.
---
 libavcodec/Makefile  |   1 +
 libavcodec/jpeg2000_parser.c | 190 +++
 libavcodec/parsers.c |   1 +
 3 files changed, 192 insertions(+)
 create mode 100644 libavcodec/jpeg2000_parser.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 0a3bbc7128..5a6ea59715 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1074,6 +1074,7 @@ OBJS-$(CONFIG_H261_PARSER) += h261_parser.o
 OBJS-$(CONFIG_H263_PARSER) += h263_parser.o
 OBJS-$(CONFIG_H264_PARSER) += h264_parser.o h264_sei.o h264data.o
 OBJS-$(CONFIG_HEVC_PARSER) += hevc_parser.o hevc_data.o
+OBJS-$(CONFIG_JPEG2000_PARSER) += jpeg2000_parser.o
 OBJS-$(CONFIG_MJPEG_PARSER)+= mjpeg_parser.o
 OBJS-$(CONFIG_MLP_PARSER)  += mlp_parse.o mlp_parser.o mlp.o
 OBJS-$(CONFIG_MPEG4VIDEO_PARSER)   += mpeg4video_parser.o h263.o \
diff --git a/libavcodec/jpeg2000_parser.c b/libavcodec/jpeg2000_parser.c
new file mode 100644
index 00..79fed0cbbe
--- /dev/null
+++ b/libavcodec/jpeg2000_parser.c
@@ -0,0 +1,190 @@
+/*
+ * JPEG2000 parser
+ * Copyright (c) 2020 Gautam Ramakrishnan
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * JPEG2000 parser.
+ */
+
+#include "parser.h"
+
+/* Whether frame is jp2 file or codestream
+*/
+enum frame_type {
+jp2_file = 1,
+j2k_cstream
+};
+
+typedef struct JPEG2000ParserContext {
+ParseContext pc;
+uint64_t bytes_read;
+uint64_t fheader_state;
+uint32_t skip_bytes; // skip bytes inside codestream data
+enum frame_type ft; // 1 if file, 2 if codestream
+uint8_t fheader_read; // are we reading
+uint8_t reading_file_header;
+uint8_t skipped_codestream;
+uint8_t codestream_frame_end;
+uint8_t read_tp;
+uint8_t in_codestream;
+} JPEG2000ParserContext;
+
+static inline void reset_context(JPEG2000ParserContext *m)
+{
+ParseContext *pc = >pc;
+
+pc->frame_start_found= 0;
+pc->state = 0;
+m->bytes_read = 0;
+m->ft = 0;
+m->skipped_codestream = 0;
+m->fheader_read = 0;
+m->codestream_frame_end = 0;
+m->skip_bytes = 0;
+m->read_tp = 0;
+m->in_codestream = 0;
+}
+
+/* Returns 1 if marker has any data which can be skipped
+*/
+static uint8_t info_marker(uint16_t marker)
+{
+if (marker == 0xFF92 || marker == 0xFF4F ||
+marker == 0xFF90 || marker == 0xFF93 ||
+marker == 0xFFD9)
+return 0;
+else
+if (marker > 0xFF00) return 1;
+return 0;
+}
+
+/**
+ * Find the end of the current frame in the bitstream.
+ * @return the position of the first byte of the next frame, or -1
+ */
+static int find_frame_end(JPEG2000ParserContext *m, const uint8_t *buf, int 
buf_size)
+{
+ParseContext *pc= >pc;
+int i;
+uint32_t state;
+uint64_t state64;
+state= pc->state;
+state64 = pc->state64;
+if (buf_size == 0) {
+return 0;
+}
+
+for (i = 0; i < buf_size; i++) {
+state = state << 8 | buf[i];
+state64 = state64 << 8 | buf[i];
+m->bytes_read++;
+if (m->skip_bytes) {
+m->skip_bytes--;
+continue;
+}
+if (m->codestream_frame_end) {
+reset_context(m);
+return i;
+}
+if (m->read_tp) { // Find out how many bytes inside Tile part 
codestream to skip.
+if (m->read_tp == 1) {
+m->skip_bytes = (state64 & 0x) - 10 > 0?
+(state64 & 0x) - 10 : 0;
+}
+m->read_tp--;
+}
+if (m->fheader_read) {
+if (m->fheader_read == 1) {
+if (state64 == 0x6A5020200D0A870A) { // JP2 signature box 
value.
+if (pc->frame_start_found) {
+pc->frame_start_found = 0;
+reset_context(m);
+return i - 11;
+ 

[FFmpeg-devel] [RFC PATCH 1/2] libavcodec/jpeg2000_parser: Add jpeg2000 parser

2020-06-04 Thread gautamramk
From: Gautam Ramakrishnan 

I have attempted to write a JPEG2000 Parser. Have tested
by generating a file containing 14 frames, as mentioned
by Micheal. Have also tried testing with various packet
sizes by setting -frame_size option.
---
 libavcodec/Makefile  |   1 +
 libavcodec/jpeg2000_parser.c | 190 +++
 libavcodec/parsers.c |   1 +
 3 files changed, 192 insertions(+)
 create mode 100644 libavcodec/jpeg2000_parser.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 0a3bbc7128..5a6ea59715 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1074,6 +1074,7 @@ OBJS-$(CONFIG_H261_PARSER) += h261_parser.o
 OBJS-$(CONFIG_H263_PARSER) += h263_parser.o
 OBJS-$(CONFIG_H264_PARSER) += h264_parser.o h264_sei.o h264data.o
 OBJS-$(CONFIG_HEVC_PARSER) += hevc_parser.o hevc_data.o
+OBJS-$(CONFIG_JPEG2000_PARSER) += jpeg2000_parser.o
 OBJS-$(CONFIG_MJPEG_PARSER)+= mjpeg_parser.o
 OBJS-$(CONFIG_MLP_PARSER)  += mlp_parse.o mlp_parser.o mlp.o
 OBJS-$(CONFIG_MPEG4VIDEO_PARSER)   += mpeg4video_parser.o h263.o \
diff --git a/libavcodec/jpeg2000_parser.c b/libavcodec/jpeg2000_parser.c
new file mode 100644
index 00..06571d30e3
--- /dev/null
+++ b/libavcodec/jpeg2000_parser.c
@@ -0,0 +1,190 @@
+/*
+ * JPEG2000 parser
+ * Copyright (c) 2020 Gautam Ramakrishnan
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * JPEG2000 parser.
+ */
+
+#include "parser.h"
+
+/* Whether frame is jp2 file or codestream
+*/
+enum frame_type {
+jp2_file = 1,
+j2k_cstream
+};
+
+typedef struct JPEG2000ParserContext{
+ParseContext pc;
+uint64_t bytes_read;
+uint64_t fheader_state;
+uint32_t skip_bytes; // skip bytes inside codestream data
+enum frame_type ft; // 1 if file, 2 if codestream
+uint8_t fheader_read; // are we reading
+uint8_t reading_file_header;
+uint8_t skipped_codestream;
+uint8_t codestream_frame_end;
+uint8_t read_tp;
+uint8_t in_codestream;
+}JPEG2000ParserContext;
+
+static inline void reset_context(JPEG2000ParserContext *m)
+{
+ParseContext *pc = >pc;
+
+pc->frame_start_found= 0;
+pc->state = 0;
+m->bytes_read = 0;
+m->ft = 0;
+m->skipped_codestream = 0;
+m->fheader_read = 0;
+m->codestream_frame_end = 0;
+m->skip_bytes = 0;
+m->read_tp = 0;
+m->in_codestream = 0;
+}
+
+/* Returns 1 if marker has any data which can be skipped
+*/
+static uint8_t info_marker(uint16_t marker)
+{
+if (marker == 0xFF92 || marker == 0xFF4F ||
+marker == 0xFF90 || marker == 0xFF93 ||
+marker == 0xFFD9)
+return 0;
+else
+if (marker > 0xFF00) return 1;
+return 0;
+}
+
+/**
+ * Find the end of the current frame in the bitstream.
+ * @return the position of the first byte of the next frame, or -1
+ */
+static int find_frame_end(JPEG2000ParserContext *m, const uint8_t *buf, int 
buf_size)
+{
+ParseContext *pc= >pc;
+int i;
+uint32_t state;
+uint64_t state64;
+state= pc->state;
+state64 = pc->state64;
+if (buf_size == 0) {
+return 0;
+}
+
+for (i = 0; i < buf_size; i++) {
+state = state << 8 | buf[i];
+state64 = state64 << 8 | buf[i];
+m->bytes_read++;
+if (m->skip_bytes) {
+m->skip_bytes--;
+continue;
+}
+if (m->codestream_frame_end) {
+reset_context(m);
+return i;
+}
+if (m->read_tp) { // Find out how many bytes inside Tile part 
codestream to skip.
+if (m->read_tp == 1){
+m->skip_bytes = (state64 & 0x) - 10 > 0?
+(state64 & 0x) - 10 : 0;
+}
+m->read_tp--;
+}
+if (m->fheader_read) {
+if (m->fheader_read == 1) {
+if (state64 == 0x6A5020200D0A870A) { // JP2 signature box 
value.
+if (pc->frame_start_found) {
+pc->frame_start_found = 0;
+reset_context(m);
+return i - 11;
+} else {
+

[FFmpeg-devel] [RFC PATCH 2/2] fate/video.mak: fate test for jpeg2000 parser

2020-06-04 Thread gautamramk
From: Gautam Ramakrishnan 

This test generates a rawvideo video of jpeg2000 frames
using lavfi testsrc2 as input. The reference file as 25 frames.
---
 tests/fate-run.sh  |  4 +++-
 tests/fate/video.mak   |  3 +++
 tests/ref/fate/jpeg2000-parser | 32 
 3 files changed, 38 insertions(+), 1 deletion(-)
 create mode 100644 tests/ref/fate/jpeg2000-parser

diff --git a/tests/fate-run.sh b/tests/fate-run.sh
index 002944b010..28c74ecbc5 100755
--- a/tests/fate-run.sh
+++ b/tests/fate-run.sh
@@ -221,7 +221,9 @@ transcode(){
 ffprobe_opts=$7
 encfile="${outdir}/${test}.${enc_fmt}"
 test "$6" = -keep || cleanfiles="$cleanfiles $encfile"
-tsrcfile=$(target_path $srcfile)
+test $src_fmt = "lavfi" ||\
+tsrcfile=$(target_path $srcfile) &&\
+tsrcfile=$srcfile
 tencfile=$(target_path $encfile)
 ffmpeg -f $src_fmt $DEC_OPTS -i $tsrcfile $ENC_OPTS $enc_opt $FLAGS \
 -f $enc_fmt -y $tencfile || return
diff --git a/tests/fate/video.mak b/tests/fate/video.mak
index d2d43e518d..c8d2e66b4a 100644
--- a/tests/fate/video.mak
+++ b/tests/fate/video.mak
@@ -192,6 +192,9 @@ fate-interplay-mve-16bit: CMD = framecrc -i 
$(TARGET_SAMPLES)/interplay-mve/desc
 FATE_VIDEO-$(call DEMDEC, MXF, JPEG2000) += fate-jpeg2000-dcinema
 fate-jpeg2000-dcinema: CMD = framecrc -flags +bitexact -c:v jpeg2000 -i 
$(TARGET_SAMPLES)/jpeg2000/chiens_dcinema2K.mxf -pix_fmt xyz12le
 
+FATE_VIDEO-$(call DEMDEC, MXF, JPEG2000) += fate-jpeg2000-parser
+fate-jpeg2000-parser: CMD = transcode lavfi "testsrc2=s=640x480" rawvideo "-t 
1 -c:v jpeg2000"
+
 FATE_VIDEO-$(call DEMDEC, JV, JV) += fate-jv
 fate-jv: CMD = framecrc -i $(TARGET_SAMPLES)/jv/intro.jv -an -pix_fmt rgb24
 
diff --git a/tests/ref/fate/jpeg2000-parser b/tests/ref/fate/jpeg2000-parser
new file mode 100644
index 00..f30e27a24e
--- /dev/null
+++ b/tests/ref/fate/jpeg2000-parser
@@ -0,0 +1,32 @@
+d2ea224b9c7319e5240af18260e82c4d *tests/data/fate/jpeg2000-parser.rawvideo
+1511061 tests/data/fate/jpeg2000-parser.rawvideo
+#tb 0: 1/25
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 640x480
+#sar 0: 0/1
+0,  0,  0,1,   460800, 0x82d0c5fe
+0,  1,  1,1,   460800, 0x1bf42314
+0,  2,  2,1,   460800, 0x2d5598d4
+0,  3,  3,1,   460800, 0x159e4c0b
+0,  4,  4,1,   460800, 0x0652826a
+0,  5,  5,1,   460800, 0x23f718f4
+0,  6,  6,1,   460800, 0xc56cb13c
+0,  7,  7,1,   460800, 0x2ba79edd
+0,  8,  8,1,   460800, 0x8b2f8adb
+0,  9,  9,1,   460800, 0x5e9d58e9
+0, 10, 10,1,   460800, 0xf655492b
+0, 11, 11,1,   460800, 0xa590743b
+0, 12, 12,1,   460800, 0xbaa67b19
+0, 13, 13,1,   460800, 0x4ca64768
+0, 14, 14,1,   460800, 0x43978755
+0, 15, 15,1,   460800, 0x173a7c5c
+0, 16, 16,1,   460800, 0x3135a88e
+0, 17, 17,1,   460800, 0x17cbea25
+0, 18, 18,1,   460800, 0x21c222ab
+0, 19, 19,1,   460800, 0x7444efcf
+0, 20, 20,1,   460800, 0x860921d2
+0, 21, 21,1,   460800, 0x9ea5a718
+0, 22, 22,1,   460800, 0xb8c1a9d7
+0, 23, 23,1,   460800, 0xe4214b2c
+0, 24, 24,1,   460800, 0x3f00181d
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [RFC PATCH] libavcodec/jpeg2000_parser: Add jpeg2000 parser

2020-05-28 Thread gautamramk
From: Gautam Ramakrishnan 

I have attempted to write a JPEG2000 Parser. Have tested
by generating a file containing 14 frames, as mentioned
by Micheal. Have also tried testing with various packet
sizes by setting -frame_size option. Need feedback on the
code and on further testing.
---
 libavcodec/Makefile  |   1 +
 libavcodec/jpeg2000_parser.c | 190 +++
 libavcodec/parsers.c |   1 +
 3 files changed, 192 insertions(+)
 create mode 100644 libavcodec/jpeg2000_parser.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index d0917a656f..1f7c91a91b 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1073,6 +1073,7 @@ OBJS-$(CONFIG_H261_PARSER) += h261_parser.o
 OBJS-$(CONFIG_H263_PARSER) += h263_parser.o
 OBJS-$(CONFIG_H264_PARSER) += h264_parser.o h264_sei.o h264data.o
 OBJS-$(CONFIG_HEVC_PARSER) += hevc_parser.o hevc_data.o
+OBJS-$(CONFIG_JPEG2000_PARSER) += jpeg2000_parser.o
 OBJS-$(CONFIG_MJPEG_PARSER)+= mjpeg_parser.o
 OBJS-$(CONFIG_MLP_PARSER)  += mlp_parse.o mlp_parser.o mlp.o
 OBJS-$(CONFIG_MPEG4VIDEO_PARSER)   += mpeg4video_parser.o h263.o \
diff --git a/libavcodec/jpeg2000_parser.c b/libavcodec/jpeg2000_parser.c
new file mode 100644
index 00..c28b694219
--- /dev/null
+++ b/libavcodec/jpeg2000_parser.c
@@ -0,0 +1,190 @@
+/*
+ * JPEG2000 parser
+ * Copyright (c) 2000, 2001 Fabrice Bellard
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * JPEG2000 parser.
+ */
+
+#include "parser.h"
+
+/* Whether frame is jp2 file or codestream
+*/
+enum frame_type {
+jp2_file = 1,
+j2k_cstream
+};
+
+typedef struct JPEG2000ParserContext{
+ParseContext pc;
+uint64_t bytes_read;
+uint64_t fheader_state;
+uint32_t skip_bytes; // skip bytes inside codestream data
+enum frame_type ft; // 1 if file, 2 if codestream
+uint8_t fheader_read; // are we reading
+uint8_t reading_file_header;
+uint8_t skipped_codestream;
+uint8_t codestream_frame_end;
+uint8_t read_tp;
+uint8_t in_codestream;
+}JPEG2000ParserContext;
+
+static inline void reset_context(JPEG2000ParserContext *m)
+{
+ParseContext *pc = >pc;
+
+pc->frame_start_found= 0;
+pc->state = 0;
+m->bytes_read = 0;
+m->ft = 0;
+m->skipped_codestream = 0;
+m->fheader_read = 0;
+m->codestream_frame_end = 0;
+m->skip_bytes = 0;
+m->read_tp = 0;
+m->in_codestream = 0;
+}
+
+/* Returns 1 if marker has any data which can be skipped
+*/
+static uint8_t info_marker(uint16_t marker)
+{
+if (marker == 0xFF92 || marker == 0xFF4F ||
+marker == 0xFF90 || marker == 0xFF93 ||
+marker == 0xFFD9)
+return 0;
+else
+if (marker > 0xFF00) return 1;
+return 0;
+}
+
+/**
+ * Find the end of the current frame in the bitstream.
+ * @return the position of the first byte of the next frame, or -1
+ */
+static int find_frame_end(JPEG2000ParserContext *m, const uint8_t *buf, int 
buf_size)
+{
+ParseContext *pc= >pc;
+int i;
+uint32_t state;
+uint64_t state64;
+state= pc->state;
+state64 = pc->state64;
+if (buf_size == 0) {
+return 0;
+}
+
+for (i = 0; i < buf_size; i++) {
+state = state << 8 | buf[i];
+state64 = state64 << 8 | buf[i];
+m->bytes_read++;
+if (m->skip_bytes) {
+m->skip_bytes--;
+continue;
+}
+if (m->codestream_frame_end) {
+reset_context(m);
+return i;
+}
+if (m->read_tp) { // Find out how many bytes inside Tile part 
codestream to skip.
+if (m->read_tp == 1){
+m->skip_bytes = (state64 & 0x) - 10 > 0?
+(state64 & 0x) - 10 : 0;
+}
+m->read_tp--;
+}
+if (m->fheader_read) {
+if (m->fheader_read == 1) {
+if (state64 == 0x6A5020200D0A870A) { // JP2 signature box 
value.
+if (pc->frame_start_found) {
+pc->frame_start_found = 0;
+reset_context(m);
+return i - 11;
+} 

[FFmpeg-devel] [PATCH v3] libavcodec/jpeg2000dec.c: ROI marker support

2020-04-22 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support for decoding images
with a Region of Interest. Allows decoding
samples such as p0_03.j2k. This patch should
fix ticket #4681.
---
 libavcodec/jpeg2000.h|  1 +
 libavcodec/jpeg2000dec.c | 66 ++--
 2 files changed, 64 insertions(+), 3 deletions(-)

diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index 7b78c0193e..0f82716981 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -210,6 +210,7 @@ typedef struct Jpeg2000Component {
 int *i_data;
 int coord[2][2];   // border coordinates {{x0, x1}, {y0, y1}} -- can be 
reduced with lowres option
 int coord_o[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- original 
values from jpeg2000 headers
+uint8_t roi_shift; // ROI scaling value for the component
 } Jpeg2000Component;
 
 /* misc tools */
diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 5a7d9e7882..460a4ad95c 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -117,6 +117,7 @@ typedef struct Jpeg2000DecoderContext {
 Jpeg2000CodingStyle codsty[4];
 Jpeg2000QuantStyle  qntsty[4];
 Jpeg2000POC poc;
+uint8_t roi_shift[4];
 
 int bit_index;
 
@@ -598,6 +599,31 @@ static int get_coc(Jpeg2000DecoderContext *s, 
Jpeg2000CodingStyle *c,
 return 0;
 }
 
+static int get_rgn(Jpeg2000DecoderContext *s, int n)
+{
+uint16_t compno;
+compno = (s->ncomponents < 257)? bytestream2_get_byte(>g):
+ bytestream2_get_be16u(>g);
+if (bytestream2_get_byte(>g)) {
+av_log(s->avctx, AV_LOG_ERROR, "Invalid RGN header.\n");
+return AVERROR_INVALIDDATA; // SRgn field value is 0
+}
+// SPrgn field
+// Currently compno cannot be greater than 4.
+// However, future implementation should support compno up to 65536
+if (compno < s->ncomponents) {
+if (s->curtileno == -1)
+s->roi_shift[compno] = bytestream2_get_byte(>g);
+else {
+if (s->tile[s->curtileno].tp_idx != 0)
+return AVERROR_INVALIDDATA; // marker occurs only in first 
tile part of tile
+s->tile[s->curtileno].comp[compno].roi_shift = 
bytestream2_get_byte(>g);
+}
+return 0;
+}
+return AVERROR_INVALIDDATA;
+}
+
 /* Get common part for QCD and QCC segments. */
 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
 {
@@ -947,6 +973,9 @@ static int init_tile(Jpeg2000DecoderContext *s, int tileno)
 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], 
s->reduction_factor);
 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], 
s->reduction_factor);
 
+if (!comp->roi_shift)
+comp->roi_shift = s->roi_shift[compno];
+
 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
  s->cbps[compno], s->cdx[compno],
  s->cdy[compno], s->avctx))
@@ -1615,9 +1644,9 @@ static void decode_clnpass(Jpeg2000DecoderContext *s, 
Jpeg2000T1Context *t1,
 
 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
-   int width, int height, int bandpos)
+   int width, int height, int bandpos, uint8_t roi_shift)
 {
-int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1;
+int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + 
roi_shift;
 int pass_cnt = 0;
 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
 int term_cnt = 0;
@@ -1691,6 +1720,19 @@ static int decode_cblk(Jpeg2000DecoderContext *s, 
Jpeg2000CodingStyle *codsty,
 return 1;
 }
 
+static inline int roi_shift_param(Jpeg2000Component *comp,
+   int quan_parameter)
+{
+uint8_t roi_shift;
+int val;
+roi_shift = comp->roi_shift;
+val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
+
+if (val > (1 << roi_shift))
+return (quan_parameter < 0)?-(val >> roi_shift):(val >> roi_shift);
+return quan_parameter;
+}
+
 /* TODO: Verify dequantization for lossless case
  * comp->data can be float or int
  * band->stepsize can be float or int
@@ -1775,6 +1817,19 @@ static inline void mct_decode(Jpeg2000DecoderContext *s, 
Jpeg2000Tile *tile)
 s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], 
csize);
 }
 
+static inline void roi_scale_cblk(Jpeg2000Cblk *cblk,
+  Jpeg2000Component *comp,
+  Jpeg2000T1Context *t1)
+{
+int i, j;
+int w = cblk->coord[0][1] - cblk->coord[0][0];
+for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
+int *src = t1->data + j*t1->stride;
+for (i = 0; i < w; ++i)
+

[FFmpeg-devel] [PATCH v2 1/2] libavcodec/jpeg2000dec.c: Support for CRG marker

2020-04-22 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support to skip the CRG marker.
The CRG marker, is an informational marker.
Allows samples such as p0_03.j2k to be decoded.
---
 libavcodec/jpeg2000dec.c | 12 
 1 file changed, 12 insertions(+)

diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index af6dcee228..5a7d9e7882 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -798,6 +798,15 @@ static int get_sot(Jpeg2000DecoderContext *s, int n)
 return 0;
 }
 
+static int read_crg(Jpeg2000DecoderContext *s, int n)
+{
+if (s->ncomponents*4 != n - 2) {
+av_log(s->avctx, AV_LOG_ERROR, "Invalid CRG marker.\n");
+return AVERROR_INVALIDDATA;
+}
+bytestream2_skip(>g, n - 2);
+return 0;
+}
 /* Tile-part lengths: see ISO 15444-1:2002, section A.7.1
  * Used to know the number of tile parts and lengths.
  * There may be multiple TLMs in the header.
@@ -2061,6 +2070,9 @@ static int 
jpeg2000_read_main_headers(Jpeg2000DecoderContext *s)
 // the comment is ignored
 bytestream2_skip(>g, len - 2);
 break;
+case JPEG2000_CRG:
+ret = read_crg(s, len);
+break;
 case JPEG2000_TLM:
 // Tile-part lengths
 ret = get_tlm(s, len);
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

[FFmpeg-devel] [PATCH v2 2/2] libavcodec/jpeg2000dec.c: ROI marker support

2020-04-22 Thread gautamramk
From: Gautam Ramakrishnan 

This patch adds support for decoding images
with a Region of Interest. Allows decoding
samples such as p0_03.j2k. This patch should
fix ticket #4681.
---
 libavcodec/jpeg2000.h|  1 +
 libavcodec/jpeg2000dec.c | 67 ++--
 2 files changed, 65 insertions(+), 3 deletions(-)

diff --git a/libavcodec/jpeg2000.h b/libavcodec/jpeg2000.h
index 7b78c0193e..0f82716981 100644
--- a/libavcodec/jpeg2000.h
+++ b/libavcodec/jpeg2000.h
@@ -210,6 +210,7 @@ typedef struct Jpeg2000Component {
 int *i_data;
 int coord[2][2];   // border coordinates {{x0, x1}, {y0, y1}} -- can be 
reduced with lowres option
 int coord_o[2][2]; // border coordinates {{x0, x1}, {y0, y1}} -- original 
values from jpeg2000 headers
+uint8_t roi_shift; // ROI scaling value for the component
 } Jpeg2000Component;
 
 /* misc tools */
diff --git a/libavcodec/jpeg2000dec.c b/libavcodec/jpeg2000dec.c
index 5a7d9e7882..bcc6cc628b 100644
--- a/libavcodec/jpeg2000dec.c
+++ b/libavcodec/jpeg2000dec.c
@@ -117,6 +117,7 @@ typedef struct Jpeg2000DecoderContext {
 Jpeg2000CodingStyle codsty[4];
 Jpeg2000QuantStyle  qntsty[4];
 Jpeg2000POC poc;
+uint8_t roi_shift[4];
 
 int bit_index;
 
@@ -598,6 +599,31 @@ static int get_coc(Jpeg2000DecoderContext *s, 
Jpeg2000CodingStyle *c,
 return 0;
 }
 
+static int get_rgn(Jpeg2000DecoderContext *s, int n)
+{
+uint16_t compno;
+compno = (s->ncomponents < 257)? bytestream2_get_byte(>g):
+ bytestream2_get_be16u(>g);
+if (bytestream2_get_byte(>g)) {
+av_log(s->avctx, AV_LOG_ERROR, "Invalid RGN header.\n");
+return AVERROR_INVALIDDATA; // SRgn field value is 0
+}
+// SPrgn field
+// Currently compno cannot be greater than 4.
+// However, future implementation should support compno up to 65536
+if (compno < s->ncomponents) {
+if (s->curtileno == -1)
+s->roi_shift[compno] = bytestream2_get_byte(>g);
+else {
+if (s->tile[s->curtileno].tp_idx != 0)
+return AVERROR_INVALIDDATA; // marker occurs only in first 
tile part of tile
+s->tile[s->curtileno].comp[compno].roi_shift = 
bytestream2_get_byte(>g);
+}
+return 0;
+}
+return AVERROR_INVALIDDATA;
+}
+
 /* Get common part for QCD and QCC segments. */
 static int get_qcx(Jpeg2000DecoderContext *s, int n, Jpeg2000QuantStyle *q)
 {
@@ -947,6 +973,9 @@ static int init_tile(Jpeg2000DecoderContext *s, int tileno)
 comp->coord[1][0] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][0], 
s->reduction_factor);
 comp->coord[1][1] = ff_jpeg2000_ceildivpow2(comp->coord_o[1][1], 
s->reduction_factor);
 
+if (!comp->roi_shift)
+comp->roi_shift = s->roi_shift[compno];
+
 if (ret = ff_jpeg2000_init_component(comp, codsty, qntsty,
  s->cbps[compno], s->cdx[compno],
  s->cdy[compno], s->avctx))
@@ -1615,9 +1644,9 @@ static void decode_clnpass(Jpeg2000DecoderContext *s, 
Jpeg2000T1Context *t1,
 
 static int decode_cblk(Jpeg2000DecoderContext *s, Jpeg2000CodingStyle *codsty,
Jpeg2000T1Context *t1, Jpeg2000Cblk *cblk,
-   int width, int height, int bandpos)
+   int width, int height, int bandpos, uint8_t roi_shift)
 {
-int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1;
+int passno = cblk->npasses, pass_t = 2, bpno = cblk->nonzerobits - 1 + 
roi_shift;
 int pass_cnt = 0;
 int vert_causal_ctx_csty_symbol = codsty->cblk_style & JPEG2000_CBLK_VSC;
 int term_cnt = 0;
@@ -1691,6 +1720,19 @@ static int decode_cblk(Jpeg2000DecoderContext *s, 
Jpeg2000CodingStyle *codsty,
 return 1;
 }
 
+static inline int roi_shift_param(Jpeg2000Component *comp,
+   int quan_parameter)
+{
+uint8_t roi_shift;
+int val;
+roi_shift = comp->roi_shift;
+val = (quan_parameter < 0)?-quan_parameter:quan_parameter;
+
+if (val > (1 << roi_shift))
+return (quan_parameter < 0)?-(val >> roi_shift):(val >> roi_shift);
+return quan_parameter;
+}
+
 /* TODO: Verify dequantization for lossless case
  * comp->data can be float or int
  * band->stepsize can be float or int
@@ -1775,6 +1817,20 @@ static inline void mct_decode(Jpeg2000DecoderContext *s, 
Jpeg2000Tile *tile)
 s->dsp.mct_decode[tile->codsty[0].transform](src[0], src[1], src[2], 
csize);
 }
 
+static inline void roi_scale_cblk(int x, int y, Jpeg2000Cblk *cblk,
+  Jpeg2000Component *comp,
+  Jpeg2000T1Context *t1)
+{
+int i, j;
+int w = cblk->coord[0][1] - cblk->coord[0][0];
+for (j = 0; j < (cblk->coord[1][1] - cblk->coord[1][0]); ++j) {
+int32_t *datap = >i_data[(comp->coord[0][1] - 

  1   2   >