-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo.cpp
More file actions
201 lines (182 loc) · 6.14 KB
/
Copy pathvideo.cpp
File metadata and controls
201 lines (182 loc) · 6.14 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
//
// PRACTICAS REDES MULTIMEDIA (RMM)
// FACULTAD DE INFORMATICA - UPV
//
// COMPRESION DE VIDEO
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CompVideo.h"
#include "Cuantiza.h"
#include "DCT.h"
#include "Huffman.h"
#define COMPRIME 1
#define DESCOMPRIME 2
#define VERBOSE 1
#define MIN_SIMBOL_DE_HUFFMAN -15
#define MAX_SIMBOL_DE_HUFFMAN +15
// PARAMETROS DEL VIDEO A COMPRIMIR
static char *ficheroEntradaPorDefecto="salesman";
static int opcionPorDefecto=COMPRIME;
static int anchoPorDefecto=352;
static int altoPorDefecto=288;
static float cuantizacionPorDefecto=3.5;
static int NFRAMESPorDefecto=21;
static int MetodoPorDefecto=COD_DIF;
static int PframesPorDefecto=0;
static int TamVentanaPorDefecto=8;
// FIN DE PARAMETROS DEL VIDEO A COMPRIMIR
static char *ficheroEntrada=ficheroEntradaPorDefecto;
static int opcion=opcionPorDefecto;
static int ancho=anchoPorDefecto;
static int alto=altoPorDefecto;
static float cuantizacion=cuantizacionPorDefecto;
static int NFRAMES=NFRAMESPorDefecto;
static int Metodo=MetodoPorDefecto;
static int Pframes=PframesPorDefecto;
static int TamVentana=TamVentanaPorDefecto;
void CompruebaError(int error)
{
if (error!=0)
{
if (error==1) printf("\nError al leer fichero\n");
if (error==2) printf("\nEl ancho y alto de todos los planos deben de ser multiplos de 16\n");
if (error==3) printf("\nError al escribir fichero\n");
if (error==4) printf("\nCuantizacion fuera de rango\n");
exit(1);
}
}
void ProcesaArgumentos(int argc, char **argv)
{
if (argc>=2)
{
if (
(argc!=8 && argc!=10) || (strcmp(argv[1],"-c") && strcmp(argv[1],"-d")) || atoi(argv[3])==0 || atoi(argv[4])==0 ||
atoi(argv[6])==0 || atof(argv[5])==0.0 || (argc==10 && strcmp(argv[8],"-e")) || (argc==10 && atoi(argv[9])==0)
)
{
printf("\nTeclea \"{-c|-d} DirVideo ancho alto cuantizacion NFrames PFrames {-e tamVent}\"\n");
exit(1);
}
if (!strcmp(argv[1],"-c")) opcion=COMPRIME; else opcion=DESCOMPRIME;
ficheroEntrada=argv[2];
ancho=atoi(argv[3]);
alto=atoi(argv[4]);
cuantizacion=(float)atof(argv[5]);
NFRAMES=atoi(argv[6]);
Pframes=atoi(argv[7]);
if (argc==10)
{
Metodo=ESTIM_MOV;
TamVentana=atoi(argv[9]);
}
}
}
void CompruebaHuffman(char *NombreFich)
{
unsigned char cadenaPrueba[19]={0,0,1,0,0,0,2,2,4,0,0,2,2,2,0,3,4,4,4};
int f; unsigned int ns;
printf("Histograma.cpp y Huffman.cpp... ");
if (InicializaHuffmanCod(5,19,(char *)cadenaPrueba, NombreFich)!=0)
{
printf("\nFALLO AL INICIALIZAR CODIFICACION HUFFMAN\n");
exit(1);
}
for (f=0;f<19;f++) CodificaSimbolo(cadenaPrueba[f]);
FinalizaHuffmanCod();
if (InicializaHuffmanDecod(5,&ns,NombreFich)!=0)
{
printf("\nFALLO AL INICIALIZAR DECODIFICACION HUFFMAN\n");
exit(1);
}
for (f=0;f<19;f++) if (DecodificaSimbolo()!=cadenaPrueba[f])
{
printf("\nHUFFMAN MAL IMPLEMENTADO, revisa la practica de Huffman\n");
exit(1);
}
printf("OK.\n");
}
void CompruebaDCT()
{
double linea[8]={140,144,147,140,140,155,179,175};
int result1[8]={431,-33,20,-7,-10,6,-3,2};
int result2[8]={140,144,147,140,140,155,179,175};
printf("DCT.cpp... ");
int f;
DCTn_1D(linea);
for (f=0;f<8;f++) if ((int)linea[f]!=(int)result1[f])
{
printf("\n1D_DCT NORMAL MAL IMPLEMENTADA, revisa la practica de Imagen\n");
exit(1);
}
IDCTn_1D(linea);
for (f=0;f<8;f++) if ((((int)linea[f])!=(int)result2[f]) && (((int)linea[f])+1!=(int)result2[f]))
{
printf("\n1D_IDCT NORMAL MAL IMPLEMENTADA, revisa la practica de Imagen\n");
exit(1);
}
printf("OK.\n");
}
void CompruebaCuantizacion()
{
double **bloque,**bloquecomp,Q=3.5;
int x,y,g;
printf("Cuantiza.cpp... ");
bloque=(double**)malloc(8*sizeof(double*));
for (g=0;g<8;g++) bloque[g]=(double*)malloc(8*sizeof(double));
bloquecomp=(double**)malloc(8*sizeof(double*));
for (g=0;g<8;g++) bloquecomp[g]=(double*)malloc(8*sizeof(double));
for (x=0;x<8;x++) for (y=0;y<8;y++) {bloque[y][x]=((double)x+y*8); bloquecomp[y][x]=((int)((double)x+y*8))/((int)(2.0*Q));}
CuantizaUniformeBloque(Q,bloque);
for (x=0;x<8;x++) for (y=0;y<8;y++) if (bloque[y][x]!=bloquecomp[y][x])
{
printf("\nCALCULO DE LA CUANTIZACION MAL IMPLEMENTADO, revisa la practica de Imagen\n");
exit(1);
}
CuantizaUniformeInversaBloque(Q,bloque);
for (x=0;x<8;x++) for (y=0;y<8;y++) bloquecomp[y][x]=(bloquecomp[y][x]<0.0?(2*bloquecomp[y][x]+1)*Q:(bloquecomp[y][x]>0.0?(2*bloquecomp[y][x]-1)*Q:0.0));
for (x=0;x<8;x++) for (y=0;y<8;y++) if (bloque[y][x]!=bloquecomp[y][x])
{
printf("\nCALCULO DE LA CUANTIZACION INVERSA MAL IMPLEMENTADO, revisa la practica de Imagen\n");
exit(1);
}
for (g=0;g<8;g++) free(bloque[g]); free(bloque);
for (g=0;g<8;g++) free(bloquecomp[g]); free(bloquecomp);
printf("OK.\n");
}
void CompruebaModulosParaImagen(char *NombreFich)
{
printf("\nComprobando la correccion de los modulos:\n");
CompruebaHuffman(NombreFich);
CompruebaDCT();
CompruebaCuantizacion();
}
int main(int argc, char **argv)
{
int error;
printf("\n--------------------------------\n");
printf("PRACTICAS REDES MULTIMEDIA (RMM)\n");
printf("Facultad de Informatica, UPV\n");
printf("--------------------------------\n");
printf("Practica de compresion de video\n");
printf("--------------------------------\n");
ProcesaArgumentos(argc,argv);
// hacer que se pase por parametro el numero de frames en ambos casos
if (opcion==COMPRIME)
{
CompruebaModulosParaImagen("~temp.tmp");
printf("\n\nCOMPRIMIENDO VIDEO %s\n\n",ficheroEntrada);
error=ComprimeVideoLuminancia(ficheroEntrada,ancho,alto,NFRAMES,cuantizacion,MIN_SIMBOL_DE_HUFFMAN,MAX_SIMBOL_DE_HUFFMAN,Pframes,Metodo,TamVentana,VERBOSE);
CompruebaError(error);
printf("\n\nCOMPRESION TERMINADA\n\n");
}
else
{
printf("\n\nDESCOMPRIMIENDO VIDEO %s\n\n",ficheroEntrada);
error=DescomprimeVideoLuminancia(ficheroEntrada,ancho,alto,NFRAMES,MIN_SIMBOL_DE_HUFFMAN,MAX_SIMBOL_DE_HUFFMAN,Pframes,Metodo,VERBOSE);
CompruebaError(error);
printf("\n\nDESCOMPRESION TERMINADA\n\n");
}
return 0;
}