annotate yuv.c @ 1386:3e12e3ca0ce3 libavformat

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