]> git.sesse.net Git - ffmpeg/blob - libavformat/rawutils.c
avformat/avienc: Use avi_write_packet_internal() to store raw rgb in a more spec...
[ffmpeg] / libavformat / rawutils.c
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, AVCodecContext *enc, int expected_stride)
26 {
27     int ret;
28     AVPacket *pkt = *ppkt;
29     int64_t bpc = enc->bits_per_coded_sample != 15 ? enc->bits_per_coded_sample : 16;
30     int min_stride = (enc->width * bpc + 7) >> 3;
31     int with_pal_size = min_stride * enc->height + 1024;
32     int size = bpc == 8 && pkt->size == with_pal_size ? min_stride * enc->height : pkt->size;
33     int stride = size / enc->height;
34     int padding = expected_stride - FFMIN(expected_stride, stride);
35     int y;
36     AVPacket *new_pkt;
37
38     if (pkt->size == expected_stride * enc->height)
39         return 0;
40     if (size != stride * enc->height)
41         return 0;
42
43     new_pkt = av_packet_alloc();
44     if (!new_pkt)
45         return AVERROR(ENOMEM);
46
47     ret = av_new_packet(new_pkt, expected_stride * enc->height);
48     if (ret < 0)
49         goto fail;
50
51     ret = av_packet_copy_props(new_pkt, pkt);
52     if (ret < 0)
53         goto fail;
54
55     for (y = 0; y<enc->height; y++) {
56         memcpy(new_pkt->data + y*expected_stride, pkt->data + y*stride, FFMIN(expected_stride, stride));
57         memset(new_pkt->data + y*expected_stride + expected_stride - padding, 0, padding);
58     }
59
60     *ppkt = new_pkt;
61     return 1;
62 fail:
63     av_packet_free(&new_pkt);
64
65     return ret;
66 }