]> git.sesse.net Git - ffmpeg/blob - libavformat/pva.c
remove unnecessary shift
[ffmpeg] / libavformat / pva.c
1 /*
2  * TechnoTrend PVA (.pva) demuxer
3  * Copyright (c) 2007, 2008 Ivo van Poorten
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
24 #define PVA_MAX_PAYLOAD_LENGTH  0x17f8
25 #define PVA_VIDEO_PAYLOAD       0x01
26 #define PVA_AUDIO_PAYLOAD       0x02
27 #define PVA_MAGIC               (('A' << 8) + 'V')
28
29 typedef struct {
30     int continue_pes;
31 } PVAContext;
32
33 static int pva_probe(AVProbeData * pd) {
34     unsigned char *buf = pd->buf;
35
36     if (AV_RB16(buf) == PVA_MAGIC && buf[2] && buf[2] < 3 && buf[4] == 0x55)
37         return AVPROBE_SCORE_MAX / 2;
38
39     return 0;
40 }
41
42 static int pva_read_header(AVFormatContext *s, AVFormatParameters *ap) {
43     AVStream *st;
44
45     if (!(st = av_new_stream(s, 0)))
46         return AVERROR(ENOMEM);
47     st->codec->codec_type = CODEC_TYPE_VIDEO;
48     st->codec->codec_id   = CODEC_ID_MPEG2VIDEO;
49     st->need_parsing      = AVSTREAM_PARSE_FULL;
50     av_set_pts_info(st, 32, 1, 90000);
51
52     if (!(st = av_new_stream(s, 1)))
53         return AVERROR(ENOMEM);
54     st->codec->codec_type = CODEC_TYPE_AUDIO;
55     st->codec->codec_id   = CODEC_ID_MP3;
56     st->need_parsing      = AVSTREAM_PARSE_HEADERS;
57     av_set_pts_info(st, 33, 1, 90000);
58
59     /* the parameters will be extracted from the compressed bitstream */
60     return 0;
61 }
62
63 static int pva_read_packet(AVFormatContext *s, AVPacket *pkt) {
64     ByteIOContext *pb = s->pb;
65     PVAContext *pvactx = s->priv_data;
66     int ret, syncword, streamid, reserved, flags, length, pts_flag;
67     long long pva_pts = 0;
68
69     syncword = get_be16(pb);
70     streamid = get_byte(pb);
71     get_byte(pb);               /* counter not used */
72     reserved = get_byte(pb);
73     flags    = get_byte(pb);
74     length   = get_be16(pb);
75
76     pts_flag  = flags & 0x10;
77
78     if (syncword != PVA_MAGIC) {
79         av_log(s, AV_LOG_ERROR, "invalid syncword\n");
80         return AVERROR(EIO);
81     }
82     if (reserved != 0x55) {
83         av_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
84     }
85     if (length > PVA_MAX_PAYLOAD_LENGTH) {
86         av_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
87         return AVERROR(EIO);
88     }
89
90     if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
91         pva_pts = get_be32(pb);
92         length -= 4;
93     } else if (streamid == PVA_AUDIO_PAYLOAD) {
94         /* PVA Audio Packets either start with a signaled PES packet or
95          * are a continuation of the previous PES packet. New PES packets
96          * always start at the beginning of a PVA Packet, never somewhere in
97          * the middle. */
98         if (!pvactx->continue_pes) {
99             int pes_signal, pes_header_data_length, pes_packet_length,
100                 pes_flags;
101             unsigned char pes_header_data[256];
102
103             pes_signal             = get_be24(pb);
104             get_byte(pb);
105             pes_packet_length      = get_be16(pb);
106             pes_flags              = get_be16(pb);
107             pes_header_data_length = get_byte(pb);
108
109             if (pes_signal != 1) {
110                 av_log(s, AV_LOG_ERROR, "expected signaled PES packet\n");
111                 return AVERROR(EIO);
112             }
113
114             get_buffer(pb, pes_header_data, pes_header_data_length);
115             length -= 9 + pes_header_data_length;
116
117             pes_packet_length -= 3 + pes_header_data_length;
118
119             pvactx->continue_pes = pes_packet_length;
120
121             if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20) {
122                 pva_pts  = ((long long) *pes_header_data & 0x0e) << 29;
123                 pva_pts += (AV_RB16(pes_header_data+1) >> 1) << 15;
124                 pva_pts +=  AV_RB16(pes_header_data+3) >> 1;
125             }
126         }
127
128         pvactx->continue_pes -= length;
129
130         if (pvactx->continue_pes < 0) {
131             av_log(s, AV_LOG_WARNING, "audio data corruption\n");
132             pvactx->continue_pes = 0;
133         }
134     }
135
136     if ((ret = av_get_packet(pb, pkt, length)) <= 0)
137         return AVERROR(EIO);
138
139     pkt->stream_index = streamid - 1;
140     if (pva_pts)
141         pkt->pts = pva_pts;
142
143     return ret;
144 }
145
146 AVInputFormat pva_demuxer = {
147     "pva",
148     "pva file and stream format",
149     sizeof(PVAContext),
150     pva_probe,
151     pva_read_header,
152     pva_read_packet,
153 };