00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "pg_decode.h"
00021
00022 #include "util/macro.h"
00023 #include "util/logging.h"
00024 #include "util/bits.h"
00025
00026 #include <string.h>
00027 #include <stdlib.h>
00028
00029
00030 void pg_decode_video_descriptor(BITBUFFER *bb, BD_PG_VIDEO_DESCRIPTOR *p)
00031 {
00032 p->video_width = bb_read(bb, 16);
00033 p->video_height = bb_read(bb, 16);
00034 p->frame_rate = bb_read(bb, 4);
00035 bb_skip(bb, 4);
00036 }
00037
00038 void pg_decode_composition_descriptor(BITBUFFER *bb, BD_PG_COMPOSITION_DESCRIPTOR *p)
00039 {
00040 p->number = bb_read(bb, 16);
00041 p->state = bb_read(bb, 2);
00042 bb_skip(bb, 6);
00043 }
00044
00045 void pg_decode_sequence_descriptor(BITBUFFER *bb, BD_PG_SEQUENCE_DESCRIPTOR *p)
00046 {
00047 p->first_in_seq = bb_read(bb, 1);
00048 p->last_in_seq = bb_read(bb, 1);
00049 bb_skip(bb, 6);
00050 }
00051
00052 void pg_decode_window(BITBUFFER *bb, BD_PG_WINDOW *p)
00053 {
00054 p->id = bb_read(bb, 8);
00055 p->x = bb_read(bb, 16);
00056 p->y = bb_read(bb, 16);
00057 p->width = bb_read(bb, 16);
00058 p->height = bb_read(bb, 16);
00059 }
00060
00061 void pg_decode_composition_object(BITBUFFER *bb, BD_PG_COMPOSITION_OBJECT *p)
00062 {
00063 p->object_id_ref = bb_read(bb, 16);
00064 p->window_id_ref = bb_read(bb, 8);
00065
00066 p->crop_flag = bb_read(bb, 1);
00067 p->forced_on_flag = bb_read(bb, 1);
00068 bb_skip(bb, 6);
00069
00070 p->x = bb_read(bb, 16);
00071 p->y = bb_read(bb, 16);
00072
00073 if (p->crop_flag) {
00074 p->crop_x = bb_read(bb, 16);
00075 p->crop_y = bb_read(bb, 16);
00076 p->crop_w = bb_read(bb, 16);
00077 p->crop_h = bb_read(bb, 16);
00078 }
00079 }
00080
00081
00082
00083
00084
00085 int pg_decode_palette_update(BITBUFFER *bb, BD_PG_PALETTE *p)
00086 {
00087 p->id = bb_read(bb, 8);
00088 p->version = bb_read(bb, 8);
00089
00090 while (!bb_eof(bb)) {
00091 uint8_t entry_id = bb_read(bb, 8);
00092
00093 p->entry[entry_id].Y = bb_read(bb, 8);
00094 p->entry[entry_id].Cr = bb_read(bb, 8);
00095 p->entry[entry_id].Cb = bb_read(bb, 8);
00096 p->entry[entry_id].T = bb_read(bb, 8);
00097 }
00098
00099 return 1;
00100 }
00101
00102 int pg_decode_palette(BITBUFFER *bb, BD_PG_PALETTE *p)
00103 {
00104 memset(p->entry, 0, sizeof(p->entry));
00105
00106 return pg_decode_palette_update(bb, p);
00107 }
00108
00109 static int _decode_rle(BITBUFFER *bb, BD_PG_OBJECT *p)
00110 {
00111 int pixels_left = p->width * p->height;
00112 int num_rle = 0;
00113 int rle_size = p->width * p->height / 4;
00114
00115 p->img = realloc(p->img, rle_size * sizeof(BD_PG_RLE_ELEM));
00116
00117 while (!bb_eof(bb)) {
00118 uint32_t len = 1;
00119 uint8_t color = 0;
00120
00121 if (!(color = bb_read(bb, 8))) {
00122 if (!bb_read(bb, 1)) {
00123 if (!bb_read(bb, 1)) {
00124 len = bb_read(bb, 6);
00125 } else {
00126 len = bb_read(bb, 14);
00127 }
00128 } else {
00129 if (!bb_read(bb, 1)) {
00130 len = bb_read(bb, 6);
00131 } else {
00132 len = bb_read(bb, 14);
00133 }
00134 color = bb_read(bb, 8);
00135 }
00136 }
00137
00138 p->img[num_rle].len = len;
00139 p->img[num_rle].color = color;
00140
00141 pixels_left -= len;
00142
00143 num_rle++;
00144 if (num_rle >= rle_size) {
00145 rle_size *= 2;
00146 p->img = realloc(p->img, rle_size * sizeof(BD_PG_RLE_ELEM));
00147 }
00148 }
00149
00150 if (pixels_left > 0) {
00151 BD_DEBUG(DBG_DECODE, "pg_decode_object(): missing %d pixels\n", pixels_left);
00152 return 0;
00153 }
00154 if (pixels_left < 0) {
00155 BD_DEBUG(DBG_DECODE, "pg_decode_object(): too many pixels (%d)\n", -pixels_left);
00156 return 0;
00157 }
00158
00159 return 1;
00160 }
00161
00162 int pg_decode_object(BITBUFFER *bb, BD_PG_OBJECT *p)
00163 {
00164 BD_PG_SEQUENCE_DESCRIPTOR sd;
00165
00166 p->id = bb_read(bb, 16);
00167 p->version = bb_read(bb, 8);
00168
00169 pg_decode_sequence_descriptor(bb, &sd);
00170
00171
00172 if (!sd.first_in_seq) {
00173 BD_DEBUG(DBG_DECODE, "pg_decode_object(): not first in sequence\n");
00174 return 0;
00175 }
00176 if (!sd.last_in_seq) {
00177 BD_DEBUG(DBG_DECODE, "pg_decode_object(): not last in sequence\n");
00178 return 0;
00179 }
00180
00181 if (!bb_is_align(bb, 0x07)) {
00182 BD_DEBUG(DBG_DECODE, "pg_decode_object(): alignment error\n");
00183 return 0;
00184 }
00185
00186 uint32_t data_len = bb_read(bb, 24);
00187 uint32_t buf_len = bb->p_end - bb->p;
00188 if (data_len != buf_len) {
00189 BD_DEBUG(DBG_DECODE, "pg_decode_object(): buffer size mismatch (expected %d, have %d)\n", data_len, buf_len);
00190 return 0;
00191 }
00192
00193 p->width = bb_read(bb, 16);
00194 p->height = bb_read(bb, 16);
00195
00196 return _decode_rle(bb, p);
00197 }
00198
00199 int pg_decode_composition(BITBUFFER *bb, BD_PG_COMPOSITION *p)
00200 {
00201 unsigned ii;
00202
00203 pg_decode_video_descriptor(bb, &p->video_descriptor);
00204 pg_decode_composition_descriptor(bb, &p->composition_descriptor);
00205
00206 p->palette_update_flag = bb_read(bb, 1);
00207 bb_skip(bb, 7);
00208
00209 p->palette_id_ref = bb_read(bb, 8);
00210
00211 p->num_composition_objects = bb_read(bb, 8);
00212 p->composition_object = calloc(p->num_composition_objects, sizeof(BD_PG_COMPOSITION_OBJECT));
00213
00214 for (ii = 0; ii < p->num_composition_objects; ii++) {
00215 pg_decode_composition_object(bb, &p->composition_object[ii]);
00216 }
00217
00218 return 1;
00219 }
00220
00221 int pg_decode_windows(BITBUFFER *bb, BD_PG_WINDOWS *p)
00222 {
00223 unsigned ii;
00224
00225 p->num_windows = bb_read(bb, 8);
00226 p->window = calloc(p->num_windows, sizeof(BD_PG_WINDOW));
00227
00228 for (ii = 0; ii < p->num_windows; ii++) {
00229 pg_decode_window(bb, &p->window[ii]);
00230 }
00231
00232 return 1;
00233 }
00234
00235
00236
00237
00238
00239 void pg_clean_object(BD_PG_OBJECT *p)
00240 {
00241 if (p) {
00242 X_FREE(p->img);
00243 }
00244 }
00245
00246 void pg_clean_composition(BD_PG_COMPOSITION *p)
00247 {
00248 if (p) {
00249 X_FREE(p->composition_object);
00250 }
00251 }
00252
00253 void pg_clean_windows(BD_PG_WINDOWS *p)
00254 {
00255 if (p) {
00256 X_FREE(p->window);
00257 }
00258 }
00259
00260 void pg_free_palette(BD_PG_PALETTE **p)
00261 {
00262 if (p && *p) {
00263 X_FREE(*p);
00264 }
00265 }
00266
00267 void pg_free_object(BD_PG_OBJECT **p)
00268 {
00269 if (p && *p) {
00270 pg_clean_object(*p);
00271 X_FREE(*p);
00272 }
00273 }
00274
00275 void pg_free_composition(BD_PG_COMPOSITION **p)
00276 {
00277 if (p && *p) {
00278 pg_clean_composition(*p);
00279 X_FREE(*p);
00280 }
00281 }
00282
00283 void pg_free_windows(BD_PG_WINDOWS **p)
00284 {
00285 if (p && *p) {
00286 pg_clean_windows(*p);
00287 X_FREE(*p);
00288 }
00289 }