FFmpeg  4.4.9
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 
22 #include "avformat.h"
23 #include "internal.h"
24 
25 int ff_reshuffle_raw_rgb(AVFormatContext *s, AVPacket **ppkt, AVCodecParameters *par, int expected_stride)
26 {
27  int ret;
28  AVPacket *pkt = *ppkt;
29  int64_t bpc = par->bits_per_coded_sample != 15 ? par->bits_per_coded_sample : 16;
30  int64_t min_stride = (par->width * bpc + 7) >> 3;
31  int with_pal_size, contains_pal, size, stride, padding, y;
32  AVPacket *new_pkt;
33 
34  if (par->height <= 0 || min_stride <= 0 || expected_stride <= 0 ||
35  min_stride > (INT_MAX - 1024) / par->height ||
36  expected_stride > (INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) / par->height)
37  return AVERROR(EINVAL);
38 
39  with_pal_size = min_stride * par->height + 1024;
40  contains_pal = bpc == 8 && pkt->size == with_pal_size;
41  size = contains_pal ? min_stride * par->height : pkt->size;
42  stride = size / par->height;
43  padding = expected_stride - FFMIN(expected_stride, stride);
44 
45  if (pkt->size == expected_stride * par->height)
46  return 0;
47  if (size != stride * par->height)
48  return 0;
49 
50  new_pkt = av_packet_alloc();
51  if (!new_pkt)
52  return AVERROR(ENOMEM);
53 
54  ret = av_new_packet(new_pkt, expected_stride * par->height);
55  if (ret < 0)
56  goto fail;
57 
58  ret = av_packet_copy_props(new_pkt, pkt);
59  if (ret < 0)
60  goto fail;
61 
62  for (y = 0; y<par->height; y++) {
63  memcpy(new_pkt->data + y*expected_stride, pkt->data + y*stride, FFMIN(expected_stride, stride));
64  memset(new_pkt->data + y*expected_stride + expected_stride - padding, 0, padding);
65  }
66 
67  *ppkt = new_pkt;
68  return 1 + contains_pal;
69 fail:
70  av_packet_free(&new_pkt);
71 
72  return ret;
73 }
Main libavformat public API header.
#define s(width, name)
Definition: cbs_vp9.c:257
#define fail()
Definition: checkasm.h:133
#define FFMIN(a, b)
Definition: common.h:105
long long int64_t
Definition: coverity.c:34
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition: avcodec.h:215
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:75
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:64
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:600
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
#define AVERROR(e)
Definition: error.h:43
common internal API header
int stride
Definition: mace.c:144
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:25
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
int width
Video only.
Definition: codec_par.h:126
Format I/O context.
Definition: avformat.h:1232
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
AVPacket * pkt
Definition: movenc.c:59
int size