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
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: main.cpp
* Author: cmisip
*
* Created on July 22, 2017, 7:10 PM
*/
#include <cstdlib>
#include <cstring>
#include <stdint.h>
#include <iostream>
using namespace std;
/*
*
*/
struct mmal_motion_vector {
char x_vector;
char y_vector;
short sad;
};
struct motion_vector{
uint16_t x_mag;
uint16_t y_mag;
};
int main(int argc, char** argv) {
uint16_t size=20;
uint16_t mv_size=20*4+4;
std::cout << "MV SIZE " << mv_size << std::endl;
std::cout << "ARRAY SIZE " << mv_size/sizeof(mmal_motion_vector) -1 << std::endl;
//1. Artificially construct the source of the data
struct mmal_motion_vector mvarray[size];
//Fill array with values
for (int i=0;i < size ; i++) {
mvarray[i].x_vector=(char)i;
mvarray[i].y_vector=(char)(10+i);
mvarray[i].sad = 7;
std::cout << "COUNTS " << i << " x_vector " << (int)mvarray[i].x_vector << " y_vector " << (int)mvarray[i].y_vector << std::endl;
}
//2. Package the source of data in a uint8_t buffer since that is what project code is using to pass data around.
uint8_t * mvsource=(uint8_t *)mvarray; //This cast just simulates that the source pointer is uint8_t * buffer
//3. WRITE to mvect_buffer
uint8_t * mvect_buffer=(uint8_t*)malloc(mv_size);
uint8_t s_offset = 0; //Start reading source address at byte 0
uint8_t t_offset = 4; //Start writing target at 4th byte, since we will save size and vec_type in first 4 bytes
memset(mvect_buffer,0,mv_size);
for (int i=0;i < size ; i++) {
mmal_motion_vector mv;
memcpy(&mv,mvsource+s_offset,sizeof(mmal_motion_vector));
motion_vector wv;
wv.x_mag = mv.x_vector;
wv.y_mag = mv.y_vector;
std::cout << "COUNTW " << i << " x_vector " << (int)wv.x_mag << " Y_vector " << (int)wv.y_mag << std::endl; //Values displayed
memcpy(mvect_buffer+t_offset, &wv, sizeof(motion_vector));
s_offset+=sizeof(mmal_motion_vector);
t_offset+=sizeof(motion_vector);
}
uint16_t vec_type = 69;
//Save the size and vec_type
memcpy(mvect_buffer,&size,2);
memcpy(mvect_buffer+2,&vec_type,2);
//4.READ
uint16_t rsize2;
uint16_t vtype2;
memcpy(&rsize2, mvect_buffer,2);
memcpy(&vtype2, mvect_buffer+2,2);
uint8_t offset=4; //Start reading vector data at address 4
for (int i=0;i < size ; i++) {
motion_vector mv;
memcpy(&mv,mvect_buffer+offset,sizeof(mv));
offset+=sizeof(motion_vector);
std::cout << "COUNTR " << i << " x_vector " << (int)mv.x_mag << " Y_vector " << (int)mv.y_mag << std::endl;
}
std::cout << "SIZE " << rsize2 << " size " << sizeof(rsize2) << std::endl;
std::cout << "VECTS " << vtype2 << " size " << sizeof(vtype2) << std::endl;
free(mvect_buffer);
return 0;
}
|