]> git.sesse.net Git - ffmpeg/blob - libavformat/dv.c
dv file format support patch by (Roman Shaposhnick <rvs at sun dot com>)
[ffmpeg] / libavformat / dv.c
1 /* 
2  * Raw DV format
3  * Copyright (c) 2002 Fabrice Bellard.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 #include "avformat.h"
20
21 #define NTSC_FRAME_SIZE 120000
22 #define PAL_FRAME_SIZE  144000
23
24 typedef struct DVDemuxContext {
25     int       is_audio;
26     uint8_t   buf[PAL_FRAME_SIZE];
27     int       size;
28 } DVDemuxContext;
29
30 /* raw input */
31 static int dv_read_header(AVFormatContext *s,
32                           AVFormatParameters *ap)
33 {
34     AVStream *vst, *ast;
35     DVDemuxContext *c = s->priv_data;
36
37     vst = av_new_stream(s, 0);
38     if (!vst)
39         return AVERROR_NOMEM;
40     vst->codec.codec_type = CODEC_TYPE_VIDEO;
41     vst->codec.codec_id = CODEC_ID_DVVIDEO;
42
43
44     ast = av_new_stream(s, 1);
45     if (!ast)
46         return AVERROR_NOMEM;
47
48     ast->codec.codec_type = CODEC_TYPE_AUDIO;
49     ast->codec.codec_id = CODEC_ID_DVAUDIO;
50     c->is_audio = 0;
51
52     return 0;
53 }
54
55 static void __destruct_pkt(struct AVPacket *pkt)
56 {
57     pkt->data = NULL; pkt->size = 0;
58     return;
59 }
60
61 static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
62 {
63     int ret, dsf;
64     DVDemuxContext *c = s->priv_data;
65     
66     if (!c->is_audio) {
67         ret = get_buffer(&s->pb, c->buf, 4);
68         if (ret <= 0) 
69             return -EIO;
70         dsf = c->buf[3] & 0x80;
71         if (!dsf)
72             c->size = NTSC_FRAME_SIZE;
73         else
74             c->size = PAL_FRAME_SIZE;
75         
76         ret = get_buffer(&s->pb, c->buf + 4, c->size - 4);
77         if (ret <= 0)
78             return -EIO;
79     }
80     
81     av_init_packet(pkt);
82     pkt->destruct = __destruct_pkt;
83     pkt->data     = c->buf;
84     pkt->size     = c->size;
85     pkt->stream_index = c->is_audio;
86     
87     c->is_audio = !c->is_audio;
88     return c->size;
89 }
90
91 static int dv_read_close(AVFormatContext *s)
92 {
93     return 0;
94 }
95
96 static AVInputFormat dv_iformat = {
97     "dv",
98     "DV video format",
99     sizeof(DVDemuxContext),
100     NULL,
101     dv_read_header,
102     dv_read_packet,
103     dv_read_close,
104     .extensions = "dv",
105 };
106
107
108 int dv_write_header(struct AVFormatContext *s)
109 {
110     return 0;
111 }
112
113 int dv_write_packet(struct AVFormatContext *s, 
114                      int stream_index,
115                      unsigned char *buf, int size, int force_pts)
116 {
117     put_buffer(&s->pb, buf, size);
118     put_flush_packet(&s->pb);
119     return 0;
120 }
121
122 int dv_write_trailer(struct AVFormatContext *s)
123 {
124     return 0;
125 }
126
127 AVOutputFormat dv_oformat = {
128     "dv",
129     "DV video format",
130     NULL,
131     "dv",
132     0,
133     CODEC_ID_DVVIDEO,
134     CODEC_ID_DVAUDIO,
135     dv_write_header,
136     dv_write_packet,
137     dv_write_trailer,
138 };
139
140 int dv_init(void)
141 {
142     av_register_input_format(&dv_iformat);
143     av_register_output_format(&dv_oformat);
144     return 0;
145 }