annotate yuv.c @ 437:50bae308f71e libavformat

moving nearly identical binary search code from nut/mpeg/asf to utils.c
author michael
date Mon, 12 Apr 2004 16:50:03 +0000
parents a58a8a53eb46
children 0fdc96c2f2fe
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
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
56
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
57 /* XXX: hack hack */
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
58 h = url_fileno(f);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
59 img_size = url_seek(h, 0, SEEK_END);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
60 url_get_filename(h, fname, sizeof(fname));
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
61
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
62 if (infer_size(&info->width, &info->height, img_size) < 0) {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
63 return -EIO;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
64 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
65 info->pix_fmt = PIX_FMT_YUV420P;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
66
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
67 ret = alloc_cb(opaque, info);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
68 if (ret)
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
69 return ret;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
70
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
71 size = info->width * info->height;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
72
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
73 p = strrchr(fname, '.');
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
74 if (!p || p[1] != 'Y')
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
75 return -EIO;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
76
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
77 get_buffer(f, info->pict.data[0], size);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
78
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
79 p[1] = 'U';
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
80 if (url_fopen(pb, fname, URL_RDONLY) < 0)
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
81 return -EIO;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
82
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
83 get_buffer(pb, info->pict.data[1], size / 4);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
84 url_fclose(pb);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
85
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
86 p[1] = 'V';
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
87 if (url_fopen(pb, fname, URL_RDONLY) < 0)
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
88 return -EIO;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
89
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
90 get_buffer(pb, info->pict.data[2], size / 4);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
91 url_fclose(pb);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
92 return 0;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
93 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
94
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
95 static int yuv_write(ByteIOContext *pb2, AVImageInfo *info)
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
96 {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
97 ByteIOContext pb1, *pb;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
98 char fname[1024], *p;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
99 int i, j, width, height;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 20
diff changeset
100 uint8_t *ptr;
20
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
101 URLContext *h;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
102 static const char *ext = "YUV";
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
103
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
104 /* XXX: hack hack */
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
105 h = url_fileno(pb2);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
106 url_get_filename(h, fname, sizeof(fname));
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
107
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
108 p = strrchr(fname, '.');
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
109 if (!p || p[1] != 'Y')
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
110 return -EIO;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
111
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
112 width = info->width;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
113 height = info->height;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
114
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
115 for(i=0;i<3;i++) {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
116 if (i == 1) {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
117 width >>= 1;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
118 height >>= 1;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
119 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
120
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
121 if (i >= 1) {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
122 pb = &pb1;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
123 p[1] = ext[i];
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
124 if (url_fopen(pb, fname, URL_WRONLY) < 0)
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
125 return -EIO;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
126 } else {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
127 pb = pb2;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
128 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
129
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
130 ptr = info->pict.data[i];
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
131 for(j=0;j<height;j++) {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
132 put_buffer(pb, ptr, width);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
133 ptr += info->pict.linesize[i];
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
134 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
135 put_flush_packet(pb);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
136 if (i >= 1) {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
137 url_fclose(pb);
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
138 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
139 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
140 return 0;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
141 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
142
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
143 static int yuv_probe(AVProbeData *pd)
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
144 {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
145 if (match_ext(pd->filename, "Y"))
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
146 return AVPROBE_SCORE_MAX;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
147 else
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
148 return 0;
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
149 }
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
150
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
151 AVImageFormat yuv_image_format = {
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
152 "yuv",
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
153 "Y",
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
154 yuv_probe,
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
155 yuv_read,
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
156 (1 << PIX_FMT_YUV420P),
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
157 yuv_write,
3d52de18ecc3 added still image support
bellard
parents:
diff changeset
158 };