aboutsummaryrefslogtreecommitdiffstats
path: root/src/inputs/InputMpegFile.cpp
blob: 480a2511d6f33bf31320615482105fbb663b0998 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
   Copyright (C) 2009 Her Majesty the Queen in Right of Canada (Communications
   Research Center Canada)

   Copyright (C) 2013 Matthias P. Braendli
   http://mpb.li

   Base class for all file and fifo-based inputs.

   */
/*
   This file is part of CRC-DabMux.

   CRC-DabMux is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   CRC-DabMux 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with CRC-DabMux.  If not, see <http://www.gnu.org/licenses/>.
   */

#include "inputs/Input.h"

#ifdef HAVE_FORMAT_MPEG
#ifdef HAVE_INPUT_FILE

#include "mpeg.h"
#include "TcpLog.h"

#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#ifndef _WIN32
#   define O_BINARY 0
#endif

extern TcpLog etiLog;

#define MPEG_FREQUENCY      -2
#define MPEG_PADDING        -3
#define MPEG_COPYRIGHT      -4
#define MPEG_ORIGINAL       -5
#define MPEG_EMPHASIS       -6

const char* InputMpegFile::GetName()
{
    return m_name.c_str();
}

int checkDabMpegFrame(void* data)
{
    mpegHeader* header = (mpegHeader*)data;
    unsigned long* headerData = (unsigned long*)data;
    if ((*headerData & 0x0f0ffcff) == 0x0004fcff) return 0;
    if ((*headerData & 0x0f0ffcff) == 0x0004f4ff) return 0;
    if (getMpegFrequency(header) != 48000) {
        if (getMpegFrequency(header) != 24000) {
            return MPEG_FREQUENCY;
        }
    }
    if (header->padding != 0) {
        return MPEG_PADDING;
    }
    if (header->copyright != 0) {
        return MPEG_COPYRIGHT;
    }
    if (header->original != 0) {
        return MPEG_ORIGINAL;
    }
    if (header->emphasis != 0) {
        return MPEG_EMPHASIS;
    }
    return -1;
}

int InputMpegFile::ReadSource(void* buffer, int size)
{
    int result;
    bool rewind = false;
READ_SUBCHANNEL:
    if (this->parity) {
        result = readData(this->file, buffer, size, 2);
        this->parity = false;
        return 0;
    }
    else {
        result = readMpegHeader(this->file, buffer, size);
        if (result > 0) {
            result = readMpegFrame(this->file, buffer, size);
            if (result < 0 && getMpegFrequency(buffer) == 24000) {
                this->parity = true;
                result = size;
            }
        }
    }

    switch (result) {
        case MPEG_BUFFER_UNDERFLOW:
            etiLog.print(TcpLog::WARNING, "data underflow -> frame muted\n");
            goto MUTE_SUBCHANNEL;
        case MPEG_BUFFER_OVERFLOW:
            etiLog.print(TcpLog::WARNING, "bitrate too high -> frame muted\n");
            goto MUTE_SUBCHANNEL;
        case MPEG_FILE_EMPTY:
            if (rewind) {
                etiLog.print(TcpLog::ERR, "file rewinded and still empty "
                        "-> frame muted\n");
                goto MUTE_SUBCHANNEL;
            } else {
                rewind = true;
                etiLog.print(TcpLog::NOTICE, "reach end of file -> rewinding\n");
                lseek(this->file, 0, SEEK_SET);
                goto READ_SUBCHANNEL;
            }
        case MPEG_FILE_ERROR:
            etiLog.print(TcpLog::CRIT, "can't read file (%i) -> frame muted\n", errno);
            perror("");
            goto MUTE_SUBCHANNEL;
        case MPEG_SYNC_NOT_FOUND:
            etiLog.print(TcpLog::CRIT, "mpeg sync not found, maybe is not a valid file "
                    "-> frame muted\n");
            goto MUTE_SUBCHANNEL;
        case MPEG_INVALID_FRAME:
            etiLog.print(TcpLog::CRIT, "file is not a valid mpeg file "
                    "-> frame muted\n");
            goto MUTE_SUBCHANNEL;
        default:
            if (result < 0) {
                etiLog.print(TcpLog::CRIT,
                        "unknown error (code = %i) -> frame muted\n",
                        result);
MUTE_SUBCHANNEL:
                memset(buffer, 0, size);
            }
            else {
                if (result < size) {
                    etiLog.print(TcpLog::WARNING, "bitrate too low from file "
                            "-> frame padded\n");
                    memset((char*)buffer + result, 0, size - result);
                }
                result = checkDabMpegFrame(buffer);
                switch (result) {
                    case MPEG_FREQUENCY:
                        etiLog.print(TcpLog::ERR, "file has a frame with an invalid "
                                "frequency: %i, should be 48000 or 24000\n",
                                getMpegFrequency(buffer));
                        break;
                    case MPEG_PADDING:
                        etiLog.print(TcpLog::WARNING,
                                "file has a frame with padding bit set\n");
                        break;
                    case MPEG_COPYRIGHT:
                        etiLog.print(TcpLog::WARNING,
                                "file has a frame with copyright bit set\n");
                        break;
                    case MPEG_ORIGINAL:
                        etiLog.print(TcpLog::WARNING,
                                "file has a frame with original bit set\n");
                        break;
                    case MPEG_EMPHASIS:
                        etiLog.print(TcpLog::WARNING,
                                "file has a frame with emphasis bits set\n");
                        break;
                    default:
                        if (result < 0) {
                            etiLog.print(TcpLog::CRIT, "mpeg file has an invalid DAB "
                                    "mpeg frame (unknown reason: %i)\n", result);
                        }
                        break;
                }
            }
    }
    return result;
}

#endif
#endif