FFmpeg
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 "libavutil/intreadwrite.h"
23 #include "libavcodec/packet.h"
24 #include "avformat.h"
25 #include "rawutils.h"
26 
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 
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 
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 }
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:54
AVPacket::data
uint8_t * data
Definition: packet.h:374
AV_PKT_DATA_PALETTE
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition: packet.h:47
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:73
fail
#define fail()
Definition: checkasm.h:134
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:97
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:128
AVFormatContext
Format I/O context.
Definition: avformat.h:1104
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
AVPALETTE_COUNT
#define AVPALETTE_COUNT
Definition: pixfmt.h:33
AVPacket::size
int size
Definition: packet.h:375
size
int size
Definition: twinvq_data.h:10344
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:62
av_packet_copy_props
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:385
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
packet.h
av_packet_get_side_data
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition: avpacket.c:251
AVCodecParameters::height
int height
Definition: codec_par.h:129
ff_get_packet_palette
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:71
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
stride
#define stride
Definition: h264pred_template.c:537
ret
ret
Definition: filter_design.txt:187
rawutils.h
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
ff_reshuffle_raw_rgb
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
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:104
AVPacket
This structure stores compressed data.
Definition: packet.h:351
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
CONTAINS_PAL
#define CONTAINS_PAL
Definition: rawutils.h:29