GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/dvdsub_parser.c Lines: 31 36 86.1 %
Date: 2019-11-18 18:00:01 Branches: 13 20 65.0 %

Line Branch Exec Source
1
/*
2
 * DVD subtitle decoding
3
 * Copyright (c) 2005 Fabrice Bellard
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
#include <string.h>
23
24
#include "libavutil/intreadwrite.h"
25
#include "libavutil/mem.h"
26
#include "avcodec.h"
27
28
/* parser definition */
29
typedef struct DVDSubParseContext {
30
    uint8_t *packet;
31
    int packet_len;
32
    int packet_index;
33
} DVDSubParseContext;
34
35
6
static av_cold int dvdsub_parse_init(AVCodecParserContext *s)
36
{
37
6
    return 0;
38
}
39
40
12
static int dvdsub_parse(AVCodecParserContext *s,
41
                        AVCodecContext *avctx,
42
                        const uint8_t **poutbuf, int *poutbuf_size,
43
                        const uint8_t *buf, int buf_size)
44
{
45
12
    DVDSubParseContext *pc = s->priv_data;
46
47
12
    *poutbuf      = buf;
48
12
    *poutbuf_size = buf_size;
49
50
12
    if (pc->packet_index == 0) {
51

4
        if (buf_size < 2 || AV_RB16(buf) && buf_size < 6) {
52
2
            if (buf_size)
53
                av_log(avctx, AV_LOG_DEBUG, "Parser input %d too small\n", buf_size);
54
2
            return buf_size;
55
        }
56
2
        pc->packet_len = AV_RB16(buf);
57
2
        if (pc->packet_len == 0) /* HD-DVD subpicture packet */
58
            pc->packet_len = AV_RB32(buf+2);
59
2
        av_freep(&pc->packet);
60
2
        if ((unsigned)pc->packet_len > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) {
61
            av_log(avctx, AV_LOG_ERROR, "packet length %d is invalid\n", pc->packet_len);
62
            return buf_size;
63
        }
64
2
        pc->packet = av_malloc(pc->packet_len + AV_INPUT_BUFFER_PADDING_SIZE);
65
    }
66
10
    if (pc->packet) {
67
10
        if (pc->packet_index + buf_size <= pc->packet_len) {
68
10
            memcpy(pc->packet + pc->packet_index, buf, buf_size);
69
10
            pc->packet_index += buf_size;
70
10
            if (pc->packet_index >= pc->packet_len) {
71
2
                *poutbuf = pc->packet;
72
2
                *poutbuf_size = pc->packet_len;
73
2
                pc->packet_index = 0;
74
2
                return buf_size;
75
            }
76
        } else {
77
            /* erroneous size */
78
            pc->packet_index = 0;
79
        }
80
    }
81
8
    *poutbuf = NULL;
82
8
    *poutbuf_size = 0;
83
8
    return buf_size;
84
}
85
86
6
static av_cold void dvdsub_parse_close(AVCodecParserContext *s)
87
{
88
6
    DVDSubParseContext *pc = s->priv_data;
89
6
    av_freep(&pc->packet);
90
6
}
91
92
AVCodecParser ff_dvdsub_parser = {
93
    .codec_ids      = { AV_CODEC_ID_DVD_SUBTITLE },
94
    .priv_data_size = sizeof(DVDSubParseContext),
95
    .parser_init    = dvdsub_parse_init,
96
    .parser_parse   = dvdsub_parse,
97
    .parser_close   = dvdsub_parse_close,
98
};