]> git.sesse.net Git - ffmpeg/blob - libavformat/dv.c
raw ac3 auto detects parameters
[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     ast->codec.channels = 2;
51     c->is_audio = 0;
52
53     return 0;
54 }
55
56 static void __destruct_pkt(struct AVPacket *pkt)
57 {
58     pkt->data = NULL; pkt->size = 0;
59     return;
60 }
61
62 static int dv_read_packet(AVFormatContext *s, AVPacket *pkt)
63 {
64     int ret, dsf;
65     DVDemuxContext *c = s->priv_data;
66     
67     if (!c->is_audio) {
68         ret = get_buffer(&s->pb, c->buf, 4);
69         if (ret <= 0) 
70             return -EIO;
71         dsf = c->buf[3] & 0x80;
72         if (!dsf)
73             c->size = NTSC_FRAME_SIZE;
74         else
75             c->size = PAL_FRAME_SIZE;
76         
77         ret = get_buffer(&s->pb, c->buf + 4, c->size - 4);
78         if (ret <= 0)
79             return -EIO;
80     }
81     
82     av_init_packet(pkt);
83     pkt->destruct = __destruct_pkt;
84     pkt->data     = c->buf;
85     pkt->size     = c->size;
86     pkt->stream_index = c->is_audio;
87     
88     c->is_audio = !c->is_audio;
89     return c->size;
90 }
91
92 static int dv_read_close(AVFormatContext *s)
93 {
94     return 0;
95 }
96
97 static AVInputFormat dv_iformat = {
98     "dv",
99     "DV video format",
100     sizeof(DVDemuxContext),
101     NULL,
102     dv_read_header,
103     dv_read_packet,
104     dv_read_close,
105     .extensions = "dv",
106 };
107
108 #if 0
109 int dv_write_header(struct AVFormatContext *s)
110 {
111     return 0;
112 }
113
114 int dv_write_packet(struct AVFormatContext *s, 
115                      int stream_index,
116                      unsigned char *buf, int size, int force_pts)
117 {
118     put_buffer(&s->pb, buf, size);
119     put_flush_packet(&s->pb);
120     return 0;
121 }
122
123 int dv_write_trailer(struct AVFormatContext *s)
124 {
125     return 0;
126 }
127
128 AVOutputFormat dv_oformat = {
129     "dv",
130     "DV video format",
131     NULL,
132     "dv",
133     0,
134     CODEC_ID_DVVIDEO,
135     CODEC_ID_DVAUDIO,
136     dv_write_header,
137     dv_write_packet,
138     dv_write_trailer,
139 };
140 #endif
141
142 int dv_init(void)
143 {
144     av_register_input_format(&dv_iformat);
145     //    av_register_output_format(&dv_oformat);
146     return 0;
147 }