| 1 |
|
| 2 |
/* C++ header */ |
| 3 |
#include <iostream> |
| 4 |
|
| 5 |
/* C header */ |
| 6 |
#include <cstdio> |
| 7 |
#include <cstring> |
| 8 |
#include <cstdlib> |
| 9 |
#include <cstdint> |
| 10 |
#include <cmath> |
| 11 |
|
| 12 |
#include <endian.h> |
| 13 |
|
| 14 |
/* UNIX header */ |
| 15 |
#include <unistd.h> |
| 16 |
|
| 17 |
/* ffmpeg */ |
| 18 |
extern "C" { |
| 19 |
#include <libavutil/cpu.h> |
| 20 |
} |
| 21 |
|
| 22 |
/* AVIFILE header */ |
| 23 |
#include <avifile.h> |
| 24 |
#include <aviplay.h> |
| 25 |
#include <avm_fourcc.h> |
| 26 |
#include <avm_cpuinfo.h> |
| 27 |
#include <avm_except.h> |
| 28 |
#include <avm_creators.h> |
| 29 |
#include <utils.h> |
| 30 |
#include <version.h> |
| 31 |
#include <videodecoder.h> |
| 32 |
#include <audiodecoder.h> |
| 33 |
#include <renderer.h> |
| 34 |
|
| 35 |
/* AUBIO */ |
| 36 |
#include <aubio/aubio.h> |
| 37 |
#include <aubio/fvec.h> |
| 38 |
|
| 39 |
float getFreq(const char *audioBuf, int audioBufSize, int bitsPerSample, int channels, int sampleRate) { |
| 40 |
int i; |
| 41 |
int n=0; |
| 42 |
float freq=0.0; |
| 43 |
fvec_t *input; |
| 44 |
fvec_t *out; |
| 45 |
aubio_pitch_t *pitch1; |
| 46 |
|
| 47 |
freq = nanf(""); |
| 48 |
if (audioBuf == NULL || audioBufSize <= 0) { |
| 49 |
return freq; |
| 50 |
} |
| 51 |
|
| 52 |
if (bitsPerSample == 32) { |
| 53 |
n = audioBufSize / 4; |
| 54 |
n = n / channels; |
| 55 |
pitch1 = new_aubio_pitch ("default", n, n, sampleRate); |
| 56 |
input = new_fvec(n); |
| 57 |
out = new_fvec(1); |
| 58 |
for (i = 0; i<n; i++) { |
| 59 |
uint32_t *p2; |
| 60 |
uint32_t data2; |
| 61 |
int32_t data1 = 0; |
| 62 |
int i1; |
| 63 |
i1 = i*4*channels; |
| 64 |
if (i1 < audioBufSize) { |
| 65 |
p2 = (uint32_t *)&(audioBuf[i1]); |
| 66 |
data2 = *p2; |
| 67 |
data1 = (int32_t) le32toh(data2); |
| 68 |
} |
| 69 |
fvec_set_sample(input, ((float)(data1)) / (((float)(INT32_MAX))+1.0), i); |
| 70 |
} |
| 71 |
aubio_pitch_do(pitch1, input, out); |
| 72 |
freq = fvec_get_sample(out, 0); |
| 73 |
del_fvec (out); |
| 74 |
del_fvec (input); |
| 75 |
del_aubio_pitch (pitch1); |
| 76 |
return freq; |
| 77 |
} else if (bitsPerSample == 16) { |
| 78 |
n = audioBufSize / 2; |
| 79 |
n = n / channels; |
| 80 |
pitch1 = new_aubio_pitch ("default", n, n, sampleRate); |
| 81 |
input = new_fvec(n); |
| 82 |
out = new_fvec(1); |
| 83 |
for (i = 0; i<n; i++) { |
| 84 |
uint16_t *p2; |
| 85 |
uint16_t data2; |
| 86 |
int16_t data1 = 0; |
| 87 |
int i1; |
| 88 |
i1 = i*2*channels; |
| 89 |
if (i1 < audioBufSize) { |
| 90 |
p2 = (uint16_t *)&(audioBuf[i1]); |
| 91 |
data2 = *p2; |
| 92 |
data1 = (int16_t) le16toh(data2); |
| 93 |
} |
| 94 |
fvec_set_sample(input, ((float)(data1)) / (((float)(INT16_MAX))+1.0), i); |
| 95 |
} |
| 96 |
aubio_pitch_do(pitch1, input, out); |
| 97 |
freq = fvec_get_sample(out, 0); |
| 98 |
del_fvec (out); |
| 99 |
del_fvec (input); |
| 100 |
del_aubio_pitch (pitch1); |
| 101 |
aubio_cleanup (); |
| 102 |
return freq; |
| 103 |
} else if (bitsPerSample == 8) { |
| 104 |
n = audioBufSize; |
| 105 |
n = n / channels; |
| 106 |
pitch1 = new_aubio_pitch ("default", n, n, sampleRate); |
| 107 |
input = new_fvec(n); |
| 108 |
out = new_fvec(1); |
| 109 |
for (i = 0; i<n; i++) { |
| 110 |
int8_t *p2; |
| 111 |
int8_t data1 = 0; |
| 112 |
int i1; |
| 113 |
i1 = i*channels; |
| 114 |
if (i1 < audioBufSize) { |
| 115 |
p2 = (int8_t *)&(audioBuf[i1]); |
| 116 |
data1 = *p2; |
| 117 |
} |
| 118 |
fvec_set_sample(input, ((float)(data1)) / (((float)(INT8_MAX))+1.0), i); |
| 119 |
} |
| 120 |
aubio_pitch_do(pitch1, input, out); |
| 121 |
freq = fvec_get_sample(out, 0); |
| 122 |
del_fvec (out); |
| 123 |
del_fvec (input); |
| 124 |
del_aubio_pitch (pitch1); |
| 125 |
aubio_cleanup (); |
| 126 |
return freq; |
| 127 |
} |
| 128 |
return freq; |
| 129 |
} |
| 130 |
|
| 131 |
int main(int argc, char *argv[]) { |
| 132 |
int version; |
| 133 |
char *filename; |
| 134 |
avm::IReadFile *file = NULL; |
| 135 |
avm::IReadStream *videoStream; |
| 136 |
avm::IReadStream *audioStream; |
| 137 |
BITMAPINFOHEADER bh; |
| 138 |
BITMAPINFOHEADER bhy; |
| 139 |
WAVEFORMATEX wave_fmt; |
| 140 |
fourcc_t fcc; |
| 141 |
avm::IVideoDecoder::CAPS caps; |
| 142 |
int width = 0; |
| 143 |
int height = 0; |
| 144 |
char audioBuf[102400]; |
| 145 |
size_t totalAudioSamplesRead = 0; |
| 146 |
size_t totalAudioBytesRead = 0; |
| 147 |
bool audioCheckedFlag = false; |
| 148 |
char *arch; |
| 149 |
|
| 150 |
/* check version */ |
| 151 |
version = GetAvifileVersion(); |
| 152 |
std::cout << "This binary was compiled for Avifile ver. " << AVIFILE_VERSION |
| 153 |
<< ", " |
| 154 |
<< "the library is ver. " << GetAvifileVersion() |
| 155 |
<< std::endl; |
| 156 |
if (version != AVIFILE_VERSION) { |
| 157 |
std::cerr << "Version mismatched. " << " - Aborting." << std::endl; |
| 158 |
return 1; |
| 159 |
} |
| 160 |
|
| 161 |
/* disable neon when architecture is armhf */ |
| 162 |
arch = getenv("DEB_BUILD_ARCH"); |
| 163 |
if (arch && (strcmp(arch, "armhf") == 0)) { |
| 164 |
unsigned flags; |
| 165 |
|
| 166 |
flags = av_get_cpu_flags(); |
| 167 |
std::cout << "Old cpu flags: 0x" << std::hex << flags |
| 168 |
<< std::dec << std::endl; |
| 169 |
if (av_parse_cpu_caps(&flags, "-neon") >= 0) { |
| 170 |
av_force_cpu_flags(flags); |
| 171 |
std::cout << "New cpu flags: 0x" << std::hex << flags |
| 172 |
<< std::dec << std::endl; |
| 173 |
} else { |
| 174 |
std::cout << "failed disable neon, ffmpeg built without support for it?" << std::endl; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/* get filename */ |
| 179 |
if (argc != 2) { |
| 180 |
std::cerr << "No filename provided" << std::endl; |
| 181 |
return 2; |
| 182 |
} |
| 183 |
filename = argv[1]; |
| 184 |
|
| 185 |
/* open file */ |
| 186 |
file = avm::CreateReadFile(filename); |
| 187 |
if (!file) { |
| 188 |
std::cerr << "Open file " << filename << " error" << std::endl; |
| 189 |
return 3; |
| 190 |
} |
| 191 |
|
| 192 |
/* open video stream */ |
| 193 |
videoStream = file->GetStream(0, avm::IStream::Video); |
| 194 |
if (!videoStream) { |
| 195 |
std::cerr << "Cannot get video stream" << std::endl; |
| 196 |
return 4; |
| 197 |
} |
| 198 |
if (videoStream->StartStreaming() != 0) { |
| 199 |
std::cerr << "Cannot decode video stream" << std::endl; |
| 200 |
return 5; |
| 201 |
} |
| 202 |
|
| 203 |
/* open audio stream */ |
| 204 |
audioStream = file->GetStream(0, avm::IStream::Audio); |
| 205 |
if (!audioStream) { |
| 206 |
std::cerr << "Cannot get audio stream" << std::endl; |
| 207 |
return 4; |
| 208 |
} |
| 209 |
if (audioStream->StartStreaming() != 0) { |
| 210 |
std::cerr << "Cannot decode audio stream" << std::endl; |
| 211 |
return 5; |
| 212 |
} |
| 213 |
|
| 214 |
/* get information of video stream */ |
| 215 |
videoStream->GetOutputFormat(&bh, sizeof(bh)); |
| 216 |
std::cout << "bh.biHeight = " << bh.biHeight << ", " |
| 217 |
<< "bh.biWidth = " << bh.biWidth << std::endl; |
| 218 |
if (bh.biHeight < 0) { |
| 219 |
bh.biHeight = -bh.biHeight; |
| 220 |
std::cout << "bh.biHeight < 0, correct the value to " |
| 221 |
<< bh.biHeight << std::endl; |
| 222 |
} |
| 223 |
std::cout << "Movie size: " << bh.biWidth << " x " << bh.biHeight |
| 224 |
<< " [" << ((char *)(&bh.biCompression)) << "]" << std::endl; |
| 225 |
width = bh.biWidth; |
| 226 |
height = bh.biHeight; |
| 227 |
if (width <= 0 || height <= 0) { |
| 228 |
return 6; |
| 229 |
} |
| 230 |
|
| 231 |
/* get information of audio stream */ |
| 232 |
audioStream->GetAudioDecoder()->GetOutputFormat(&wave_fmt); |
| 233 |
std::cout << "audio format: " << wave_fmt.wFormatTag << ", " |
| 234 |
<< "Channels: " << wave_fmt.nChannels << ", " |
| 235 |
<< "Samples/sec: " << wave_fmt.nSamplesPerSec << ", " |
| 236 |
<< "Bits/Sample: " << wave_fmt.wBitsPerSample << ", " |
| 237 |
<< "Bytes/sec: " << wave_fmt.nAvgBytesPerSec |
| 238 |
<< std::endl; |
| 239 |
if (wave_fmt.nChannels <= 0 || wave_fmt.nSamplesPerSec <= 0) { |
| 240 |
return 6; |
| 241 |
} |
| 242 |
|
| 243 |
/* video length */ |
| 244 |
std::cout << "Video Length: " << videoStream->GetLength() << std::endl; |
| 245 |
std::cout << "Video Pos: " << videoStream->GetPos() << std::endl; |
| 246 |
|
| 247 |
if (videoStream->GetLength() <= 0) { |
| 248 |
return 7; |
| 249 |
} |
| 250 |
|
| 251 |
/* audio length */ |
| 252 |
std::cout << "Audio Length: " << audioStream->GetLength() << std::endl; |
| 253 |
std::cout << "Audio Pos: " << audioStream->GetPos() << std::endl; |
| 254 |
|
| 255 |
if (audioStream->GetLength() <= 0) { |
| 256 |
return 7; |
| 257 |
} |
| 258 |
|
| 259 |
/* video format */ |
| 260 |
videoStream->GetVideoFormat(&bhy, sizeof(bhy)); |
| 261 |
caps = videoStream->GetVideoDecoder()->GetCapabilities(); |
| 262 |
std::cout << "Decoder YUV capabilities: 0x" << std::hex << caps |
| 263 |
<< std::dec << std::endl; |
| 264 |
if (caps & avm::IVideoDecoder::CAP_YUY2) { |
| 265 |
fcc = fccYUY2; |
| 266 |
std::cout << "CAPS is CAP_YUY2" << std::endl; |
| 267 |
} else if (caps & avm::IVideoDecoder::CAP_YV12) { |
| 268 |
fcc = fccYV12; |
| 269 |
std::cout << "CAPS is CAP_YV12" << std::endl; |
| 270 |
} else if (caps & avm::IVideoDecoder::CAP_UYVY) { |
| 271 |
fcc = fccUYVY; |
| 272 |
std::cout << "CAPS is CAP_UYVY" << std::endl; |
| 273 |
} else { |
| 274 |
std::cerr << "YUV format unsupported by decoder" << std::endl; |
| 275 |
return 8; |
| 276 |
} |
| 277 |
|
| 278 |
if (fcc) { |
| 279 |
if (videoStream->GetVideoDecoder()->SetDestFmt(0, fcc)) { |
| 280 |
std::cerr << "Error setting YUV decoder output" << std::endl; |
| 281 |
return 9; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
/* decoding video/audio */ |
| 286 |
videoStream->SetBuffering(4, 0); |
| 287 |
while (!videoStream->Eof() || !audioStream->Eof()) { |
| 288 |
avm::CImage* im; |
| 289 |
uint8_t *pixelData; |
| 290 |
|
| 291 |
std::cout << "TIME " << videoStream->GetTime() << " " << videoStream->GetPos() << std::endl; |
| 292 |
|
| 293 |
if (!videoStream->Eof()) { |
| 294 |
videoStream->ReadFrame(true); |
| 295 |
im = videoStream->GetFrame(); |
| 296 |
} else { |
| 297 |
im = NULL; |
| 298 |
} |
| 299 |
if (im) { |
| 300 |
if (im->Width() != width || im->Height() != height) { |
| 301 |
std::cerr << "IMAGE " << im->Width() << " x " << im->Height() << std::endl; |
| 302 |
return 10; |
| 303 |
} |
| 304 |
|
| 305 |
avm::BitmapInfo bi1(im->Width(), im->Height(), 24); |
| 306 |
bi1.SetRGB(); |
| 307 |
avm::CImage imRGB(im, &bi1); |
| 308 |
|
| 309 |
if (imRGB.Width() != im->Width() || imRGB.Height() != im->Height()) { |
| 310 |
std::cerr << "imRGB size (" << imRGB.Width() |
| 311 |
<< " x " << imRGB.Height() << ")" |
| 312 |
<< " != " << "im size (" << im->Width() |
| 313 |
<< " x " << im->Height() << ")" << std::endl; |
| 314 |
return 11; |
| 315 |
} |
| 316 |
|
| 317 |
if (imRGB.Bpp() != 3) { |
| 318 |
std::cerr << "imRGB.Bpp() = " << imRGB.Bpp() |
| 319 |
<< " != 3" << std::endl; |
| 320 |
return 12; |
| 321 |
} |
| 322 |
|
| 323 |
if (imRGB.Depth() != 24) { |
| 324 |
std::cerr << "imRGB.Depth() = " << imRGB.Depth() |
| 325 |
<< " != 24" << std::endl; |
| 326 |
return 13; |
| 327 |
} |
| 328 |
|
| 329 |
/* Test for frame no. 100 ~ 120 */ |
| 330 |
if (100 <= videoStream->GetPos() && videoStream->GetPos() <= 120) { |
| 331 |
/* check for Red */ |
| 332 |
pixelData = imRGB.At(im->Width()*11/14,im->Height()*1/3); |
| 333 |
if (!(pixelData[0] > 128 && pixelData[1] < 128 && pixelData[2] < 128)) { |
| 334 |
std::cerr << "Red check failed at frame " << videoStream->GetPos() << std::endl; |
| 335 |
return 14; |
| 336 |
} |
| 337 |
/* check for Green */ |
| 338 |
pixelData = imRGB.At(im->Width()*7/14,im->Height()*1/3); |
| 339 |
if (!(pixelData[0] < 128 && pixelData[1] > 128 && pixelData[2] < 128)) { |
| 340 |
std::cerr << "Green check failed at frame " << videoStream->GetPos() << std::endl; |
| 341 |
return 15; |
| 342 |
} |
| 343 |
/* check for Blue */ |
| 344 |
pixelData = imRGB.At(im->Width()*13/14,im->Height()*1/3); |
| 345 |
if (!(pixelData[0] < 128 && pixelData[1] < 128 && pixelData[2] > 128)) { |
| 346 |
std::cerr << "Blue check failed at frame " << videoStream->GetPos() << std::endl; |
| 347 |
return 16; |
| 348 |
} |
| 349 |
std::cout << "RGB check passed" << std::endl; |
| 350 |
} |
| 351 |
im->Release(); |
| 352 |
} else { |
| 353 |
std::cout << "Zero image!" << std::endl; |
| 354 |
} |
| 355 |
while (!audioStream->Eof() && (audioStream->GetTime() <= videoStream->GetTime() || videoStream->Eof()) ) { |
| 356 |
size_t samples_read; |
| 357 |
size_t bytes_read; |
| 358 |
audioStream->ReadFrames(audioBuf, sizeof(audioBuf), 1024, samples_read, bytes_read); |
| 359 |
std::cout << "Audio read " << samples_read << " samples, " |
| 360 |
<< bytes_read << " bytes." << std::endl; |
| 361 |
|
| 362 |
float freq; |
| 363 |
freq = getFreq(audioBuf, bytes_read, wave_fmt.wBitsPerSample, wave_fmt.nChannels, wave_fmt.nSamplesPerSec); |
| 364 |
std::cout << "Audio frequency " << freq << " hz" << std::endl; |
| 365 |
if (350.0 <= freq && freq <= 500.0) { |
| 366 |
audioCheckedFlag = true; |
| 367 |
} |
| 368 |
totalAudioSamplesRead += samples_read; |
| 369 |
totalAudioBytesRead += bytes_read; |
| 370 |
} |
| 371 |
} |
| 372 |
std::cout << "audio: totalAudioSamplesRead = " << totalAudioSamplesRead |
| 373 |
<< std::endl; |
| 374 |
std::cout << "audio: totalAudioBytesRead = " << totalAudioBytesRead |
| 375 |
<< std::endl; |
| 376 |
if (totalAudioSamplesRead != audioStream->GetLength()) { |
| 377 |
std::cerr << "totalAudioSampleRead " << totalAudioSamplesRead |
| 378 |
<< " != " << audioStream->GetLength() << std::endl; |
| 379 |
return 17; |
| 380 |
} |
| 381 |
if (!audioCheckedFlag) { |
| 382 |
std::cerr << "Didn't detect audio track with 440 hz" << std::endl; |
| 383 |
return 18; |
| 384 |
} |
| 385 |
std::cout << "Test Pass" << std::endl; |
| 386 |
aubio_cleanup (); |
| 387 |
return 0; |
| 388 |
|
| 389 |
} |