History

avformat/cdxl: add support for custom 24bit pal8 formats

Also stop discarding half of audio samples and use planar pcm s8.

Paul B Mahol committed 5 years ago

Commit e8189511 parent e6254d53 files · +37 −22Browse files

Unified · Split

3 files
  1. libavcodec/cdxl.c +20 −9
  2. libavformat/cdxl.c +16 −12
  3. tests/ref/fate/cdxl-demux +1 −1
libavcodec/cdxl.c+20−9
@@ -43,6 +43,7 @@ · 42 unmodified lines
4343 typedef struct CDXLVideoContext {
4444 AVCodecContext *avctx;
4545 int bpp;
46+ int type;
4647 int format;
4748 int padded_bits;
4849 const uint8_t *palette;
@@ -65,14 +66,19 @@ static av_cold int cdxl_decode_init(AVCodecContext *avctx) · 16 unmodified lines
6566
6667 static void import_palette(CDXLVideoContext *c, uint32_t *new_palette)
6768 {
68− int i;
69−
70− for (i = 0; i < c->palette_size / 2; i++) {
71− unsigned rgb = AV_RB16(&c->palette[i * 2]);
72− unsigned r = ((rgb >> 8) & 0xF) * 0x11;
73− unsigned g = ((rgb >> 4) & 0xF) * 0x11;
74− unsigned b = (rgb & 0xF) * 0x11;
75− AV_WN32(&new_palette[i], (0xFFU << 24) | (r << 16) | (g << 8) | b);
69+ if (c->type == 1) {
70+ for (int i = 0; i < c->palette_size / 2; i++) {
71+ unsigned rgb = AV_RB16(&c->palette[i * 2]);
72+ unsigned r = ((rgb >> 8) & 0xF) * 0x11;
73+ unsigned g = ((rgb >> 4) & 0xF) * 0x11;
74+ unsigned b = (rgb & 0xF) * 0x11;
75+ AV_WN32(&new_palette[i], (0xFFU << 24) | (r << 16) | (g << 8) | b);
76+ }
77+ } else {
78+ for (int i = 0; i < c->palette_size / 3; i++) {
79+ unsigned rgb = AV_RB24(&c->palette[i * 3]);
80+ AV_WN32(&new_palette[i], (0xFFU << 24) | rgb);
81+ }
7682 }
7783 }
7884
@@ -246,6 +252,7 @@ static int cdxl_decode_frame(AVCodecContext *avctx, void *data, · 167 unmodified lines
246252
247253 if (buf_size < 32)
248254 return AVERROR_INVALIDDATA;
255+ c->type = buf[0];
249256 encoding = buf[1] & 7;
250257 c->format = buf[1] & 0xE0;
251258 w = AV_RB16(&buf[14]);
@@ -256,7 +263,11 @@ static int cdxl_decode_frame(AVCodecContext *avctx, void *data, · 4 unmodified lines
256263 c->video = c->palette + c->palette_size;
257264 c->video_size = buf_size - c->palette_size - 32;
258265
259− if (c->palette_size > 512)
266+ if (c->type > 1)
267+ return AVERROR_INVALIDDATA;
268+ if (c->type == 1 && c->palette_size > 512)
269+ return AVERROR_INVALIDDATA;
270+ if (c->type == 0 && c->palette_size > 768)
260271 return AVERROR_INVALIDDATA;
261272 if (buf_size < c->palette_size + 32)
262273 return AVERROR_INVALIDDATA;
libavformat/cdxl.c+16−12
@@ -47,16 +47,18 @@ static int cdxl_read_probe(const AVProbeData *p) · 46 unmodified lines
4747 if (p->buf_size < CDXL_HEADER_SIZE)
4848 return 0;
4949
50− /* reserved bytes should always be set to 0 */
51− if (AV_RN64(&p->buf[24]) || AV_RN16(&p->buf[10]))
50+ /* check type */
51+ if (p->buf[0] > 1)
5252 return 0;
5353
54− /* check type */
55− if (p->buf[0] != 1)
54+ /* reserved bytes should always be set to 0 */
55+ if (p->buf[0] == 1 && (AV_RN64(&p->buf[24]) || AV_RN16(&p->buf[10])))
5656 return 0;
5757
5858 /* check palette size */
59− if (AV_RB16(&p->buf[20]) > 512)
59+ if (p->buf[0] == 1 && AV_RB16(&p->buf[20]) > 512)
60+ return 0;
61+ if (p->buf[0] == 0 && AV_RB16(&p->buf[20]) > 768)
6062 return 0;
6163
6264 /* check number of planes */
@@ -111,7 +113,7 @@ static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt) · 48 unmodified lines
111113 uint32_t current_size, video_size, image_size;
112114 uint16_t audio_size, palette_size, width, height;
113115 int64_t pos;
114− int format, frames, ret;
116+ int type, format, frames, ret;
115117
116118 if (avio_feof(pb))
117119 return AVERROR_EOF;
@@ -120,17 +122,18 @@ static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt) · 2 unmodified lines
120122 if (!cdxl->read_chunk &&
121123 avio_read(pb, cdxl->header, CDXL_HEADER_SIZE) != CDXL_HEADER_SIZE)
122124 return AVERROR_EOF;
123− if (cdxl->header[0] != 1) {
124− av_log(s, AV_LOG_ERROR, "non-standard cdxl file\n");
125+ if (cdxl->header[0] > 1) {
126+ av_log(s, AV_LOG_ERROR, "unsupported cdxl file\n");
125127 return AVERROR_INVALIDDATA;
126128 }
127129
130+ type = cdxl->header[0];
128131 format = cdxl->header[1] & 0xE0;
129132 current_size = AV_RB32(&cdxl->header[2]);
130133 width = AV_RB16(&cdxl->header[14]);
131134 height = AV_RB16(&cdxl->header[16]);
132135 palette_size = AV_RB16(&cdxl->header[20]);
133− audio_size = AV_RB16(&cdxl->header[22]);
136+ audio_size = AV_RB16(&cdxl->header[22]) * (1 + !!(cdxl->header[1] & 0x10));
134137 if (cdxl->header[19] == 0 ||
135138 FFALIGN(width, 16) * (uint64_t)height * cdxl->header[19] > INT_MAX)
136139 return AVERROR_INVALIDDATA;
@@ -140,7 +143,8 @@ static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt) · 3 unmodified lines
140143 image_size = FFALIGN(width, 16) * height * cdxl->header[19] / 8;
141144 video_size = palette_size + image_size;
142145
143− if (palette_size > 512)
146+ if ((type == 1 && palette_size > 512) ||
147+ (type == 0 && palette_size > 768))
144148 return AVERROR_INVALIDDATA;
145149 if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE)
146150 return AVERROR_INVALIDDATA;
@@ -153,7 +157,7 @@ static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt) · 6 unmodified lines
153157
154158 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
155159 st->codecpar->codec_tag = 0;
156− st->codecpar->codec_id = AV_CODEC_ID_PCM_S8;
160+ st->codecpar->codec_id = AV_CODEC_ID_PCM_S8_PLANAR;
157161 if (cdxl->header[1] & 0x10) {
158162 st->codecpar->channels = 2;
159163 st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
@@ -189,7 +193,7 @@ static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt) · 29 unmodified lines
189193 if (audio_size + video_size && cdxl->filesize > 0) {
190194 frames = cdxl->filesize / (audio_size + video_size);
191195
192− if(cdxl->framerate)
196+ if (cdxl->framerate)
193197 st->duration = frames;
194198 else
195199 st->duration = frames * (int64_t)audio_size;
tests/ref/fate/cdxl-demux+1−1
@@ -5,7 +5,7 @@ · 4 unmodified lines
55 #sar 0: 0/1
66 #tb 1: 1/11025
77 #media_type 1: audio
8−#codec_id 1: pcm_s8
8+#codec_id 1: pcm_s8_planar
99 #sample_rate 1: 11025
1010 #channel_layout 1: 4
1111 #channel_layout_name 1: mono

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t