Merge remote-tracking branch 'qatar/master'
[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 FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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     },
130     [PIX_FMT_YUYV422] = {
131         .name = "yuyv422",
132         .nb_components= 3,
133         .log2_chroma_w= 1,
134         .log2_chroma_h= 0,
135         .comp = {
136             {0,1,1,0,7},        /* Y */
137             {0,3,2,0,7},        /* U */
138             {0,3,4,0,7},        /* V */
139         },
140     },
141     [PIX_FMT_RGB24] = {
142         .name = "rgb24",
143         .nb_components= 3,
144         .log2_chroma_w= 0,
145         .log2_chroma_h= 0,
146         .comp = {
147             {0,2,1,0,7},        /* R */
148             {0,2,2,0,7},        /* G */
149             {0,2,3,0,7},        /* B */
150         },
151     },
152     [PIX_FMT_BGR24] = {
153         .name = "bgr24",
154         .nb_components= 3,
155         .log2_chroma_w= 0,
156         .log2_chroma_h= 0,
157         .comp = {
158             {0,2,1,0,7},        /* B */
159             {0,2,2,0,7},        /* G */
160             {0,2,3,0,7},        /* R */
161         },
162     },
163     [PIX_FMT_YUV422P] = {
164         .name = "yuv422p",
165         .nb_components= 3,
166         .log2_chroma_w= 1,
167         .log2_chroma_h= 0,
168         .comp = {
169             {0,0,1,0,7},        /* Y */
170             {1,0,1,0,7},        /* U */
171             {2,0,1,0,7},        /* V */
172         },
173     },
174     [PIX_FMT_YUV444P] = {
175         .name = "yuv444p",
176         .nb_components= 3,
177         .log2_chroma_w= 0,
178         .log2_chroma_h= 0,
179         .comp = {
180             {0,0,1,0,7},        /* Y */
181             {1,0,1,0,7},        /* U */
182             {2,0,1,0,7},        /* V */
183         },
184     },
185     [PIX_FMT_YUV410P] = {
186         .name = "yuv410p",
187         .nb_components= 3,
188         .log2_chroma_w= 2,
189         .log2_chroma_h= 2,
190         .comp = {
191             {0,0,1,0,7},        /* Y */
192             {1,0,1,0,7},        /* U */
193             {2,0,1,0,7},        /* V */
194         },
195     },
196     [PIX_FMT_YUV411P] = {
197         .name = "yuv411p",
198         .nb_components= 3,
199         .log2_chroma_w= 2,
200         .log2_chroma_h= 0,
201         .comp = {
202             {0,0,1,0,7},        /* Y */
203             {1,0,1,0,7},        /* U */
204             {2,0,1,0,7},        /* V */
205         },
206     },
207     [PIX_FMT_GRAY8] = {
208         .name = "gray",
209         .nb_components= 1,
210         .log2_chroma_w= 0,
211         .log2_chroma_h= 0,
212         .comp = {
213             {0,0,1,0,7},        /* Y */
214         },
215         .flags = PIX_FMT_PAL,
216     },
217     [PIX_FMT_MONOWHITE] = {
218         .name = "monow",
219         .nb_components= 1,
220         .log2_chroma_w= 0,
221         .log2_chroma_h= 0,
222         .comp = {
223             {0,0,1,0,0},        /* Y */
224         },
225         .flags = PIX_FMT_BITSTREAM,
226     },
227     [PIX_FMT_MONOBLACK] = {
228         .name = "monob",
229         .nb_components= 1,
230         .log2_chroma_w= 0,
231         .log2_chroma_h= 0,
232         .comp = {
233             {0,0,1,7,0},        /* Y */
234         },
235         .flags = PIX_FMT_BITSTREAM,
236     },
237     [PIX_FMT_PAL8] = {
238         .name = "pal8",
239         .nb_components= 1,
240         .log2_chroma_w= 0,
241         .log2_chroma_h= 0,
242         .comp = {
243             {0,0,1,0,7},
244         },
245         .flags = PIX_FMT_PAL,
246     },
247     [PIX_FMT_YUVJ420P] = {
248         .name = "yuvj420p",
249         .nb_components= 3,
250         .log2_chroma_w= 1,
251         .log2_chroma_h= 1,
252         .comp = {
253             {0,0,1,0,7},        /* Y */
254             {1,0,1,0,7},        /* U */
255             {2,0,1,0,7},        /* V */
256         },
257     },
258     [PIX_FMT_YUVJ422P] = {
259         .name = "yuvj422p",
260         .nb_components= 3,
261         .log2_chroma_w= 1,
262         .log2_chroma_h= 0,
263         .comp = {
264             {0,0,1,0,7},        /* Y */
265             {1,0,1,0,7},        /* U */
266             {2,0,1,0,7},        /* V */
267         },
268     },
269     [PIX_FMT_YUVJ444P] = {
270         .name = "yuvj444p",
271         .nb_components= 3,
272         .log2_chroma_w= 0,
273         .log2_chroma_h= 0,
274         .comp = {
275             {0,0,1,0,7},        /* Y */
276             {1,0,1,0,7},        /* U */
277             {2,0,1,0,7},        /* V */
278         },
279     },
280     [PIX_FMT_XVMC_MPEG2_MC] = {
281         .name = "xvmcmc",
282         .flags = PIX_FMT_HWACCEL,
283     },
284     [PIX_FMT_XVMC_MPEG2_IDCT] = {
285         .name = "xvmcidct",
286         .flags = PIX_FMT_HWACCEL,
287     },
288     [PIX_FMT_UYVY422] = {
289         .name = "uyvy422",
290         .nb_components= 3,
291         .log2_chroma_w= 1,
292         .log2_chroma_h= 0,
293         .comp = {
294             {0,1,2,0,7},        /* Y */
295             {0,3,1,0,7},        /* U */
296             {0,3,3,0,7},        /* V */
297         },
298     },
299     [PIX_FMT_UYYVYY411] = {
300         .name = "uyyvyy411",
301         .nb_components= 3,
302         .log2_chroma_w= 2,
303         .log2_chroma_h= 0,
304         .comp = {
305             {0,3,2,0,7},        /* Y */
306             {0,5,1,0,7},        /* U */
307             {0,5,4,0,7},        /* V */
308         },
309     },
310     [PIX_FMT_BGR8] = {
311         .name = "bgr8",
312         .nb_components= 3,
313         .log2_chroma_w= 0,
314         .log2_chroma_h= 0,
315         .comp = {
316             {0,0,1,6,1},        /* B */
317             {0,0,1,3,2},        /* G */
318             {0,0,1,0,2},        /* R */
319         },
320         .flags = PIX_FMT_PAL,
321     },
322     [PIX_FMT_BGR4] = {
323         .name = "bgr4",
324         .nb_components= 3,
325         .log2_chroma_w= 0,
326         .log2_chroma_h= 0,
327         .comp = {
328             {0,3,1,0,0},        /* B */
329             {0,3,2,0,1},        /* G */
330             {0,3,4,0,0},        /* R */
331         },
332         .flags = PIX_FMT_BITSTREAM,
333     },
334     [PIX_FMT_BGR4_BYTE] = {
335         .name = "bgr4_byte",
336         .nb_components= 3,
337         .log2_chroma_w= 0,
338         .log2_chroma_h= 0,
339         .comp = {
340             {0,0,1,3,0},        /* B */
341             {0,0,1,1,1},        /* G */
342             {0,0,1,0,0},        /* R */
343         },
344         .flags = PIX_FMT_PAL,
345     },
346     [PIX_FMT_RGB8] = {
347         .name = "rgb8",
348         .nb_components= 3,
349         .log2_chroma_w= 0,
350         .log2_chroma_h= 0,
351         .comp = {
352             {0,0,1,6,1},        /* R */
353             {0,0,1,3,2},        /* G */
354             {0,0,1,0,2},        /* B */
355         },
356         .flags = PIX_FMT_PAL,
357     },
358     [PIX_FMT_RGB4] = {
359         .name = "rgb4",
360         .nb_components= 3,
361         .log2_chroma_w= 0,
362         .log2_chroma_h= 0,
363         .comp = {
364             {0,3,1,0,0},       /* R */
365             {0,3,2,0,1},       /* G */
366             {0,3,4,0,0},       /* B */
367         },
368         .flags = PIX_FMT_BITSTREAM,
369     },
370     [PIX_FMT_RGB4_BYTE] = {
371         .name = "rgb4_byte",
372         .nb_components= 3,
373         .log2_chroma_w= 0,
374         .log2_chroma_h= 0,
375         .comp = {
376             {0,0,1,3,0},        /* R */
377             {0,0,1,1,1},        /* G */
378             {0,0,1,0,0},        /* B */
379         },
380         .flags = PIX_FMT_PAL,
381     },
382     [PIX_FMT_NV12] = {
383         .name = "nv12",
384         .nb_components= 3,
385         .log2_chroma_w= 1,
386         .log2_chroma_h= 1,
387         .comp = {
388             {0,0,1,0,7},        /* Y */
389             {1,1,1,0,7},        /* U */
390             {1,1,2,0,7},        /* V */
391         },
392     },
393     [PIX_FMT_NV21] = {
394         .name = "nv21",
395         .nb_components= 3,
396         .log2_chroma_w= 1,
397         .log2_chroma_h= 1,
398         .comp = {
399             {0,0,1,0,7},        /* Y */
400             {1,1,1,0,7},        /* V */
401             {1,1,2,0,7},        /* U */
402         },
403     },
404     [PIX_FMT_ARGB] = {
405         .name = "argb",
406         .nb_components= 4,
407         .log2_chroma_w= 0,
408         .log2_chroma_h= 0,
409         .comp = {
410             {0,3,1,0,7},        /* A */
411             {0,3,2,0,7},        /* R */
412             {0,3,3,0,7},        /* G */
413             {0,3,4,0,7},        /* B */
414         },
415     },
416     [PIX_FMT_RGBA] = {
417         .name = "rgba",
418         .nb_components= 4,
419         .log2_chroma_w= 0,
420         .log2_chroma_h= 0,
421         .comp = {
422             {0,3,1,0,7},        /* R */
423             {0,3,2,0,7},        /* G */
424             {0,3,3,0,7},        /* B */
425             {0,3,4,0,7},        /* A */
426         },
427     },
428     [PIX_FMT_ABGR] = {
429         .name = "abgr",
430         .nb_components= 4,
431         .log2_chroma_w= 0,
432         .log2_chroma_h= 0,
433         .comp = {
434             {0,3,1,0,7},        /* A */
435             {0,3,2,0,7},        /* B */
436             {0,3,3,0,7},        /* G */
437             {0,3,4,0,7},        /* R */
438         },
439     },
440     [PIX_FMT_BGRA] = {
441         .name = "bgra",
442         .nb_components= 4,
443         .log2_chroma_w= 0,
444         .log2_chroma_h= 0,
445         .comp = {
446             {0,3,1,0,7},        /* B */
447             {0,3,2,0,7},        /* G */
448             {0,3,3,0,7},        /* R */
449             {0,3,4,0,7},        /* A */
450         },
451     },
452     [PIX_FMT_GRAY16BE] = {
453         .name = "gray16be",
454         .nb_components= 1,
455         .log2_chroma_w= 0,
456         .log2_chroma_h= 0,
457         .comp = {
458             {0,1,1,0,15},       /* Y */
459         },
460         .flags = PIX_FMT_BE,
461     },
462     [PIX_FMT_GRAY16LE] = {
463         .name = "gray16le",
464         .nb_components= 1,
465         .log2_chroma_w= 0,
466         .log2_chroma_h= 0,
467         .comp = {
468             {0,1,1,0,15},       /* Y */
469         },
470     },
471     [PIX_FMT_YUV440P] = {
472         .name = "yuv440p",
473         .nb_components= 3,
474         .log2_chroma_w= 0,
475         .log2_chroma_h= 1,
476         .comp = {
477             {0,0,1,0,7},        /* Y */
478             {1,0,1,0,7},        /* U */
479             {2,0,1,0,7},        /* V */
480         },
481     },
482     [PIX_FMT_YUVJ440P] = {
483         .name = "yuvj440p",
484         .nb_components= 3,
485         .log2_chroma_w= 0,
486         .log2_chroma_h= 1,
487         .comp = {
488             {0,0,1,0,7},        /* Y */
489             {1,0,1,0,7},        /* U */
490             {2,0,1,0,7},        /* V */
491         },
492     },
493     [PIX_FMT_YUVA420P] = {
494         .name = "yuva420p",
495         .nb_components= 4,
496         .log2_chroma_w= 1,
497         .log2_chroma_h= 1,
498         .comp = {
499             {0,0,1,0,7},        /* Y */
500             {1,0,1,0,7},        /* U */
501             {2,0,1,0,7},        /* V */
502             {3,0,1,0,7},        /* A */
503         },
504     },
505     [PIX_FMT_VDPAU_H264] = {
506         .name = "vdpau_h264",
507         .log2_chroma_w = 1,
508         .log2_chroma_h = 1,
509         .flags = PIX_FMT_HWACCEL,
510     },
511     [PIX_FMT_VDPAU_MPEG1] = {
512         .name = "vdpau_mpeg1",
513         .log2_chroma_w = 1,
514         .log2_chroma_h = 1,
515         .flags = PIX_FMT_HWACCEL,
516     },
517     [PIX_FMT_VDPAU_MPEG2] = {
518         .name = "vdpau_mpeg2",
519         .log2_chroma_w = 1,
520         .log2_chroma_h = 1,
521         .flags = PIX_FMT_HWACCEL,
522     },
523     [PIX_FMT_VDPAU_WMV3] = {
524         .name = "vdpau_wmv3",
525         .log2_chroma_w = 1,
526         .log2_chroma_h = 1,
527         .flags = PIX_FMT_HWACCEL,
528     },
529     [PIX_FMT_VDPAU_VC1] = {
530         .name = "vdpau_vc1",
531         .log2_chroma_w = 1,
532         .log2_chroma_h = 1,
533         .flags = PIX_FMT_HWACCEL,
534     },
535     [PIX_FMT_VDPAU_MPEG4] = {
536         .name = "vdpau_mpeg4",
537         .log2_chroma_w = 1,
538         .log2_chroma_h = 1,
539         .flags = PIX_FMT_HWACCEL,
540     },
541     [PIX_FMT_RGB48BE] = {
542         .name = "rgb48be",
543         .nb_components= 3,
544         .log2_chroma_w= 0,
545         .log2_chroma_h= 0,
546         .comp = {
547             {0,5,1,0,15},       /* R */
548             {0,5,3,0,15},       /* G */
549             {0,5,5,0,15},       /* B */
550         },
551         .flags = PIX_FMT_BE,
552     },
553     [PIX_FMT_RGB48LE] = {
554         .name = "rgb48le",
555         .nb_components= 3,
556         .log2_chroma_w= 0,
557         .log2_chroma_h= 0,
558         .comp = {
559             {0,5,1,0,15},       /* R */
560             {0,5,3,0,15},       /* G */
561             {0,5,5,0,15},       /* B */
562         },
563     },
564     [PIX_FMT_RGBA64BE] = {
565         .name = "rgba64be",
566         .nb_components= 4,
567         .log2_chroma_w= 0,
568         .log2_chroma_h= 0,
569         .comp = {
570             {0,5,1,0,15},       /* R */
571             {0,5,3,0,15},       /* G */
572             {0,5,5,0,15},       /* B */
573             {0,5,7,0,15},       /* A */
574         },
575         .flags = PIX_FMT_BE,
576     },
577     [PIX_FMT_RGBA64LE] = {
578         .name = "rgba64le",
579         .nb_components= 4,
580         .log2_chroma_w= 0,
581         .log2_chroma_h= 0,
582         .comp = {
583             {0,5,1,0,15},       /* R */
584             {0,5,3,0,15},       /* G */
585             {0,5,5,0,15},       /* B */
586             {0,5,7,0,15},       /* B */
587         },
588     },
589     [PIX_FMT_RGB565BE] = {
590         .name = "rgb565be",
591         .nb_components= 3,
592         .log2_chroma_w= 0,
593         .log2_chroma_h= 0,
594         .comp = {
595             {0,1,0,3,4},        /* R */
596             {0,1,1,5,5},        /* G */
597             {0,1,1,0,4},        /* B */
598         },
599         .flags = PIX_FMT_BE,
600     },
601     [PIX_FMT_RGB565LE] = {
602         .name = "rgb565le",
603         .nb_components= 3,
604         .log2_chroma_w= 0,
605         .log2_chroma_h= 0,
606         .comp = {
607             {0,1,2,3,4},        /* R */
608             {0,1,1,5,5},        /* G */
609             {0,1,1,0,4},        /* B */
610         },
611     },
612     [PIX_FMT_RGB555BE] = {
613         .name = "rgb555be",
614         .nb_components= 3,
615         .log2_chroma_w= 0,
616         .log2_chroma_h= 0,
617         .comp = {
618             {0,1,0,2,4},        /* R */
619             {0,1,1,5,4},        /* G */
620             {0,1,1,0,4},        /* B */
621         },
622         .flags = PIX_FMT_BE,
623     },
624     [PIX_FMT_RGB555LE] = {
625         .name = "rgb555le",
626         .nb_components= 3,
627         .log2_chroma_w= 0,
628         .log2_chroma_h= 0,
629         .comp = {
630             {0,1,2,2,4},        /* R */
631             {0,1,1,5,4},        /* G */
632             {0,1,1,0,4},        /* B */
633         },
634     },
635     [PIX_FMT_RGB444BE] = {
636         .name = "rgb444be",
637         .nb_components= 3,
638         .log2_chroma_w= 0,
639         .log2_chroma_h= 0,
640         .comp = {
641             {0,1,0,0,3},        /* R */
642             {0,1,1,4,3},        /* G */
643             {0,1,1,0,3},        /* B */
644         },
645         .flags = PIX_FMT_BE,
646     },
647     [PIX_FMT_RGB444LE] = {
648         .name = "rgb444le",
649         .nb_components= 3,
650         .log2_chroma_w= 0,
651         .log2_chroma_h= 0,
652         .comp = {
653             {0,1,2,0,3},        /* R */
654             {0,1,1,4,3},        /* G */
655             {0,1,1,0,3},        /* B */
656         },
657     },
658     [PIX_FMT_BGR48BE] = {
659         .name = "bgr48be",
660         .nb_components= 3,
661         .log2_chroma_w= 0,
662         .log2_chroma_h= 0,
663         .comp = {
664             {0,5,1,0,15},       /* B */
665             {0,5,3,0,15},       /* G */
666             {0,5,5,0,15},       /* R */
667         },
668         .flags = PIX_FMT_BE,
669     },
670     [PIX_FMT_BGR48LE] = {
671         .name = "bgr48le",
672         .nb_components= 3,
673         .log2_chroma_w= 0,
674         .log2_chroma_h= 0,
675         .comp = {
676             {0,5,1,0,15},       /* B */
677             {0,5,3,0,15},       /* G */
678             {0,5,5,0,15},       /* R */
679         },
680     },
681     [PIX_FMT_BGRA64BE] = {
682         .name = "bgra64be",
683         .nb_components= 4,
684         .log2_chroma_w= 0,
685         .log2_chroma_h= 0,
686         .comp = {
687             {0,5,1,0,15},       /* B */
688             {0,5,3,0,15},       /* G */
689             {0,5,5,0,15},       /* R */
690             {0,5,7,0,15},       /* A */
691         },
692         .flags = PIX_FMT_BE,
693     },
694     [PIX_FMT_BGRA64LE] = {
695         .name = "bgra64le",
696         .nb_components= 4,
697         .log2_chroma_w= 0,
698         .log2_chroma_h= 0,
699         .comp = {
700             {0,5,1,0,15},       /* B */
701             {0,5,3,0,15},       /* G */
702             {0,5,5,0,15},       /* R */
703             {0,5,7,0,15},       /* A */
704         },
705     },
706     [PIX_FMT_BGR565BE] = {
707         .name = "bgr565be",
708         .nb_components= 3,
709         .log2_chroma_w= 0,
710         .log2_chroma_h= 0,
711         .comp = {
712             {0,1,0,3,4},        /* B */
713             {0,1,1,5,5},        /* G */
714             {0,1,1,0,4},        /* R */
715         },
716         .flags = PIX_FMT_BE,
717     },
718     [PIX_FMT_BGR565LE] = {
719         .name = "bgr565le",
720         .nb_components= 3,
721         .log2_chroma_w= 0,
722         .log2_chroma_h= 0,
723         .comp = {
724             {0,1,2,3,4},        /* B */
725             {0,1,1,5,5},        /* G */
726             {0,1,1,0,4},        /* R */
727         },
728     },
729     [PIX_FMT_BGR555BE] = {
730         .name = "bgr555be",
731         .nb_components= 3,
732         .log2_chroma_w= 0,
733         .log2_chroma_h= 0,
734         .comp = {
735             {0,1,0,2,4},       /* B */
736             {0,1,1,5,4},       /* G */
737             {0,1,1,0,4},       /* R */
738         },
739         .flags = PIX_FMT_BE,
740      },
741     [PIX_FMT_BGR555LE] = {
742         .name = "bgr555le",
743         .nb_components= 3,
744         .log2_chroma_w= 0,
745         .log2_chroma_h= 0,
746         .comp = {
747             {0,1,2,2,4},        /* B */
748             {0,1,1,5,4},        /* G */
749             {0,1,1,0,4},        /* R */
750         },
751     },
752     [PIX_FMT_BGR444BE] = {
753         .name = "bgr444be",
754         .nb_components= 3,
755         .log2_chroma_w= 0,
756         .log2_chroma_h= 0,
757         .comp = {
758             {0,1,0,0,3},       /* B */
759             {0,1,1,4,3},       /* G */
760             {0,1,1,0,3},       /* R */
761         },
762         .flags = PIX_FMT_BE,
763      },
764     [PIX_FMT_BGR444LE] = {
765         .name = "bgr444le",
766         .nb_components= 3,
767         .log2_chroma_w= 0,
768         .log2_chroma_h= 0,
769         .comp = {
770             {0,1,2,0,3},        /* B */
771             {0,1,1,4,3},        /* G */
772             {0,1,1,0,3},        /* R */
773         },
774     },
775     [PIX_FMT_VAAPI_MOCO] = {
776         .name = "vaapi_moco",
777         .log2_chroma_w = 1,
778         .log2_chroma_h = 1,
779         .flags = PIX_FMT_HWACCEL,
780     },
781     [PIX_FMT_VAAPI_IDCT] = {
782         .name = "vaapi_idct",
783         .log2_chroma_w = 1,
784         .log2_chroma_h = 1,
785         .flags = PIX_FMT_HWACCEL,
786     },
787     [PIX_FMT_VAAPI_VLD] = {
788         .name = "vaapi_vld",
789         .log2_chroma_w = 1,
790         .log2_chroma_h = 1,
791         .flags = PIX_FMT_HWACCEL,
792     },
793     [PIX_FMT_YUV420P9LE] = {
794         .name = "yuv420p9le",
795         .nb_components= 3,
796         .log2_chroma_w= 1,
797         .log2_chroma_h= 1,
798         .comp = {
799             {0,1,1,0,8},        /* Y */
800             {1,1,1,0,8},        /* U */
801             {2,1,1,0,8},        /* V */
802         },
803     },
804     [PIX_FMT_YUV420P9BE] = {
805         .name = "yuv420p9be",
806         .nb_components= 3,
807         .log2_chroma_w= 1,
808         .log2_chroma_h= 1,
809         .comp = {
810             {0,1,1,0,8},        /* Y */
811             {1,1,1,0,8},        /* U */
812             {2,1,1,0,8},        /* V */
813         },
814         .flags = PIX_FMT_BE,
815     },
816     [PIX_FMT_YUV420P10LE] = {
817         .name = "yuv420p10le",
818         .nb_components= 3,
819         .log2_chroma_w= 1,
820         .log2_chroma_h= 1,
821         .comp = {
822             {0,1,1,0,9},        /* Y */
823             {1,1,1,0,9},        /* U */
824             {2,1,1,0,9},        /* V */
825         },
826     },
827     [PIX_FMT_YUV420P10BE] = {
828         .name = "yuv420p10be",
829         .nb_components= 3,
830         .log2_chroma_w= 1,
831         .log2_chroma_h= 1,
832         .comp = {
833             {0,1,1,0,9},        /* Y */
834             {1,1,1,0,9},        /* U */
835             {2,1,1,0,9},        /* V */
836         },
837         .flags = PIX_FMT_BE,
838     },
839     [PIX_FMT_YUV420P16LE] = {
840         .name = "yuv420p16le",
841         .nb_components= 3,
842         .log2_chroma_w= 1,
843         .log2_chroma_h= 1,
844         .comp = {
845             {0,1,1,0,15},        /* Y */
846             {1,1,1,0,15},        /* U */
847             {2,1,1,0,15},        /* V */
848         },
849     },
850     [PIX_FMT_YUV420P16BE] = {
851         .name = "yuv420p16be",
852         .nb_components= 3,
853         .log2_chroma_w= 1,
854         .log2_chroma_h= 1,
855         .comp = {
856             {0,1,1,0,15},        /* Y */
857             {1,1,1,0,15},        /* U */
858             {2,1,1,0,15},        /* V */
859         },
860         .flags = PIX_FMT_BE,
861     },
862     [PIX_FMT_YUV422P9LE] = {
863         .name = "yuv422p9le",
864         .nb_components= 3,
865         .log2_chroma_w= 1,
866         .log2_chroma_h= 0,
867         .comp = {
868             {0,1,1,0,8},        /* Y */
869             {1,1,1,0,8},        /* U */
870             {2,1,1,0,8},        /* V */
871         },
872     },
873     [PIX_FMT_YUV422P9BE] = {
874         .name = "yuv422p9be",
875         .nb_components= 3,
876         .log2_chroma_w= 1,
877         .log2_chroma_h= 0,
878         .comp = {
879             {0,1,1,0,8},        /* Y */
880             {1,1,1,0,8},        /* U */
881             {2,1,1,0,8},        /* V */
882         },
883         .flags = PIX_FMT_BE,
884     },
885     [PIX_FMT_YUV422P10LE] = {
886         .name = "yuv422p10le",
887         .nb_components= 3,
888         .log2_chroma_w= 1,
889         .log2_chroma_h= 0,
890         .comp = {
891             {0,1,1,0,9},        /* Y */
892             {1,1,1,0,9},        /* U */
893             {2,1,1,0,9},        /* V */
894         },
895     },
896     [PIX_FMT_YUV422P10BE] = {
897         .name = "yuv422p10be",
898         .nb_components= 3,
899         .log2_chroma_w= 1,
900         .log2_chroma_h= 0,
901         .comp = {
902             {0,1,1,0,9},        /* Y */
903             {1,1,1,0,9},        /* U */
904             {2,1,1,0,9},        /* V */
905         },
906         .flags = PIX_FMT_BE,
907     },
908     [PIX_FMT_YUV422P16LE] = {
909         .name = "yuv422p16le",
910         .nb_components= 3,
911         .log2_chroma_w= 1,
912         .log2_chroma_h= 0,
913         .comp = {
914             {0,1,1,0,15},        /* Y */
915             {1,1,1,0,15},        /* U */
916             {2,1,1,0,15},        /* V */
917         },
918     },
919     [PIX_FMT_YUV422P16BE] = {
920         .name = "yuv422p16be",
921         .nb_components= 3,
922         .log2_chroma_w= 1,
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_BE,
930     },
931     [PIX_FMT_YUV444P16LE] = {
932         .name = "yuv444p16le",
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     },
942     [PIX_FMT_YUV444P16BE] = {
943         .name = "yuv444p16be",
944         .nb_components= 3,
945         .log2_chroma_w= 0,
946         .log2_chroma_h= 0,
947         .comp = {
948             {0,1,1,0,15},        /* Y */
949             {1,1,1,0,15},        /* U */
950             {2,1,1,0,15},        /* V */
951         },
952         .flags = PIX_FMT_BE,
953     },
954     [PIX_FMT_YUV444P10LE] = {
955         .name = "yuv444p10le",
956         .nb_components= 3,
957         .log2_chroma_w= 0,
958         .log2_chroma_h= 0,
959         .comp = {
960             {0,1,1,0,9},        /* Y */
961             {1,1,1,0,9},        /* U */
962             {2,1,1,0,9},        /* V */
963         },
964     },
965     [PIX_FMT_YUV444P10BE] = {
966         .name = "yuv444p10be",
967         .nb_components= 3,
968         .log2_chroma_w= 0,
969         .log2_chroma_h= 0,
970         .comp = {
971             {0,1,1,0,9},        /* Y */
972             {1,1,1,0,9},        /* U */
973             {2,1,1,0,9},        /* V */
974         },
975         .flags = PIX_FMT_BE,
976     },
977     [PIX_FMT_YUV444P9LE] = {
978         .name = "yuv444p9le",
979         .nb_components= 3,
980         .log2_chroma_w= 0,
981         .log2_chroma_h= 0,
982         .comp = {
983             {0,1,1,0,8},        /* Y */
984             {1,1,1,0,8},        /* U */
985             {2,1,1,0,8},        /* V */
986         },
987     },
988     [PIX_FMT_YUV444P9BE] = {
989         .name = "yuv444p9be",
990         .nb_components= 3,
991         .log2_chroma_w= 0,
992         .log2_chroma_h= 0,
993         .comp = {
994             {0,1,1,0,8},        /* Y */
995             {1,1,1,0,8},        /* U */
996             {2,1,1,0,8},        /* V */
997         },
998         .flags = PIX_FMT_BE,
999     },
1000     [PIX_FMT_DXVA2_VLD] = {
1001         .name = "dxva2_vld",
1002         .log2_chroma_w = 1,
1003         .log2_chroma_h = 1,
1004         .flags = PIX_FMT_HWACCEL,
1005     },
1006     [PIX_FMT_GRAY8A] = {
1007         .name = "gray8a",
1008         .nb_components= 2,
1009         .comp = {
1010             {0,1,1,0,7},        /* Y */
1011             {0,1,2,0,7},        /* A */
1012         },
1013     },
1014     [PIX_FMT_GBR24P] = {
1015         .name = "gbr24p",
1016         .nb_components= 3,
1017         .comp = {
1018             {1,0,1,0,7},        /* B */
1019             {0,0,1,0,7},        /* G */
1020             {2,0,1,0,7},        /* R */
1021         },
1022     },
1023 };
1024
1025 static enum PixelFormat get_pix_fmt_internal(const char *name)
1026 {
1027     enum PixelFormat pix_fmt;
1028
1029     for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
1030         if (av_pix_fmt_descriptors[pix_fmt].name &&
1031             !strcmp(av_pix_fmt_descriptors[pix_fmt].name, name))
1032             return pix_fmt;
1033
1034     return PIX_FMT_NONE;
1035 }
1036
1037 const char *av_get_pix_fmt_name(enum PixelFormat pix_fmt)
1038 {
1039     return (unsigned)pix_fmt < PIX_FMT_NB ?
1040         av_pix_fmt_descriptors[pix_fmt].name : NULL;
1041 }
1042
1043 #if HAVE_BIGENDIAN
1044 #   define X_NE(be, le) be
1045 #else
1046 #   define X_NE(be, le) le
1047 #endif
1048
1049 enum PixelFormat av_get_pix_fmt(const char *name)
1050 {
1051     enum PixelFormat pix_fmt;
1052
1053     if (!strcmp(name, "rgb32"))
1054         name = X_NE("argb", "bgra");
1055     else if (!strcmp(name, "bgr32"))
1056         name = X_NE("abgr", "rgba");
1057
1058     pix_fmt = get_pix_fmt_internal(name);
1059     if (pix_fmt == PIX_FMT_NONE) {
1060         char name2[32];
1061
1062         snprintf(name2, sizeof(name2), "%s%s", name, X_NE("be", "le"));
1063         pix_fmt = get_pix_fmt_internal(name2);
1064     }
1065     return pix_fmt;
1066 }
1067
1068 int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
1069 {
1070     int c, bits = 0;
1071     int log2_pixels = pixdesc->log2_chroma_w + pixdesc->log2_chroma_h;
1072
1073     for (c = 0; c < pixdesc->nb_components; c++) {
1074         int s = c==1 || c==2 ? 0 : log2_pixels;
1075         bits += (pixdesc->comp[c].depth_minus1+1) << s;
1076     }
1077
1078     return bits >> log2_pixels;
1079 }
1080
1081 char *av_get_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
1082 {
1083     /* print header */
1084     if (pix_fmt < 0) {
1085         snprintf (buf, buf_size, "name      " " nb_components" " nb_bits");
1086     } else {
1087         const AVPixFmtDescriptor *pixdesc = &av_pix_fmt_descriptors[pix_fmt];
1088         snprintf(buf, buf_size, "%-11s %7d %10d",
1089                  pixdesc->name, pixdesc->nb_components, av_get_bits_per_pixel(pixdesc));
1090     }
1091
1092     return buf;
1093 }