File: write-dv.c

package info (click to toggle)
xawtv 3.107-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,204 kB
  • sloc: ansic: 46,254; perl: 749; sh: 422; cpp: 184; makefile: 126; xml: 74
file content (228 lines) | stat: -rw-r--r-- 4,932 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>

#include <libdv/dv.h>

#include "grab-ng.h"
#include "list.h"

/* ----------------------------------------------------------------------- */

struct dv_frame {
    struct list_head  list;
    int               seq;
    int               video,audio;
    unsigned char     obuf[0];
};

struct dv_handle {
    /* handles */
    int fd;
    dv_encoder_t  *enc;

    /* format */
    struct ng_video_fmt video;
    struct ng_audio_fmt audio;

    /* misc */
    int framesize, fvideo, faudio;
    struct list_head frames;
};

/* ----------------------------------------------------------------------- */

static struct dv_frame*
dv_get_frame(struct dv_handle *h, int nr)
{
    struct dv_frame *frame = NULL;
    struct list_head *item;

    list_for_each(item,&h->frames) {
	frame = list_entry(item,struct dv_frame,list);
	if (frame->seq == nr)
	    break;
    }
    if (NULL == frame || frame->seq != nr) {
	frame = malloc(sizeof(*frame) + h->framesize);
	memset(frame,0,sizeof(*frame) + h->framesize);
	frame->seq = nr;
	list_add_tail(&frame->list,&h->frames);
    }
    return frame;
}

static int dv_put_frame(struct dv_handle *h, struct dv_frame *frame)
{
    int rc;

    if (h->video.fmtid  &&  !frame->video)
	return 0;
    if (h->audio.fmtid  &&  !frame->audio)
	return 0;

    if (ng_debug)
	fprintf(stderr,"dv: write frame #%d\n",frame->seq);
    rc = write(h->fd, frame->obuf, h->framesize);
    list_del(&frame->list);
    free(frame);
    return (rc == h->framesize) ? 0 : -1;
}

/* ----------------------------------------------------------------------- */

static void*
dv_open(char *filename, char *dummy,
	struct ng_video_fmt *video, const void *priv_video, int fps,
	struct ng_audio_fmt *audio, const void *priv_audio)
{
    struct dv_handle *h;

    if (NULL == (h = malloc(sizeof(*h))))
	return NULL;

    memset(h,0,sizeof(*h));
    h->video      = *video;
    h->audio      = *audio;

    if (-1 == (h->fd = open(filename,O_CREAT | O_RDWR | O_TRUNC, 0666))) {
	fprintf(stderr,"open %s: %s\n",filename,strerror(errno));
	goto fail;
    }
    h->enc = dv_encoder_new(0,0,0);
    if (NULL == h->enc) {
	fprintf(stderr,"dv: dv_encoder_new failed\n");
	goto fail;
    }

    if (h->audio.fmtid != AUDIO_NONE) {
    }
    if (h->video.fmtid != VIDEO_NONE) {
	if (720   == h->video.width  &&
	    480   == h->video.height &&
	    30000 == fps) {
	    /* NTSC */
	    h->enc->isPAL = 0;
	    h->framesize  = 120000;
	} else if (720   == h->video.width  &&
		   576   == h->video.height &&
		   25000 == fps) {
	    /* PAL */
	    h->enc->isPAL = 1;
	    h->framesize  = 144000;
	} else {
	    fprintf(stderr,
		    "dv: %dx%d @ %d fps is not allowed for digital video\n"
		    "dv: use 720x480/30 (NTSC) or 720x576/25 (PAL)\n",
		    h->video.width, h->video.height, fps/1000);
	    goto fail;
	}
    }
    INIT_LIST_HEAD(&h->frames);
    return h;

 fail:
    if (h->enc)
	dv_encoder_free(h->enc);
    if (-1 != h->fd)
	close(h->fd);
    free(h);
    return NULL;
}

static int
dv_video(void *handle, struct ng_video_buf *buf)
{
    struct dv_handle *h = handle;
    struct dv_frame *frame;
    unsigned char *pixels[3];

    frame = dv_get_frame(h,h->fvideo);
    pixels[0] = buf->data;
    switch (buf->fmt.fmtid) {
    case VIDEO_YUYV:
	dv_encode_full_frame(h->enc,pixels,e_dv_color_yuv,frame->obuf);
	break;
    case VIDEO_RGB24:
	dv_encode_full_frame(h->enc,pixels,e_dv_color_rgb,frame->obuf);
	break;
    case VIDEO_BGR32:
	dv_encode_full_frame(h->enc,pixels,e_dv_color_bgr0,frame->obuf);
	break;
    default:
	BUG_ON(1,"unknown fmtid");
    }
    frame->video = 1;
    dv_put_frame(h,frame);
    h->fvideo++;
    return 0;
}

static int
dv_audio(void *handle, struct ng_audio_buf *buf)
{
    //struct dv_handle *h = handle;
    //struct dv_frame *frame;

    return -1;
}

static int
dv_close(void *handle)
{
    struct dv_handle *h = handle;

    dv_encoder_free(h->enc);
    close(h->fd);
    free(h);
    return 0;
}

/* ----------------------------------------------------------------------- */

static const struct ng_format_list dv_vformats[] = {
    {
	.name =  "dv",
	.ext =   "dv",
	.desc =  "digital video",
	.fmtid = VIDEO_YUYV,
    },{
	/* EOF */
    }
};

#if 0
static const struct ng_format_list dv_aformats[] = {
    {
	.name =  "stereo16",
	.ext =   "dv",
	.fmtid = AUDIO_S16_NATIVE_STEREO,
    },{
	/* EOF */
    }
};
#endif

struct ng_writer dv_writer = {
    .name =      "dv",
    .desc =      "Digital Video",
    //.combined =  1,
    .video =     dv_vformats,
    //.audio =     dv_aformats,
    .wr_open =   dv_open,
    .wr_video =  dv_video,
    .wr_audio =  dv_audio,
    .wr_close =  dv_close,
};

extern void ng_plugin_init(void);
void ng_plugin_init(void)
{
    //ng_writer_register(NG_PLUGIN_MAGIC,__FILE__,&dv_writer);
}