MK(BE)TAG: avoid undefined shifts
[ffmpeg.git] / libavutil / pixdesc.c
1 /*
2  * pixel format descriptor
3  * Copyright (c) 2009 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include "pixfmt.h"
25 #include "pixdesc.h"
26
27 #include "intreadwrite.h"
28
29 void av_read_image_line(uint16_t *dst, const uint8_t *data[4], const int linesize[4],
30                         const AVPixFmtDescriptor *desc, int x, int y, int c, int w, int read_pal_component)
31 {
32     AVComponentDescriptor comp= desc->comp[c];
33     int plane= comp.plane;
34     int depth= comp.depth_minus1+1;
35     int mask = (1<<depth)-1;
36     int shift= comp.shift;
37     int step = comp.step_minus1+1;
38     int flags= desc->flags;
39
40     if (flags & PIX_FMT_BITSTREAM){
41         int skip = x*step + comp.offset_plus1-1;
42         const uint8_t *p = data[plane] + y*linesize[plane] + (skip>>3);
43         int shift = 8 - depth - (skip&7);
44
45         while(w--){
46             int val = (*p >> shift) & mask;
47             if(read_pal_component)
48                 val= data[1][4*val + c];
49             shift -= step;
50             p -= shift>>3;
51             shift &= 7;
52             *dst++= val;
53         }
54     } else {
55         const uint8_t *p = data[plane]+ y*linesize[plane] + x*step + comp.offset_plus1-1;
56         int is_8bit = shift + depth <= 8;
57
58         if (is_8bit)
59             p += !!(flags & PIX_FMT_BE);
60
61         while(w--){
62             int val = is_8bit ? *p :
63                 flags & PIX_FMT_BE ? AV_RB16(p) : AV_RL16(p);
64             val = (val>>shift) & mask;
65             if(read_pal_component)
66                 val= data[1][4*val + c];
67             p+= step;
68             *dst++= val;
69         }
70     }
71 }
72
73 void av_write_image_line(const uint16_t *src, uint8_t *data[4], const int linesize[4],
74                          const AVPixFmtDescriptor *desc, int x, int y, int c, int w)
75 {
76     AVComponentDescriptor comp = desc->comp[c];
77     int plane = comp.plane;
78     int depth = comp.depth_minus1+1;
79     int step  = comp.step_minus1+1;
80     int flags = desc->flags;
81
82     if (flags & PIX_FMT_BITSTREAM) {
83         int skip = x*step + comp.offset_plus1-1;
84         uint8_t *p = data[plane] + y*linesize[plane] + (skip>>3);
85         int shift = 8 - depth - (skip&7);
86
87         while (w--) {
88             *p |= *src++ << shift;
89             shift -= step;
90             p -= shift>>3;
91             shift &= 7;
92         }
93     } else {
94         int shift = comp.shift;
95         uint8_t *p = data[plane]+ y*linesize[plane] + x*step + comp.offset_plus1-1;
96
97         if (shift + depth <= 8) {
98             p += !!(flags & PIX_FMT_BE);
99             while (w--) {
100                 *p |= (*src++<<shift);
101                 p += step;
102             }
103         } else {
104             while (w--) {
105                 if (flags & PIX_FMT_BE) {
106                     uint16_t val = AV_RB16(p) | (*src++<<shift);
107                     AV_WB16(p, val);
108                 } else {
109                     uint16_t val = AV_RL16(p) | (*src++<<shift);
110                     AV_WL16(p, val);
111                 }
112                 p+= step;
113             }
114         }
115     }
116 }
117
118 const AVPixFmtDescriptor av_pix_fmt_descriptors[PIX_FMT_NB] = {
119     [PIX_FMT_YUV420P] = {
120         .name = "yuv420p",
121         .nb_components= 3,
122         .log2_chroma_w= 1,
123         .log2_chroma_h= 1,
124         .comp = {
125             {0,0,1,0,7},        /* Y */
126             {1,0,1,0,7},        /* U */
127             {2,0,1,0,7},        /* V */
128         },
129         .flags = PIX_FMT_PLANAR,
130     },
131     [PIX_FMT_YUYV422] = {
132         .name = "yuyv422",
133         .nb_components= 3,
134         .log2_chroma_w= 1,
135         .log2_chroma_h= 0,
136         .comp = {
137             {0,1,1,0,7},        /* Y */
138             {0,3,2,0,7},        /* U */
139             {0,3,4,0,7},        /* V */
140         },
141     },
142     [PIX_FMT_RGB24] = {
143         .name = "rgb24",
144         .nb_components= 3,
145         .log2_chroma_w= 0,
146         .log2_chroma_h= 0,
147         .comp = {
148             {0,2,1,0,7},        /* R */
149             {0,2,2,0,7},        /* G */
150             {0,2,3,0,7},        /* B */
151         },
152         .flags = PIX_FMT_RGB,
153     },
154     [PIX_FMT_BGR24] = {
155         .name = "bgr24",
156         .nb_components= 3,
157         .log2_chroma_w= 0,
158         .log2_chroma_h= 0,
159         .comp = {
160             {0,2,1,0,7},        /* B */
161             {0,2,2,0,7},        /* G */
162             {0,2,3,0,7},        /* R */
163         },
164         .flags = PIX_FMT_RGB,
165     },
166     [PIX_FMT_YUV422P] = {
167         .name = "yuv422p",
168         .nb_components= 3,
169         .log2_chroma_w= 1,
170         .log2_chroma_h= 0,
171         .comp = {
172             {0,0,1,0,7},        /* Y */
173             {1,0,1,0,7},        /* U */
174             {2,0,1,0,7},        /* V */
175         },
176         .flags = PIX_FMT_PLANAR,
177     },
178     [PIX_FMT_YUV444P] = {
179         .name = "yuv444p",
180         .nb_components= 3,
181         .log2_chroma_w= 0,
182         .log2_chroma_h= 0,
183         .comp = {
184             {0,0,1,0,7},        /* Y */
185             {1,0,1,0,7},        /* U */
186             {2,0,1,0,7},        /* V */
187         },
188         .flags = PIX_FMT_PLANAR,
189     },
190     [PIX_FMT_YUV410P] = {
191         .name = "yuv410p",
192         .nb_components= 3,
193         .log2_chroma_w= 2,
194         .log2_chroma_h= 2,
195         .comp = {
196             {0,0,1,0,7},        /* Y */
197             {1,0,1,0,7},        /* U */
198             {2,0,1,0,7},        /* V */
199         },
200         .flags = PIX_FMT_PLANAR,
201     },
202     [PIX_FMT_YUV411P] = {
203         .name = "yuv411p",
204         .nb_components= 3,
205         .log2_chroma_w= 2,
206         .log2_chroma_h= 0,
207         .comp = {
208             {0,0,1,0,7},        /* Y */
209             {1,0,1,0,7},        /* U */
210             {2,0,1,0,7},        /* V */
211         },
212         .flags = PIX_FMT_PLANAR,
213     },
214     [PIX_FMT_GRAY8] = {
215         .name = "gray",
216         .nb_components= 1,
217         .log2_chroma_w= 0,
218         .log2_chroma_h= 0,
219         .comp = {
220             {0,0,1,0,7},        /* Y */
221         },
222     },
223     [PIX_FMT_MONOWHITE] = {
224         .name = "monow",
225         .nb_components= 1,
226         .log2_chroma_w= 0,
227         .log2_chroma_h= 0,
228         .comp = {
229             {0,0,1,0,0},        /* Y */
230         },
231         .flags = PIX_FMT_BITSTREAM,
232     },
233     [PIX_FMT_MONOBLACK] = {
234         .name = "monob",
235         .nb_components= 1,
236         .log2_chroma_w= 0,
237         .log2_chroma_h= 0,
238         .comp = {
239             {0,0,1,7,0},        /* Y */
240         },
241         .flags = PIX_FMT_BITSTREAM,
242     },
243     [PIX_FMT_PAL8] = {
244         .name = "pal8",
245         .nb_components= 1,
246         .log2_chroma_w= 0,
247         .log2_chroma_h= 0,
248         .comp = {
249             {0,0,1,0,7},
250         },
251         .flags = PIX_FMT_PAL,
252     },
253     [PIX_FMT_YUVJ420P] = {
254         .name = "yuvj420p",
255         .nb_components= 3,
256         .log2_chroma_w= 1,
257         .log2_chroma_h= 1,
258         .comp = {
259             {0,0,1,0,7},        /* Y */
260             {1,0,1,0,7},        /* U */
261             {2,0,1,0,7},        /* V */
262         },
263         .flags = PIX_FMT_PLANAR,
264     },
265     [PIX_FMT_YUVJ422P] = {
266         .name = "yuvj422p",
267         .nb_components= 3,
268         .log2_chroma_w= 1,
269         .log2_chroma_h= 0,
270         .comp = {
271             {0,0,1,0,7},        /* Y */
272             {1,0,1,0,7},        /* U */
273             {2,0,1,0,7},        /* V */
274         },
275         .flags = PIX_FMT_PLANAR,
276     },
277     [PIX_FMT_YUVJ444P] = {
278         .name = "yuvj444p",
279         .nb_components= 3,
280         .log2_chroma_w= 0,
281         .log2_chroma_h= 0,
282         .comp = {
283             {0,0,1,0,7},        /* Y */
284             {1,0,1,0,7},        /* U */
285             {2,0,1,0,7},        /* V */
286         },
287         .flags = PIX_FMT_PLANAR,
288     },
289     [PIX_FMT_XVMC_MPEG2_MC] = {
290         .name = "xvmcmc",
291         .flags = PIX_FMT_HWACCEL,
292     },
293     [PIX_FMT_XVMC_MPEG2_IDCT] = {
294         .name = "xvmcidct",
295         .flags = PIX_FMT_HWACCEL,
296     },
297     [PIX_FMT_UYVY422] = {
298         .name = "uyvy422",
299         .nb_components= 3,
300         .log2_chroma_w= 1,
301         .log2_chroma_h= 0,
302         .comp = {
303             {0,1,2,0,7},        /* Y */
304             {0,3,1,0,7},        /* U */
305             {0,3,3,0,7},        /* V */
306         },
307     },
308     [PIX_FMT_UYYVYY411] = {
309         .name = "uyyvyy411",
310         .nb_components= 3,
311         .log2_chroma_w= 2,
312         .log2_chroma_h= 0,
313         .comp = {
314             {0,3,2,0,7},        /* Y */
315             {0,5,1,0,7},        /* U */
316             {0,5,4,0,7},        /* V */
317         },
318     },
319     [PIX_FMT_BGR8] = {
320         .name = "bgr8",
321         .nb_components= 3,
322         .log2_chroma_w= 0,
323         .log2_chroma_h= 0,
324         .comp = {
325             {0,0,1,6,1},        /* B */
326             {0,0,1,3,2},        /* G */
327             {0,0,1,0,2},        /* R */
328         },
329         .flags = PIX_FMT_PAL | PIX_FMT_RGB,
330     },
331     [PIX_FMT_BGR4] = {
332         .name = "bgr4",
333         .nb_components= 3,
334         .log2_chroma_w= 0,
335         .log2_chroma_h= 0,
336         .comp = {
337             {0,3,1,0,0},        /* B */
338             {0,3,2,0,1},        /* G */
339             {0,3,4,0,0},        /* R */
340         },
341         .flags = PIX_FMT_BITSTREAM | PIX_FMT_RGB,
342     },
343     [PIX_FMT_BGR4_BYTE] = {
344         .name = "bgr4_byte",
345         .nb_components= 3,
346         .log2_chroma_w= 0,
347         .log2_chroma_h= 0,
348         .comp = {
349             {0,0,1,3,0},        /* B */
350             {0,0,1,1,1},        /* G */
351             {0,0,1,0,0},        /* R */
352         },
353         .flags = PIX_FMT_PAL | PIX_FMT_RGB,
354     },
355     [PIX_FMT_RGB8] = {
356         .name = "rgb8",
357         .nb_components= 3,
358         .log2_chroma_w= 0,
359         .log2_chroma_h= 0,
360         .comp = {
361             {0,0,1,6,1},        /* R */
362             {0,0,1,3,2},        /* G */
363             {0,0,1,0,2},        /* B */
364         },
365         .flags = PIX_FMT_PAL | PIX_FMT_RGB,
366     },
367     [PIX_FMT_RGB4] = {
368         .name = "rgb4",
369         .nb_components= 3,
370         .log2_chroma_w= 0,
371         .log2_chroma_h= 0,
372         .comp = {
373             {0,3,1,0,0},       /* R */
374             {0,3,2,0,1},       /* G */
375             {0,3,4,0,0},       /* B */
376         },
377         .flags = PIX_FMT_BITSTREAM | PIX_FMT_RGB,
378     },
379     [PIX_FMT_RGB4_BYTE] = {
380         .name = "rgb4_byte",
381         .nb_components= 3,
382         .log2_chroma_w= 0,
383         .log2_chroma_h= 0,
384         .comp = {
385             {0,0,1,3,0},        /* R */
386             {0,0,1,1,1},        /* G */
387             {0,0,1,0,0},        /* B */
388         },
389         .flags = PIX_FMT_PAL | PIX_FMT_RGB,
390     },
391     [PIX_FMT_NV12] = {
392         .name = "nv12",
393         .nb_components= 3,
394         .log2_chroma_w= 1,
395         .log2_chroma_h= 1,
396         .comp = {
397             {0,0,1,0,7},        /* Y */
398             {1,1,1,0,7},        /* U */
399             {1,1,2,0,7},        /* V */
400         },
401         .flags = PIX_FMT_PLANAR,
402     },
403     [PIX_FMT_NV21] = {
404         .name = "nv21",
405         .nb_components= 3,
406         .log2_chroma_w= 1,
407         .log2_chroma_h= 1,
408         .comp = {
409             {0,0,1,0,7},        /* Y */
410             {1,1,1,0,7},        /* V */
411             {1,1,2,0,7},        /* U */
412         },
413         .flags = PIX_FMT_PLANAR,
414     },
415     [PIX_FMT_ARGB] = {
416         .name = "argb",
417         .nb_components= 4,
418         .log2_chroma_w= 0,
419         .log2_chroma_h= 0,
420         .comp = {
421             {0,3,1,0,7},        /* A */
422             {0,3,2,0,7},        /* R */
423             {0,3,3,0,7},        /* G */
424             {0,3,4,0,7},        /* B */
425         },
426         .flags = PIX_FMT_RGB,
427     },
428     [PIX_FMT_RGBA] = {
429         .name = "rgba",
430         .nb_components= 4,
431         .log2_chroma_w= 0,
432         .log2_chroma_h= 0,
433         .comp = {
434             {0,3,1,0,7},        /* R */
435             {0,3,2,0,7},        /* G */
436             {0,3,3,0,7},        /* B */
437             {0,3,4,0,7},        /* A */
438         },
439         .flags = PIX_FMT_RGB,
440     },
441     [PIX_FMT_ABGR] = {
442         .name = "abgr",
443         .nb_components= 4,
444         .log2_chroma_w= 0,
445         .log2_chroma_h= 0,
446         .comp = {
447             {0,3,1,0,7},        /* A */
448             {0,3,2,0,7},        /* B */
449             {0,3,3,0,7},        /* G */
450             {0,3,4,0,7},        /* R */
451         },
452         .flags = PIX_FMT_RGB,
453     },
454     [PIX_FMT_BGRA] = {
455         .name = "bgra",
456         .nb_components= 4,
457         .log2_chroma_w= 0,
458         .log2_chroma_h= 0,
459         .comp = {
460             {0,3,1,0,7},        /* B */
461             {0,3,2,0,7},        /* G */
462             {0,3,3,0,7},        /* R */
463             {0,3,4,0,7},        /* A */
464         },
465         .flags = PIX_FMT_RGB,
466     },
467     [PIX_FMT_GRAY16BE] = {
468         .name = "gray16be",
469         .nb_components= 1,
470         .log2_chroma_w= 0,
471         .log2_chroma_h= 0,
472         .comp = {
473             {0,1,1,0,15},       /* Y */
474         },
475         .flags = PIX_FMT_BE,
476     },
477     [PIX_FMT_GRAY16LE] = {
478         .name = "gray16le",
479         .nb_components= 1,
480         .log2_chroma_w= 0,
481         .log2_chroma_h= 0,
482         .comp = {
483             {0,1,1,0,15},       /* Y */
484         },
485     },
486     [PIX_FMT_YUV440P] = {
487         .name = "yuv440p",
488         .nb_components= 3,
489         .log2_chroma_w= 0,
490         .log2_chroma_h= 1,
491         .comp = {
492             {0,0,1,0,7},        /* Y */
493             {1,0,1,0,7},        /* U */
494             {2,0,1,0,7},        /* V */
495         },
496         .flags = PIX_FMT_PLANAR,
497     },
498     [PIX_FMT_YUVJ440P] = {
499         .name = "yuvj440p",
500         .nb_components= 3,
501         .log2_chroma_w= 0,
502         .log2_chroma_h= 1,
503         .comp = {
504             {0,0,1,0,7},        /* Y */
505             {1,0,1,0,7},        /* U */
506             {2,0,1,0,7},        /* V */
507         },
508         .flags = PIX_FMT_PLANAR,
509     },
510     [PIX_FMT_YUVA420P] = {
511         .name = "yuva420p",
512         .nb_components= 4,
513         .log2_chroma_w= 1,
514         .log2_chroma_h= 1,
515         .comp = {
516             {0,0,1,0,7},        /* Y */
517             {1,0,1,0,7},        /* U */
518             {2,0,1,0,7},        /* V */
519             {3,0,1,0,7},        /* A */
520         },
521         .flags = PIX_FMT_PLANAR,
522     },
523     [PIX_FMT_VDPAU_H264] = {
524         .name = "vdpau_h264",
525         .log2_chroma_w = 1,
526         .log2_chroma_h = 1,
527         .flags = PIX_FMT_HWACCEL,
528     },
529     [PIX_FMT_VDPAU_MPEG1] = {
530         .name = "vdpau_mpeg1",
531         .log2_chroma_w = 1,
532         .log2_chroma_h = 1,
533         .flags = PIX_FMT_HWACCEL,
534     },
535     [PIX_FMT_VDPAU_MPEG2] = {
536         .name = "vdpau_mpeg2",
537         .log2_chroma_w = 1,
538         .log2_chroma_h = 1,
539         .flags = PIX_FMT_HWACCEL,
540     },
541     [PIX_FMT_VDPAU_WMV3] = {
542         .name = "vdpau_wmv3",
543         .log2_chroma_w = 1,
544         .log2_chroma_h = 1,
545         .flags = PIX_FMT_HWACCEL,
546     },
547     [PIX_FMT_VDPAU_VC1] = {
548         .name = "vdpau_vc1",
549         .log2_chroma_w = 1,
550         .log2_chroma_h = 1,
551         .flags = PIX_FMT_HWACCEL,
552     },
553     [PIX_FMT_VDPAU_MPEG4] = {
554         .name = "vdpau_mpeg4",
555         .log2_chroma_w = 1,
556         .log2_chroma_h = 1,
557         .flags = PIX_FMT_HWACCEL,
558     },
559     [PIX_FMT_RGB48BE] = {
560         .name = "rgb48be",
561         .nb_components= 3,
562         .log2_chroma_w= 0,
563         .log2_chroma_h= 0,
564         .comp = {
565             {0,5,1,0,15},       /* R */
566             {0,5,3,0,15},       /* G */
567             {0,5,5,0,15},       /* B */
568         },
569         .flags = PIX_FMT_RGB | PIX_FMT_BE,
570     },
571     [PIX_FMT_RGB48LE] = {
572         .name = "rgb48le",
573         .nb_components= 3,
574         .log2_chroma_w= 0,
575         .log2_chroma_h= 0,
576         .comp = {
577             {0,5,1,0,15},       /* R */
578             {0,5,3,0,15},       /* G */
579             {0,5,5,0,15},       /* B */
580         },
581         .flags = PIX_FMT_RGB,
582     },
583     [PIX_FMT_RGB565BE] = {
584         .name = "rgb565be",
585         .nb_components= 3,
586         .log2_chroma_w= 0,
587         .log2_chroma_h= 0,
588         .comp = {
589             {0,1,0,3,4},        /* R */
590             {0,1,1,5,5},        /* G */
591             {0,1,1,0,4},        /* B */
592         },
593         .flags = PIX_FMT_BE | PIX_FMT_RGB,
594     },
595     [PIX_FMT_RGB565LE] = {
596         .name = "rgb565le",
597         .nb_components= 3,
598         .log2_chroma_w= 0,
599         .log2_chroma_h= 0,
600         .comp = {
601             {0,1,2,3,4},        /* R */
602             {0,1,1,5,5},        /* G */
603             {0,1,1,0,4},        /* B */
604         },
605         .flags = PIX_FMT_RGB,
606     },
607     [PIX_FMT_RGB555BE] = {
608         .name = "rgb555be",
609         .nb_components= 3,
610         .log2_chroma_w= 0,
611         .log2_chroma_h= 0,
612         .comp = {
613             {0,1,0,2,4},        /* R */
614             {0,1,1,5,4},        /* G */
615             {0,1,1,0,4},        /* B */
616         },
617         .flags = PIX_FMT_BE | PIX_FMT_RGB,
618     },
619     [PIX_FMT_RGB555LE] = {
620         .name = "rgb555le",
621         .nb_components= 3,
622         .log2_chroma_w= 0,
623         .log2_chroma_h= 0,
624         .comp = {
625             {0,1,2,2,4},        /* R */
626             {0,1,1,5,4},        /* G */
627             {0,1,1,0,4},        /* B */
628         },
629         .flags = PIX_FMT_RGB,
630     },
631     [PIX_FMT_RGB444BE] = {
632         .name = "rgb444be",
633         .nb_components= 3,
634         .log2_chroma_w= 0,
635         .log2_chroma_h= 0,
636         .comp = {
637             {0,1,0,0,3},        /* R */
638             {0,1,1,4,3},        /* G */
639             {0,1,1,0,3},        /* B */
640         },
641         .flags = PIX_FMT_BE | PIX_FMT_RGB,
642     },
643     [PIX_FMT_RGB444LE] = {
644         .name = "rgb444le",
645         .nb_components= 3,
646         .log2_chroma_w= 0,
647         .log2_chroma_h= 0,
648         .comp = {
649             {0,1,2,0,3},        /* R */
650             {0,1,1,4,3},        /* G */
651             {0,1,1,0,3},        /* B */
652         },
653         .flags = PIX_FMT_RGB,
654     },
655     [PIX_FMT_BGR48BE] = {
656         .name = "bgr48be",
657         .nb_components= 3,
658         .log2_chroma_w= 0,
659         .log2_chroma_h= 0,
660         .comp = {
661             {0,5,1,0,15},       /* B */
662             {0,5,3,0,15},       /* G */
663             {0,5,5,0,15},       /* R */
664         },
665         .flags = PIX_FMT_BE | PIX_FMT_RGB,
666     },
667     [PIX_FMT_BGR48LE] = {
668         .name = "bgr48le",
669         .nb_components= 3,
670         .log2_chroma_w= 0,
671         .log2_chroma_h= 0,
672         .comp = {
673             {0,5,1,0,15},       /* B */
674             {0,5,3,0,15},       /* G */
675             {0,5,5,0,15},       /* R */
676         },
677         .flags = PIX_FMT_RGB,
678     },
679     [PIX_FMT_BGR565BE] = {
680         .name = "bgr565be",
681         .nb_components= 3,
682         .log2_chroma_w= 0,
683         .log2_chroma_h= 0,
684         .comp = {
685             {0,1,0,3,4},        /* B */
686             {0,1,1,5,5},        /* G */
687             {0,1,1,0,4},        /* R */
688         },
689         .flags = PIX_FMT_BE | PIX_FMT_RGB,
690     },
691     [PIX_FMT_BGR565LE] = {
692         .name = "bgr565le",
693         .nb_components= 3,
694         .log2_chroma_w= 0,
695         .log2_chroma_h= 0,
696         .comp = {
697             {0,1,2,3,4},        /* B */
698             {0,1,1,5,5},        /* G */
699             {0,1,1,0,4},        /* R */
700         },
701         .flags = PIX_FMT_RGB,
702     },
703     [PIX_FMT_BGR555BE] = {
704         .name = "bgr555be",
705         .nb_components= 3,
706         .log2_chroma_w= 0,
707         .log2_chroma_h= 0,
708         .comp = {
709             {0,1,0,2,4},       /* B */
710             {0,1,1,5,4},       /* G */
711             {0,1,1,0,4},       /* R */
712         },
713         .flags = PIX_FMT_BE | PIX_FMT_RGB,
714      },
715     [PIX_FMT_BGR555LE] = {
716         .name = "bgr555le",
717         .nb_components= 3,
718         .log2_chroma_w= 0,
719         .log2_chroma_h= 0,
720         .comp = {
721             {0,1,2,2,4},        /* B */
722             {0,1,1,5,4},        /* G */
723             {0,1,1,0,4},        /* R */
724         },
725         .flags = PIX_FMT_RGB,
726     },
727     [PIX_FMT_BGR444BE] = {
728         .name = "bgr444be",
729         .nb_components= 3,
730         .log2_chroma_w= 0,
731         .log2_chroma_h= 0,
732         .comp = {
733             {0,1,0,0,3},       /* B */
734             {0,1,1,4,3},       /* G */
735             {0,1,1,0,3},       /* R */
736         },
737         .flags = PIX_FMT_BE | PIX_FMT_RGB,
738      },
739     [PIX_FMT_BGR444LE] = {
740         .name = "bgr444le",
741         .nb_components= 3,
742         .log2_chroma_w= 0,
743         .log2_chroma_h= 0,
744         .comp = {
745             {0,1,2,0,3},        /* B */
746             {0,1,1,4,3},        /* G */
747             {0,1,1,0,3},        /* R */
748         },
749         .flags = PIX_FMT_RGB,
750     },
751     [PIX_FMT_VAAPI_MOCO] = {
752         .name = "vaapi_moco",
753         .log2_chroma_w = 1,
754         .log2_chroma_h = 1,
755         .flags = PIX_FMT_HWACCEL,
756     },
757     [PIX_FMT_VAAPI_IDCT] = {
758         .name = "vaapi_idct",
759         .log2_chroma_w = 1,
760         .log2_chroma_h = 1,
761         .flags = PIX_FMT_HWACCEL,
762     },
763     [PIX_FMT_VAAPI_VLD] = {
764         .name = "vaapi_vld",
765         .log2_chroma_w = 1,
766         .log2_chroma_h = 1,
767         .flags = PIX_FMT_HWACCEL,
768     },
769     [PIX_FMT_VDA_VLD] = {
770         .name = "vda_vld",
771         .log2_chroma_w = 1,
772         .log2_chroma_h = 1,
773         .flags = PIX_FMT_HWACCEL,
774     },
775     [PIX_FMT_YUV420P9LE] = {
776         .name = "yuv420p9le",
777         .nb_components= 3,
778         .log2_chroma_w= 1,
779         .log2_chroma_h= 1,
780         .comp = {
781             {0,1,1,0,8},        /* Y */
782             {1,1,1,0,8},        /* U */
783             {2,1,1,0,8},        /* V */
784         },
785         .flags = PIX_FMT_PLANAR,
786     },
787     [PIX_FMT_YUV420P9BE] = {
788         .name = "yuv420p9be",
789         .nb_components= 3,
790         .log2_chroma_w= 1,
791         .log2_chroma_h= 1,
792         .comp = {
793             {0,1,1,0,8},        /* Y */
794             {1,1,1,0,8},        /* U */
795             {2,1,1,0,8},        /* V */
796         },
797         .flags = PIX_FMT_BE | PIX_FMT_PLANAR,
798     },
799     [PIX_FMT_YUV420P10LE] = {
800         .name = "yuv420p10le",
801         .nb_components= 3,
802         .log2_chroma_w= 1,
803         .log2_chroma_h= 1,
804         .comp = {
805             {0,1,1,0,9},        /* Y */
806             {1,1,1,0,9},        /* U */
807             {2,1,1,0,9},        /* V */
808         },
809         .flags = PIX_FMT_PLANAR,
810     },
811     [PIX_FMT_YUV420P10BE] = {
812         .name = "yuv420p10be",
813         .nb_components= 3,
814         .log2_chroma_w= 1,
815         .log2_chroma_h= 1,
816         .comp = {
817             {0,1,1,0,9},        /* Y */
818             {1,1,1,0,9},        /* U */
819             {2,1,1,0,9},        /* V */
820         },
821         .flags = PIX_FMT_BE | PIX_FMT_PLANAR,
822     },
823     [PIX_FMT_YUV420P16LE] = {
824         .name = "yuv420p16le",
825         .nb_components= 3,
826         .log2_chroma_w= 1,
827         .log2_chroma_h= 1,
828         .comp = {
829             {0,1,1,0,15},        /* Y */
830             {1,1,1,0,15},        /* U */
831             {2,1,1,0,15},        /* V */
832         },
833         .flags = PIX_FMT_PLANAR,
834     },
835     [PIX_FMT_YUV420P16BE] = {
836         .name = "yuv420p16be",
837         .nb_components= 3,
838         .log2_chroma_w= 1,
839         .log2_chroma_h= 1,
840         .comp = {
841             {0,1,1,0,15},        /* Y */
842             {1,1,1,0,15},        /* U */
843             {2,1,1,0,15},        /* V */
844         },
845         .flags = PIX_FMT_BE | PIX_FMT_PLANAR,
846     },
847     [PIX_FMT_YUV422P9LE] = {
848         .name = "yuv422p9le",
849         .nb_components= 3,
850         .log2_chroma_w= 1,
851         .log2_chroma_h= 0,
852         .comp = {
853             {0,1,1,0,8},        /* Y */
854             {1,1,1,0,8},        /* U */
855             {2,1,1,0,8},        /* V */
856         },
857         .flags = PIX_FMT_PLANAR,
858     },
859     [PIX_FMT_YUV422P9BE] = {
860         .name = "yuv422p9be",
861         .nb_components= 3,
862         .log2_chroma_w= 1,
863         .log2_chroma_h= 0,
864         .comp = {
865             {0,1,1,0,8},        /* Y */
866             {1,1,1,0,8},        /* U */
867             {2,1,1,0,8},        /* V */
868         },
869         .flags = PIX_FMT_BE | PIX_FMT_PLANAR,
870     },
871     [PIX_FMT_YUV422P10LE] = {
872         .name = "yuv422p10le",
873         .nb_components= 3,
874         .log2_chroma_w= 1,
875         .log2_chroma_h= 0,
876         .comp = {
877             {0,1,1,0,9},        /* Y */
878             {1,1,1,0,9},        /* U */
879             {2,1,1,0,9},        /* V */
880         },
881         .flags = PIX_FMT_PLANAR,
882     },
883     [PIX_FMT_YUV422P10BE] = {
884         .name = "yuv422p10be",
885         .nb_components= 3,
886         .log2_chroma_w= 1,
887         .log2_chroma_h= 0,
888         .comp = {
889             {0,1,1,0,9},        /* Y */
890             {1,1,1,0,9},        /* U */
891             {2,1,1,0,9},        /* V */
892         },
893         .flags = PIX_FMT_BE | PIX_FMT_PLANAR,
894     },
895     [PIX_FMT_YUV422P16LE] = {
896         .name = "yuv422p16le",
897         .nb_components= 3,
898         .log2_chroma_w= 1,
899         .log2_chroma_h= 0,
900         .comp = {
901             {0,1,1,0,15},        /* Y */
902             {1,1,1,0,15},        /* U */
903             {2,1,1,0,15},        /* V */
904         },
905         .flags = PIX_FMT_PLANAR,
906     },
907     [PIX_FMT_YUV422P16BE] = {
908         .name = "yuv422p16be",
909         .nb_components= 3,
910         .log2_chroma_w= 1,
911         .log2_chroma_h= 0,
912         .comp = {
913             {0,1,1,0,15},        /* Y */
914             {1,1,1,0,15},        /* U */
915             {2,1,1,0,15},        /* V */
916         },
917         .flags = PIX_FMT_BE | PIX_FMT_PLANAR,
918     },
919     [PIX_FMT_YUV444P16LE] = {
920         .name = "yuv444p16le",
921         .nb_components= 3,
922         .log2_chroma_w= 0,
923         .log2_chroma_h= 0,
924         .comp = {
925             {0,1,1,0,15},        /* Y */
926             {1,1,1,0,15},        /* U */
927             {2,1,1,0,15},        /* V */
928         },
929         .flags = PIX_FMT_PLANAR,
930     },
931     [PIX_FMT_YUV444P16BE] = {
932         .name = "yuv444p16be",
933         .nb_components= 3,
934         .log2_chroma_w= 0,
935         .log2_chroma_h= 0,
936         .comp = {
937             {0,1,1,0,15},        /* Y */
938             {1,1,1,0,15},        /* U */
939             {2,1,1,0,15},        /* V */
940         },
941         .flags = PIX_FMT_BE | PIX_FMT_PLANAR,
942     },
943     [PIX_FMT_YUV444P10LE] = {
944         .name = "yuv444p10le",
945         .nb_components= 3,
946         .log2_chroma_w= 0,
947         .log2_chroma_h= 0,
948         .comp = {
949             {0,1,1,0,9},        /* Y */
950             {1,1,1,0,9},        /* U */
951             {2,1,1,0,9},        /* V */
952         },
953         .flags = PIX_FMT_PLANAR,
954     },
955     [PIX_FMT_YUV444P10BE] = {
956         .name = "yuv444p10be",
957         .nb_components= 3,
958         .log2_chroma_w= 0,
959         .log2_chroma_h= 0,
960         .comp = {
961             {0,1,1,0,9},        /* Y */
962             {1,1,1,0,9},        /* U */
963             {2,1,1,0,9},        /* V */
964         },
965         .flags = PIX_FMT_BE | PIX_FMT_PLANAR,
966     },
967     [PIX_FMT_YUV444P9LE] = {
968         .name = "yuv444p9le",
969         .nb_components= 3,
970         .log2_chroma_w= 0,
971         .log2_chroma_h= 0,
972         .comp = {
973             {0,1,1,0,8},        /* Y */
974             {1,1,1,0,8},        /* U */
975             {2,1,1,0,8},        /* V */
976         },
977         .flags = PIX_FMT_PLANAR,
978     },
979     [PIX_FMT_YUV444P9BE] = {
980         .name = "yuv444p9be",
981         .nb_components= 3,
982         .log2_chroma_w= 0,
983         .log2_chroma_h= 0,
984         .comp = {
985             {0,1,1,0,8},        /* Y */
986             {1,1,1,0,8},        /* U */
987             {2,1,1,0,8},        /* V */
988         },
989         .flags = PIX_FMT_BE | PIX_FMT_PLANAR,
990     },
991     [PIX_FMT_DXVA2_VLD] = {
992         .name = "dxva2_vld",
993         .log2_chroma_w = 1,
994         .log2_chroma_h = 1,
995         .flags = PIX_FMT_HWACCEL,
996     },
997     [PIX_FMT_Y400A] = {
998         .name = "y400a",
999         .nb_components= 2,
1000         .comp = {
1001             {0,1,1,0,7},        /* Y */
1002             {0,1,2,0,7},        /* A */
1003         },
1004     },
1005     [PIX_FMT_GBRP] = {
1006         .name = "gbrp",
1007         .nb_components= 3,
1008         .log2_chroma_w= 0,
1009         .log2_chroma_h= 0,
1010         .comp = {
1011             {0,0,1,0,7},        /* G */
1012             {1,0,1,0,7},        /* B */
1013             {2,0,1,0,7},        /* R */
1014         },
1015         .flags = PIX_FMT_PLANAR | PIX_FMT_RGB,
1016     },
1017     [PIX_FMT_GBRP9LE] = {
1018         .name = "gbrp9le",
1019         .nb_components= 3,
1020         .log2_chroma_w= 0,
1021         .log2_chroma_h= 0,
1022         .comp = {
1023             {0,1,1,0,8},        /* G */
1024             {1,1,1,0,8},        /* B */
1025             {2,1,1,0,8},        /* R */
1026         },
1027         .flags = PIX_FMT_PLANAR | PIX_FMT_RGB,
1028     },
1029     [PIX_FMT_GBRP9BE] = {
1030         .name = "gbrp9be",
1031         .nb_components= 3,
1032         .log2_chroma_w= 0,
1033         .log2_chroma_h= 0,
1034         .comp = {
1035             {0,1,1,0,8},        /* G */
1036             {1,1,1,0,8},        /* B */
1037             {2,1,1,0,8},        /* R */
1038         },
1039         .flags = PIX_FMT_BE | PIX_FMT_PLANAR | PIX_FMT_RGB,
1040     },
1041     [PIX_FMT_GBRP10LE] = {
1042         .name = "gbrp10le",
1043         .nb_components= 3,
1044         .log2_chroma_w= 0,
1045         .log2_chroma_h= 0,
1046         .comp = {
1047             {0,1,1,0,9},        /* G */
1048             {1,1,1,0,9},        /* B */
1049             {2,1,1,0,9},        /* R */
1050         },
1051         .flags = PIX_FMT_PLANAR | PIX_FMT_RGB,
1052     },
1053     [PIX_FMT_GBRP10BE] = {
1054         .name = "gbrp10be",
1055         .nb_components= 3,
1056         .log2_chroma_w= 0,
1057         .log2_chroma_h= 0,
1058         .comp = {
1059             {0,1,1,0,9},        /* G */
1060             {1,1,1,0,9},        /* B */
1061             {2,1,1,0,9},        /* R */
1062         },
1063         .flags = PIX_FMT_BE | PIX_FMT_PLANAR | PIX_FMT_RGB,
1064     },
1065     [PIX_FMT_GBRP16LE] = {
1066         .name = "gbrp16le",
1067         .nb_components= 3,
1068         .log2_chroma_w= 0,
1069         .log2_chroma_h= 0,
1070         .comp = {
1071             {0,1,1,0,15},       /* G */
1072             {1,1,1,0,15},       /* B */
1073             {2,1,1,0,15},       /* R */
1074         },
1075         .flags = PIX_FMT_PLANAR | PIX_FMT_RGB,
1076     },
1077     [PIX_FMT_GBRP16BE] = {
1078         .name = "gbrp16be",
1079         .nb_components= 3,
1080         .log2_chroma_w= 0,
1081         .log2_chroma_h= 0,
1082         .comp = {
1083             {0,1,1,0,15},       /* G */
1084             {1,1,1,0,15},       /* B */
1085             {2,1,1,0,15},       /* R */
1086         },
1087         .flags = PIX_FMT_BE | PIX_FMT_PLANAR | PIX_FMT_RGB,
1088     },
1089 };
1090
1091 static enum PixelFormat get_pix_fmt_internal(const char *name)
1092 {
1093     enum PixelFormat pix_fmt;
1094
1095     for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
1096         if (av_pix_fmt_descriptors[pix_fmt].name &&
1097             !strcmp(av_pix_fmt_descriptors[pix_fmt].name, name))
1098             return pix_fmt;
1099
1100     return PIX_FMT_NONE;
1101 }
1102
1103 const char *av_get_pix_fmt_name(enum PixelFormat pix_fmt)
1104 {
1105     return (unsigned)pix_fmt < PIX_FMT_NB ?
1106         av_pix_fmt_descriptors[pix_fmt].name : NULL;
1107 }
1108
1109 #if HAVE_BIGENDIAN
1110 #   define X_NE(be, le) be
1111 #else
1112 #   define X_NE(be, le) le
1113 #endif
1114
1115 enum PixelFormat av_get_pix_fmt(const char *name)
1116 {
1117     enum PixelFormat pix_fmt;
1118
1119     if (!strcmp(name, "rgb32"))
1120         name = X_NE("argb", "bgra");
1121     else if (!strcmp(name, "bgr32"))
1122         name = X_NE("abgr", "rgba");
1123
1124     pix_fmt = get_pix_fmt_internal(name);
1125     if (pix_fmt == PIX_FMT_NONE) {
1126         char name2[32];
1127
1128         snprintf(name2, sizeof(name2), "%s%s", name, X_NE("be", "le"));
1129         pix_fmt = get_pix_fmt_internal(name2);
1130     }
1131     return pix_fmt;
1132 }
1133
1134 int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
1135 {
1136     int c, bits = 0;
1137     int log2_pixels = pixdesc->log2_chroma_w + pixdesc->log2_chroma_h;
1138
1139     for (c = 0; c < pixdesc->nb_components; c++) {
1140         int s = c==1 || c==2 ? 0 : log2_pixels;
1141         bits += (pixdesc->comp[c].depth_minus1+1) << s;
1142     }
1143
1144     return bits >> log2_pixels;
1145 }
1146
1147 char *av_get_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
1148 {
1149     /* print header */
1150     if (pix_fmt < 0) {
1151         snprintf (buf, buf_size, "name      " " nb_components" " nb_bits");
1152     } else {
1153         const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[pix_fmt];
1154         snprintf(buf, buf_size, "%-11s %7d %10d",
1155                  pixdesc->name, pixdesc->nb_components, av_get_bits_per_pixel(pixdesc));
1156     }
1157
1158     return buf;
1159 }