OpenCores
URL https://opencores.org/ocsvn/funbase_ip_library/funbase_ip_library/trunk

Subversion Repositories funbase_ip_library

[/] [funbase_ip_library/] [trunk/] [TUT/] [ip.application/] [h.263_encoder_main/] [1.0/] [src/] [main.c] - Blame information for rev 145

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 145 lanttu
/*---------------------------------------------------------------------------
2
 *
3
 *  Course:   SoC design
4
 *
5
 *  File:     main.c
6
 *
7
 *  Purpose:  This is main code of H.263 video encoder
8
 *
9
 *  Group:
10
 *
11
 *  Authors:
12
 *
13
 *
14
 *  Notes:    This encoder code implements only a part of H.263 features.
15
 *            As you know, H.263 standard defines only the decoder - not
16
 *            the encoder. Thus, this encoder code produces bitstream that
17
 *            any H.263 compliant decoder is able to decode.
18
 *
19
 *            This implementation is an extremely simplified version
20
 *            H.263 video encoder. For instance, parallel execution and
21
 *            motion estimation is not applied at all in these codes.
22
 *            Limitations:
23
 *            - Only INTRA coding mode is supported
24
 *            - Only QCIF picture format supported
25
 *            - None of the optional coding modes supported
26
 *
27
 *---------------------------------------------------------------------------
28
 */
29
 
30
//#include <assert.h>
31
#include <mcapi.h>
32
#include <stdio.h>
33
 
34
#include "headers.h"
35
//#include "carphone_data.h"
36
 
37
 
38
FILE* OUTPUT_STREAM;
39
 
40
#define XMBS 11
41
#define YMBS 9
42
 
43
// MACRO BLOCK COUNT FOR DCT
44
#define MB_COUNT 6
45
#define MB_SIZE 64 
46
 
47
// MCAPI NODE NUMBER
48
#define MAIN_NODE_NUM 0
49
#define DCT_NODE_NUM 1
50
 
51
#define WAIT_TIMEOUT 0xFFFFFFFF
52
 
53
#define mcapi_assert_success(s) \
54
        if (s != MCAPI_SUCCESS) { printf("%s:%d status %d\n", __FILE__, __LINE__, s); abort(); }
55
 
56
 
57
 
58
int encoder(){
59
 
60
  //printf("Encoder starts MCAPI initialization\r\n");
61
 
62
  // mcapi initialization starts here
63
  mcapi_priority_t priority = 1;
64
  mcapi_status_t status;
65
  mcapi_request_t request, send_request, recv_request;
66
  size_t size, recv_size, send_size;
67
  mcapi_version_t version;
68
  mcapi_endpoint_t  block_count_out, dct_data_out, dct_data_in ,
69
    ext_dct_data_in, ext_dct_data_out, ext_dct_block_count_in;
70
 
71
  mcapi_pktchan_send_hndl_t       send_handle;
72
  mcapi_pktchan_recv_hndl_t       recv_handle;
73
 
74
  unsigned int my_node_id = MAIN_NODE_NUM;
75
 
76
  mcapi_initialize(my_node_id,&version,&status);
77
  mcapi_assert_success(status);
78
 
79
  /* create endpoints */
80
  block_count_out = mcapi_create_endpoint ( 0 ,&status);
81
  mcapi_assert_success(status);
82
 
83
  dct_data_out = mcapi_create_endpoint ( 1 ,&status);
84
  mcapi_assert_success(status);
85
 
86
  dct_data_in = mcapi_create_endpoint ( 2 ,&status);
87
  mcapi_assert_success(status);
88
 
89
  //printf("Endpoints created succesfully\r\n");
90
 
91
  /* get DCT's endpoints from database */
92
  ext_dct_data_in = mcapi_get_endpoint (DCT_NODE_NUM, 1,&status);
93
  mcapi_assert_success(status);
94
 
95
  ext_dct_data_out = mcapi_get_endpoint (DCT_NODE_NUM, 2, &status);
96
  mcapi_assert_success(status);
97
 
98
  ext_dct_block_count_in = mcapi_get_endpoint (DCT_NODE_NUM, 0, &status);
99
  mcapi_assert_success(status);
100
 
101
  //printf("Remote endpoints obtained succesfully\r\n");
102
 
103
  /* connect packet channels */
104
  mcapi_connect_pktchan_i(dct_data_out , ext_dct_data_in,&request,&status);
105
  while (!mcapi_test(&request,&size,&status)) {}
106
  mcapi_assert_success(status);
107
 
108
  mcapi_connect_pktchan_i(ext_dct_data_out, dct_data_in,&request,&status);
109
  while (!mcapi_test(&request,&size,&status)) {}
110
  mcapi_assert_success(status);
111
 
112
  //printf("Packet channels connected succesfully\r\n");
113
 
114
  /* open pktchan for sending macroblocks */
115
  mcapi_open_pktchan_send_i(&send_handle, dct_data_out, &send_request, &status);
116
  //printf("First channel opened!\r\n");
117
  mcapi_assert_success(status);
118
 
119
  /* open pktchan for receiving macroblocks from DCT */
120
  mcapi_open_pktchan_recv_i(&recv_handle, dct_data_in, &recv_request, &status);
121
  mcapi_assert_success(status);
122
  //printf("Second channel opened!\r\n");
123
 
124
  mcapi_wait(&send_request, &size, &status, WAIT_TIMEOUT);
125
  mcapi_assert_success(status);
126
 
127
  //printf("First wait!\r\n");
128
 
129
  mcapi_wait(&recv_request, &size, &status, WAIT_TIMEOUT);
130
  mcapi_assert_success(status);
131
 
132
  //printf("MCAPI initialization done!\r\n");
133
 
134
 
135
  size = 1;
136
  uint8 msg = MB_COUNT;
137
 
138
  //printf("Sending configuration message to DCT!\r\n");
139
  mcapi_msg_send(block_count_out, ext_dct_block_count_in, &msg, size, priority,&status);
140
 
141
  sint32 lastFrame;
142
  uint8 currentImage[38016];
143
 
144
  vchar inputFileName[] = "carphone.qcif";
145
  vchar streamFileName[] = "carphone.263";
146
 
147
  unsigned int cur_frame;
148
 
149
  BitStreamType stream;
150
 
151
  FILE *inputfile;
152
 
153
  inputfile=yuvOpenInputFile(inputFileName, &lastFrame);
154
 
155
 
156
  //  assert(inputfile != NULL);
157
 
158
 
159
  bitstreamInitBuffer(&stream);
160
  stream.file=fopen(streamFileName, "wb");
161
 
162
  uint8 *mb_data;
163
  MBType dct_data;
164
 
165
  mb_data=(uint8*)malloc(MB_COUNT*MB_SIZE);
166
  dct_data.data=(sint16*)malloc(2*MB_COUNT*MB_SIZE);
167
//  char start;
168
//  getc(start);
169
 
170
  for(cur_frame=0;cur_frame<lastFrame;cur_frame++)
171
  {
172
    yuvReadFrame(inputfile,currentImage);
173
    //currentImage=&carphone_data[cur_frame*38016];
174
 
175
    PictureType ptype;
176
    ptype.TR=cur_frame;
177
    ptype.QUANT=QPI_DEF;
178
    codePictureHeader(&ptype,&stream);
179
 
180
    unsigned int xpos,ypos;
181
/*
182
    uint8 *mb_data;
183
    MBType dct_data;
184
 
185
 
186
    mb_data=(uint8*)malloc(MB_COUNT*MB_SIZE);
187
    dct_data.data=(sint16*)malloc(2*MB_COUNT*MB_SIZE);
188
  */
189
    send_size = MB_COUNT*MB_SIZE;
190
 
191
    //sint16 *incoming;
192
 
193
    for(ypos=0;ypos<YMBS; ypos++)
194
    {
195
      for(xpos=0;xpos<XMBS; xpos++)
196
      {
197
        //printf("Blokin aloitus: f=%d: x=%d, y=%d\r\n", cur_frame, xpos, ypos);
198
       memoryLoadMB(ypos,xpos,(unsigned char*)currentImage, mb_data);
199
       //printf("Memory load ok.\r\n");
200
 
201
       /* send six macro blocks to dct via mcapi */
202
       //printf("Sending macroblock packets to DCT.\r\n");
203
       mcapi_pktchan_send(send_handle, mb_data, send_size, &status);
204
       mcapi_assert_success(status);
205
       //printf("Sent successfully!\r\n");
206
 
207
       /* receive data from dct */
208
       mcapi_pktchan_recv(recv_handle,(void *)&dct_data.data ,&recv_size,&status);
209
       mcapi_assert_success(status);
210
 
211
 
212
       //printf("Received transformed macroblocks from DCT.\r\n");
213
       fdct_8x8(mb_data, dct_data.data, 6);
214
       // printf("fdct ok.\r\n");
215
       quantizeIntraMB(QPI_DEF, &dct_data);
216
       //printf("quantize ok.\r\n");
217
       codeIntraMB(&dct_data, &stream);
218
       //printf("code ok.\r\n");
219
 
220
       mcapi_pktchan_free(dct_data.data, &status);
221
      }
222
    }
223
 
224
 
225
    bitstreamAlign(&stream);
226
 
227
 
228
  }
229
    free(mb_data);
230
    //free(dct_data.data);
231
 
232
  bitstreamFlushBufferToFile(&stream);
233
 
234
  fclose(stream.file);
235
  //printf("Job well done!\n");
236
  return 1;
237
}
238
 
239
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.