t k / Mbed 2 deprecated MovPlayer

Dependencies:   AsciiFont GR-PEACH_video GraphicsFramework LCD_shield_config R_BSP TLV320_RBSP mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MovFile.cpp Source File

MovFile.cpp

00001 #include "MovFile.hpp"
00002 #ifdef __MBED__
00003 #include <cmsis.h>
00004 #else
00005 #include <arpa/inet.h>
00006 #endif
00007 
00008 uint32_t MovFile::frameSizes[];
00009 uint32_t MovFile::audioSizes[];
00010 MovFile MovFile::singleton;
00011 
00012 #define FourConstant(a, b, c, d) ((a) << 24 | (b) << 16 | (c) << 8 | (d))
00013 
00014 #ifndef htonl
00015 #   define htonl(x) __REV(x)
00016 #endif
00017 #ifndef ntohl
00018 #   define ntohl(x) __REV(x)
00019 #endif
00020 
00021 MovFile::MovFile() : frameSizesP(frameSizes), audioSizesP(audioSizes), stszAddress(0), stcoAddress(0), lastFrameAddress(0), availableCount(bufSize)
00022 {
00023 }
00024 
00025 void MovFile::search(uint32_t pattern)
00026 {
00027     Buffer buf;
00028     uint8_t first = pattern & 0xFF;
00029     buf.value = 0;
00030     while (1) {
00031         size_t size = fread(buf.array, sizeof(int8_t), 1, file);
00032         if (size == 0) {
00033             return;
00034         }
00035         if (buf.array[0] == first) {
00036             fread(&buf.array[1], sizeof(int8_t), 3, file);
00037             if (buf.value == pattern) {
00038                 break;
00039             }
00040         }
00041     }
00042 }
00043 
00044 void MovFile::fillCaches()
00045 {
00046     Buffer buf[bufSize];
00047     Buffer *bufP = buf;
00048     uint32_t lastFrame = lastFrameAddress;
00049     
00050     fseek(file, stszAddress, SEEK_SET);
00051     fread(frameSizes, sizeof(uint32_t), bufSize, file);
00052     stszAddress += sizeof(uint32_t) * bufSize;
00053     
00054     fseek(file, stcoAddress, SEEK_SET);
00055     stcoAddress += sizeof(uint32_t) * bufSize;
00056     fread(buf, sizeof(Buffer), bufSize, file);
00057     availableCount = bufSize;
00058     frameSizesP = frameSizes;
00059     audioSizesP = audioSizes;
00060     do {
00061         uint32_t frameAddress = htonl(bufP->value);
00062         *frameSizesP = htonl(*frameSizesP);
00063         *audioSizesP++ = frameAddress - lastFrameAddress - *frameSizesP;
00064         lastFrameAddress = frameAddress;
00065         ++frameSizesP;
00066         ++bufP;
00067     } while (--availableCount);
00068     
00069     fseek(file, lastFrame, SEEK_SET);
00070     availableCount = bufSize;
00071     frameSizesP = frameSizes;
00072     audioSizesP = audioSizes;
00073 }
00074 
00075 void MovFile::start(FILE *f)
00076 {
00077     Buffer buf;
00078     file = f;
00079     frameSizesP = frameSizes;
00080     audioSizesP = audioSizes;
00081     
00082     search(htonl(FourConstant('s', 't', 's', 'z')));
00083     printf("stsz is at 0x%lX\n", ftell(file) - 4);
00084     fseek(file, 8, SEEK_CUR);
00085     fread(&buf, sizeof(Buffer), 1, file);
00086     numOfFrames = htonl(buf.value);
00087     printf("Number of frames: %lu\n", numOfFrames);
00088     stszAddress = ftell(file);
00089     fread(&buf, sizeof(Buffer), 1, file);
00090     
00091     search(htonl(FourConstant('s', 't', 'c', 'o')));
00092     printf("stco is at 0x%lX\n", ftell(file) - 4);
00093     fseek(file, 4, SEEK_CUR);
00094     fread(&buf, sizeof(Buffer), 1, file);
00095     if (numOfFrames != htonl(buf.value)) {
00096         printf("Different number of frames\n");
00097         return;
00098     }
00099     fread(&buf, sizeof(Buffer), 1, file);
00100     stcoAddress = ftell(file);
00101     lastFrameAddress = htonl(buf.value);
00102     printf("First frame is at 0x%lX\n", lastFrameAddress);
00103     
00104     fillCaches();
00105 }
00106 
00107 bool MovFile::read(char *videoBuf, char *audioBuf, uint32_t *audioSize)
00108 {
00109     if (numOfFrames) {
00110         uint32_t aSize = *audioSizesP++;
00111         fread(videoBuf, *frameSizesP++, 1, file);
00112         *audioSize = (uint32_t)fread(audioBuf, 1, aSize, file);
00113         if (--availableCount == 0) {
00114             fillCaches();
00115         }
00116         return numOfFrames--;
00117     }
00118     return false;
00119 }
00120