[PATCH]Fix linesize for pix_fmt pal8
Hi! FFmpeg currently cannot read raw pal8 files written with ffmpeg if width mod 4 != 0. Attached patch fixes that (and allows QuickTime to read the files if a palette is written). Please review, Carl Eugen
On date Wednesday 2012-06-13 15:36:22 +0200, Carl Eugen Hoyos encoded:
Hi!
FFmpeg currently cannot read raw pal8 files written with ffmpeg if width mod 4 != 0. Attached patch fixes that (and allows QuickTime to read the files if a palette is written).
Please review, Carl Eugen
diff --git a/libavutil/imgutils.c b/libavutil/imgutils.c index ef7d413..39f086f 100644 --- a/libavutil/imgutils.c +++ b/libavutil/imgutils.c @@ -60,6 +60,11 @@ int image_get_linesize(int width, int plane, linesize = max_step * shifted_w; if (desc->flags & PIX_FMT_BITSTREAM) linesize = (linesize + 7) >> 3; + else if (desc->flags & PIX_FMT_PAL) { + // 2 would be sufficient for QuickTime, + // but 4 works and is what avi and mov samples use + linesize = FFALIGN(linesize, 4); + } return linesize;
Can you elaborate why this is needed? Note that the alignment is defined by av_image_alloc(), and in general I don't see why a paletted image should need an aligned size, so I'd prefer to avoid the special casing, or in other words this seems more a libavcodec issue (and should be addressed there). -- FFmpeg = Frightening Freak Most Patchable Educated Governor
Stefano Sabatini <stefasab <at> gmail.com> writes:
+ else if (desc->flags & PIX_FMT_PAL) { + // 2 would be sufficient for QuickTime, + // but 4 works and is what avi and mov samples use + linesize = FFALIGN(linesize, 4); + } return linesize;
Can you elaborate why this is needed?
$ ./ffmpeg -i tests/lena.pnm -s 257x257 -pix_fmt pal8 -vcodec rawvideo out.avi $ ./ffplay out.avi
Note that the alignment is defined by av_image_alloc(), and in general I don't see why a paletted image should need an aligned size, so I'd prefer to avoid the special casing, or in other words this seems more a libavcodec issue (and should be addressed there).
Please elaborate. Carl Eugen
On date Thursday 2012-06-14 06:42:04 +0000, Carl Eugen Hoyos encoded:
Stefano Sabatini <stefasab <at> gmail.com> writes:
+ else if (desc->flags & PIX_FMT_PAL) { + // 2 would be sufficient for QuickTime, + // but 4 works and is what avi and mov samples use + linesize = FFALIGN(linesize, 4); + } return linesize;
Can you elaborate why this is needed?
$ ./ffmpeg -i tests/lena.pnm -s 257x257 -pix_fmt pal8 -vcodec rawvideo out.avi $ ./ffplay out.avi
Please give me a day or two to have a look at it. -- FFmpeg = Free and Frightening Mega Philosofic Educated God
On date Thursday 2012-06-14 06:42:04 +0000, Carl Eugen Hoyos encoded:
Stefano Sabatini <stefasab <at> gmail.com> writes:
+ else if (desc->flags & PIX_FMT_PAL) { + // 2 would be sufficient for QuickTime, + // but 4 works and is what avi and mov samples use + linesize = FFALIGN(linesize, 4); + } return linesize;
Can you elaborate why this is needed?
$ ./ffmpeg -i tests/lena.pnm -s 257x257 -pix_fmt pal8 -vcodec rawvideo out.avi $ ./ffplay out.avi
Note that the alignment is defined by av_image_alloc(), and in general I don't see why a paletted image should need an aligned size, so I'd prefer to avoid the special casing, or in other words this seems more a libavcodec issue (and should be addressed there).
Please elaborate.
Elaborating more... The rawvideo decoder uses avpicture_fill() for "filling" a picture given a buffer, then it makes some assumptions on the alignment of the linesize based on the pixel format (libavcodec/rawvideo.c:raw_decode()). So it uses avpicture_fill() which calls av_image_fill_linesizes(), which assumes no alignment, and then fixes linesize[0] according to the pixel format. At the same time the rawvideo encoder (libavcodec/rawenc.c:raw_encode()) is calling avpicture_layout() which in turn calls av_image_fill_linesizes() which again assumes no alignment. So the inconsistency is not in av_image_fill_linesizes(), but in the raw encoder. So we have this information path: raw_encode() avpicture_layout() (picture -> packet) av_image_fill_linesizes() assumes no alignment ... raw_decode() avpicture_fill() (packet -> picture) av_image_fill_linesizes() fixes alignment based on pixel format Changing the alignment behavior of av_image_fill_linesizes() doesn't seem the right thing to do, indeed you're restricting the flexibility of the function, the usual way to do that is to fix alignment *after* the function was called (alternatively: we could extend the function to take an additional align argument). So the problem should be fixed at the libavcodec level. Unfortunately there is no easy way, because you have no way to tell avpicture layout which alignment to assume (depending on the pixel format). Possible solutions: * add the alignment code in avpicture_layout(): seems wrong, as avpicture_layout() is supposed to be more general (and changing its behavior may break existing code) * extend avpicture_layout() and avpicture_fill() to support an align parameter. This sounds more correct to me, and should also allow to choose more descriptive names for the two functions (I was always befuddled by those two) and maybe move them to lavu. -- FFmpeg = Fiendish & Frenzy Mystic Pure Enlightening God
Stefano Sabatini <stefasab <at> gmail.com> writes:
$ ./ffmpeg -i tests/lena.pnm -s 257x257 -pix_fmt pal8 -vcodec rawvideo out.avi $ ./ffplay out.avi
Elaborating more...
[...] Afaict what you write is correct. All I can add is that: Afaict, the decoder behaves correctly (matching WMP and QT). The encoder behaves differently and I don't think avpicture_layout() is correct for PAL8 and any use case. I am probably unable to suggest another solution. Carl Eugen
On date Sunday 2012-06-17 21:24:23 +0000, Carl Eugen Hoyos encoded:
Stefano Sabatini <stefasab <at> gmail.com> writes:
$ ./ffmpeg -i tests/lena.pnm -s 257x257 -pix_fmt pal8 -vcodec rawvideo out.avi $ ./ffplay out.avi
Elaborating more...
[...]
Afaict what you write is correct.
All I can add is that: Afaict, the decoder behaves correctly (matching WMP and QT). The encoder behaves differently and I don't think avpicture_layout() is correct for PAL8 and any use case.
Check attached patch, it should fix the problem when coupled with the new imgutils functions. Note that I don't think rawenc+rawdec can still work, consider the cases in rawdec.c/raw_decode: //2bpp and 4bpp raw in avi and mov (yes this is ugly ...) ... they are not covered at all in rawenc. Also we could rename the array ff_force_align_pix_fmts to ff_force_align4_pix_fmts or even better create a function which maps rawvideo pixel format -> alignment. -- FFmpeg = Fiendish and Fancy Minimalistic Philosophical Ephemeral Generator
Stefano Sabatini <stefasab <at> gmail.com> writes:
Note that I don't think rawenc+rawdec can still work, consider the cases in rawdec.c/raw_decode: //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
PIX_FMT_PAL8 is always 8bpp - rawenc does not support encoding less. Or do I misunderstand? Carl Eugen
Stefano Sabatini <stefasab <at> gmail.com> writes:
Check attached patch, it should fix the problem when coupled with the new imgutils functions.
Could you point me to those functions? Carl Eugen
On date Tuesday 2012-06-19 19:01:28 +0000, Carl Eugen Hoyos encoded:
Stefano Sabatini <stefasab <at> gmail.com> writes:
Check attached patch, it should fix the problem when coupled with the new imgutils functions.
Could you point me to those functions?
http://thread.gmane.org/gmane.comp.video.ffmpeg.devel/146308 -- FFmpeg = Fantastic Forgiving Magical Puristic Enlightened Gymnast
Stefano Sabatini <stefasab <at> gmail.com> writes:
Check attached patch, it should fix the problem when coupled with the new imgutils functions.
The patch breaks decoding some of the samples from ticket #823. Carl Eugen
On date Wednesday 2012-06-20 07:33:04 +0000, Carl Eugen Hoyos encoded:
Stefano Sabatini <stefasab <at> gmail.com> writes:
Check attached patch, it should fix the problem when coupled with the new imgutils functions.
The patch breaks decoding some of the samples from ticket #823.
Can you explain the following code from raw_decode()? //2bpp and 4bpp raw in avi and mov (yes this is ugly ...) if (context->buffer) { int i; uint8_t *dst = context->buffer; buf_size = context->length - 256*4; if (avctx->bits_per_coded_sample == 4){ for(i=0; 2*i+1 < buf_size && i<avpkt->size; i++){ dst[2*i+0]= buf[i]>>4; dst[2*i+1]= buf[i]&15; } linesize_align = 8; } else { for(i=0; 4*i+3 < buf_size && i<avpkt->size; i++){ dst[4*i+0]= buf[i]>>6; dst[4*i+1]= buf[i]>>4&3; dst[4*i+2]= buf[i]>>2&3; dst[4*i+3]= buf[i] &3; } linesize_align = 16; } buf= dst; } -- FFmpeg = Fanciful & Fabulous Majestic Peaceless Ephemeral Goblin
On date Monday 2012-06-25 17:24:11 +0200, Stefano Sabatini encoded:
On date Wednesday 2012-06-20 07:33:04 +0000, Carl Eugen Hoyos encoded:
Stefano Sabatini <stefasab <at> gmail.com> writes:
Check attached patch, it should fix the problem when coupled with the new imgutils functions.
The patch breaks decoding some of the samples from ticket #823.
Can you explain the following code from raw_decode()?
//2bpp and 4bpp raw in avi and mov (yes this is ugly ...) if (context->buffer) { int i; uint8_t *dst = context->buffer; buf_size = context->length - 256*4; if (avctx->bits_per_coded_sample == 4){ for(i=0; 2*i+1 < buf_size && i<avpkt->size; i++){ dst[2*i+0]= buf[i]>>4; dst[2*i+1]= buf[i]&15; } linesize_align = 8; } else { for(i=0; 4*i+3 < buf_size && i<avpkt->size; i++){ dst[4*i+0]= buf[i]>>6; dst[4*i+1]= buf[i]>>4&3; dst[4*i+2]= buf[i]>>2&3; dst[4*i+3]= buf[i] &3; } linesize_align = 16;
shouldn't we use a specific pixel format for this (PIX_FMT_PALETTE4/2)? -- FFmpeg = Foolish & Freak Marvellous Powered Elitarian Game
Stefano Sabatini <stefasab <at> gmail.com> writes:
Can you explain the following code from raw_decode()?
It allows decoding 2bpp and 4bpp rawvideo. But I suspect that wasn't your question... [...]
shouldn't we use a specific pixel format for this (PIX_FMT_PALETTE4/2)?
I was under the impression (nearly) everybody agrees we already have too many pix_fmts... And I believe supporting decoding of completely obsolete formats is sufficient (but encoding samples that cannot be decoded is a problem). Carl Eugen
On date Wednesday 2012-06-20 07:33:04 +0000, Carl Eugen Hoyos encoded:
Stefano Sabatini <stefasab <at> gmail.com> writes:
Check attached patch, it should fix the problem when coupled with the new imgutils functions.
The patch breaks decoding some of the samples from ticket #823.
New try (I still don't like very much this approach), but at least I realized that changing the pal8 output alignment is just wrong. And we could use a better heuristic for detecting the linesize (for example iterating the loop: aligned_size(A) <= buf_size && aligned_size(A+1) >= buf_size => choose A and may be also useful to be able to override the alignment in case of need (working on a patch for that). But apart from this I see more failures with other formats: ffmpeg -i tests/lena.pnm -s 257x257 -vcodec rawvideo -pix_fmt PIX_FMT -y out.avi in particular with formats monob, rgb565, rgb24, still investigating (hints are welcome). -- FFmpeg = Frenzy & Fascinating Monstrous Prodigious Elastic Ghost
On Sat, Jun 30, 2012 at 11:41:39PM +0200, Stefano Sabatini wrote:
On date Wednesday 2012-06-20 07:33:04 +0000, Carl Eugen Hoyos encoded:
Stefano Sabatini <stefasab <at> gmail.com> writes:
Check attached patch, it should fix the problem when coupled with the new imgutils functions.
The patch breaks decoding some of the samples from ticket #823.
New try (I still don't like very much this approach), but at least I realized that changing the pal8 output alignment is just wrong. And we could use a better heuristic for detecting the linesize (for example iterating the loop: aligned_size(A) <= buf_size && aligned_size(A+1) >= buf_size => choose A
and may be also useful to be able to override the alignment in case of need (working on a patch for that).
But apart from this I see more failures with other formats: ffmpeg -i tests/lena.pnm -s 257x257 -vcodec rawvideo -pix_fmt PIX_FMT -y out.avi
in particular with formats monob, rgb565, rgb24, still investigating (hints are welcome). -- FFmpeg = Frenzy & Fascinating Monstrous Prodigious Elastic Ghost
rawdec.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) c02246ed0069dbe581c989237cc382e919c87a20 0004-lavc-rawdec-fix-palette-alignment-setting.patch From dc6fd4caa5b7ee4925845785fab67a697f24aeb0 Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Sat, 30 Jun 2012 19:50:48 +0200 Subject: [PATCH] lavc/rawdec: fix palette alignment setting
--- libavcodec/rawdec.c | 23 +++++++++++++++-------- 1 files changed, 15 insertions(+), 8 deletions(-)
diff --git a/libavcodec/rawdec.c b/libavcodec/rawdec.c index a0211b6..91135fc 100644 --- a/libavcodec/rawdec.c +++ b/libavcodec/rawdec.c @@ -217,15 +217,22 @@ static int raw_decode(AVCodecContext *avctx, frame->palette_has_changed = 1; } } - if((avctx->pix_fmt==PIX_FMT_BGR24 || - avctx->pix_fmt==PIX_FMT_GRAY8 || - avctx->pix_fmt==PIX_FMT_RGB555LE || - avctx->pix_fmt==PIX_FMT_RGB555BE || - avctx->pix_fmt==PIX_FMT_RGB565LE || + if (avctx->pix_fmt==PIX_FMT_BGR24 || + avctx->pix_fmt==PIX_FMT_GRAY8 || + avctx->pix_fmt==PIX_FMT_RGB555LE || + avctx->pix_fmt==PIX_FMT_RGB555BE || + avctx->pix_fmt==PIX_FMT_RGB565LE || avctx->pix_fmt==PIX_FMT_MONOWHITE || - avctx->pix_fmt==PIX_FMT_PAL8) && - FFALIGN(frame->linesize[0], linesize_align)*avctx->height <= buf_size) - frame->linesize[0] = FFALIGN(frame->linesize[0], linesize_align); + avctx->pix_fmt==PIX_FMT_PAL8) { + int aligned_plane_size = FFALIGN(frame->linesize[0], linesize_align)*avctx->height; + int plane_buf_size = avctx->pix_fmt == PIX_FMT_PAL8 && !context->buffer ? + buf_size - AVPALETTE_SIZE : buf_size; + if (aligned_plane_size < plane_buf_size) { ^^^ + av_log(avctx, AV_LOG_DEBUG, "aligned plane size %d <= plane buffer size %d, assuming alignment of %d\n", ^^^^
mismatch otherwise if it works it should be ok [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Frequently ignored awnser#1 FFmpeg bugs should be sent to our bugtracker. User questions about the command line tools should be sent to the ffmpeg-user ML. And questions about how to use libav* should be sent to the libav-user ML.
On Tue, Jul 03, 2012 at 05:57:45PM +0200, Michael Niedermayer wrote:
On Sat, Jun 30, 2012 at 11:41:39PM +0200, Stefano Sabatini wrote:
On date Wednesday 2012-06-20 07:33:04 +0000, Carl Eugen Hoyos encoded:
Stefano Sabatini <stefasab <at> gmail.com> writes:
Check attached patch, it should fix the problem when coupled with the new imgutils functions.
The patch breaks decoding some of the samples from ticket #823.
New try (I still don't like very much this approach), but at least I realized that changing the pal8 output alignment is just wrong. And we could use a better heuristic for detecting the linesize (for example iterating the loop: aligned_size(A) <= buf_size && aligned_size(A+1) >= buf_size => choose A
and may be also useful to be able to override the alignment in case of need (working on a patch for that).
But apart from this I see more failures with other formats: ffmpeg -i tests/lena.pnm -s 257x257 -vcodec rawvideo -pix_fmt PIX_FMT -y out.avi
in particular with formats monob, rgb565, rgb24, still investigating (hints are welcome). -- FFmpeg = Frenzy & Fascinating Monstrous Prodigious Elastic Ghost
rawdec.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) c02246ed0069dbe581c989237cc382e919c87a20 0004-lavc-rawdec-fix-palette-alignment-setting.patch From dc6fd4caa5b7ee4925845785fab67a697f24aeb0 Mon Sep 17 00:00:00 2001 From: Stefano Sabatini <stefasab@gmail.com> Date: Sat, 30 Jun 2012 19:50:48 +0200 Subject: [PATCH] lavc/rawdec: fix palette alignment setting
--- libavcodec/rawdec.c | 23 +++++++++++++++-------- 1 files changed, 15 insertions(+), 8 deletions(-)
diff --git a/libavcodec/rawdec.c b/libavcodec/rawdec.c index a0211b6..91135fc 100644 --- a/libavcodec/rawdec.c +++ b/libavcodec/rawdec.c @@ -217,15 +217,22 @@ static int raw_decode(AVCodecContext *avctx, frame->palette_has_changed = 1; } } - if((avctx->pix_fmt==PIX_FMT_BGR24 || - avctx->pix_fmt==PIX_FMT_GRAY8 || - avctx->pix_fmt==PIX_FMT_RGB555LE || - avctx->pix_fmt==PIX_FMT_RGB555BE || - avctx->pix_fmt==PIX_FMT_RGB565LE || + if (avctx->pix_fmt==PIX_FMT_BGR24 || + avctx->pix_fmt==PIX_FMT_GRAY8 || + avctx->pix_fmt==PIX_FMT_RGB555LE || + avctx->pix_fmt==PIX_FMT_RGB555BE || + avctx->pix_fmt==PIX_FMT_RGB565LE || avctx->pix_fmt==PIX_FMT_MONOWHITE || - avctx->pix_fmt==PIX_FMT_PAL8) && - FFALIGN(frame->linesize[0], linesize_align)*avctx->height <= buf_size) - frame->linesize[0] = FFALIGN(frame->linesize[0], linesize_align); + avctx->pix_fmt==PIX_FMT_PAL8) { + int aligned_plane_size = FFALIGN(frame->linesize[0], linesize_align)*avctx->height; + int plane_buf_size = avctx->pix_fmt == PIX_FMT_PAL8 && !context->buffer ? + buf_size - AVPALETTE_SIZE : buf_size; + if (aligned_plane_size < plane_buf_size) { ^^^ + av_log(avctx, AV_LOG_DEBUG, "aligned plane size %d <= plane buffer size %d, assuming alignment of %d\n", ^^^^
mismatch
otherwise if it works it should be ok
ping [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB The greatest way to live with honor in this world is to be what we pretend to be. -- Socrates
On Sun, Jun 17, 2012 at 11:07:15PM +0200, Stefano Sabatini wrote: [...]
* extend avpicture_layout() and avpicture_fill() to support an align parameter. This sounds more correct to me, and should also allow to choose more descriptive names for the two functions (I was always befuddled by those two) and maybe move them to lavu.
agree [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB The worst form of inequality is to try to make unequal things equal. -- Aristotle
On Mon, Jun 18, 2012 at 12:54:10AM +0200, Michael Niedermayer wrote:
On Sun, Jun 17, 2012 at 11:07:15PM +0200, Stefano Sabatini wrote: [...]
* extend avpicture_layout() and avpicture_fill() to support an align parameter. This sounds more correct to me, and should also allow to choose more descriptive names for the two functions (I was always befuddled by those two) and maybe move them to lavu.
agree
whats the status of this ? [...] -- Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB Avoid a single point of failure, be that a person or equipment.
Carl Eugen Hoyos <cehoyos <at> ag.or.at> writes:
FFmpeg currently cannot read raw pal8 files written with ffmpeg if width mod 4 != 0. Attached patch fixes that (and allows QuickTime to read the files if a palette is written).
Ping, the issue is still reproducible and the patch still applies. Please advise if this should be fixed differently. Carl Eugen
participants (3)
-
Carl Eugen Hoyos -
Michael Niedermayer -
Stefano Sabatini