Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIB-11: raw (low level) ISRC reads #6

Open
wants to merge 47 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
fbf133a
read ISRCs raw on Linux
JonnyJD Feb 4, 2013
5fbf4d4
remove TODO comment
JonnyJD Feb 7, 2013
c31d46b
Merge branch 'master' into isrc_raw
JonnyJD Mar 10, 2013
f8a82dd
move read_track_isrc to disc_scsi.c
JonnyJD Mar 10, 2013
a1d73d6
rename disc_scsi.c -> scsi.c
JonnyJD Mar 10, 2013
261aac8
move scsi function declarations in scsi header
JonnyJD Mar 10, 2013
d5a059b
add HACKING file
JonnyJD Mar 11, 2013
b1616d3
prepend mb_scsi to scsi functions
JonnyJD Mar 11, 2013
63c6dc8
include info about SCSI standards
JonnyJD Mar 11, 2013
de09b17
linux: stop the device after reading
JonnyJD Mar 11, 2013
1b859ac
Merge branch 'master' into isrc_raw
JonnyJD Apr 15, 2013
1c013f8
add scsi.h to distribution
JonnyJD Apr 15, 2013
20b7fcb
locate CRC in q-channel
JonnyJD Apr 26, 2013
81e062b
Merge branch 'master' into isrc_raw
JonnyJD May 11, 2013
4df857c
Merge branch 'master' into isrc_raw
JonnyJD Jun 20, 2013
0618e64
Merge branch 'master' into isrc_raw
JonnyJD Jun 29, 2013
a470a34
implement CRC check for raw ISRCs
JonnyJD Aug 2, 2013
5dd8ca2
Merge branch 'master' into crc
JonnyJD Aug 2, 2013
867889f
improve comments and names for raw isrcs
JonnyJD Aug 2, 2013
e77dd95
LIB-11: read ISRCs until CRC is valid
JonnyJD Aug 3, 2013
b8d3ed3
free raw disc read data
JonnyJD Aug 3, 2013
d163282
raw isrcs: don't return main channel data
JonnyJD Aug 3, 2013
fd63dc9
LIB-11: read every sector separately
JonnyJD Aug 3, 2013
02b733d
implement scsi_cmd for Windows
JonnyJD Aug 3, 2013
9e5e389
prepare autotools build with windows scsi_cmd
JonnyJD Aug 5, 2013
80d627e
work around windows dllexport issue
JonnyJD Aug 5, 2013
fb400d6
add ntddscsi.h for MSVC
JonnyJD Aug 5, 2013
ecd2d58
msvc: remove new warnings
JonnyJD Aug 5, 2013
f3ba048
Windows: fix scsi_cmd (return, GENERIC_WRITE)
JonnyJD Aug 8, 2013
724df7c
scsi_cmd: fix return code documentation
JonnyJD Aug 8, 2013
bc102db
add more debug output for w32 scsi_cmd
JonnyJD Aug 8, 2013
4a4ebab
check if there are ISRCs at all
JonnyJD Sep 19, 2013
73e1fa6
define SECTORS_ISRC and read 10 more
JonnyJD Sep 19, 2013
ee22ed7
check for data_len, rather than 96
JonnyJD Sep 19, 2013
aed1d9f
create mb_scsi_handle struct
JonnyJD Sep 19, 2013
e34d987
Merge branch 'scsi_handle' into scsi_windows
JonnyJD Sep 19, 2013
e2fc17f
Merge branch 'scsi_windows' into isrc_raw
JonnyJD Sep 19, 2013
2803ca0
return usable error codes from scsi_cmd
JonnyJD Sep 24, 2013
bdd74be
fix warnings on MinGW
JonnyJD Sep 24, 2013
046d1c0
fall back to normal when raw ISRC fails
JonnyJD Sep 24, 2013
cd58b5d
implement scsi feature detection
JonnyJD Sep 24, 2013
7031c1b
rename subchannel feature to isrc
JonnyJD Sep 24, 2013
55d274f
windows: restructure isrc section
JonnyJD Sep 25, 2013
3923e5a
add newline to stderr message
JonnyJD Oct 1, 2013
6533105
Merge branch 'master' into isrc_raw
JonnyJD Oct 3, 2013
90dbfe5
Merge branch 'master' into isrc_raw
JonnyJD Oct 3, 2013
477da28
Merge branch 'master' into isrc_raw
JonnyJD Oct 3, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/libdiscid.pc.in ${CMAKE_CURRENT_BINAR
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile)

IF(CMAKE_SYSTEM_NAME MATCHES "Linux")
SET(libdiscid_OSDEP_SRCS src/disc_linux.c)
SET(libdiscid_OSDEP_SRCS src/disc_linux.c src/disc_scsi.c)
ELSEIF(CMAKE_SYSTEM_NAME MATCHES "Darwin")
SET(libdiscid_OSDEP_SRCS src/disc_darwin.c)
FIND_LIBRARY(COREFOUNDATION_LIBRARY CoreFoundation)
Expand Down
3 changes: 3 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ dnl The file containing the operating system dependent disc access code.
if test "${os}" = "win32"; then
DISC_OS_OBJ="disc_${os}.lo disc_${os}_new.lo"
AC_MSG_NOTICE([using discid implementation disc_${os}.c disc_${os}_new.c])
elif test "${os}" = "linux"; then
DISC_OS_OBJ="disc_${os}.lo disc_scsi.lo"
AC_MSG_NOTICE([using discid implementation disc_${os}.c disc_scsi.c])
else
DISC_OS_OBJ="disc_${os}.lo"
AC_MSG_NOTICE([using discid implementation disc_${os}.c])
Expand Down
12 changes: 5 additions & 7 deletions src/disc_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

/* TODO: make sure it's available */
int snprintf(char *str, size_t size, const char *format, ...);
void read_track_isrc_raw(int fd, mb_disc_private *disc, int track_num);


static int read_toc_header(int fd, int *first, int *last) {
Expand Down Expand Up @@ -134,7 +135,7 @@ static void read_disc_mcn(int fd, mb_disc_private *disc)
}

/* Send a scsi command and receive data. */
static int scsi_cmd(int fd, unsigned char *cmd, int cmd_len,
int scsi_cmd(int fd, unsigned char *cmd, int cmd_len,
const char *data, int data_len) {
int device_fd;
char sense_buffer[SG_MAX_SENSE]; /* for "error situations" */
Expand Down Expand Up @@ -173,11 +174,6 @@ static void read_track_isrc(int fd, mb_disc_private *disc, int track_num) {
memset(data, 0, sizeof data);
memset(buffer, 0, sizeof buffer);

/* data read from the last appropriate sector encountered
* by a current or previous media access operation.
* The Logical Unit accesses the media when there is/was no access.
* TODO: force access at a specific block? -> no duplicate ISRCs?
*/
cmd[0] = 0x42; /* READ SUB-CHANNEL */
/* cmd[1] reserved / MSF bit (unused) */
cmd[2] = 1 << 6; /* 6th bit set (SUBQ) -> get sub-channel data */
Expand Down Expand Up @@ -257,7 +253,9 @@ int mb_disc_read_unportable(mb_disc_private *disc, const char *device) {
disc->track_offsets[i] = lba + 150;

/* Read the ISRC for the track */
read_track_isrc(fd, disc, i);
// TODO: test if raw actually is available
//read_track_isrc(fd, disc, i);
read_track_isrc_raw(fd, disc, i);
}

close(fd);
Expand Down
170 changes: 170 additions & 0 deletions src/disc_scsi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/* --------------------------------------------------------------------------

MusicBrainz -- The Internet music metadatabase

Copyright (C) 2013 Johannes Dewender

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


--------------------------------------------------------------------------- */

#include <stdio.h>
#include <string.h>

#include "discid/discid_private.h"

/* Send a scsi command and receive data. */
int scsi_cmd(int fd, unsigned char *cmd, int cmd_len,
const char *data, int data_len);

static void decode_isrc(unsigned char *q_channel, char *isrc) {
int isrc_pos;
int data_pos;
int bit_pos;
int bit;
unsigned char buffer;

isrc_pos = 0;
/* q_channel[0] = 0x03 -> mode 3 -> ISRC data */
data_pos = 1;
bit_pos = 7;
buffer = 0;
/* first 5 chars of ISRC are alphanumeric and 6-bit BCD encoded */
for (bit = 0; bit < 5*6; bit++) {
buffer = buffer << 1;
if ((q_channel[data_pos] & (1 << bit_pos)) == (1 << bit_pos)) {
buffer++;
}
bit_pos--;
if ((bit + 1)%8 == 0) {
bit_pos = 7;
data_pos++;
}
if ((bit + 1)%6 == 0) {
/* 0x3f = only lowest 6 bits set */
isrc[isrc_pos] = '0' + (buffer & 0x3f);
isrc_pos++;
buffer = 0;
}
}
/* buffer[4] includes 2 zero bits
* last 7 chars of ISRC are only numeric and 4-bit encoded
*/
isrc[5] = '0' + (q_channel[5] >> 4);
isrc[6] = '0' + (q_channel[5] & 0x0f);
isrc[7] = '0' + (q_channel[6] >> 4);
isrc[8] = '0' + (q_channel[6] & 0x0f);
isrc[9] = '0' + (q_channel[7] >> 4);
isrc[10] = '0' + (q_channel[7] & 0x0f);
isrc[11] = '0' + (q_channel[8] >> 4);
/* q_channel[9] are zero bits
* q_channel 10-12 are AFRAME
*/

}

void read_track_isrc_raw(int fd, mb_disc_private *disc, int track_num) {
/* there should be at least one ISRC in 100 sectors per spec
* We try 150 (= 2 seconds) to be sure, but break when successfull
*/
const int SEC_NUM = 150;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make sure that these 150 sectors (= 2 seconds) are part of the track and check less sectors for VERY short tracks.

unsigned char cmd[12];
unsigned char data[SEC_NUM*2448]; /* overall data */
unsigned char q_buffer;
unsigned char q_data[12]; /* sub-channel data in 1 sector */
char isrc[ISRC_STR_LENGTH+1];
unsigned long offset;
int char_num;
int sector;
int i;

memset(cmd, 0, sizeof cmd);
memset(data, 0, sizeof data);
memset(isrc, 0, sizeof isrc);

offset = disc->track_offsets[track_num];

/* 0xbe = READ CD, implementation optional in contrast to 0x42
* support given with GET CONFIGURATION (0x46)
* Support part of:
* Multi-Read Feature (0x001d) and
* CD Read Feature (0x001e),
*/
cmd[0] = 0xbe; /* READ CD */
cmd[2] = offset >> 32;
cmd[3] = offset >> 16;
cmd[4] = offset >> 8;
cmd[5] = offset; /* from where to start reading */
cmd[6] = SEC_NUM >> 16;
cmd[7] = SEC_NUM >> 8;
cmd[8] = SEC_NUM; /* sectors to read */
cmd[9] = 0xF8; /* 11111000 sync=1 all-header=11 user=1, ecc=1
* no-error=00 */
cmd[10] = 0x01; /* Sub-Channel Selection raw P-W=001*/
/* cmd[11] = control byte */

if (scsi_cmd(fd, cmd, sizeof cmd, data, sizeof data) != 0) {
fprintf(stderr, "Warning: Cannot get ISRC code for track %d\n",
track_num);
return;
}

/* each sector has 96 bytes for one Q-channel type
* We try until we finde an ISRC Q-channel
*/
for (sector = 0; sector < SEC_NUM; sector++) {
char_num = 0;
memset(q_data, 0, sizeof q_data);
q_buffer = 0;
/* the first 2352 bytes are raw data without sub-channels
* We only want the 96 bit sub-channel data
*/
for (i = 2352; i < 2448; i++) {
q_buffer = q_buffer << 1;
/* the 6th bit is the Q-channel bit
* we want to collect these bits
*/
if ((data[i + (sector*2448)] & (1 << 6)) == (1 << 6)) {
q_buffer++;
}
if ((i+1)%8 == 0) {
/* we have gathered one complete byte */
q_data[char_num] = q_buffer;
if (char_num == 0) {
/* test if we got the right type
* of q-channel (ADR = 0x03)
* upper 4 bit are CONTROL
* Go to next sector otherwise
*/
if ((q_buffer & 0x0F) != 0x03) {
break;
}
}
char_num++;
q_buffer = 0;
}
}
if ((q_data[0] & 0x0F) == 0x03) {
/* we found a Q-channel with ISRC data */
decode_isrc(q_data, isrc);
break;
}
}
strncpy(disc->isrc[track_num], isrc, ISRC_STR_LENGTH);
}


/* EOF */