ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VLIB / P263 / p263_picture_layer.c
1 #include <VP_Os/vp_os_malloc.h>
2
3 #include <VLIB/video_controller.h>
4 #include <VLIB/video_packetizer.h>
5
6 #include "p263_codec.h"
7 #include "p263_layers.h"
8 #include "p263_huffman.h"
9
10 C_RESULT p263_read_picture_layer( video_controller_t* controller, video_stream_t* stream )
11 {
12   uint32_t pei = 0;
13   p263_codec_t* p263_codec = (p263_codec_t*) controller->video_codec;
14   p263_picture_layer_t* picture_layer = &p263_codec->picture_layer;
15
16   p263_codec->mb_types  = &standard_mb_types[0];
17   p263_codec->cbpys     = &cbpy_standard[0];
18
19   // Read Temporal Reference (TR) (8 bits)
20   picture_layer->tr = 0;
21   video_read_data( stream, &picture_layer->tr, 8 );
22
23   // Read Type Information (PTYPE) (Variable Length)
24   picture_layer->ptype      = 0;
25   picture_layer->plusptype  = 0;
26   picture_layer->opptype    = 0;
27   video_read_data( stream, &picture_layer->ptype, 8 );
28
29   switch( PICTURE_FORMAT(picture_layer->ptype) )
30   {
31     case P263_PICTURE_FORMAT_FORBIDDEN:
32       break;
33
34     case P263_PICTURE_FORMAT_SUBQCIF:
35       video_controller_set_format( controller, 128, 96 );
36       goto P263_PICTURE_FORMAT_NOT_EXTENDED;
37
38     case P263_PICTURE_FORMAT_QCIF:
39       video_controller_set_format( controller, 176, 144 );
40       goto P263_PICTURE_FORMAT_NOT_EXTENDED;
41
42     case P263_PICTURE_FORMAT_CIF:
43       video_controller_set_format( controller, 352, 288 );
44       goto P263_PICTURE_FORMAT_NOT_EXTENDED;
45
46     case P263_PICTURE_FORMAT_4QCIF:
47       video_controller_set_format( controller, 704, 576 );
48       goto P263_PICTURE_FORMAT_NOT_EXTENDED;
49
50     case P263_PICTURE_FORMAT_16CIF:
51       video_controller_set_format( controller, 1408, 1152 );
52
53     P263_PICTURE_FORMAT_NOT_EXTENDED:
54       video_read_data( stream, &picture_layer->ptype, 5 );
55       video_controller_set_picture_type( controller, PICTURE_TYPE(picture_layer->ptype) );
56       break;
57
58     case P263_PICTURE_FORMAT_RESERVED:
59       break;
60
61     case P263_PICTURE_FORMAT_EXTENDED:
62       // Read Plus PTYPE (PLUSPTYPE) (Variable Length) -- Optionnal, see PTYPE
63       // Read UFEP
64       video_read_data( stream, &picture_layer->plusptype, 3 );
65       if( picture_layer->plusptype == 1 )
66       {
67         // Read OPPTYPE
68         video_read_data( stream, &picture_layer->opptype, 18 );
69       }
70
71       // Read MPPTYPE
72       video_read_data( stream, &picture_layer->plusptype, 9 );
73       video_controller_set_picture_type( controller, PICTURE_EXTENDED_TYPE(picture_layer->plusptype) );
74       break;
75   }
76
77   if( picture_layer->plusptype )
78   {
79     // Read Continuous Presence Multipoint and Video Multiplex (CPM) (1 bit, see Annex C)
80     video_read_data( stream, &picture_layer->cpm, 1 );
81
82     if( picture_layer->cpm )
83     {
84       // Read Picture Sub-Bitstream Indicator (PSBI) (2 bits)
85       video_read_data( stream, &picture_layer->psbi, 2 );
86     }
87   }
88
89   if( picture_layer->opptype ) // eg UFEP == 001b
90   {
91     if( HAS_CUSTOM_PICTURE_FORMAT( picture_layer->opptype ) )
92     {
93       // Read Custom Picture Format (CPFMT) (23 bits)
94       video_read_data( stream, &picture_layer->cpfmt, 23 );
95
96       if( (picture_layer->cpfmt >> 19) == 0xF )
97       {
98         // Read Extended Pixel Aspect Ratio (EPAR) (16 bits)
99         video_read_data( stream, &picture_layer->epar, 16 );
100       }
101     }
102
103     if( HAS_CUSTOM_PCF(picture_layer->opptype) )
104     {
105       // Read Custom Picture Clock Frequency Code (CPCFC) (8 bits)
106       video_read_data( stream, &picture_layer->cpcfc, 8 );
107
108       // Read Extended Temporal Reference (ETR) (2 bits)
109       video_read_data( stream, &picture_layer->etr, 2 );
110     }
111
112     if( HAS_UNRESTRICTED_MOTION_VECTOR(picture_layer->opptype) )
113     {
114       // Read Unlimited Unrestricted Motion Vectors Indicator (UUI) (Variable length) -- Optionnal
115       video_read_data( stream, &picture_layer->uui, 1 );
116       if( picture_layer->uui == 0 )
117         video_read_data( stream, &picture_layer->uui, 1 );
118     }
119
120     if( HAS_SLICE_STRUCTURED(picture_layer->sss) )
121     {
122       // Read Slice Structured Submode bits (SSS) (2 bits) -- Optionnal
123       video_read_data( stream, &picture_layer->sss, 2 );
124     }
125   }
126
127   if( controller->picture_type == VIDEO_PICTURE_EP )
128   {
129     // Read Enhancement Layer Number (ELNUM) (4 bits) -- Optionnal
130     video_read_data( stream, &picture_layer->elnum, 4 );
131
132     if( picture_layer->opptype ) // eg UFEP == 001b
133     {
134       // Read Reference Layer Number (RLNUM) (4 bits) -- Optionnal
135       video_read_data( stream, &picture_layer->rlnum, 4 );
136     }
137   }
138
139   if( HAS_REFERENCE_PICTURE_SELECTION( picture_layer->opptype ) )
140   {
141     // Read Reference Picture Selection Mode Flags (RPSMF) (3 bits) -- Optionnal
142     video_read_data( stream, &picture_layer->rpsmf, 3 );
143   }
144
145   if( picture_layer->rpsmf )
146   {
147     // Read Temporal Reference for Prediction Indication (TRPI) (1 bit) -- Optionnal
148     video_read_data( stream, &picture_layer->trpi, 1 );
149
150     if( picture_layer->trpi )
151     {
152       // Read Temporal Reference for Prediction (TRP) (10 bits) -- Optionnal
153       video_read_data( stream, &picture_layer->trp, 10 );
154     }
155
156     // Read Back-Channel message Indication (BCI) (Variable length, 1 or 2 bits) -- Optionnal
157     video_read_data( stream, &picture_layer->bci, 1 );
158
159     if( picture_layer->bci == 1 )
160     {
161       // TODO
162       // Read Back-Channel Message (BCM) (Variable length, see N.4.2) -- Optionnal
163     }
164     else
165     {
166       video_read_data( stream, &picture_layer->bci, 1 ); // read '01' end of back channel message
167     }
168   }
169
170   if( HAS_REFERENCE_PICTURE_RESAMPLING( picture_layer->plusptype ) )
171   {
172     // TODO
173     // Read Reference Picture Resampling Parameters (RPRP) (Variable length, see Annex P) -- Optionnal
174   }
175
176   // Read Quantizer Information (PQUANT) (5 bits)
177   video_read_data( stream, &picture_layer->pquant, 5 );
178   controller->Qp = picture_layer->pquant;
179
180   if( !picture_layer->plusptype )
181   {
182     // Read Continuous Presence Multipoint and Video Multiplex (CPM) (1 bit, see Annex C)
183     video_read_data( stream, &picture_layer->cpm, 1 );
184
185     if( picture_layer->cpm )
186     {
187       // Read Picture Sub-Bitstream Indicator (PSBI) (2 bits)
188       video_read_data( stream, &picture_layer->psbi, 2 );
189     }
190   }
191
192   if( controller->picture_type == VIDEO_PICTURE_PB )
193   {
194     // TODO
195     // Read Temporal Reference for B-pictures in PB-frames (TRB) (3/5 bits) -- Optionnal
196
197     // Read Quantization information for B-pictures in PB-frames (DBQUANT) (2 bits)
198   }
199
200   // Read Extra Insertion Information (PEI) (1 bit)
201   picture_layer->pei = 0;
202   video_read_data( stream, &pei, 1 );
203
204   while( pei )
205   {
206     // Read Supplemental Enhancement Information (PSUPP) (0/8/16 ... bits) -- Optionnal (See Annex L)
207     video_read_data( stream, &pei, 8 );
208
209     // picture_layer->psupp[picture_layer->pei++] = pei; TODO: Alloc psupp
210
211     video_read_data( stream, &pei, 1 );
212   }
213
214   return C_OK;
215 }