Coverage Report

Created: 2025-08-28 07:12

/src/ffmpeg/libavcodec/m101.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016 Michael Niedermayer
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#include "libavutil/intreadwrite.h"
22
23
#include "avcodec.h"
24
#include "codec_internal.h"
25
#include "decode.h"
26
27
28
static av_cold int m101_decode_init(AVCodecContext *avctx)
29
630
{
30
630
    if (avctx->extradata_size < 6*4) {
31
183
        avpriv_request_sample(avctx, "Missing or too small extradata (size %d)", avctx->extradata_size);
32
183
        return AVERROR_INVALIDDATA;
33
183
    }
34
35
447
    if (avctx->extradata[2*4] == 10)
36
140
        avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
37
307
    else if (avctx->extradata[2*4] == 8) {
38
279
        avctx->pix_fmt = AV_PIX_FMT_YUYV422;
39
279
    } else {
40
28
        avpriv_request_sample(avctx, "BPS %d", avctx->extradata[2*4]);
41
28
        return AVERROR_INVALIDDATA;
42
28
    }
43
44
419
    return 0;
45
447
}
46
47
static int m101_decode_frame(AVCodecContext *avctx, AVFrame *frame,
48
                             int *got_frame, AVPacket *avpkt)
49
365k
{
50
365k
    const uint8_t *buf = avpkt->data;
51
365k
    int stride, ret;
52
365k
    int x, y;
53
365k
    int min_stride = 2 * avctx->width;
54
365k
    int bits = avctx->extradata[2*4];
55
56
365k
    stride = AV_RL32(avctx->extradata + 5*4);
57
58
365k
    if (avctx->pix_fmt == AV_PIX_FMT_YUV422P10)
59
63.4k
        min_stride = (avctx->width + 15) / 16 * 40;
60
61
365k
    if (stride < min_stride || avpkt->size < stride * (uint64_t)avctx->height) {
62
255k
        av_log(avctx, AV_LOG_ERROR, "stride (%d) is invalid for packet sized %d\n",
63
255k
               stride, avpkt->size);
64
255k
        return AVERROR_INVALIDDATA;
65
255k
    }
66
67
109k
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
68
4.19k
        return ret;
69
105k
    if ((avctx->extradata[3*4] & 3) != 3) {
70
6.58k
        frame->flags |= AV_FRAME_FLAG_INTERLACED;
71
6.58k
        if (avctx->extradata[3*4] & 1)
72
5.38k
            frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
73
6.58k
    }
74
75
1.34M
    for (y = 0; y < avctx->height; y++) {
76
1.23M
        int src_y = y;
77
1.23M
        if (frame->flags & AV_FRAME_FLAG_INTERLACED)
78
1.03M
            src_y = ((y&1) ^ !!(frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST)) ? y/2 : (y/2 + avctx->height/2);
79
1.23M
        if (bits == 8) {
80
1.23M
            uint8_t *line = frame->data[0] + y*frame->linesize[0];
81
1.23M
            memcpy(line, buf + src_y*stride, 2*avctx->width);
82
1.23M
        } else {
83
971
            int block;
84
971
            uint16_t *luma = (uint16_t*)&frame->data[0][y*frame->linesize[0]];
85
971
            uint16_t *cb   = (uint16_t*)&frame->data[1][y*frame->linesize[1]];
86
971
            uint16_t *cr   = (uint16_t*)&frame->data[2][y*frame->linesize[2]];
87
19.2k
            for (block = 0; 16*block < avctx->width; block ++) {
88
18.2k
                const uint8_t *buf_src = buf + src_y*stride + 40*block;
89
304k
                for (x = 0; x < 16 && x + 16*block < avctx->width; x++) {
90
286k
                    int xd = x + 16*block;
91
286k
                    if (x&1) {
92
142k
                        luma [xd] = (4*buf_src[2*x + 0]) + ((buf_src[32 + (x>>1)]>>4)&3);
93
143k
                    } else {
94
143k
                        luma [xd] = (4*buf_src[2*x + 0]) +  (buf_src[32 + (x>>1)]    &3);
95
143k
                        cb[xd>>1] = (4*buf_src[2*x + 1]) + ((buf_src[32 + (x>>1)]>>2)&3);
96
143k
                        cr[xd>>1] = (4*buf_src[2*x + 3]) +  (buf_src[32 + (x>>1)]>>6);
97
143k
                    }
98
286k
                }
99
18.2k
            }
100
971
        }
101
1.23M
    }
102
103
105k
    *got_frame = 1;
104
105k
    return avpkt->size;
105
109k
}
106
107
const FFCodec ff_m101_decoder = {
108
    .p.name         = "m101",
109
    CODEC_LONG_NAME("Matrox Uncompressed SD"),
110
    .p.type         = AVMEDIA_TYPE_VIDEO,
111
    .p.id           = AV_CODEC_ID_M101,
112
    .init           = m101_decode_init,
113
    FF_CODEC_DECODE_CB(m101_decode_frame),
114
    .p.capabilities = AV_CODEC_CAP_DR1,
115
};