3 * Copyright (c) 2016 Michael Niedermayer
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/intreadwrite.h"
23 #include "libavcodec/packet.h"
27 int ff_reshuffle_raw_rgb(AVFormatContext
*s
, AVPacket
**ppkt
, AVCodecParameters
*par
, int expected_stride
)
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
);
41 if (pkt
->size
== expected_stride
* par
->height
)
43 if (size
!= stride
* par
->height
)
46 new_pkt
= av_packet_alloc();
48 return AVERROR(ENOMEM
);
50 ret
= av_new_packet(new_pkt
, expected_stride
* par
->height
);
54 ret
= av_packet_copy_props(new_pkt
, pkt
);
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
);
64 return 1 + contains_pal
;
66 av_packet_free(&new_pkt
);
71 int ff_get_packet_palette(AVFormatContext
*s
, AVPacket
*pkt
, int ret
, uint32_t *palette
)
76 side_data
= av_packet_get_side_data(pkt
, AV_PKT_DATA_PALETTE
, &size
);
78 if (size
!= AVPALETTE_SIZE
) {
79 av_log(s
, AV_LOG_ERROR
, "Invalid palette side data\n");
80 return AVERROR_INVALIDDATA
;
82 memcpy(palette
, side_data
, AVPALETTE_SIZE
);
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);