Debian Codegraph @

src/libchdr_codec_cdfl.c

raw symbols SCIP
1 #include "codec_cdfl.h"
2
3 #include <stddef.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include "../include/libchdr/cdrom.h"
8
9 static uint32_t cdfl_codec_blocksize(uint32_t bytes)
10 {
11 /* for CDs it seems that CD_MAX_SECTOR_DATA is the right target */
12 uint32_t blocksize = bytes / 4;
13 while (blocksize > CD_MAX_SECTOR_DATA)
14 blocksize /= 2;
15 return blocksize;
16 }
17
18 chd_error cdfl_codec_init(void *codec, uint32_t hunkbytes)
19 {
20 #if WANT_SUBCODE
21 chd_error ret;
22 #endif
23 cdfl_codec_data *cdfl = (cdfl_codec_data*)codec;
24
25 /* make sure the CHD's hunk size is an even multiple of the frame size */
26 if (hunkbytes % CD_FRAME_SIZE != 0)
27 return CHDERR_CODEC_ERROR;
28
29 cdfl->buffer = (uint8_t*)malloc(sizeof(uint8_t) * hunkbytes);
30 if (cdfl->buffer == NULL)
31 return CHDERR_OUT_OF_MEMORY;
32
33 /* determine whether we want native or swapped samples */
34 cdfl->swap_endian = flac_decoder_detect_native_endian();
35
36 #if WANT_SUBCODE
37 /* init zlib inflater */
38 ret = zlib_codec_init(&cdfl->subcode_decompressor, (hunkbytes / CD_FRAME_SIZE) * CD_MAX_SECTOR_DATA);
39 if (ret != CHDERR_NONE)
40 return ret;
41 #endif
42
43 /* flac decoder init */
44 if (flac_decoder_init(&cdfl->decoder))
45 return CHDERR_OUT_OF_MEMORY;
46
47 return CHDERR_NONE;
48 }
49
50 void cdfl_codec_free(void *codec)
51 {
52 cdfl_codec_data *cdfl = (cdfl_codec_data*)codec;
53 flac_decoder_free(&cdfl->decoder);
54 #if WANT_SUBCODE
55 zlib_codec_free(&cdfl->subcode_decompressor);
56 #endif
57 if (cdfl->buffer)
58 free(cdfl->buffer);
59 }
60
61 chd_error cdfl_codec_decompress(void *codec, const uint8_t *src, uint32_t complen, uint8_t *dest, uint32_t destlen)
62 {
63 uint32_t framenum;
64 uint8_t *buffer;
65 #if WANT_SUBCODE
66 uint32_t offset;
67 chd_error ret;
68 #endif
69 cdfl_codec_data *cdfl = (cdfl_codec_data*)codec;
70
71 /* reset and decode */
72 uint32_t frames = destlen / CD_FRAME_SIZE;
73
74 if (!flac_decoder_reset(&cdfl->decoder, 44100, 2, cdfl_codec_blocksize(frames * CD_MAX_SECTOR_DATA), src, complen))
75 return CHDERR_DECOMPRESSION_ERROR;
76 buffer = &cdfl->buffer[0];
77 if (!flac_decoder_decode_interleaved(&cdfl->decoder, (int16_t *)(buffer), frames * CD_MAX_SECTOR_DATA/4, cdfl->swap_endian))
78 return CHDERR_DECOMPRESSION_ERROR;
79
80 #if WANT_SUBCODE
81 /* inflate the subcode data */
82 offset = flac_decoder_finish(&cdfl->decoder);
83 ret = zlib_codec_decompress(&cdfl->subcode_decompressor, src + offset, complen - offset, &cdfl->buffer[frames * CD_MAX_SECTOR_DATA], frames * CD_MAX_SUBCODE_DATA);
84 if (ret != CHDERR_NONE)
85 return ret;
86 #else
87 flac_decoder_finish(&cdfl->decoder);
88 #endif
89
90 /* reassemble the data */
91 for (framenum = 0; framenum < frames; framenum++)
92 {
93 memcpy(&dest[framenum * CD_FRAME_SIZE], &cdfl->buffer[framenum * CD_MAX_SECTOR_DATA], CD_MAX_SECTOR_DATA);
94 #if WANT_SUBCODE
95 memcpy(&dest[framenum * CD_FRAME_SIZE + CD_MAX_SECTOR_DATA], &cdfl->buffer[frames * CD_MAX_SECTOR_DATA + framenum * CD_MAX_SUBCODE_DATA], CD_MAX_SUBCODE_DATA);
96 #endif
97 }
98
99 return CHDERR_NONE;
100 }