Skip to content

Commit

Permalink
read ISRCs raw on Linux
Browse files Browse the repository at this point in the history
We read the raw data from the disc,
extract the Q sub-channel and decode the ISRC.

This gives better results (no duplicate ISRCs)
than using the read ISRC sub-channel command.
Probably due to bad implementation on some drives / drivers.
  • Loading branch information
JonnyJD committed Feb 4, 2013
1 parent 9831b34 commit fbf133a
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 3 deletions.
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
7 changes: 5 additions & 2 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 @@ -257,7 +258,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

This comment has been minimized.

Copy link
@JonnyJD

JonnyJD Apr 26, 2013

Author Contributor

I think I might be mistaken here. I found a SCSI-3 MMC draft which states there are 4 ZERO bits (which would be the rest of q_channel[8] and 8 bits AFRAME, which would be q_channel[9].
So q_channel 10-11 ar e the 16 CRC bits. and there is no q_channel[12]. (there are only 12 bytes).

This comment has been minimized.

Copy link
@JonnyJD

JonnyJD Apr 26, 2013

Author Contributor

Fixed with 20b7fcb.

*/

}

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;
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 */

0 comments on commit fbf133a

Please sign in to comment.