[FFmpeg-devel] [PATCH] avcodec/mjpeg: don't override color information from demuxer

Steinar H. Gunderson steinar+ffmpeg at gunderson.no
Wed Dec 5 01:31:10 EET 2018


Some demuxers, like Matroska, allow for sending colorspace information
that override MJPEG defaults when it comes to Y'CbCr coefficients or
chroma location. Don't override such data if the demuxer already has
set it; this allows decoding such MJPEG streams correctly.

Also document in avcodec.h that these fields are actually set first by
libavformat, even if libavcodec is usually the one to set them. This is
not new behavior; e.g., dnxhd already works this way.
---
 libavcodec/avcodec.h                      | 10 +++++-----
 libavcodec/mjpegdec.c                     |  8 ++++++--
 tests/fate/matroska.mak                   |  3 +++
 tests/ref/fate/matroska-mjpeg-color-space | 22 ++++++++++++++++++++++
 4 files changed, 36 insertions(+), 7 deletions(-)
 create mode 100644 tests/ref/fate/matroska-mjpeg-color-space

diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 3922e89331..b7ea99d8ab 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -2145,35 +2145,35 @@ typedef struct AVCodecContext {
     /**
      * Chromaticity coordinates of the source primaries.
      * - encoding: Set by user
-     * - decoding: Set by libavcodec
+     * - decoding: Set by libavformat and/or libavcodec
      */
     enum AVColorPrimaries color_primaries;
 
     /**
      * Color Transfer Characteristic.
      * - encoding: Set by user
-     * - decoding: Set by libavcodec
+     * - decoding: Set by libavformat and/or libavcodec
      */
     enum AVColorTransferCharacteristic color_trc;
 
     /**
      * YUV colorspace type.
      * - encoding: Set by user
-     * - decoding: Set by libavcodec
+     * - decoding: Set by libavformat and/or libavcodec
      */
     enum AVColorSpace colorspace;
 
     /**
      * MPEG vs JPEG YUV range.
      * - encoding: Set by user
-     * - decoding: Set by libavcodec
+     * - decoding: Set by libavformat and/or libavcodec
      */
     enum AVColorRange color_range;
 
     /**
      * This defines the location of chroma samples.
      * - encoding: Set by user
-     * - decoding: Set by libavcodec
+     * - decoding: Set by libavformat and/or libavcodec
      */
     enum AVChromaLocation chroma_sample_location;
 
diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index 2f1635838a..9ee2daadf8 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -158,8 +158,12 @@ av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
     s->first_picture = 1;
     s->got_picture   = 0;
     s->org_height    = avctx->coded_height;
-    avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
-    avctx->colorspace = AVCOL_SPC_BT470BG;
+    if (avctx->chroma_sample_location == AVCHROMA_LOC_UNSPECIFIED) {
+        avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
+    }
+    if (avctx->colorspace == AVCOL_SPC_UNSPECIFIED) {
+        avctx->colorspace = AVCOL_SPC_BT470BG;
+    }
     s->hwaccel_pix_fmt = s->hwaccel_sw_pix_fmt = AV_PIX_FMT_NONE;
 
     if ((ret = init_default_huffman_tables(s)) < 0)
diff --git a/tests/fate/matroska.mak b/tests/fate/matroska.mak
index 2747496e1e..1768ce6ee2 100644
--- a/tests/fate/matroska.mak
+++ b/tests/fate/matroska.mak
@@ -9,5 +9,8 @@ fate-matroska-remux: REF = 1ed49a4f2b6790357fac268938357353
 FATE_MATROSKA_FFPROBE-$(call ALLYES, MATROSKA_DEMUXER) += fate-matroska-spherical-mono
 fate-matroska-spherical-mono: CMD = run ffprobe$(PROGSSUF)$(EXESUF) -show_entries stream_side_data_list -select_streams v -v 0 $(TARGET_SAMPLES)/mkv/spherical.mkv
 
+FATE_MATROSKA_FFPROBE-$(call ALLYES, MATROSKA_DEMUXER) += fate-matroska-mjpeg-color-space
+fate-matroska-mjpeg-color-space: CMD = run ffprobe$(PROGSSUF)$(EXESUF) -show_entries frame=color_range,color_space,color_primaries,color_transfer,chroma_location $(TARGET_SAMPLES)/mkv/mjpeg-color-space.mkv
+
 FATE_SAMPLES_AVCONV += $(FATE_MATROSKA-yes)
 FATE_SAMPLES_FFPROBE += $(FATE_MATROSKA_FFPROBE-yes)
diff --git a/tests/ref/fate/matroska-mjpeg-color-space b/tests/ref/fate/matroska-mjpeg-color-space
new file mode 100644
index 0000000000..d3cd39f967
--- /dev/null
+++ b/tests/ref/fate/matroska-mjpeg-color-space
@@ -0,0 +1,22 @@
+[FRAME]
+color_range=tv
+color_space=bt709
+color_primaries=bt709
+color_transfer=iec61966-2-1
+chroma_location=left
+[SIDE_DATA]
+[/SIDE_DATA]
+[SIDE_DATA]
+[/SIDE_DATA]
+[/FRAME]
+[FRAME]
+color_range=tv
+color_space=bt709
+color_primaries=bt709
+color_transfer=iec61966-2-1
+chroma_location=left
+[SIDE_DATA]
+[/SIDE_DATA]
+[SIDE_DATA]
+[/SIDE_DATA]
+[/FRAME]
-- 
2.19.1



More information about the ffmpeg-devel mailing list