| [2] | 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2003-2010, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: budp_seiparse.c $ |
|---|
| 11 | * $brcm_Revision: Hydra_Software_Devel/1 $ |
|---|
| 12 | * $brcm_Date: 9/22/10 8:26p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /magnum/commonutils/udp/budp_seiparse.c $ |
|---|
| 19 | * |
|---|
| 20 | * Hydra_Software_Devel/1 9/22/10 8:26p darnstein |
|---|
| 21 | * SW7405-4817: implementation of SEI message parser. |
|---|
| 22 | * |
|---|
| 23 | ***************************************************************************/ |
|---|
| 24 | |
|---|
| 25 | /* For debugging */ |
|---|
| 26 | /* #define BUDP_P_GETUD_DUMP 1 */ |
|---|
| 27 | #ifdef BUDP_P_GETUD_DUMP |
|---|
| 28 | static const char* BUDP_P_Getud_Filename = "userdata.getud"; |
|---|
| 29 | #include <stdio.h> |
|---|
| 30 | #endif |
|---|
| 31 | |
|---|
| 32 | #include "bstd.h" |
|---|
| 33 | #include "bavc.h" |
|---|
| 34 | #include "bdbg.h" |
|---|
| 35 | #include "bvlc.h" |
|---|
| 36 | #include "budp.h" |
|---|
| 37 | #include "budp_seiparse.h" |
|---|
| 38 | |
|---|
| 39 | BDBG_MODULE(BUDP); |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | /*************************************************************************** |
|---|
| 43 | * Forward declarations of static (private) functions |
|---|
| 44 | ***************************************************************************/ |
|---|
| 45 | |
|---|
| 46 | static size_t FindSeiUserdataStart ( |
|---|
| 47 | const uint8_t* userdata, size_t length); |
|---|
| 48 | |
|---|
| 49 | #ifdef BUDP_P_GETUD_DUMP |
|---|
| 50 | static void dump_getud ( |
|---|
| 51 | const BAVC_USERDATA_info* pUserdata_info, size_t offset); |
|---|
| 52 | #endif |
|---|
| 53 | |
|---|
| 54 | /*************************************************************************** |
|---|
| 55 | * Static data (tables, etc.) |
|---|
| 56 | ***************************************************************************/ |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | /*************************************************************************** |
|---|
| 60 | * Implementation of "BUDP_SEIparse_" API functions |
|---|
| 61 | ***************************************************************************/ |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | /*************************************************************************** |
|---|
| 65 | * |
|---|
| 66 | */ |
|---|
| 67 | BERR_Code BUDP_SEIparse ( |
|---|
| 68 | const BAVC_USERDATA_info* pUserdata_info, |
|---|
| 69 | size_t offset, |
|---|
| 70 | size_t* pBytesParsed, |
|---|
| 71 | BUDP_SEIparse_3ddata* p3ddata |
|---|
| 72 | ) |
|---|
| 73 | { |
|---|
| 74 | size_t bytesParsedSub; |
|---|
| 75 | size_t length; |
|---|
| 76 | uint8_t* userdata; |
|---|
| 77 | uint8_t seiType; |
|---|
| 78 | BERR_Code eErr; |
|---|
| 79 | |
|---|
| 80 | int vlc_decoded; |
|---|
| 81 | unsigned vlc_index; |
|---|
| 82 | unsigned vlc_bit; |
|---|
| 83 | |
|---|
| 84 | BDBG_ENTER(BUDP_SEIparse); |
|---|
| 85 | |
|---|
| 86 | /* Check for obvious errors from user */ |
|---|
| 87 | if ((pUserdata_info == 0x0) || |
|---|
| 88 | (pBytesParsed == 0x0) || |
|---|
| 89 | (pCCdata == 0x0) ) |
|---|
| 90 | { |
|---|
| 91 | return BERR_INVALID_PARAMETER; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /* Programming note: all function parameters are now validated */ |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | #ifdef BUDP_P_GETUD_DUMP |
|---|
| 98 | dump_getud (pUserdata_info, offset); |
|---|
| 99 | #endif |
|---|
| 100 | |
|---|
| 101 | /* Take care of a special case */ |
|---|
| 102 | userdata = (uint8_t*)(pUserdata_info->pUserDataBuffer) + offset; |
|---|
| 103 | length = pUserdata_info->ui32UserDataBufSize - offset; |
|---|
| 104 | if (length < 4) |
|---|
| 105 | { |
|---|
| 106 | *pBytesParsed = length; |
|---|
| 107 | return BERR_BUDP_NO_DATA; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /* jump past the first SEI userdata start code */ |
|---|
| 111 | bytesParsedSub = FindSeiUserdataStart (userdata, length); |
|---|
| 112 | *pBytesParsed = bytesParsedSub; |
|---|
| 113 | length -= bytesParsedSub; |
|---|
| 114 | userdata += bytesParsedSub; |
|---|
| 115 | |
|---|
| 116 | /* If we did not find a start code, bail out now */ |
|---|
| 117 | if (length == 0) |
|---|
| 118 | { |
|---|
| 119 | return BERR_BUDP_NO_DATA; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | /* We must have at least one type byte and some VLC data */ |
|---|
| 123 | if (length < 2) |
|---|
| 124 | { |
|---|
| 125 | return BERR_BUDP_NO_DATA; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | /* Check the message type byte */ |
|---|
| 129 | seiType = *userdata; |
|---|
| 130 | ++(*pBytesParsed); |
|---|
| 131 | --length; |
|---|
| 132 | if (seiType != 0x07) |
|---|
| 133 | { |
|---|
| 134 | return BERR_BUDP_NO_DATA; |
|---|
| 135 | } |
|---|
| 136 | ++userdata; |
|---|
| 137 | |
|---|
| 138 | /* Initialize the return data */ |
|---|
| 139 | BKNI_Memset ((void*)(p3ddata), 0x0, sizeof(BUDP_SEIparse_3ddata)); |
|---|
| 140 | |
|---|
| 141 | /* Drop into VLC mode */ |
|---|
| 142 | eErr = BERR_BUDP_PARSE_ERROR; |
|---|
| 143 | do |
|---|
| 144 | { |
|---|
| 145 | vlc_index = 0; |
|---|
| 146 | vlc_bit = 7; |
|---|
| 147 | |
|---|
| 148 | vlc_decoded = BVLC_Decode ( |
|---|
| 149 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 150 | if (vlc_decoded == -1) |
|---|
| 151 | break; |
|---|
| 152 | p3ddata->frame_packing_arrangement_id = vlc_decoded; |
|---|
| 153 | |
|---|
| 154 | vlc_decoded = BVLC_Decode ( |
|---|
| 155 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 156 | if (vlc_decoded == -1) |
|---|
| 157 | break; |
|---|
| 158 | p3ddata->frame_packing_arrangement_cancel_flag = (vlc_decoded != 0); |
|---|
| 159 | |
|---|
| 160 | if (!p3ddata->frame_packing_arrangement_cancel_flag) |
|---|
| 161 | { |
|---|
| 162 | vlc_decoded = BVLC_Decode ( |
|---|
| 163 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 164 | if (vlc_decoded == -1) |
|---|
| 165 | break; |
|---|
| 166 | p3ddata->frame_packing_arrangement_type = vlc_decoded; |
|---|
| 167 | |
|---|
| 168 | vlc_decoded = BVLC_Decode ( |
|---|
| 169 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 170 | if (vlc_decoded == -1) |
|---|
| 171 | break; |
|---|
| 172 | p3ddata->quincunx_sampling_flag = (vlc_decoded != 0); |
|---|
| 173 | |
|---|
| 174 | vlc_decoded = BVLC_Decode ( |
|---|
| 175 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 176 | if (vlc_decoded == -1) |
|---|
| 177 | break; |
|---|
| 178 | p3ddata->content_interpretation_type = vlc_decoded; |
|---|
| 179 | |
|---|
| 180 | vlc_decoded = BVLC_Decode ( |
|---|
| 181 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 182 | if (vlc_decoded == -1) |
|---|
| 183 | break; |
|---|
| 184 | p3ddata->spatial_flipping_flag = (vlc_decoded != 0); |
|---|
| 185 | |
|---|
| 186 | vlc_decoded = BVLC_Decode ( |
|---|
| 187 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 188 | if (vlc_decoded == -1) |
|---|
| 189 | break; |
|---|
| 190 | p3ddata->frame0_flipped_flag = (vlc_decoded != 0); |
|---|
| 191 | |
|---|
| 192 | vlc_decoded = BVLC_Decode ( |
|---|
| 193 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 194 | if (vlc_decoded == -1) |
|---|
| 195 | break; |
|---|
| 196 | p3ddata->field_views_flag = (vlc_decoded != 0); |
|---|
| 197 | |
|---|
| 198 | vlc_decoded = BVLC_Decode ( |
|---|
| 199 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 200 | if (vlc_decoded == -1) |
|---|
| 201 | break; |
|---|
| 202 | p3ddata->current_frame_is_frame0_flag = (vlc_decoded != 0); |
|---|
| 203 | |
|---|
| 204 | vlc_decoded = BVLC_Decode ( |
|---|
| 205 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 206 | if (vlc_decoded == -1) |
|---|
| 207 | break; |
|---|
| 208 | p3ddata->frame0_self_contained_flag = (vlc_decoded != 0); |
|---|
| 209 | |
|---|
| 210 | vlc_decoded = BVLC_Decode ( |
|---|
| 211 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 212 | if (vlc_decoded == -1) |
|---|
| 213 | break; |
|---|
| 214 | p3ddata->frame1_self_contained_flag = (vlc_decoded != 0); |
|---|
| 215 | |
|---|
| 216 | if (!(p3ddata->quincunx_sampling_flag) && |
|---|
| 217 | (p3ddata->frame_packing_arrangement_type != 5)) |
|---|
| 218 | { |
|---|
| 219 | vlc_decoded = BVLC_Decode ( |
|---|
| 220 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 221 | if (vlc_decoded == -1) |
|---|
| 222 | break; |
|---|
| 223 | p3ddata->frame0_grid_position_x = vlc_decoded; |
|---|
| 224 | |
|---|
| 225 | vlc_decoded = BVLC_Decode ( |
|---|
| 226 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 227 | if (vlc_decoded == -1) |
|---|
| 228 | break; |
|---|
| 229 | p3ddata->frame0_grid_position_y = vlc_decoded; |
|---|
| 230 | |
|---|
| 231 | vlc_decoded = BVLC_Decode ( |
|---|
| 232 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 233 | if (vlc_decoded == -1) |
|---|
| 234 | break; |
|---|
| 235 | p3ddata->frame1_grid_position_x = vlc_decoded; |
|---|
| 236 | |
|---|
| 237 | vlc_decoded = BVLC_Decode ( |
|---|
| 238 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 239 | if (vlc_decoded == -1) |
|---|
| 240 | break; |
|---|
| 241 | p3ddata->frame1_grid_position_y = vlc_decoded; |
|---|
| 242 | } |
|---|
| 243 | /* Reserved byte */ |
|---|
| 244 | vlc_decoded = BVLC_Decode ( |
|---|
| 245 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 246 | if (vlc_decoded == -1) |
|---|
| 247 | break; |
|---|
| 248 | if (vlc_decoded != 0) |
|---|
| 249 | break; |
|---|
| 250 | |
|---|
| 251 | vlc_decoded = BVLC_Decode ( |
|---|
| 252 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 253 | if (vlc_decoded == -1) |
|---|
| 254 | break; |
|---|
| 255 | p3ddata->frame_packing_arrangement_repetition_period = vlc_decoded; |
|---|
| 256 | } |
|---|
| 257 | vlc_decoded = BVLC_Decode ( |
|---|
| 258 | userdata, length, vlc_index, vlc_bit, &vlc_index, &vlc_bit); |
|---|
| 259 | if (vlc_decoded == -1) |
|---|
| 260 | break; |
|---|
| 261 | p3ddata->frame_packing_arrangement_extension_flag = (vlc_decoded != 0); |
|---|
| 262 | |
|---|
| 263 | /* Signify success */ |
|---|
| 264 | eErr = BERR_SUCCESS; |
|---|
| 265 | } |
|---|
| 266 | while (false); |
|---|
| 267 | |
|---|
| 268 | /* Process results from VLC mode */ |
|---|
| 269 | userdata += vlc_index; |
|---|
| 270 | (*pBytesParsed) += vlc_index; |
|---|
| 271 | length -= vlc_index; |
|---|
| 272 | |
|---|
| 273 | BDBG_LEAVE(BUDP_SEIparse); |
|---|
| 274 | return eErr; |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | /*************************************************************************** |
|---|
| 279 | * This function finds the next userdata startcode 0x00000106. It |
|---|
| 280 | * indicates the byte following this startcode by its return value. |
|---|
| 281 | * If no startcode was found, it simply returns the length of the |
|---|
| 282 | * input data. |
|---|
| 283 | */ |
|---|
| 284 | static size_t FindSeiUserdataStart ( |
|---|
| 285 | const uint8_t* userdata, size_t length) |
|---|
| 286 | { |
|---|
| 287 | size_t count = 0; |
|---|
| 288 | uint8_t saved[4]; |
|---|
| 289 | |
|---|
| 290 | /* Special case (failure) */ |
|---|
| 291 | if (length < 4) |
|---|
| 292 | return length; |
|---|
| 293 | |
|---|
| 294 | /* Initialize */ |
|---|
| 295 | saved[1] = *userdata++ |
|---|
| 296 | saved[2] = *userdata++ |
|---|
| 297 | saved[3] = *userdata++ |
|---|
| 298 | |
|---|
| 299 | while (length >= 4) |
|---|
| 300 | { |
|---|
| 301 | /* Read in another byte */ |
|---|
| 302 | saved[0] = saved[1]; |
|---|
| 303 | saved[1] = saved[2]; |
|---|
| 304 | saved[2] = saved[3]; |
|---|
| 305 | saved[3] = *userdata++ |
|---|
| 306 | |
|---|
| 307 | if ((saved[0] == 0x00) && |
|---|
| 308 | (saved[1] == 0x00) && |
|---|
| 309 | (saved[2] == 0x01) && |
|---|
| 310 | (saved[3] == 0x06) ) |
|---|
| 311 | { |
|---|
| 312 | /* Found it! */ |
|---|
| 313 | break; |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | /* proceed to the next byte */ |
|---|
| 317 | --length; |
|---|
| 318 | ++count; |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | if (length >= 4) |
|---|
| 322 | { |
|---|
| 323 | /* found the pattern before the end of stream */ |
|---|
| 324 | return count + 4; |
|---|
| 325 | } |
|---|
| 326 | else |
|---|
| 327 | { |
|---|
| 328 | /* Didn't find any start code */ |
|---|
| 329 | return count + 3; |
|---|
| 330 | } |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | #ifdef BUDP_P_GETUD_DUMP |
|---|
| 335 | static void dump_getud ( |
|---|
| 336 | const BAVC_USERDATA_info* pUserdata_info, size_t offset) |
|---|
| 337 | { |
|---|
| 338 | unsigned int iByte; |
|---|
| 339 | static FILE* fd = NULL; |
|---|
| 340 | static unsigned int nPicture; |
|---|
| 341 | uint8_t* userdata = (uint8_t*)(pUserdata_info->pUserDataBuffer) + offset; |
|---|
| 342 | size_t length = pUserdata_info->ui32UserDataBufSize - offset; |
|---|
| 343 | |
|---|
| 344 | /* Initialization */ |
|---|
| 345 | if (fd == NULL) |
|---|
| 346 | { |
|---|
| 347 | if ((fd = fopen (BUDP_P_Getud_Filename, "w")) == 0) |
|---|
| 348 | { |
|---|
| 349 | fprintf (stderr, "ERROR: could not open %s for debug output\n", |
|---|
| 350 | BUDP_P_Getud_Filename); |
|---|
| 351 | return; |
|---|
| 352 | } |
|---|
| 353 | fprintf (fd, "getud output format version 1\n"); |
|---|
| 354 | nPicture = 0; |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | fprintf (fd, "\nPic %u LOC %06lx TR %u\n", ++nPicture, 0UL, 0U); |
|---|
| 358 | fprintf (fd, "PS %d TFF %d RFF %d\n", |
|---|
| 359 | pUserdata_info->eSourcePolarity, |
|---|
| 360 | pUserdata_info->bTopFieldFirst, |
|---|
| 361 | pUserdata_info->bRepeatFirstField); |
|---|
| 362 | fprintf (fd, "UDBYTES %u\n", length); |
|---|
| 363 | for (iByte = 0 ; iByte < length ; ++iByte) |
|---|
| 364 | { |
|---|
| 365 | fprintf (fd, "%02x", userdata[iByte]); |
|---|
| 366 | if ((iByte % 16) == 15) |
|---|
| 367 | putc ('\n', fd); |
|---|
| 368 | else |
|---|
| 369 | putc (' ', fd); |
|---|
| 370 | } |
|---|
| 371 | if ((iByte % 16) != 15) |
|---|
| 372 | putc ('\n', fd); |
|---|
| 373 | } |
|---|
| 374 | #endif |
|---|