|
| 1 | +/** |
| 2 | + ****************************************************************************** |
| 3 | + @file JPEG/JPEG_MJPEG_VideoDecodingFromQSPI/CM7/Src/AVI_parser.c |
| 4 | + @author MCD Application Team |
| 5 | + @brief This file provides routines for AVI parsing. |
| 6 | + ****************************************************************************** |
| 7 | + @attention |
| 8 | +
|
| 9 | + <h2><center>© Copyright (c) 2019 STMicroelectronics. |
| 10 | + All rights reserved.</center></h2> |
| 11 | +
|
| 12 | + This software component is licensed by ST under BSD 3-Clause license, |
| 13 | + the "License"; You may not use this file except in compliance with the |
| 14 | + License. You may obtain a copy of the License at: |
| 15 | + opensource.org/licenses/BSD-3-Clause |
| 16 | +
|
| 17 | + ****************************************************************************** |
| 18 | +*/ |
| 19 | + |
| 20 | +/* Includes ------------------------------------------------------------------*/ |
| 21 | + |
| 22 | +#include "AVI_parser.h" |
| 23 | + |
| 24 | + |
| 25 | +/* Exported types ------------------------------------------------------------*/ |
| 26 | +/* Exported variables --------------------------------------------------------*/ |
| 27 | +/* Exported constants --------------------------------------------------------*/ |
| 28 | +uint8_t* const AVI_VIDS_FLAG_TBL[2] = {(uint8_t*)"00dc", (uint8_t*)"01dc"}; |
| 29 | +uint8_t* const AVI_AUDS_FLAG_TBL[2] = {(uint8_t*)"00wb", (uint8_t*)"01wb"}; |
| 30 | + |
| 31 | +uint32_t FrameIndex = 0; |
| 32 | + |
| 33 | +#define AVI_BUFFER_SIZE ((uint32_t)0xFFFF) |
| 34 | + |
| 35 | + |
| 36 | +/* Private functions ---------------------------------------------------------*/ |
| 37 | +AVISTATUS __AVI_Init( AVI_CONTEXT * pavi, uint8_t *buf, uint32_t size); |
| 38 | +uint32_t __AVI_SearchID(uint8_t* buf, uint32_t size, uint8_t *id); |
| 39 | +AVISTATUS __AVI_GetStreamInfo( AVI_CONTEXT * pavi, uint8_t* buf); |
| 40 | + |
| 41 | + |
| 42 | +/** |
| 43 | + @brief Search AVI ID |
| 44 | + @param buf: |
| 45 | + @param size: |
| 46 | + @param id: |
| 47 | + @retval ID |
| 48 | +*/ |
| 49 | +uint32_t __AVI_SearchID(uint8_t* buf, uint32_t size, uint8_t *id) |
| 50 | +{ |
| 51 | + uint16_t i; |
| 52 | + size -= 4; |
| 53 | + for (i = 0; i < size; i++) |
| 54 | + { |
| 55 | + if (buf[i] == id[0]) |
| 56 | + if (buf[i + 1] == id[1]) |
| 57 | + if (buf[i + 2] == id[2]) |
| 58 | + if (buf[i + 3] == id[3]) |
| 59 | + return i; |
| 60 | + } |
| 61 | + return 0; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + @brief Get AVI file information |
| 66 | + @param havi: AVI handle |
| 67 | + @param buf: |
| 68 | + @retval AVI status |
| 69 | +*/ |
| 70 | +AVISTATUS __AVI_GetStreamInfo( AVI_CONTEXT * pavi, uint8_t* buf) |
| 71 | +{ |
| 72 | + |
| 73 | + pavi->aviInfo.StreamID = AVI_MAKEWORD (buf + 2); |
| 74 | + pavi->aviInfo.StreamSize = AVI_MAKEDWORD (buf + 4); |
| 75 | + |
| 76 | + if (pavi->aviInfo.StreamSize % 2) |
| 77 | + pavi->aviInfo.StreamSize++; |
| 78 | + |
| 79 | + if ((pavi->aviInfo.StreamID == AVI_VIDS_FLAG) || (pavi->aviInfo.StreamID == AVI_AUDS_FLAG)) |
| 80 | + { |
| 81 | + return AVI_OK; |
| 82 | + } |
| 83 | + |
| 84 | + return AVI_STREAM_ERR; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + @brief Initialize AVI |
| 89 | + @param havi: AVI context |
| 90 | + @param buf: |
| 91 | + @param size: AVI file size |
| 92 | + @retval AVI status |
| 93 | +*/ |
| 94 | +AVISTATUS __AVI_Init( AVI_CONTEXT * pavi, uint8_t *buf, uint32_t size) |
| 95 | +{ |
| 96 | + uint16_t offset; |
| 97 | + uint8_t *tbuf; |
| 98 | + AVISTATUS res = AVI_OK; |
| 99 | + AVI_HEADER *aviheader; |
| 100 | + LIST_HEADER *listheader; |
| 101 | + AVIH_HEADER *avihheader; |
| 102 | + STRH_HEADER *strhheader; |
| 103 | + |
| 104 | + STRF_BMPHEADER *bmpheader; |
| 105 | + STRF_WAVHEADER *wavheader; |
| 106 | + |
| 107 | + tbuf = buf; |
| 108 | + aviheader = (AVI_HEADER*)buf; |
| 109 | + if (aviheader->RiffID != AVI_RIFF_ID) |
| 110 | + { |
| 111 | + return AVI_RIFF_ERR; |
| 112 | + } |
| 113 | + pavi->FileSize = aviheader->FileSize; |
| 114 | + |
| 115 | + if (aviheader->AviID != AVI_AVI_ID) |
| 116 | + { |
| 117 | + return AVI_AVI_ERR; |
| 118 | + } |
| 119 | + |
| 120 | + buf += sizeof(AVI_HEADER); |
| 121 | + listheader = (LIST_HEADER*)(buf); |
| 122 | + |
| 123 | + if (listheader->ListID != AVI_LIST_ID) |
| 124 | + { |
| 125 | + return AVI_LIST_ERR; |
| 126 | + } |
| 127 | + |
| 128 | + if (listheader->ListType != AVI_HDRL_ID) |
| 129 | + { |
| 130 | + return AVI_HDRL_ERR; |
| 131 | + } |
| 132 | + |
| 133 | + buf += sizeof(LIST_HEADER); |
| 134 | + avihheader = (AVIH_HEADER*)(buf); |
| 135 | + if (avihheader->BlockID != AVI_AVIH_ID) |
| 136 | + { |
| 137 | + return AVI_AVIH_ERR; |
| 138 | + } |
| 139 | + |
| 140 | + pavi->aviInfo.SecPerFrame = avihheader->SecPerFrame; |
| 141 | + pavi->aviInfo.TotalFrame = avihheader->TotalFrame; |
| 142 | + buf += avihheader->BlockSize + 8; |
| 143 | + listheader = (LIST_HEADER*)(buf); |
| 144 | + |
| 145 | + if (listheader->ListID != AVI_LIST_ID) |
| 146 | + { |
| 147 | + return AVI_LIST_ERR; |
| 148 | + } |
| 149 | + |
| 150 | + if (listheader->ListType != AVI_STRL_ID) |
| 151 | + { |
| 152 | + return AVI_STRL_ERR; |
| 153 | + } |
| 154 | + |
| 155 | + strhheader = (STRH_HEADER*)(buf + 12); |
| 156 | + if (strhheader->BlockID != AVI_STRH_ID) |
| 157 | + { |
| 158 | + return AVI_STRH_ERR; |
| 159 | + } |
| 160 | + |
| 161 | + if (strhheader->StreamType == AVI_VIDS_STREAM) |
| 162 | + { |
| 163 | + if (strhheader->Handler != AVI_FORMAT_MJPG) |
| 164 | + { |
| 165 | + printf("strhheader->Handler: %x %d\n", strhheader->Handler, strhheader->Handler); |
| 166 | + return AVI_FORMAT_ERR; |
| 167 | + } |
| 168 | + |
| 169 | + pavi->aviInfo.VideoFLAG = (uint8_t*)AVI_VIDS_FLAG_TBL[0]; |
| 170 | + pavi->aviInfo.AudioFLAG = (uint8_t*)AVI_AUDS_FLAG_TBL[1]; |
| 171 | + bmpheader = (STRF_BMPHEADER*)(buf + 12 + strhheader->BlockSize + 8); |
| 172 | + if (bmpheader->BlockID != AVI_STRF_ID) |
| 173 | + { |
| 174 | + return AVI_STRF_ERR; |
| 175 | + } |
| 176 | + |
| 177 | + pavi->aviInfo.Width = bmpheader->bmiHeader.Width; |
| 178 | + pavi->aviInfo.Height = bmpheader->bmiHeader.Height; |
| 179 | + buf += listheader->BlockSize + 8; |
| 180 | + listheader = (LIST_HEADER*)(buf); |
| 181 | + if (listheader->ListID != AVI_LIST_ID) |
| 182 | + { |
| 183 | + pavi->aviInfo.SampleRate = 0; |
| 184 | + pavi->aviInfo.Channels = 0; |
| 185 | + pavi->aviInfo.AudioType = 0; |
| 186 | + |
| 187 | + } else |
| 188 | + { |
| 189 | + if (listheader->ListType != AVI_STRL_ID) |
| 190 | + { |
| 191 | + return AVI_STRL_ERR; |
| 192 | + } |
| 193 | + |
| 194 | + strhheader = (STRH_HEADER*)(buf + 12); |
| 195 | + if (strhheader->BlockID != AVI_STRH_ID) |
| 196 | + { |
| 197 | + return AVI_STRH_ERR; |
| 198 | + } |
| 199 | + |
| 200 | + if (strhheader->StreamType != AVI_AUDS_STREAM) |
| 201 | + { |
| 202 | + return AVI_FORMAT_ERR; |
| 203 | + } |
| 204 | + |
| 205 | + wavheader = (STRF_WAVHEADER*)(buf + 12 + strhheader->BlockSize + 8); |
| 206 | + if (wavheader->BlockID != AVI_STRF_ID) |
| 207 | + { |
| 208 | + return AVI_STRF_ERR; |
| 209 | + } |
| 210 | + |
| 211 | + pavi->aviInfo.SampleRate = wavheader->SampleRate; |
| 212 | + pavi->aviInfo.Channels = wavheader->Channels; |
| 213 | + pavi->aviInfo.AudioType = wavheader->FormatTag; |
| 214 | + } |
| 215 | + } else if (strhheader->StreamType == AVI_AUDS_STREAM) |
| 216 | + { |
| 217 | + pavi->aviInfo.VideoFLAG = (uint8_t*)AVI_VIDS_FLAG_TBL[1]; |
| 218 | + pavi->aviInfo.AudioFLAG = (uint8_t*)AVI_AUDS_FLAG_TBL[0]; |
| 219 | + wavheader = (STRF_WAVHEADER*)(buf + 12 + strhheader->BlockSize + 8); |
| 220 | + if (wavheader->BlockID != AVI_STRF_ID) |
| 221 | + { |
| 222 | + return AVI_STRF_ERR; |
| 223 | + } |
| 224 | + |
| 225 | + pavi->aviInfo.SampleRate = wavheader->SampleRate; |
| 226 | + pavi->aviInfo.Channels = wavheader->Channels; |
| 227 | + pavi->aviInfo.AudioType = wavheader->FormatTag; |
| 228 | + buf += listheader->BlockSize + 8; |
| 229 | + listheader = (LIST_HEADER*)(buf); |
| 230 | + if (listheader->ListID != AVI_LIST_ID) |
| 231 | + { |
| 232 | + return AVI_LIST_ERR; |
| 233 | + } |
| 234 | + |
| 235 | + if (listheader->ListType != AVI_STRL_ID) |
| 236 | + { |
| 237 | + return AVI_STRL_ERR; |
| 238 | + } |
| 239 | + |
| 240 | + strhheader = (STRH_HEADER*)(buf + 12); |
| 241 | + if (strhheader->BlockID != AVI_STRH_ID) |
| 242 | + { |
| 243 | + return AVI_STRH_ERR; |
| 244 | + } |
| 245 | + |
| 246 | + if (strhheader->StreamType != AVI_VIDS_STREAM) |
| 247 | + { |
| 248 | + return AVI_FORMAT_ERR; |
| 249 | + } |
| 250 | + |
| 251 | + bmpheader = (STRF_BMPHEADER*)(buf + 12 + strhheader->BlockSize + 8); |
| 252 | + if (bmpheader->BlockID != AVI_STRF_ID) |
| 253 | + { |
| 254 | + return AVI_STRF_ERR; |
| 255 | + } |
| 256 | + |
| 257 | + if (bmpheader->bmiHeader.Compression != AVI_FORMAT_MJPG) |
| 258 | + { |
| 259 | + return AVI_FORMAT_ERR; |
| 260 | + } |
| 261 | + |
| 262 | + pavi->aviInfo.Width = bmpheader->bmiHeader.Width; |
| 263 | + pavi->aviInfo.Height = bmpheader->bmiHeader.Height; |
| 264 | + } |
| 265 | + offset = __AVI_SearchID(tbuf, size, (uint8_t*)"movi"); |
| 266 | + if (offset == 0) |
| 267 | + { |
| 268 | + return AVI_MOVI_ERR; |
| 269 | + } |
| 270 | + |
| 271 | + if (pavi->aviInfo.SampleRate) |
| 272 | + { |
| 273 | + tbuf += offset; |
| 274 | + offset = __AVI_SearchID(tbuf, size, pavi->aviInfo.AudioFLAG); |
| 275 | + if (offset == 0) |
| 276 | + { |
| 277 | + return AVI_STREAM_ERR; |
| 278 | + } |
| 279 | + |
| 280 | + tbuf += offset + 4; |
| 281 | + pavi->aviInfo.AudioBufSize = *((uint16_t*)tbuf); |
| 282 | + } |
| 283 | + |
| 284 | + return res; |
| 285 | +} |
| 286 | + |
| 287 | + |
| 288 | +/** |
| 289 | + @brief parse AVI file header |
| 290 | + @param pavi: AVI context |
| 291 | + @param AVI_FileAddress: AVI file address |
| 292 | + @retval AVI status (0 : no Error, 1: Error occured) |
| 293 | +*/ |
| 294 | +uint32_t AVI_ParserInit(AVI_CONTEXT * pavi, uint32_t AVI_FileAddress) |
| 295 | +{ |
| 296 | + pavi->AVIFileAddress = AVI_FileAddress; |
| 297 | + |
| 298 | + pavi->pVideoBuffer = (uint8_t *)pavi->AVIFileAddress; |
| 299 | + pavi->pAudioBuffer = (uint8_t *)pavi->AVIFileAddress; |
| 300 | + |
| 301 | + int ret; |
| 302 | + if ((ret = __AVI_Init(pavi, pavi->pVideoBuffer, (uint16_t)AVI_BUFFER_SIZE)) != AVI_OK) |
| 303 | + { |
| 304 | + printf("AVI init error: %d\n", ret); |
| 305 | + return 1; |
| 306 | + } |
| 307 | + |
| 308 | + pavi->CurrentImage = 0; |
| 309 | + pavi->Offset = 0; |
| 310 | + |
| 311 | + return 0; |
| 312 | +} |
| 313 | + |
| 314 | +/** |
| 315 | + @brief Get AVI frame |
| 316 | + @param pavi: AVI context |
| 317 | + @param file: AVI file |
| 318 | + @retval type of frame (audio frame or video frame ) |
| 319 | +*/ |
| 320 | +uint32_t AVI_GetFrame(AVI_CONTEXT * pavi) |
| 321 | +{ |
| 322 | + if (pavi->CurrentImage == 0 ) |
| 323 | + { |
| 324 | + pavi->pVideoBuffer = (uint8_t *)pavi->AVIFileAddress; |
| 325 | + |
| 326 | + /* Check for "movi" tag */ |
| 327 | + pavi->Offset = __AVI_SearchID(pavi->pVideoBuffer, (uint16_t)AVI_BUFFER_SIZE, (uint8_t*)"movi"); |
| 328 | + /* Get first Frame info*/ |
| 329 | + __AVI_GetStreamInfo(pavi, pavi->pVideoBuffer + pavi->Offset + 4); |
| 330 | + /* go to the first frame offset in the avi file*/ |
| 331 | + pavi->Offset += 12; |
| 332 | + } |
| 333 | + |
| 334 | + /* Get the current frame size */ |
| 335 | + pavi->FrameSize = pavi->aviInfo.StreamSize; |
| 336 | + |
| 337 | + if (pavi->aviInfo.StreamID == AVI_VIDS_FLAG) |
| 338 | + { |
| 339 | + /* the Frame is a Video Frame */ |
| 340 | + |
| 341 | + /* Go to The current frame */ |
| 342 | + pavi->pVideoBuffer = ((uint8_t *)pavi->AVIFileAddress) + pavi->Offset; |
| 343 | + /* Increment the offset with the current frame size + the header of the next frame (8 bytes)*/ |
| 344 | + pavi->Offset += pavi->FrameSize + 8; |
| 345 | + |
| 346 | + /* Get the info of the next frame */ |
| 347 | + __AVI_GetStreamInfo(pavi, pavi->pVideoBuffer + pavi->aviInfo.StreamSize ); |
| 348 | + /* Return VIDEO frame */ |
| 349 | + return AVI_VIDEO_FRAME; |
| 350 | + } |
| 351 | + if (pavi->aviInfo.StreamID == AVI_AUDS_FLAG) |
| 352 | + { /* the Frame is an Audio Frame */ |
| 353 | + |
| 354 | + /* Go to The current frame */ |
| 355 | + pavi->pAudioBuffer = ((uint8_t *)pavi->AVIFileAddress) + pavi->Offset; |
| 356 | + /* Increment the offset with the current frame size + the header of the next frame (8 bytes)*/ |
| 357 | + pavi->Offset += pavi->FrameSize + 8; |
| 358 | + |
| 359 | + /* Get the info of the next frame */ |
| 360 | + __AVI_GetStreamInfo(pavi, pavi->pAudioBuffer + pavi->aviInfo.StreamSize ); |
| 361 | + /* Return VIDEO frame */ |
| 362 | + return AVI_AUDIO_FRAME; |
| 363 | + |
| 364 | + } |
| 365 | + |
| 366 | + return AVI_END_FILE; |
| 367 | +} |
| 368 | + |
| 369 | + |
| 370 | + |
| 371 | + |
| 372 | + |
| 373 | +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
0 commit comments