Browse code

p*menc: use the AVFrame API properly.

Anton Khirnov authored on 2013/11/09 18:14:46
Showing 5 changed files
... ...
@@ -279,21 +279,21 @@ OBJS-$(CONFIG_NELLYMOSER_DECODER)      += nellymoserdec.o nellymoser.o
279 279
 OBJS-$(CONFIG_NELLYMOSER_ENCODER)      += nellymoserenc.o nellymoser.o
280 280
 OBJS-$(CONFIG_NUV_DECODER)             += nuv.o rtjpeg.o
281 281
 OBJS-$(CONFIG_PAM_DECODER)             += pnmdec.o pnm.o
282
-OBJS-$(CONFIG_PAM_ENCODER)             += pamenc.o pnm.o
282
+OBJS-$(CONFIG_PAM_ENCODER)             += pamenc.o
283 283
 OBJS-$(CONFIG_PBM_DECODER)             += pnmdec.o pnm.o
284
-OBJS-$(CONFIG_PBM_ENCODER)             += pnmenc.o pnm.o
284
+OBJS-$(CONFIG_PBM_ENCODER)             += pnmenc.o
285 285
 OBJS-$(CONFIG_PCX_DECODER)             += pcx.o
286 286
 OBJS-$(CONFIG_PCX_ENCODER)             += pcxenc.o
287 287
 OBJS-$(CONFIG_PGM_DECODER)             += pnmdec.o pnm.o
288
-OBJS-$(CONFIG_PGM_ENCODER)             += pnmenc.o pnm.o
288
+OBJS-$(CONFIG_PGM_ENCODER)             += pnmenc.o
289 289
 OBJS-$(CONFIG_PGMYUV_DECODER)          += pnmdec.o pnm.o
290
-OBJS-$(CONFIG_PGMYUV_ENCODER)          += pnmenc.o pnm.o
290
+OBJS-$(CONFIG_PGMYUV_ENCODER)          += pnmenc.o
291 291
 OBJS-$(CONFIG_PGSSUB_DECODER)          += pgssubdec.o
292 292
 OBJS-$(CONFIG_PICTOR_DECODER)          += pictordec.o cga_data.o
293 293
 OBJS-$(CONFIG_PNG_DECODER)             += png.o pngdec.o pngdsp.o
294 294
 OBJS-$(CONFIG_PNG_ENCODER)             += png.o pngenc.o
295 295
 OBJS-$(CONFIG_PPM_DECODER)             += pnmdec.o pnm.o
296
-OBJS-$(CONFIG_PPM_ENCODER)             += pnmenc.o pnm.o
296
+OBJS-$(CONFIG_PPM_ENCODER)             += pnmenc.o
297 297
 OBJS-$(CONFIG_PRORES_DECODER)          += proresdec.o proresdata.o proresdsp.o
298 298
 OBJS-$(CONFIG_PRORES_ENCODER)          += proresenc.o proresdata.o proresdsp.o
299 299
 OBJS-$(CONFIG_PTX_DECODER)             += ptx.o
... ...
@@ -22,14 +22,12 @@
22 22
 #include "avcodec.h"
23 23
 #include "bytestream.h"
24 24
 #include "internal.h"
25
-#include "pnm.h"
26
-
27 25
 
28 26
 static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
29 27
                             const AVFrame *pict, int *got_packet)
30 28
 {
31
-    PNMContext *s     = avctx->priv_data;
32
-    AVFrame * const p = &s->picture;
29
+    uint8_t *bytestream_start, *bytestream, *bytestream_end;
30
+    const AVFrame * const p = pict;
33 31
     int i, h, w, n, linesize, depth, maxval, ret;
34 32
     const char *tuple_type;
35 33
     uint8_t *ptr;
... ...
@@ -41,13 +39,9 @@ static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
41 41
         return ret;
42 42
     }
43 43
 
44
-    *p           = *pict;
45
-    p->pict_type = AV_PICTURE_TYPE_I;
46
-    p->key_frame = 1;
47
-
48
-    s->bytestream_start =
49
-    s->bytestream       = pkt->data;
50
-    s->bytestream_end   = pkt->data + pkt->size;
44
+    bytestream_start =
45
+    bytestream       = pkt->data;
46
+    bytestream_end   = pkt->data + pkt->size;
51 47
 
52 48
     h = avctx->height;
53 49
     w = avctx->width;
... ...
@@ -79,10 +73,10 @@ static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
79 79
     default:
80 80
         return -1;
81 81
     }
82
-    snprintf(s->bytestream, s->bytestream_end - s->bytestream,
82
+    snprintf(bytestream, bytestream_end - bytestream,
83 83
              "P7\nWIDTH %d\nHEIGHT %d\nDEPTH %d\nMAXVAL %d\nTUPLTYPE %s\nENDHDR\n",
84 84
              w, h, depth, maxval, tuple_type);
85
-    s->bytestream += strlen(s->bytestream);
85
+    bytestream += strlen(bytestream);
86 86
 
87 87
     ptr      = p->data[0];
88 88
     linesize = p->linesize[0];
... ...
@@ -94,33 +88,50 @@ static int pam_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
94 94
         for (i = 0; i < h; i++) {
95 95
             for (j = 0; j < w; j++) {
96 96
                 v = ((uint32_t *)ptr)[j];
97
-                bytestream_put_be24(&s->bytestream, v);
98
-                *s->bytestream++ = v >> 24;
97
+                bytestream_put_be24(&bytestream, v);
98
+                *bytestream++ = v >> 24;
99 99
             }
100 100
             ptr += linesize;
101 101
         }
102 102
     } else {
103 103
         for (i = 0; i < h; i++) {
104
-            memcpy(s->bytestream, ptr, n);
105
-            s->bytestream += n;
106
-            ptr           += linesize;
104
+            memcpy(bytestream, ptr, n);
105
+            bytestream += n;
106
+            ptr        += linesize;
107 107
         }
108 108
     }
109 109
 
110
-    pkt->size   = s->bytestream - s->bytestream_start;
110
+    pkt->size   = bytestream - bytestream_start;
111 111
     pkt->flags |= AV_PKT_FLAG_KEY;
112 112
     *got_packet = 1;
113 113
     return 0;
114 114
 }
115 115
 
116
+static av_cold int pam_encode_init(AVCodecContext *avctx)
117
+{
118
+    avctx->coded_frame = av_frame_alloc();
119
+    if (!avctx->coded_frame)
120
+        return AVERROR(ENOMEM);
121
+
122
+    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
123
+    avctx->coded_frame->key_frame = 1;
124
+
125
+    return 0;
126
+}
127
+
128
+static av_cold int pam_encode_close(AVCodecContext *avctx)
129
+{
130
+    av_frame_free(&avctx->coded_frame);
131
+    return 0;
132
+}
116 133
 
117 134
 AVCodec ff_pam_encoder = {
118 135
     .name           = "pam",
119 136
     .long_name      = NULL_IF_CONFIG_SMALL("PAM (Portable AnyMap) image"),
120 137
     .type           = AVMEDIA_TYPE_VIDEO,
121 138
     .id             = AV_CODEC_ID_PAM,
122
-    .priv_data_size = sizeof(PNMContext),
123
-    .init           = ff_pnm_init,
139
+    .init           = pam_encode_init,
140
+    .close          = pam_encode_close,
124 141
     .encode2        = pam_encode_frame,
125 142
     .pix_fmts       = (const enum AVPixelFormat[]){
126 143
         AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB32, AV_PIX_FMT_GRAY8, AV_PIX_FMT_MONOWHITE,
... ...
@@ -184,13 +184,3 @@ int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s)
184 184
     }
185 185
     return 0;
186 186
 }
187
-
188
-av_cold int ff_pnm_init(AVCodecContext *avctx)
189
-{
190
-    PNMContext *s = avctx->priv_data;
191
-
192
-    avcodec_get_frame_defaults(&s->picture);
193
-    avctx->coded_frame = &s->picture;
194
-
195
-    return 0;
196
-}
... ...
@@ -28,12 +28,10 @@ typedef struct PNMContext {
28 28
     uint8_t *bytestream;
29 29
     uint8_t *bytestream_start;
30 30
     uint8_t *bytestream_end;
31
-    AVFrame picture;
32 31
     int maxval;                 ///< maximum value of a pixel
33 32
     int type;
34 33
 } PNMContext;
35 34
 
36 35
 int ff_pnm_decode_header(AVCodecContext *avctx, PNMContext * const s);
37
-int ff_pnm_init(AVCodecContext *avctx);
38 36
 
39 37
 #endif /* AVCODEC_PNM_H */
... ...
@@ -23,14 +23,12 @@
23 23
 #include "avcodec.h"
24 24
 #include "bytestream.h"
25 25
 #include "internal.h"
26
-#include "pnm.h"
27
-
28 26
 
29 27
 static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
30 28
                             const AVFrame *pict, int *got_packet)
31 29
 {
32
-    PNMContext *s     = avctx->priv_data;
33
-    AVFrame * const p = &s->picture;
30
+    uint8_t *bytestream, *bytestream_start, *bytestream_end;
31
+    const AVFrame * const p = pict;
34 32
     int i, h, h1, c, n, linesize, ret;
35 33
     uint8_t *ptr, *ptr1, *ptr2;
36 34
 
... ...
@@ -41,13 +39,9 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
41 41
         return ret;
42 42
     }
43 43
 
44
-    *p           = *pict;
45
-    p->pict_type = AV_PICTURE_TYPE_I;
46
-    p->key_frame = 1;
47
-
48
-    s->bytestream_start =
49
-    s->bytestream       = pkt->data;
50
-    s->bytestream_end   = pkt->data + pkt->size;
44
+    bytestream_start =
45
+    bytestream       = pkt->data;
46
+    bytestream_end   = pkt->data + pkt->size;
51 47
 
52 48
     h  = avctx->height;
53 49
     h1 = h;
... ...
@@ -85,22 +79,22 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
85 85
     default:
86 86
         return -1;
87 87
     }
88
-    snprintf(s->bytestream, s->bytestream_end - s->bytestream,
88
+    snprintf(bytestream, bytestream_end - bytestream,
89 89
              "P%c\n%d %d\n", c, avctx->width, h1);
90
-    s->bytestream += strlen(s->bytestream);
90
+    bytestream += strlen(bytestream);
91 91
     if (avctx->pix_fmt != AV_PIX_FMT_MONOWHITE) {
92 92
         int maxdepth = (1 << (av_pix_fmt_desc_get(avctx->pix_fmt)->comp[0].depth_minus1 + 1)) - 1;
93
-        snprintf(s->bytestream, s->bytestream_end - s->bytestream,
93
+        snprintf(bytestream, bytestream_end - bytestream,
94 94
                  "%d\n", maxdepth);
95
-        s->bytestream += strlen(s->bytestream);
95
+        bytestream += strlen(bytestream);
96 96
     }
97 97
 
98 98
     ptr      = p->data[0];
99 99
     linesize = p->linesize[0];
100 100
     for (i = 0; i < h; i++) {
101
-        memcpy(s->bytestream, ptr, n);
102
-        s->bytestream += n;
103
-        ptr           += linesize;
101
+        memcpy(bytestream, ptr, n);
102
+        bytestream += n;
103
+        ptr        += linesize;
104 104
     }
105 105
 
106 106
     if (avctx->pix_fmt == AV_PIX_FMT_YUV420P || avctx->pix_fmt == AV_PIX_FMT_YUV420P16BE) {
... ...
@@ -109,21 +103,38 @@ static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
109 109
         ptr1 = p->data[1];
110 110
         ptr2 = p->data[2];
111 111
         for (i = 0; i < h; i++) {
112
-            memcpy(s->bytestream, ptr1, n);
113
-            s->bytestream += n;
114
-            memcpy(s->bytestream, ptr2, n);
115
-            s->bytestream += n;
112
+            memcpy(bytestream, ptr1, n);
113
+            bytestream += n;
114
+            memcpy(bytestream, ptr2, n);
115
+            bytestream += n;
116 116
                 ptr1 += p->linesize[1];
117 117
                 ptr2 += p->linesize[2];
118 118
         }
119 119
     }
120
-    pkt->size   = s->bytestream - s->bytestream_start;
120
+    pkt->size   = bytestream - bytestream_start;
121 121
     pkt->flags |= AV_PKT_FLAG_KEY;
122 122
     *got_packet = 1;
123 123
 
124 124
     return 0;
125 125
 }
126 126
 
127
+static av_cold int pnm_encode_init(AVCodecContext *avctx)
128
+{
129
+    avctx->coded_frame = av_frame_alloc();
130
+    if (!avctx->coded_frame)
131
+        return AVERROR(ENOMEM);
132
+
133
+    avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
134
+    avctx->coded_frame->key_frame = 1;
135
+
136
+    return 0;
137
+}
138
+
139
+static av_cold int pnm_encode_close(AVCodecContext *avctx)
140
+{
141
+    av_frame_free(&avctx->coded_frame);
142
+    return 0;
143
+}
127 144
 
128 145
 #if CONFIG_PGM_ENCODER
129 146
 AVCodec ff_pgm_encoder = {
... ...
@@ -131,8 +142,8 @@ AVCodec ff_pgm_encoder = {
131 131
     .long_name      = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
132 132
     .type           = AVMEDIA_TYPE_VIDEO,
133 133
     .id             = AV_CODEC_ID_PGM,
134
-    .priv_data_size = sizeof(PNMContext),
135
-    .init           = ff_pnm_init,
134
+    .init           = pnm_encode_init,
135
+    .close          = pnm_encode_close,
136 136
     .encode2        = pnm_encode_frame,
137 137
     .pix_fmts       = (const enum AVPixelFormat[]){
138 138
         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_NONE
... ...
@@ -146,8 +157,8 @@ AVCodec ff_pgmyuv_encoder = {
146 146
     .long_name      = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
147 147
     .type           = AVMEDIA_TYPE_VIDEO,
148 148
     .id             = AV_CODEC_ID_PGMYUV,
149
-    .priv_data_size = sizeof(PNMContext),
150
-    .init           = ff_pnm_init,
149
+    .init           = pnm_encode_init,
150
+    .close          = pnm_encode_close,
151 151
     .encode2        = pnm_encode_frame,
152 152
     .pix_fmts       = (const enum AVPixelFormat[]){
153 153
         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P16BE, AV_PIX_FMT_NONE
... ...
@@ -161,8 +172,8 @@ AVCodec ff_ppm_encoder = {
161 161
     .long_name      = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
162 162
     .type           = AVMEDIA_TYPE_VIDEO,
163 163
     .id             = AV_CODEC_ID_PPM,
164
-    .priv_data_size = sizeof(PNMContext),
165
-    .init           = ff_pnm_init,
164
+    .init           = pnm_encode_init,
165
+    .close          = pnm_encode_close,
166 166
     .encode2        = pnm_encode_frame,
167 167
     .pix_fmts       = (const enum AVPixelFormat[]){
168 168
         AV_PIX_FMT_RGB24, AV_PIX_FMT_RGB48BE, AV_PIX_FMT_NONE
... ...
@@ -176,8 +187,8 @@ AVCodec ff_pbm_encoder = {
176 176
     .long_name      = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
177 177
     .type           = AVMEDIA_TYPE_VIDEO,
178 178
     .id             = AV_CODEC_ID_PBM,
179
-    .priv_data_size = sizeof(PNMContext),
180
-    .init           = ff_pnm_init,
179
+    .init           = pnm_encode_init,
180
+    .close          = pnm_encode_close,
181 181
     .encode2        = pnm_encode_frame,
182 182
     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_MONOWHITE,
183 183
                                                   AV_PIX_FMT_NONE },