1 /*
2  * Raw video utils
3  * Copyright (c) 2016 Michael Niedermayer
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 "libavutil/intreadwrite.h"
23 #include "libavcodec/packet.h"
24 #include "avformat.h"
25 #include "rawutils.h"
26 
ff_reshuffle_raw_rgb(AVFormatContext *s, AVPacket **ppkt, AVCodecParameters *par, int expected_stride)27 int ff_reshuffle_raw_rgb(AVFormatContext *s, AVPacket **ppkt, AVCodecParameters *par, int expected_stride)
28 {
29     int ret;
30     AVPacket *pkt = *ppkt;
31     int64_t bpc = par->bits_per_coded_sample != 15 ? par->bits_per_coded_sample : 16;
32     int min_stride = (par->width * bpc + 7) >> 3;
33     int with_pal_size = min_stride * par->height + 1024;
34     int contains_pal = bpc == 8 && pkt->size == with_pal_size;
35     int size = contains_pal ? min_stride * par->height : pkt->size;
36     int stride = size / par->height;
37     int padding = expected_stride - FFMIN(expected_stride, stride);
38     int y;
39     AVPacket *new_pkt;
40 
41     if (pkt->size == expected_stride * par->height)
42         return 0;
43     if (size != stride * par->height)
44         return 0;
45 
46     new_pkt = av_packet_alloc();
47     if (!new_pkt)
48         return AVERROR(ENOMEM);
49 
50     ret = av_new_packet(new_pkt, expected_stride * par->height);
51     if (ret < 0)
52         goto fail;
53 
54     ret = av_packet_copy_props(new_pkt, pkt);
55     if (ret < 0)
56         goto fail;
57 
58     for (y = 0; y<par->height; y++) {
59         memcpy(new_pkt->data + y*expected_stride, pkt->data + y*stride, FFMIN(expected_stride, stride));
60         memset(new_pkt->data + y*expected_stride + expected_stride - padding, 0, padding);
61     }
62 
63     *ppkt = new_pkt;
64     return 1 + contains_pal;
65 fail:
66     av_packet_free(&new_pkt);
67 
68     return ret;
69 }
70 
ff_get_packet_palette(AVFormatContext *s, AVPacket *pkt, int ret, uint32_t *palette)71 int ff_get_packet_palette(AVFormatContext *s, AVPacket *pkt, int ret, uint32_t *palette)
72 {
73     uint8_t *side_data;
74     size_t size;
75 
76     side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_PALETTE, &size);
77     if (side_data) {
78         if (size != AVPALETTE_SIZE) {
79             av_log(s, AV_LOG_ERROR, "Invalid palette side data\n");
80             return AVERROR_INVALIDDATA;
81         }
82         memcpy(palette, side_data, AVPALETTE_SIZE);
83         return 1;
84     }
85 
86     if (ret == CONTAINS_PAL) {
87         for (int i = 0; i < AVPALETTE_COUNT; i++)
88             palette[i] = AV_RL32(pkt->data + pkt->size - AVPALETTE_SIZE + i*4);
89         return 1;
90     }
91 
92     return 0;
93 }
94