FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
frmdec.c
Go to the documentation of this file.
1 /*
2  * Megalux Frame demuxer
3  * Copyright (c) 2010 Peter Ross <pross@xvid.org>
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 /**
23  * @file
24  * Megalux Frame demuxer
25  */
26 
27 #include "libavcodec/raw.h"
28 #include "libavutil/intreadwrite.h"
29 #include "avformat.h"
30 
31 static const PixelFormatTag frm_pix_fmt_tags[] = {
32  { AV_PIX_FMT_RGB555, 1 },
33  { AV_PIX_FMT_RGB0, 2 },
34  { AV_PIX_FMT_RGB24, 3 },
35  { AV_PIX_FMT_BGR0, 4 },
36  { AV_PIX_FMT_BGRA, 5 },
37  { AV_PIX_FMT_NONE, 0 },
38 };
39 
40 typedef struct {
41  int count;
42 } FrmContext;
43 
45 {
46  if (p->buf_size > 8 &&
47  p->buf[0] == 'F' && p->buf[1] == 'R' && p->buf[2] == 'M' &&
48  AV_RL16(&p->buf[4]) && AV_RL16(&p->buf[6]))
49  return AVPROBE_SCORE_MAX / 4;
50  return 0;
51 }
52 
53 static int frm_read_header(AVFormatContext *avctx)
54 {
55  AVIOContext *pb = avctx->pb;
56  AVStream *st = avformat_new_stream(avctx, 0);
57  if (!st)
58  return AVERROR(ENOMEM);
59 
62  avio_skip(pb, 3);
63 
64  st->codec->pix_fmt = avpriv_find_pix_fmt(frm_pix_fmt_tags, avio_r8(pb));
65  if (!st->codec->pix_fmt)
66  return AVERROR_INVALIDDATA;
67 
68  st->codec->codec_tag = 0;
69  st->codec->width = avio_rl16(pb);
70  st->codec->height = avio_rl16(pb);
71  return 0;
72 }
73 
75 {
76  FrmContext *s = avctx->priv_data;
77  AVCodecContext *stc = avctx->streams[0]->codec;
78  int packet_size, ret;
79 
80  if (s->count)
81  return AVERROR_EOF;
82 
83  packet_size = avpicture_get_size(stc->pix_fmt, stc->width, stc->height);
84  if (packet_size < 0)
85  return AVERROR_INVALIDDATA;
86 
87  ret = av_get_packet(avctx->pb, pkt, packet_size);
88  if (ret < 0)
89  return ret;
90 
91  if (stc->pix_fmt == AV_PIX_FMT_BGRA) {
92  int i;
93  for (i = 3; i + 1 <= pkt->size; i += 4)
94  pkt->data[i] = 0xFF - pkt->data[i];
95  }
96 
97  pkt->stream_index = 0;
98  s->count++;
99 
100  return 0;
101 }
102 
104  .name = "frm",
105  .priv_data_size = sizeof(FrmContext),
106  .long_name = NULL_IF_CONFIG_SMALL("Megalux Frame"),
107  .read_probe = frm_read_probe,
108  .read_header = frm_read_header,
109  .read_packet = frm_read_packet,
110 };
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int frm_read_header(AVFormatContext *avctx)
Definition: frmdec.c:53
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:65
int size
Definition: avcodec.h:1424
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1722
static int frm_read_packet(AVFormatContext *avctx, AVPacket *pkt)
Definition: frmdec.c:74
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:277
static AVPacket pkt
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
static const PixelFormatTag frm_pix_fmt_tags[]
Definition: frmdec.c:31
Format I/O context.
Definition: avformat.h:1273
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:277
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:3749
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1341
uint8_t * data
Definition: avcodec.h:1423
#define AVERROR_EOF
End of file.
Definition: error.h:55
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:244
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:97
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:529
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:451
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
Raw Video Codec.
int width
picture width / height.
Definition: avcodec.h:1681
Stream structure.
Definition: avformat.h:842
enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags, unsigned int fourcc)
Definition: utils.c:1134
enum AVMediaType codec_type
Definition: avcodec.h:1510
enum AVCodecID codec_id
Definition: avcodec.h:1519
AVIOContext * pb
I/O context.
Definition: avformat.h:1315
main external API structure.
Definition: avcodec.h:1502
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1534
AVInputFormat ff_frm_demuxer
Definition: frmdec.c:103
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:279
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:643
Main libavformat public API header.
#define AV_PIX_FMT_RGB555
Definition: pixfmt.h:368
void * priv_data
Format private data.
Definition: avformat.h:1301
int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
Calculate the size in bytes that a picture of the given width and height would occupy if stored in th...
Definition: avpicture.c:49
int count
Definition: frmdec.c:41
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:628
int stream_index
Definition: avcodec.h:1425
This structure stores compressed data.
Definition: avcodec.h:1400
static int frm_read_probe(AVProbeData *p)
Definition: frmdec.c:44