annotate ffmpeg/libavcodec/png_parser.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * PNG parser
yading@10 3 * Copyright (c) 2009 Peter Holik
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 /**
yading@10 23 * @file
yading@10 24 * PNG parser
yading@10 25 */
yading@10 26
yading@10 27 #include "parser.h"
yading@10 28 #include "png.h"
yading@10 29
yading@10 30 typedef struct PNGParseContext
yading@10 31 {
yading@10 32 ParseContext pc;
yading@10 33 uint32_t index;
yading@10 34 uint32_t chunk_length;
yading@10 35 uint32_t remaining_size;
yading@10 36 } PNGParseContext;
yading@10 37
yading@10 38 static int png_parse(AVCodecParserContext *s, AVCodecContext *avctx,
yading@10 39 const uint8_t **poutbuf, int *poutbuf_size,
yading@10 40 const uint8_t *buf, int buf_size)
yading@10 41 {
yading@10 42 PNGParseContext *ppc = s->priv_data;
yading@10 43 int next = END_NOT_FOUND;
yading@10 44 int i = 0;
yading@10 45
yading@10 46 s->pict_type = AV_PICTURE_TYPE_NONE;
yading@10 47
yading@10 48 *poutbuf_size = 0;
yading@10 49 if (buf_size == 0)
yading@10 50 return 0;
yading@10 51
yading@10 52 if (!ppc->pc.frame_start_found) {
yading@10 53 uint64_t state64 = ppc->pc.state64;
yading@10 54 for (; i < buf_size; i++) {
yading@10 55 state64 = (state64 << 8) | buf[i];
yading@10 56 if (state64 == PNGSIG || state64 == MNGSIG) {
yading@10 57 i++;
yading@10 58 ppc->pc.frame_start_found = 1;
yading@10 59 break;
yading@10 60 }
yading@10 61 }
yading@10 62 ppc->pc.state64 = state64;
yading@10 63 } else
yading@10 64 if (ppc->remaining_size) {
yading@10 65 i = FFMIN(ppc->remaining_size, buf_size);
yading@10 66 ppc->remaining_size -= i;
yading@10 67 if (ppc->remaining_size)
yading@10 68 goto flush;
yading@10 69 if (ppc->index == -1) {
yading@10 70 next = i;
yading@10 71 goto flush;
yading@10 72 }
yading@10 73 }
yading@10 74
yading@10 75 for (;ppc->pc.frame_start_found && i < buf_size; i++) {
yading@10 76 ppc->pc.state = (ppc->pc.state<<8) | buf[i];
yading@10 77 if (ppc->index == 3) {
yading@10 78 ppc->chunk_length = ppc->pc.state;
yading@10 79 if (ppc->chunk_length > 0x7fffffff) {
yading@10 80 ppc->index = ppc->pc.frame_start_found = 0;
yading@10 81 goto flush;
yading@10 82 }
yading@10 83 ppc->chunk_length += 4;
yading@10 84 } else if (ppc->index == 7) {
yading@10 85 if (ppc->chunk_length >= buf_size - i)
yading@10 86 ppc->remaining_size = ppc->chunk_length - buf_size + i + 1;
yading@10 87 if (ppc->pc.state == MKBETAG('I', 'E', 'N', 'D')) {
yading@10 88 if (ppc->remaining_size)
yading@10 89 ppc->index = -1;
yading@10 90 else
yading@10 91 next = ppc->chunk_length + i + 1;
yading@10 92 break;
yading@10 93 } else {
yading@10 94 ppc->index = 0;
yading@10 95 if (ppc->remaining_size)
yading@10 96 break;
yading@10 97 else
yading@10 98 i += ppc->chunk_length;
yading@10 99 continue;
yading@10 100 }
yading@10 101 }
yading@10 102 ppc->index++;
yading@10 103 }
yading@10 104 flush:
yading@10 105 if (ff_combine_frame(&ppc->pc, next, &buf, &buf_size) < 0)
yading@10 106 return buf_size;
yading@10 107
yading@10 108 ppc->index = ppc->pc.frame_start_found = 0;
yading@10 109
yading@10 110 *poutbuf = buf;
yading@10 111 *poutbuf_size = buf_size;
yading@10 112 return next;
yading@10 113 }
yading@10 114
yading@10 115 AVCodecParser ff_png_parser = {
yading@10 116 .codec_ids = { AV_CODEC_ID_PNG },
yading@10 117 .priv_data_size = sizeof(PNGParseContext),
yading@10 118 .parser_parse = png_parse,
yading@10 119 .parser_close = ff_parse_close,
yading@10 120 };