Diego Biurrun | 3883a99 | 2007-05-04 19:38:10 | [diff] [blame] | 1 | /* |
Vittorio Giovara | 41ed7ab | 2016-04-27 17:45:23 | [diff] [blame] | 2 | * H.261 parser |
Diego Biurrun | 3883a99 | 2007-05-04 19:38:10 | [diff] [blame] | 3 | * Copyright (c) 2002-2004 Michael Niedermayer <[email protected]> |
| 4 | * Copyright (c) 2004 Maarten Daniels |
| 5 | * |
| 6 | * This file is part of FFmpeg. |
| 7 | * |
| 8 | * FFmpeg is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2.1 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * FFmpeg is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with FFmpeg; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | /** |
Diego Biurrun | ba87f08 | 2010-04-20 14:45:34 | [diff] [blame] | 24 | * @file |
Vittorio Giovara | 41ed7ab | 2016-04-27 17:45:23 | [diff] [blame] | 25 | * H.261 parser |
Diego Biurrun | 3883a99 | 2007-05-04 19:38:10 | [diff] [blame] | 26 | */ |
| 27 | |
Diego Biurrun | 3883a99 | 2007-05-04 19:38:10 | [diff] [blame] | 28 | #include "parser.h" |
| 29 | |
Diego Biurrun | b78f81c | 2013-04-04 12:31:58 | [diff] [blame] | 30 | static int h261_find_frame_end(ParseContext *pc, AVCodecContext *avctx, |
| 31 | const uint8_t *buf, int buf_size) |
| 32 | { |
Diego Biurrun | 3883a99 | 2007-05-04 19:38:10 | [diff] [blame] | 33 | int vop_found, i, j; |
| 34 | uint32_t state; |
| 35 | |
Diego Biurrun | b78f81c | 2013-04-04 12:31:58 | [diff] [blame] | 36 | vop_found = pc->frame_start_found; |
| 37 | state = pc->state; |
Diego Biurrun | 3883a99 | 2007-05-04 19:38:10 | [diff] [blame] | 38 | |
Diego Biurrun | b78f81c | 2013-04-04 12:31:58 | [diff] [blame] | 39 | for (i = 0; i < buf_size && !vop_found; i++) { |
| 40 | state = (state << 8) | buf[i]; |
| 41 | for (j = 0; j < 8; j++) { |
| 42 | if (((state >> j) & 0xFFFFF0) == 0x000100) { |
| 43 | vop_found = 1; |
Diego Biurrun | 3883a99 | 2007-05-04 19:38:10 | [diff] [blame] | 44 | break; |
| 45 | } |
| 46 | } |
| 47 | } |
Diego Biurrun | b78f81c | 2013-04-04 12:31:58 | [diff] [blame] | 48 | if (vop_found) { |
| 49 | for (; i < buf_size; i++) { |
| 50 | state = (state << 8) | buf[i]; |
| 51 | for (j = 0; j < 8; j++) { |
| 52 | if (((state >> j) & 0xFFFFF0) == 0x000100) { |
| 53 | pc->frame_start_found = 0; |
| 54 | pc->state = (state >> (3 * 8)) + 0xFF00; |
| 55 | return i - 2; |
Diego Biurrun | 3883a99 | 2007-05-04 19:38:10 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
Diego Biurrun | b78f81c | 2013-04-04 12:31:58 | [diff] [blame] | 61 | pc->frame_start_found = vop_found; |
| 62 | pc->state = state; |
Diego Biurrun | 3883a99 | 2007-05-04 19:38:10 | [diff] [blame] | 63 | return END_NOT_FOUND; |
| 64 | } |
| 65 | |
| 66 | static int h261_parse(AVCodecParserContext *s, |
| 67 | AVCodecContext *avctx, |
Aurelien Jacobs | c53d2d9 | 2007-05-07 00:47:03 | [diff] [blame] | 68 | const uint8_t **poutbuf, int *poutbuf_size, |
Diego Biurrun | 3883a99 | 2007-05-04 19:38:10 | [diff] [blame] | 69 | const uint8_t *buf, int buf_size) |
| 70 | { |
| 71 | ParseContext *pc = s->priv_data; |
| 72 | int next; |
| 73 | |
Reimar Döffinger | 02e3f7d | 2011-04-24 17:17:17 | [diff] [blame] | 74 | if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
| 75 | next = buf_size; |
| 76 | } else { |
Michael Niedermayer | 04b0fd7 | 2013-04-05 19:53:57 | [diff] [blame] | 77 | next = h261_find_frame_end(pc, avctx, buf, buf_size); |
Reimar Döffinger | 9a962f30 | 2011-04-24 22:02:36 | [diff] [blame] | 78 | if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { |
Michael Niedermayer | 04b0fd7 | 2013-04-05 19:53:57 | [diff] [blame] | 79 | *poutbuf = NULL; |
Reimar Döffinger | 9a962f30 | 2011-04-24 22:02:36 | [diff] [blame] | 80 | *poutbuf_size = 0; |
| 81 | return buf_size; |
| 82 | } |
Reimar Döffinger | 02e3f7d | 2011-04-24 17:17:17 | [diff] [blame] | 83 | } |
Diego Biurrun | b78f81c | 2013-04-04 12:31:58 | [diff] [blame] | 84 | *poutbuf = buf; |
Diego Biurrun | 3883a99 | 2007-05-04 19:38:10 | [diff] [blame] | 85 | *poutbuf_size = buf_size; |
| 86 | return next; |
| 87 | } |
| 88 | |
Andreas Rheinhardt | e625ae6 | 2021-03-06 23:20:43 | [diff] [blame] | 89 | const AVCodecParser ff_h261_parser = { |
Anton Khirnov | 36ef536 | 2012-08-05 09:11:04 | [diff] [blame] | 90 | .codec_ids = { AV_CODEC_ID_H261 }, |
Anton Khirnov | 5511ad1 | 2011-11-02 08:34:41 | [diff] [blame] | 91 | .priv_data_size = sizeof(ParseContext), |
| 92 | .parser_parse = h261_parse, |
| 93 | .parser_close = ff_parse_close, |
Diego Biurrun | 3883a99 | 2007-05-04 19:38:10 | [diff] [blame] | 94 | }; |