annotate yuv.c @ 1239:18130af77b09 libavformat

indention
author bcoudurier
date Mon, 07 Aug 2006 14:19:33 +0000
parents edbe5c3717f9
children 0899bfe4105c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
1 /*
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
2 * .Y.U.V image format
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
3 * Copyright (c) 2003 Fabrice Bellard.
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
4 *
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
9 *
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
13 * Lesser General Public License for more details.
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
14 *
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
896
edbe5c3717f9 Update licensing information: The FSF changed postal address.
diego
parents: 885
diff changeset
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
18 */
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
19 #include "avformat.h"
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
21 static int sizes[][2] = {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
22 { 640, 480 },
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
23 { 720, 480 },
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
24 { 720, 576 },
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
25 { 352, 288 },
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
26 { 352, 240 },
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
27 { 160, 128 },
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
28 { 512, 384 },
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
29 { 640, 352 },
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
30 { 640, 240 },
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
31 };
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
32
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
33 static int infer_size(int *width_ptr, int *height_ptr, int size)
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
34 {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
35 int i;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
36
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
37 for(i=0;i<sizeof(sizes)/sizeof(sizes[0]);i++) {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
38 if ((sizes[i][0] * sizes[i][1]) == size) {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
39 *width_ptr = sizes[i][0];
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
40 *height_ptr = sizes[i][1];
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
41 return 0;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
42 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
43 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
44 return -1;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
45 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
46
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
47 static int yuv_read(ByteIOContext *f,
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
48 int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
49 {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
50 ByteIOContext pb1, *pb = &pb1;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
51 int img_size, ret;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
52 char fname[1024], *p;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
53 int size;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
54 URLContext *h;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
55 AVImageInfo info1, *info = &info1;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 764
diff changeset
56
764
cdb845a57ae4 drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents: 482
diff changeset
57 img_size = url_fsize(f);
cdb845a57ae4 drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents: 482
diff changeset
58
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
59 /* XXX: hack hack */
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
60 h = url_fileno(f);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
61 url_get_filename(h, fname, sizeof(fname));
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
62
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
63 if (infer_size(&info->width, &info->height, img_size) < 0) {
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 65
diff changeset
64 return AVERROR_IO;
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
65 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
66 info->pix_fmt = PIX_FMT_YUV420P;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 764
diff changeset
67
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
68 ret = alloc_cb(opaque, info);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
69 if (ret)
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
70 return ret;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 764
diff changeset
71
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
72 size = info->width * info->height;
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 764
diff changeset
73
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
74 p = strrchr(fname, '.');
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
75 if (!p || p[1] != 'Y')
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 65
diff changeset
76 return AVERROR_IO;
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
77
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
78 get_buffer(f, info->pict.data[0], size);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 764
diff changeset
79
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
80 p[1] = 'U';
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
81 if (url_fopen(pb, fname, URL_RDONLY) < 0)
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 65
diff changeset
82 return AVERROR_IO;
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
83
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
84 get_buffer(pb, info->pict.data[1], size / 4);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
85 url_fclose(pb);
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 764
diff changeset
86
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
87 p[1] = 'V';
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
88 if (url_fopen(pb, fname, URL_RDONLY) < 0)
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 65
diff changeset
89 return AVERROR_IO;
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
90
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
91 get_buffer(pb, info->pict.data[2], size / 4);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
92 url_fclose(pb);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
93 return 0;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
94 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
95
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
96 static int yuv_write(ByteIOContext *pb2, AVImageInfo *info)
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
97 {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
98 ByteIOContext pb1, *pb;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
99 char fname[1024], *p;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
100 int i, j, width, height;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 20
diff changeset
101 uint8_t *ptr;
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
102 URLContext *h;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
103 static const char *ext = "YUV";
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 764
diff changeset
104
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
105 /* XXX: hack hack */
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
106 h = url_fileno(pb2);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
107 url_get_filename(h, fname, sizeof(fname));
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
108
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
109 p = strrchr(fname, '.');
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
110 if (!p || p[1] != 'Y')
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 65
diff changeset
111 return AVERROR_IO;
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
112
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
113 width = info->width;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
114 height = info->height;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
115
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
116 for(i=0;i<3;i++) {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
117 if (i == 1) {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
118 width >>= 1;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
119 height >>= 1;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
120 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
121
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
122 if (i >= 1) {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
123 pb = &pb1;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
124 p[1] = ext[i];
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
125 if (url_fopen(pb, fname, URL_WRONLY) < 0)
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 65
diff changeset
126 return AVERROR_IO;
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
127 } else {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
128 pb = pb2;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
129 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 764
diff changeset
130
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
131 ptr = info->pict.data[i];
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
132 for(j=0;j<height;j++) {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
133 put_buffer(pb, ptr, width);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
134 ptr += info->pict.linesize[i];
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
135 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
136 put_flush_packet(pb);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
137 if (i >= 1) {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
138 url_fclose(pb);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
139 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
140 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
141 return 0;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
142 }
885
da1d5db0ce5c COSMETICS: Remove all trailing whitespace.
diego
parents: 764
diff changeset
143
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
144 static int yuv_probe(AVProbeData *pd)
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
145 {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
146 if (match_ext(pd->filename, "Y"))
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
147 return AVPROBE_SCORE_MAX;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
148 else
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
149 return 0;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
150 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
151
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
152 AVImageFormat yuv_image_format = {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
153 "yuv",
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
154 "Y",
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
155 yuv_probe,
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
156 yuv_read,
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
157 (1 << PIX_FMT_YUV420P),
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
158 yuv_write,
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
159 };