FFmpeg
Loading...
Searching...
No Matches
rawutils.c
Go to the documentation of this file.
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
23#include "libavcodec/packet.h"
24#include "avformat.h"
25#include "rawutils.h"
26
27int 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 int64_t min_stride = (par->width * bpc + 7) >> 3;
33 int with_pal_size, contains_pal, size, stride, padding, y;
34 AVPacket *new_pkt;
35
36 if (par->height <= 0 || min_stride <= 0 || expected_stride <= 0 ||
37 min_stride > (INT_MAX - 1024) / par->height ||
38 expected_stride > (INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) / par->height)
39 return AVERROR(EINVAL);
40
41 with_pal_size = min_stride * par->height + 1024;
42 contains_pal = bpc == 8 && pkt->size == with_pal_size;
43 size = contains_pal ? min_stride * par->height : pkt->size;
44 stride = size / par->height;
45 padding = expected_stride - FFMIN(expected_stride, stride);
46
47 if (pkt->size == expected_stride * par->height)
48 return 0;
49 if (size != stride * par->height)
50 return 0;
51
52 new_pkt = av_packet_alloc();
53 if (!new_pkt)
54 return AVERROR(ENOMEM);
55
56 ret = av_new_packet(new_pkt, expected_stride * par->height);
57 if (ret < 0)
58 goto fail;
59
60 ret = av_packet_copy_props(new_pkt, pkt);
61 if (ret < 0)
62 goto fail;
63
64 for (y = 0; y<par->height; y++) {
65 memcpy(new_pkt->data + y*expected_stride, pkt->data + y*stride, FFMIN(expected_stride, stride));
66 memset(new_pkt->data + y*expected_stride + expected_stride - padding, 0, padding);
67 }
68
69 *ppkt = new_pkt;
70 return 1 + contains_pal;
71fail:
72 av_packet_free(&new_pkt);
73
74 return ret;
75}
76
77int ff_get_packet_palette(AVFormatContext *s, AVPacket *pkt, int ret, uint32_t *palette)
78{
79 uint8_t *side_data;
80 size_t size;
81
83 if (side_data) {
84 if (size != AVPALETTE_SIZE) {
85 av_log(s, AV_LOG_ERROR, "Invalid palette side data\n");
87 }
88 memcpy(palette, side_data, AVPALETTE_SIZE);
89 return 1;
90 }
91
92 if (ret == CONTAINS_PAL) {
93 for (int i = 0; i < AVPALETTE_COUNT; i++)
94 palette[i] = AV_RL32(pkt->data + pkt->size - AVPALETTE_SIZE + i*4);
95 return 1;
96 }
97
98 return 0;
99}
Main libavformat public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
#define fail
Definition test.h:479
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition packet.h:47
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition packet.c:397
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
#define AV_RL32(p)
#define FFMIN(a, b)
Definition macros.h:49
#define AVPALETTE_COUNT
Definition pixfmt.h:33
#define AVPALETTE_SIZE
Definition pixfmt.h:32
int ff_get_packet_palette(AVFormatContext *s, AVPacket *pkt, int ret, uint32_t *palette)
Retrieves the palette from a packet, either from side data, or appended to the video data in the pack...
Definition rawutils.c:77
int ff_reshuffle_raw_rgb(AVFormatContext *s, AVPacket **ppkt, AVCodecParameters *par, int expected_stride)
Reshuffles the lines to use the user specified stride.
Definition rawutils.c:27
#define CONTAINS_PAL
Definition rawutils.h:29
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
int width
The width of the video frame in pixels.
Definition codec_par.h:143
Format I/O context.
Definition avformat.h:1333
This structure stores compressed data.
Definition packet.h:580
uint8_t * data
Definition packet.h:603
#define stride
#define av_log(a,...)
int size