/src/ffmpeg/libavcodec/bmp_parser.c
Line | Count | Source |
1 | | /* |
2 | | * BMP parser |
3 | | * Copyright (c) 2012 Paul B Mahol |
4 | | * |
5 | | * This file is part of FFmpeg. |
6 | | * |
7 | | * FFmpeg is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * FFmpeg is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with FFmpeg; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | |
22 | | /** |
23 | | * @file |
24 | | * BMP parser |
25 | | */ |
26 | | |
27 | | #include "libavutil/bswap.h" |
28 | | #include "libavutil/common.h" |
29 | | |
30 | | #include "parser.h" |
31 | | #include "parser_internal.h" |
32 | | |
33 | | typedef struct BMPParseContext { |
34 | | ParseContext pc; |
35 | | uint32_t fsize; |
36 | | uint32_t remaining_size; |
37 | | } BMPParseContext; |
38 | | |
39 | | static int bmp_parse(AVCodecParserContext *s, AVCodecContext *avctx, |
40 | | const uint8_t **poutbuf, int *poutbuf_size, |
41 | | const uint8_t *buf, int buf_size) |
42 | 340k | { |
43 | 340k | BMPParseContext *bpc = s->priv_data; |
44 | 340k | uint64_t state = bpc->pc.state64; |
45 | 340k | int next = END_NOT_FOUND; |
46 | 340k | int i = 0; |
47 | | |
48 | 340k | *poutbuf_size = 0; |
49 | 340k | *poutbuf = NULL; |
50 | | |
51 | 872k | restart: |
52 | 872k | if (bpc->pc.frame_start_found <= 2+4+4) { |
53 | 44.2M | for (; i < buf_size; i++) { |
54 | 44.2M | state = (state << 8) | buf[i]; |
55 | 44.2M | if (bpc->pc.frame_start_found == 0) { |
56 | 38.6M | if ((state >> 48) == (('B' << 8) | 'M')) { |
57 | 563k | bpc->fsize = av_bswap32(state >> 16); |
58 | 563k | if (bpc->fsize > 17) |
59 | 560k | bpc->pc.frame_start_found = 1; |
60 | 563k | } |
61 | 38.6M | } else if (bpc->pc.frame_start_found == 2+4+4) { |
62 | | // unsigned hsize = av_bswap32(state>>32); |
63 | 560k | unsigned ihsize = av_bswap32(state); |
64 | 560k | if (ihsize < 12 || ihsize > 200) { |
65 | 27.2k | bpc->pc.frame_start_found = 0; |
66 | 27.2k | continue; |
67 | 27.2k | } |
68 | 533k | bpc->pc.frame_start_found++; |
69 | 533k | bpc->remaining_size = bpc->fsize + i - 17; |
70 | | |
71 | 533k | if (bpc->pc.index + i > 17) { |
72 | 267k | next = i - 17; |
73 | 267k | state = 0; |
74 | 267k | break; |
75 | 267k | } else { |
76 | 266k | bpc->pc.state64 = 0; |
77 | 266k | goto restart; |
78 | 266k | } |
79 | 5.04M | } else if (bpc->pc.frame_start_found) |
80 | 5.04M | bpc->pc.frame_start_found++; |
81 | 44.2M | } |
82 | 332k | bpc->pc.state64 = state; |
83 | 332k | } else { |
84 | 273k | if (bpc->remaining_size) { |
85 | 273k | i = FFMIN(bpc->remaining_size, buf_size); |
86 | 273k | bpc->remaining_size -= i; |
87 | 273k | if (bpc->remaining_size) |
88 | 7.77k | goto flush; |
89 | | |
90 | 265k | bpc->pc.frame_start_found = 0; |
91 | 265k | goto restart; |
92 | 273k | } |
93 | 273k | } |
94 | | |
95 | 340k | flush: |
96 | 340k | if (ff_combine_frame(&bpc->pc, next, &buf, &buf_size) < 0) |
97 | 73.0k | return buf_size; |
98 | | |
99 | 267k | if (next != END_NOT_FOUND && next < 0) |
100 | 975 | bpc->pc.frame_start_found = FFMAX(bpc->pc.frame_start_found - i - 1, 0); |
101 | 266k | else |
102 | 266k | bpc->pc.frame_start_found = 0; |
103 | | |
104 | 267k | *poutbuf = buf; |
105 | 267k | *poutbuf_size = buf_size; |
106 | 267k | return next; |
107 | 340k | } |
108 | | |
109 | | const FFCodecParser ff_bmp_parser = { |
110 | | PARSER_CODEC_LIST(AV_CODEC_ID_BMP), |
111 | | .priv_data_size = sizeof(BMPParseContext), |
112 | | .parse = bmp_parse, |
113 | | .close = ff_parse_close, |
114 | | }; |