summaryrefslogtreecommitdiffstats
path: root/lib/rbcodec/metadata/ay.c
blob: 5d00264b3d24bb0778f0241864e617a6e13629a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <inttypes.h>

#include "system.h"
#include "metadata.h"
#include "metadata_common.h"
#include "metadata_parsers.h"
#include "rbunicode.h"

/* Taken from blargg's Game_Music_Emu library */

typedef unsigned char byte;

/* AY file header */
enum { header_size = 0x14 };
struct header_t
{
    byte tag[8];
    byte vers;
    byte player;
    byte unused[2];
    byte author[2];
    byte comment[2];
    byte max_track;
    byte first_track;
    byte track_info[2];
};

struct file_t {
    struct header_t const* header;
    byte const* tracks;
    byte const* end;    /* end of file data */
};

static int get_be16( const void *a )
{
    return get_short_be( (void*) a );
}

/* Given pointer to 2-byte offset of data, returns pointer to data, or NULL if
 * offset is 0 or there is less than min_size bytes of data available. */
static byte const* get_data( struct file_t const* file, byte const ptr [], int min_size )
{
    int offset = (int16_t) get_be16( ptr );
    int pos  = ptr - (byte const*) file->header;
    int size = file->end - (byte const*) file->header;
    int limit = size - min_size;
    if ( limit < 0 || !offset || (unsigned) (pos + offset) > (unsigned) limit )
        return NULL;
    return ptr + offset;
}

static const char *parse_header( byte const in [], int size, struct file_t* out )
{
    if ( size < header_size )
        return "wrong file type";

    out->header = (struct header_t const*) in;
    out->end    = in + size;
    struct header_t const* h = (struct header_t const*) in;
    if ( memcmp( h->tag, "ZXAYEMUL", 8 ) )
        return "wrong file type";

    out->tracks = get_data( out, h->track_info, (h->max_track + 1) * 4 );
    if ( !out->tracks )
        return "missing track data";

    return 0;
}

static void copy_ay_fields( struct file_t const* file, struct mp3entry* id3, int track )
{
    int track_count = file->header->max_track + 1;

    /* calculate track length based on number of subtracks */
    if (track_count > 1) {
        id3->length = file->header->max_track * 1000;
    } else {
        byte const* track_info = get_data( file, file->tracks + track * 4 + 2, 6 );
        if (track_info)
            id3->length = get_be16( track_info + 4 ) * (1000 / 50); /* frames to msec */
        else id3->length = 120 * 1000;
    }
    
    if ( id3->length <= 0 )
        id3->length = 120 * 1000;  /* 2 minutes */ 

    /* If meta info was found in the m3u skip next step */
    if (id3->title && id3->title[0]) return;

    /* If file has more than one track will
        use file name as title */
    char * tmp;
    if (track_count <= 1) {
        tmp = (char *) get_data( file, file->tracks + track * 4, 1 );
        if ( tmp ) id3->title = tmp;
    }

    /* Author */
    tmp = (char *) get_data( file, file->header->author, 1 );
    if (tmp) id3->artist = tmp;
    
    /* Comment */
    tmp = (char *) get_data( file, file->header->comment, 1 );
    if (tmp) id3->comment = tmp;
}

static bool parse_ay_header(int fd, struct mp3entry *id3)
{
    /* Use the trackname part of the id3 structure as a temporary buffer */
    unsigned char* buf = (unsigned char *)id3->id3v2buf;
    struct file_t file;
    int read_bytes;

    lseek(fd, 0, SEEK_SET);
    if ((read_bytes = read(fd, buf, ID3V2_BUF_SIZE)) < header_size)
        return false;

    buf [ID3V2_BUF_SIZE] = '\0';
    if ( parse_header( buf, read_bytes, &file ) ) 
        return false;

    copy_ay_fields( &file, id3, 0 );
    return true;
}

bool get_ay_metadata(int fd, struct mp3entry* id3)
{
    char ay_type[8];
    if ((lseek(fd, 0, SEEK_SET) < 0) ||
         read(fd, ay_type, 8) < 8)
        return false;

    id3->vbr = false;
    id3->filesize = filesize(fd);

    id3->bitrate = 706;
    id3->frequency = 44100;
  
    /* Make sure this is a ZX Ay file */
    if (memcmp( ay_type, "ZXAYEMUL", 8 ) != 0)
        return false;

    return parse_ay_header(fd, id3);
}