FFmpeg
Loading...
Searching...
No Matches
api-qtrle-palette-test.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <stdio.h>
20#include <string.h>
21
22#include "libavcodec/avcodec.h"
24
25static int decode(AVCodecContext *ctx, const AVPacket *packet, AVFrame *frame)
26{
27 int ret = avcodec_send_packet(ctx, packet);
28
29 return ret < 0 ? ret : avcodec_receive_frame(ctx, frame);
30}
31
32int main(void)
33{
34 /* One literal run of four palette indices. */
35 static const uint8_t coded[] = { 0, 0, 0, 13, 0, 0, 1, 1, 1, 2, 3, 4, 255 };
36 static const uint8_t no_change[] = { 0, 0, 0, 7, 0, 0, 0 };
37 static const uint8_t pixels[] = { 1, 2, 3, 4 };
39 AVPacket *packet = av_packet_alloc();
40 AVFrame *held = av_frame_alloc();
41 AVFrame *duplicate = av_frame_alloc();
42 uint8_t *palette;
43 int ret = 1;
44
45 if (!ctx || !packet || !held || !duplicate)
46 goto end;
47 ctx->width = 4;
48 ctx->height = 1;
49 ctx->bits_per_coded_sample = 8;
50 if (avcodec_open2(ctx, NULL, NULL) < 0 ||
51 av_new_packet(packet, sizeof(coded)) < 0)
52 goto end;
53 memcpy(packet->data, coded, sizeof(coded));
55 if (!palette)
56 goto end;
57 AV_WN32(palette + 4, 0xff123456);
58 if (decode(ctx, packet, held) < 0)
59 goto end;
60
61 /* A "no change" packet with a new palette recolors the picture
62 * without touching the frame returned before. */
63 av_shrink_packet(packet, sizeof(no_change));
64 memcpy(packet->data, no_change, sizeof(no_change));
65 AV_WN32(palette + 4, 0xff654321);
66 if (decode(ctx, packet, duplicate) < 0 ||
67 memcmp(duplicate->data[0], pixels, sizeof(pixels)) ||
68 AV_RN32(duplicate->data[1] + 4) != 0xff654321 ||
69 AV_RN32(held->data[1] + 4) != 0xff123456)
70 goto end;
71 ret = 0;
72
73end:
74 if (ret)
75 fprintf(stderr, "QTRLE duplicate palette test failed\n");
76 av_frame_free(&duplicate);
77 av_frame_free(&held);
78 av_packet_free(&packet);
80 return ret;
81}
static AVFormatContext * ctx
static int decode(AVCodecContext *ctx, const AVPacket *packet, AVFrame *frame)
int main(void)
static const uint8_t palette[16][3]
Definition apnggen.c:50
Libavcodec external API header.
#define NULL
Definition coverity.c:32
static AVFrame * frame
int ret
Definition ffplay.c:391
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition avcodec.c:142
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition options.c:149
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition allcodecs.c:999
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
Definition options.c:168
@ AV_CODEC_ID_QTRLE
Definition codec_id.h:105
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Alias for avcodec_receive_frame_flags(avctx, frame, 0).
Definition avcodec.c:718
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition decode.c:738
@ 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_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition packet.c:231
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition packet.c:113
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
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
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
#define AV_WN32(p, v)
#define AV_RN32(p)
#define AVPALETTE_SIZE
Definition pixfmt.h:32
main external API structure.
Definition avcodec.h:443
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:500
This structure stores compressed data.
Definition packet.h:587
uint8_t * data
Definition packet.h:610