Browse code

pnmdec: always output native pixel format

This simplifies the code

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2013/08/03 21:14:55
Showing 3 changed files
... ...
@@ -118,10 +118,8 @@ int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
118 118
                 avctx->pix_fmt = AV_PIX_FMT_MONOBLACK;
119 119
             } else if (maxval < 256) {
120 120
                 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
121
-            } else if (maxval < 65535) {
122
-                avctx->pix_fmt = AV_PIX_FMT_GRAY16;
123 121
             } else {
124
-                avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
122
+                avctx->pix_fmt = AV_PIX_FMT_GRAY16;
125 123
             }
126 124
         } else if (depth == 2) {
127 125
             if (maxval == 255)
... ...
@@ -130,13 +128,13 @@ int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
130 130
             if (maxval < 256) {
131 131
                 avctx->pix_fmt = AV_PIX_FMT_RGB24;
132 132
             } else {
133
-                avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
133
+                avctx->pix_fmt = AV_PIX_FMT_RGB48;
134 134
             }
135 135
         } else if (depth == 4) {
136 136
             if (maxval < 256) {
137 137
                 avctx->pix_fmt = AV_PIX_FMT_RGBA;
138 138
             } else {
139
-                avctx->pix_fmt = AV_PIX_FMT_RGBA64BE;
139
+                avctx->pix_fmt = AV_PIX_FMT_RGBA64;
140 140
             }
141 141
         } else {
142 142
             return AVERROR_INVALIDDATA;
... ...
@@ -164,16 +162,14 @@ int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
164 164
         }
165 165
         if (s->maxval >= 256) {
166 166
             if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
167
-                avctx->pix_fmt = AV_PIX_FMT_GRAY16BE;
168
-                if (s->maxval != 65535)
169
-                    avctx->pix_fmt = AV_PIX_FMT_GRAY16;
167
+                avctx->pix_fmt = AV_PIX_FMT_GRAY16;
170 168
             } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
171
-                avctx->pix_fmt = AV_PIX_FMT_RGB48BE;
169
+                avctx->pix_fmt = AV_PIX_FMT_RGB48;
172 170
             } else if (avctx->pix_fmt == AV_PIX_FMT_YUV420P && s->maxval < 65536) {
173 171
                 if (s->maxval < 512)
174
-                    avctx->pix_fmt = AV_PIX_FMT_YUV420P9BE;
172
+                    avctx->pix_fmt = AV_PIX_FMT_YUV420P9;
175 173
                 else if (s->maxval < 1024)
176
-                    avctx->pix_fmt = AV_PIX_FMT_YUV420P10BE;
174
+                    avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
177 175
                 else
178 176
                     avctx->pix_fmt = AV_PIX_FMT_YUV420P16;
179 177
             } else {
... ...
@@ -24,6 +24,17 @@
24 24
 #include "put_bits.h"
25 25
 #include "pnm.h"
26 26
 
27
+static void samplecpy(void *dst, const void *src, int n, int maxval)
28
+{
29
+    if (maxval <= 255) {
30
+        memcpy(dst, src, n);
31
+    } else {
32
+        int i;
33
+        for (i=0; i<n/2; i++) {
34
+            ((uint16_t *)dst)[i] = av_be2ne16(((uint16_t *)src)[i]);
35
+        }
36
+    }
37
+}
27 38
 
28 39
 static int pnm_decode_frame(AVCodecContext *avctx, void *data,
29 40
                             int *got_frame, AVPacket *avpkt)
... ...
@@ -51,12 +62,12 @@ static int pnm_decode_frame(AVCodecContext *avctx, void *data,
51 51
     switch (avctx->pix_fmt) {
52 52
     default:
53 53
         return AVERROR(EINVAL);
54
-    case AV_PIX_FMT_RGBA64BE:
54
+    case AV_PIX_FMT_RGBA64:
55 55
         n = avctx->width * 8;
56 56
         components=4;
57 57
         sample_len=16;
58 58
         goto do_read;
59
-    case AV_PIX_FMT_RGB48BE:
59
+    case AV_PIX_FMT_RGB48:
60 60
         n = avctx->width * 6;
61 61
         components=3;
62 62
         sample_len=16;
... ...
@@ -83,8 +94,7 @@ static int pnm_decode_frame(AVCodecContext *avctx, void *data,
83 83
         components=2;
84 84
         sample_len=8;
85 85
         goto do_read;
86
-    case AV_PIX_FMT_GRAY16BE:
87
-    case AV_PIX_FMT_GRAY16LE:
86
+    case AV_PIX_FMT_GRAY16:
88 87
         n = avctx->width * 2;
89 88
         components=1;
90 89
         sample_len=16;
... ...
@@ -124,15 +134,19 @@ static int pnm_decode_frame(AVCodecContext *avctx, void *data,
124 124
                             c = (*s->bytestream++) - '0';
125 125
                         } while (c <= 9);
126 126
                     }
127
-                    put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
127
+                    if (sample_len == 16) {
128
+                        ((uint16_t*)ptr)[j] = (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval;
129
+                    } else
130
+                        put_bits(&pb, sample_len, (((1<<sample_len)-1)*v + (s->maxval>>1))/s->maxval);
128 131
                 }
129
-                flush_put_bits(&pb);
132
+                if (sample_len != 16)
133
+                    flush_put_bits(&pb);
130 134
                 ptr+= linesize;
131 135
             }
132 136
         }else{
133 137
         for (i = 0; i < avctx->height; i++) {
134 138
             if (!upgrade)
135
-                memcpy(ptr, s->bytestream, n);
139
+                samplecpy(ptr, s->bytestream, n, s->maxval);
136 140
             else if (upgrade == 1) {
137 141
                 unsigned int j, f = (255 * 128 + s->maxval / 2) / s->maxval;
138 142
                 for (j = 0; j < n; j++)
... ...
@@ -150,8 +164,8 @@ static int pnm_decode_frame(AVCodecContext *avctx, void *data,
150 150
         }
151 151
         break;
152 152
     case AV_PIX_FMT_YUV420P:
153
-    case AV_PIX_FMT_YUV420P9BE:
154
-    case AV_PIX_FMT_YUV420P10BE:
153
+    case AV_PIX_FMT_YUV420P9:
154
+    case AV_PIX_FMT_YUV420P10:
155 155
         {
156 156
             unsigned char *ptr1, *ptr2;
157 157
 
... ...
@@ -163,7 +177,7 @@ static int pnm_decode_frame(AVCodecContext *avctx, void *data,
163 163
             if (s->bytestream + n * avctx->height * 3 / 2 > s->bytestream_end)
164 164
                 return AVERROR_INVALIDDATA;
165 165
             for (i = 0; i < avctx->height; i++) {
166
-                memcpy(ptr, s->bytestream, n);
166
+                samplecpy(ptr, s->bytestream, n, s->maxval);
167 167
                 s->bytestream += n;
168 168
                 ptr           += linesize;
169 169
             }
... ...
@@ -172,9 +186,9 @@ static int pnm_decode_frame(AVCodecContext *avctx, void *data,
172 172
             n >>= 1;
173 173
             h = avctx->height >> 1;
174 174
             for (i = 0; i < h; i++) {
175
-                memcpy(ptr1, s->bytestream, n);
175
+                samplecpy(ptr1, s->bytestream, n, s->maxval);
176 176
                 s->bytestream += n;
177
-                memcpy(ptr2, s->bytestream, n);
177
+                samplecpy(ptr2, s->bytestream, n, s->maxval);
178 178
                 s->bytestream += n;
179 179
                 ptr1 += p->linesize[1];
180 180
                 ptr2 += p->linesize[2];
... ...
@@ -235,8 +235,8 @@ if [ -n "$do_pam" ] ; then
235 235
 do_image_formats pam
236 236
 do_image_formats pam "-pix_fmt rgba"
237 237
 do_image_formats pam "-pix_fmt gray"
238
-do_image_formats pam "-pix_fmt gray16be"
239
-do_image_formats pam "-pix_fmt rgb48be"
238
+do_image_formats pam "-pix_fmt gray16be" "-pix_fmt gray16be"
239
+do_image_formats pam "-pix_fmt rgb48be" "-pix_fmt rgb48be"
240 240
 do_image_formats pam "-pix_fmt monob"
241 241
 fi
242 242