Skip to content

Commit 1c0b7fa

Browse files
brechtvllgritz
authored andcommitted
fix(ffmpeg): FFmpeg sets zero oiio:BitsPerSample (#4885)
Many codecs don't provide `bits_per_raw_sample`. The bit depth of the luma channel is the closest equivalent to a single bit depth then, while the chroma channels may be subsampled. Tested on basically all the video files on my computer, they all give a non-zero bit depth that looks correct now. Signed-off-by: Brecht Van Lommel <[email protected]>
1 parent 02b1b0b commit 1c0b7fa

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

src/ffmpeg.imageio/ffmpeginput.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern "C" { // ffmpeg is a C api
3030
#endif
3131

3232
#include <libavutil/imgutils.h>
33+
#include <libavutil/pixdesc.h>
3334
}
3435

3536

@@ -528,8 +529,19 @@ FFmpegInput::open(const std::string& name, ImageSpec& spec)
528529
m_spec.attribute("FramesPerSecond", TypeRational, &rat);
529530
m_spec.attribute("oiio:Movie", true);
530531
m_spec.attribute("oiio:subimages", int(m_frames));
531-
m_spec.attribute("oiio:BitsPerSample",
532-
m_codec_context->bits_per_raw_sample);
532+
if (m_codec_context->bits_per_raw_sample) {
533+
m_spec.attribute("oiio:BitsPerSample",
534+
m_codec_context->bits_per_raw_sample);
535+
} else {
536+
// If bits_per_raw_sample is not provided, the bit depth of the
537+
// luma channel is the closest equivalent to a single bit depth.
538+
const AVPixFmtDescriptor* pix_format_desc = av_pix_fmt_desc_get(
539+
src_pix_format);
540+
if (pix_format_desc && pix_format_desc->nb_components > 0) {
541+
m_spec.attribute("oiio:BitsPerSample",
542+
pix_format_desc->comp[0].depth);
543+
}
544+
}
533545
m_spec.attribute("ffmpeg:codec_name", m_codec_context->codec->long_name);
534546
/* The ffmpeg enums are documented to match CICP values, except the color range. */
535547
const int cicp[4]
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ref/vp9_display_p3.mkv : 192 x 108, 3 channel, uint8 FFmpeg movie
1010
FramesPerSecond: 24/1 (24)
1111
SCENE: "Scene"
1212
ffmpeg:codec_name: "Google VP9"
13-
oiio:BitsPerSample: 0
13+
oiio:BitsPerSample: 8
1414
oiio:Movie: 1
1515
oiio:subimages: 7
1616
subimage 1: 192 x 108, 3 channel, uint8 FFmpeg movie
@@ -22,7 +22,7 @@ ref/vp9_display_p3.mkv : 192 x 108, 3 channel, uint8 FFmpeg movie
2222
FramesPerSecond: 24/1 (24)
2323
SCENE: "Scene"
2424
ffmpeg:codec_name: "Google VP9"
25-
oiio:BitsPerSample: 0
25+
oiio:BitsPerSample: 8
2626
oiio:Movie: 1
2727
oiio:subimages: 7
2828
subimage 2: 192 x 108, 3 channel, uint8 FFmpeg movie
@@ -34,7 +34,7 @@ ref/vp9_display_p3.mkv : 192 x 108, 3 channel, uint8 FFmpeg movie
3434
FramesPerSecond: 24/1 (24)
3535
SCENE: "Scene"
3636
ffmpeg:codec_name: "Google VP9"
37-
oiio:BitsPerSample: 0
37+
oiio:BitsPerSample: 8
3838
oiio:Movie: 1
3939
oiio:subimages: 7
4040
subimage 3: 192 x 108, 3 channel, uint8 FFmpeg movie
@@ -46,7 +46,7 @@ ref/vp9_display_p3.mkv : 192 x 108, 3 channel, uint8 FFmpeg movie
4646
FramesPerSecond: 24/1 (24)
4747
SCENE: "Scene"
4848
ffmpeg:codec_name: "Google VP9"
49-
oiio:BitsPerSample: 0
49+
oiio:BitsPerSample: 8
5050
oiio:Movie: 1
5151
oiio:subimages: 7
5252
subimage 4: 192 x 108, 3 channel, uint8 FFmpeg movie
@@ -58,7 +58,7 @@ ref/vp9_display_p3.mkv : 192 x 108, 3 channel, uint8 FFmpeg movie
5858
FramesPerSecond: 24/1 (24)
5959
SCENE: "Scene"
6060
ffmpeg:codec_name: "Google VP9"
61-
oiio:BitsPerSample: 0
61+
oiio:BitsPerSample: 8
6262
oiio:Movie: 1
6363
oiio:subimages: 7
6464
subimage 5: 192 x 108, 3 channel, uint8 FFmpeg movie
@@ -70,7 +70,7 @@ ref/vp9_display_p3.mkv : 192 x 108, 3 channel, uint8 FFmpeg movie
7070
FramesPerSecond: 24/1 (24)
7171
SCENE: "Scene"
7272
ffmpeg:codec_name: "Google VP9"
73-
oiio:BitsPerSample: 0
73+
oiio:BitsPerSample: 8
7474
oiio:Movie: 1
7575
oiio:subimages: 7
7676
subimage 6: 192 x 108, 3 channel, uint8 FFmpeg movie
@@ -82,6 +82,6 @@ ref/vp9_display_p3.mkv : 192 x 108, 3 channel, uint8 FFmpeg movie
8282
FramesPerSecond: 24/1 (24)
8383
SCENE: "Scene"
8484
ffmpeg:codec_name: "Google VP9"
85-
oiio:BitsPerSample: 0
85+
oiio:BitsPerSample: 8
8686
oiio:Movie: 1
8787
oiio:subimages: 7

testsuite/ffmpeg/ref/out-ffmpeg8.0.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ref/vp9_display_p3.mkv : 192 x 108, 3 channel, uint8 FFmpeg movie
1010
FramesPerSecond: 24/1 (24)
1111
SCENE: "Scene"
1212
ffmpeg:codec_name: "Google VP9"
13-
oiio:BitsPerSample: 0
13+
oiio:BitsPerSample: 8
1414
oiio:Movie: 1
1515
oiio:subimages: 7
1616
subimage 1: 192 x 108, 3 channel, uint8 FFmpeg movie
@@ -22,7 +22,7 @@ ref/vp9_display_p3.mkv : 192 x 108, 3 channel, uint8 FFmpeg movie
2222
FramesPerSecond: 24/1 (24)
2323
SCENE: "Scene"
2424
ffmpeg:codec_name: "Google VP9"
25-
oiio:BitsPerSample: 0
25+
oiio:BitsPerSample: 8
2626
oiio:Movie: 1
2727
oiio:subimages: 7
2828
subimage 2: 192 x 108, 3 channel, uint8 FFmpeg movie
@@ -34,7 +34,7 @@ ref/vp9_display_p3.mkv : 192 x 108, 3 channel, uint8 FFmpeg movie
3434
FramesPerSecond: 24/1 (24)
3535
SCENE: "Scene"
3636
ffmpeg:codec_name: "Google VP9"
37-
oiio:BitsPerSample: 0
37+
oiio:BitsPerSample: 8
3838
oiio:Movie: 1
3939
oiio:subimages: 7
4040
subimage 3: 192 x 108, 3 channel, uint8 FFmpeg movie
@@ -46,7 +46,7 @@ ref/vp9_display_p3.mkv : 192 x 108, 3 channel, uint8 FFmpeg movie
4646
FramesPerSecond: 24/1 (24)
4747
SCENE: "Scene"
4848
ffmpeg:codec_name: "Google VP9"
49-
oiio:BitsPerSample: 0
49+
oiio:BitsPerSample: 8
5050
oiio:Movie: 1
5151
oiio:subimages: 7
5252
subimage 4: 192 x 108, 3 channel, uint8 FFmpeg movie
@@ -58,7 +58,7 @@ ref/vp9_display_p3.mkv : 192 x 108, 3 channel, uint8 FFmpeg movie
5858
FramesPerSecond: 24/1 (24)
5959
SCENE: "Scene"
6060
ffmpeg:codec_name: "Google VP9"
61-
oiio:BitsPerSample: 0
61+
oiio:BitsPerSample: 8
6262
oiio:Movie: 1
6363
oiio:subimages: 7
6464
subimage 5: 192 x 108, 3 channel, uint8 FFmpeg movie
@@ -70,7 +70,7 @@ ref/vp9_display_p3.mkv : 192 x 108, 3 channel, uint8 FFmpeg movie
7070
FramesPerSecond: 24/1 (24)
7171
SCENE: "Scene"
7272
ffmpeg:codec_name: "Google VP9"
73-
oiio:BitsPerSample: 0
73+
oiio:BitsPerSample: 8
7474
oiio:Movie: 1
7575
oiio:subimages: 7
7676
subimage 6: 192 x 108, 3 channel, uint8 FFmpeg movie
@@ -82,6 +82,6 @@ ref/vp9_display_p3.mkv : 192 x 108, 3 channel, uint8 FFmpeg movie
8282
FramesPerSecond: 24/1 (24)
8383
SCENE: "Scene"
8484
ffmpeg:codec_name: "Google VP9"
85-
oiio:BitsPerSample: 0
85+
oiio:BitsPerSample: 8
8686
oiio:Movie: 1
8787
oiio:subimages: 7

0 commit comments

Comments
 (0)