doxygen: add newline after \file
[blender.git] / source / blender / avi / intern / avi_endian.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup avi
22  *
23  * This is external code. Streams bytes to output depending on the
24  * endianness of the system.
25  */
26
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "AVI_avi.h"
33 #include "avi_endian.h"
34 #include "avi_intern.h"
35
36 #ifdef __BIG_ENDIAN__
37 #include "MEM_guardedalloc.h"
38 #endif
39
40 #ifdef __BIG_ENDIAN__
41
42 /* copied from BLI_endian_switch_inline.h */
43 static void invert(int *val)
44 {
45         int tval = *val;
46         *val = ((tval >> 24))             |
47                ((tval << 8) & 0x00ff0000) |
48                ((tval >> 8) & 0x0000ff00) |
49                ((tval << 24));
50 }
51
52 static void sinvert(short int *val)
53 {
54         short tval = *val;
55         *val = (tval >> 8) |
56                (tval << 8);
57 }
58
59 static void Ichunk(AviChunk *chunk)
60 {
61         invert(&chunk->fcc);
62         invert(&chunk->size);
63 }
64 #endif
65
66 #ifdef __BIG_ENDIAN__
67 static void Ilist(AviList *list)
68 {
69         invert(&list->fcc);
70         invert(&list->size);
71         invert(&list->ids);
72 }
73
74 static void Imainh(AviMainHeader *mainh)
75 {
76         invert(&mainh->fcc);
77         invert(&mainh->size);
78         invert(&mainh->MicroSecPerFrame);
79         invert(&mainh->MaxBytesPerSec);
80         invert(&mainh->PaddingGranularity);
81         invert(&mainh->Flags);
82         invert(&mainh->TotalFrames);
83         invert(&mainh->InitialFrames);
84         invert(&mainh->Streams);
85         invert(&mainh->SuggestedBufferSize);
86         invert(&mainh->Width);
87         invert(&mainh->Height);
88         invert(&mainh->Reserved[0]);
89         invert(&mainh->Reserved[1]);
90         invert(&mainh->Reserved[2]);
91         invert(&mainh->Reserved[3]);
92 }
93
94 static void Istreamh(AviStreamHeader *streamh)
95 {
96         invert(&streamh->fcc);
97         invert(&streamh->size);
98         invert(&streamh->Type);
99         invert(&streamh->Handler);
100         invert(&streamh->Flags);
101         sinvert(&streamh->Priority);
102         sinvert(&streamh->Language);
103         invert(&streamh->InitialFrames);
104         invert(&streamh->Scale);
105         invert(&streamh->Rate);
106         invert(&streamh->Start);
107         invert(&streamh->Length);
108         invert(&streamh->SuggestedBufferSize);
109         invert(&streamh->Quality);
110         invert(&streamh->SampleSize);
111         sinvert(&streamh->left);
112         sinvert(&streamh->right);
113         sinvert(&streamh->top);
114         sinvert(&streamh->bottom);
115 }
116
117 static void Ibitmaph(AviBitmapInfoHeader *bitmaph)
118 {
119         invert(&bitmaph->fcc);
120         invert(&bitmaph->size);
121         invert(&bitmaph->Size);
122         invert(&bitmaph->Width);
123         invert(&bitmaph->Height);
124         sinvert(&bitmaph->Planes);
125         sinvert(&bitmaph->BitCount);
126         invert(&bitmaph->Compression);
127         invert(&bitmaph->SizeImage);
128         invert(&bitmaph->XPelsPerMeter);
129         invert(&bitmaph->YPelsPerMeter);
130         invert(&bitmaph->ClrUsed);
131         invert(&bitmaph->ClrImportant);
132 }
133
134 static void Imjpegu(AviMJPEGUnknown *mjpgu)
135 {
136         invert(&mjpgu->a);
137         invert(&mjpgu->b);
138         invert(&mjpgu->c);
139         invert(&mjpgu->d);
140         invert(&mjpgu->e);
141         invert(&mjpgu->f);
142         invert(&mjpgu->g);
143 }
144
145 static void Iindexe(AviIndexEntry *indexe)
146 {
147         invert(&indexe->ChunkId);
148         invert(&indexe->Flags);
149         invert(&indexe->Offset);
150         invert(&indexe->Size);
151 }
152 #endif /* __BIG_ENDIAN__ */
153
154 void awrite(AviMovie *movie, void *datain, int block, int size, FILE *fp, int type)
155 {
156 #ifdef __BIG_ENDIAN__
157         void *data;
158
159         data = MEM_mallocN(size, "avi endian");
160
161         memcpy(data, datain, size);
162
163         switch (type) {
164                 case AVI_RAW:
165                         fwrite(data, block, size, fp);
166                         break;
167                 case AVI_CHUNK:
168                         Ichunk((AviChunk *) data);
169                         fwrite(data, block, size, fp);
170                         break;
171                 case AVI_LIST:
172                         Ilist((AviList *) data);
173                         fwrite(data, block, size, fp);
174                         break;
175                 case AVI_MAINH:
176                         Imainh((AviMainHeader *) data);
177                         fwrite(data, block, size, fp);
178                         break;
179                 case AVI_STREAMH:
180                         Istreamh((AviStreamHeader *) data);
181                         fwrite(data, block, size, fp);
182                         break;
183                 case AVI_BITMAPH:
184                         Ibitmaph((AviBitmapInfoHeader *) data);
185                         if (size == sizeof(AviBitmapInfoHeader) + sizeof(AviMJPEGUnknown)) {
186                                 Imjpegu((AviMJPEGUnknown *)((char *)data + sizeof(AviBitmapInfoHeader)));
187                         }
188                         fwrite(data, block, size, fp);
189                         break;
190                 case AVI_MJPEGU:
191                         Imjpegu((AviMJPEGUnknown *) data);
192                         fwrite(data, block, size, fp);
193                         break;
194                 case AVI_INDEXE:
195                         Iindexe((AviIndexEntry *) data);
196                         fwrite(data, block, size, fp);
197                         break;
198                 default:
199                         break;
200         }
201
202         MEM_freeN(data);
203 #else /* __BIG_ENDIAN__ */
204         (void)movie; /* unused */
205         (void)type; /* unused */
206         fwrite(datain, block, size, fp);
207 #endif /* __BIG_ENDIAN__ */
208 }