source: mediastreamer2/src/videodec.c @ 889:641e2a9be973

Last change on this file since 889:641e2a9be973 was 889:641e2a9be973, checked in by Aymeric Moizard <jack@…>, 3 years ago

add NULL check to avoid crash

File size: 23.0 KB
Line 
1/*
2mediastreamer2 library - modular sound and video processing and streaming
3Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)
4
5This program is free software; you can redistribute it and/or
6modify it under the terms of the GNU General Public License
7as published by the Free Software Foundation; either version 2
8of the License, or (at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18*/
19
20#ifdef HAVE_CONFIG_H
21#include "mediastreamer-config.h"
22#endif
23
24#include "ffmpeg-priv.h"
25
26#include "mediastreamer2/msfilter.h"
27#include "mediastreamer2/msvideo.h"
28#include "rfc2429.h"
29
30
31extern void ms_ffmpeg_check_init();
32
33typedef struct DecState{
34        AVCodecContext av_context;
35        AVCodec *av_codec;
36        enum CodecID codec;
37        mblk_t *input;
38        YuvBuf outbuf;
39        mblk_t *yuv_msg;
40        struct SwsContext *sws_ctx;
41        enum PixelFormat output_pix_fmt;
42        uint8_t dci[512];
43        int dci_size;
44        bool_t snow_initialized;
45}DecState;
46
47
48static void dec_init(MSFilter *f, enum CodecID cid){
49        DecState *s=(DecState *)ms_new0(DecState,1);
50        ms_ffmpeg_check_init();
51       
52        avcodec_get_context_defaults(&s->av_context);
53        s->av_codec=NULL;
54        s->codec=cid;
55        s->input=NULL;
56        s->yuv_msg=NULL;
57        s->output_pix_fmt=PIX_FMT_YUV420P;
58        s->snow_initialized=FALSE;
59        s->outbuf.w=0;
60        s->outbuf.h=0;
61        s->sws_ctx=NULL;
62        f->data=s;
63
64        s->av_codec=avcodec_find_decoder(s->codec);
65        if (s->av_codec==NULL){
66                ms_error("Could not find decoder %i!",s->codec);
67        }
68        /*
69        s->av_context.width=MS_VIDEO_SIZE_QCIF_W;
70        s->av_context.height=MS_VIDEO_SIZE_QCIF_H;
71        */
72}
73
74static void dec_h263_init(MSFilter *f){
75        dec_init(f,CODEC_ID_H263);
76}
77
78static void dec_mpeg4_init(MSFilter *f){
79        dec_init(f,CODEC_ID_MPEG4);
80}
81
82static void dec_mjpeg_init(MSFilter *f){
83        dec_init(f,CODEC_ID_MJPEG);
84}
85
86static void dec_snow_init(MSFilter *f){
87        dec_init(f,CODEC_ID_SNOW);
88}
89
90static void dec_uninit(MSFilter *f){
91        DecState *s=(DecState*)f->data;
92        if (s->input!=NULL) freemsg(s->input);
93        if (s->yuv_msg!=NULL) freemsg(s->yuv_msg);
94        if (s->sws_ctx!=NULL){
95                sws_freeContext(s->sws_ctx);
96                s->sws_ctx=NULL;
97        }
98        ms_free(s);
99}
100
101static int dec_add_fmtp(MSFilter *f, void *data){
102        const char *fmtp=(const char*)data;
103        DecState *s=(DecState*)f->data;
104        char config[512];
105        if (fmtp_get_value(fmtp,"config",config,sizeof(config))){
106                /*convert hexa decimal config string into a bitstream */
107                int i,j,max=strlen(config);
108                char octet[3];
109                octet[2]=0;
110                for(i=0,j=0;i<max;i+=2,++j){
111                        octet[0]=config[i];
112                        octet[1]=config[i+1];
113                        s->dci[j]=(uint8_t)strtol(octet,NULL,16);
114                }
115                s->dci_size=j;
116                ms_message("Got mpeg4 config string: %s",config);
117        }
118        return 0;
119}
120
121static void dec_preprocess(MSFilter *f){
122        DecState *s=(DecState*)f->data;
123        int error;
124        /* we must know picture size before initializing snow decoder*/
125        if (s->codec!=CODEC_ID_SNOW){
126                error=avcodec_open(&s->av_context, s->av_codec);
127                if (error!=0) ms_error("avcodec_open() failed: %i",error);
128                if (s->codec==CODEC_ID_MPEG4 && s->dci_size>0){
129                        s->av_context.extradata=s->dci;
130                        s->av_context.extradata_size=s->dci_size;
131                }
132        }
133}
134
135static void dec_postprocess(MSFilter *f){
136        DecState *s=(DecState*)f->data;
137        if (s->av_context.codec!=NULL){
138                avcodec_close(&s->av_context);
139                s->av_context.codec=NULL;
140        }
141}
142
143static mblk_t * skip_rfc2190_header(mblk_t *inm){
144        if (msgdsize(inm) >= 4) {
145                uint8_t *ph = inm->b_rptr;
146                int F = (ph[0]>>7) & 0x1;
147                int P = (ph[0]>>6) & 0x1;
148                if (F == 0) inm->b_rptr += 4;  // mode A
149                else if (P == 0) inm->b_rptr += 8; // mode B
150                else inm->b_rptr += 12;   // mode C
151        } else {
152                freemsg(inm);
153                inm=NULL;
154        }
155        return inm;
156}
157
158static mblk_t * skip_rfc2429_header(mblk_t *inm){
159        if (msgdsize(inm) >= 2){
160                uint32_t *p = (uint32_t*)inm->b_rptr;
161                uint8_t *ph=inm->b_rptr;
162                int PLEN;
163                int gob_num;
164                bool_t P;
165               
166                P=rfc2429_get_P(ph);
167                PLEN=rfc2429_get_PLEN(ph);
168                /*printf("receiving new packet; P=%i; V=%i; PLEN=%i; PEBIT=%i\n",P,rfc2429_get_V(ph),PLEN,rfc2429_get_PEBIT(ph));
169                */
170                gob_num = (ntohl(*p) >> 10) & 0x1f;
171                /*ms_message("gob %i, size %i", gob_num, msgdsize(inm));
172                ms_message("ms_AVdecoder_process: received %08x %08x", ntohl(p[0]), ntohl(p[1]));*/
173               
174                /* remove H.263 Payload Header */
175                if (PLEN>0){
176                        /* we ignore the redundant picture header and
177                        directly go to the bitstream */
178                        inm->b_rptr+=PLEN;
179                }
180                if (P){
181                        inm->b_rptr[0]=inm->b_rptr[1]=0;
182                }else{
183                        /* no PSC omitted */
184                        inm->b_rptr+=2;
185                }
186                return inm;
187        }else freemsg(inm);
188        return NULL;
189}
190
191static mblk_t * parse_snow_header(DecState *s,mblk_t *inm){
192        if (msgdsize(inm) >= 4){
193                uint32_t h = ntohl(*(uint32_t*)inm->b_rptr);
194                if (!s->snow_initialized){
195                        int error;
196                        s->av_context.width=h>>16;
197                        s->av_context.height=h&0xffff;
198                        error=avcodec_open(&s->av_context, s->av_codec);
199                        if (error!=0) ms_error("avcodec_open() failed for snow: %i",error);
200                        else {
201                                s->snow_initialized=TRUE;
202                                ms_message("Snow decoder initialized,size=%ix%i",
203                                s->av_context.width,
204                                s->av_context.height);
205                        }
206                }
207                inm->b_rptr+=4;
208                return inm;
209        }else {
210                freemsg(inm);
211                return NULL;
212        }
213}
214
215struct jpeghdr {
216        //unsigned int tspec:8;   /* type-specific field */
217        unsigned int off:32;    /* fragment byte offset */
218        uint8_t type;            /* id of jpeg decoder params */
219        uint8_t q;               /* quantization factor (or table id) */
220        uint8_t width;           /* frame width in 8 pixel blocks */
221        uint8_t height;          /* frame height in 8 pixel blocks */
222};
223
224struct jpeghdr_rst {
225        uint16_t dri;
226        unsigned int f:1;
227        unsigned int l:1;
228        unsigned int count:14;
229};
230
231struct jpeghdr_qtable {
232        uint8_t  mbz;
233        uint8_t  precision;
234        uint16_t length;
235};
236
237
238static u_char lum_dc_codelens[] = {
239        0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
240};
241
242static u_char lum_dc_symbols[] = {
243        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
244};
245
246static u_char lum_ac_codelens[] = {
247        0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d,
248};
249
250static u_char lum_ac_symbols[] = {
251        0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
252        0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
253        0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
254        0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
255        0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
256        0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
257        0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
258        0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
259        0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
260        0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
261        0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
262        0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
263        0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
264        0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
265        0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
266        0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
267        0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
268        0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
269        0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
270        0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
271        0xf9, 0xfa,
272};
273
274static u_char chm_dc_codelens[] = {
275        0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
276};
277
278static u_char chm_dc_symbols[] = {
279        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
280};
281
282static u_char chm_ac_codelens[] = {
283        0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77,
284};
285
286static u_char chm_ac_symbols[] = {
287        0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
288        0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
289        0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
290        0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
291        0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
292        0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
293        0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
294        0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
295        0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
296        0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
297        0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
298        0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
299        0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
300        0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
301        0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
302        0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
303        0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
304        0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
305        0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
306        0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
307        0xf9, 0xfa,
308};
309
310static u_char *
311MakeQuantHeader(u_char *p, u_char *qt, int tableNo, int table_len)
312{
313        *p++ = 0xff;
314        *p++ = 0xdb;            /* DQT */
315        *p++ = 0;               /* length msb */
316        *p++ = table_len+3;              /* length lsb */
317        *p++ = tableNo;
318        memcpy(p, qt, table_len);
319        return (p + table_len);
320}
321
322static u_char *
323MakeHuffmanHeader(u_char *p, u_char *codelens, int ncodes,
324                  u_char *symbols, int nsymbols, int tableNo,
325                  int tableClass)
326{
327        *p++ = 0xff;
328        *p++ = 0xc4;            /* DHT */
329        *p++ = 0;               /* length msb */
330        *p++ = 3 + ncodes + nsymbols; /* length lsb */
331        *p++ = (tableClass << 4) | tableNo;
332        memcpy(p, codelens, ncodes);
333        p += ncodes;
334        memcpy(p, symbols, nsymbols);
335        p += nsymbols;
336        return (p);
337}
338
339static u_char *
340MakeDRIHeader(u_char *p, u_short dri) {
341        *p++ = 0xff;
342        *p++ = 0xdd;            /* DRI */
343        *p++ = 0x0;             /* length msb */
344        *p++ = 4;               /* length lsb */
345        *p++ = dri >> 8;        /* dri msb */
346        *p++ = dri & 0xff;      /* dri lsb */
347        return (p);
348}
349
350/*
351 *  Arguments:
352 *    type, width, height: as supplied in RTP/JPEG header
353 *    lqt, cqt: quantization tables as either derived from
354 *         the Q field using MakeTables() or as specified
355 *         in section 4.2.
356 *    dri: restart interval in MCUs, or 0 if no restarts.
357 *
358 *    p: pointer to return area
359 *
360 *  Return value:
361 *    The length of the generated headers.
362 *
363 *    Generate a frame and scan headers that can be prepended to the
364 *    RTP/JPEG data payload to produce a JPEG compressed image in
365 *    interchange format (except for possible trailing garbage and
366 *    absence of an EOI marker to terminate the scan).
367 */
368static int MakeHeaders(u_char *p, int type, int w, int h, u_char *lqt,
369                u_char *cqt, unsigned table_len, u_short dri)
370{
371        u_char *start = p;
372
373        /* convert from blocks to pixels */
374        w <<= 3;
375        h <<= 3;
376
377        *p++ = 0xff;
378        *p++ = 0xd8;            /* SOI */
379
380                if (table_len>64)
381                {
382                        p = MakeQuantHeader(p, lqt, 0, table_len/2);
383                p = MakeQuantHeader(p, cqt, 1, table_len/2);
384                }
385                else
386                {
387                        p = MakeQuantHeader(p, lqt, 0, table_len);
388                        //p = MakeQuantHeader(p, lqt, 1, table_len);
389                }
390        if (dri != 0)
391                p = MakeDRIHeader(p, dri);
392
393        *p++ = 0xff;
394        *p++ = 0xc0;            /* SOF */
395        *p++ = 0;               /* length msb */
396        *p++ = 17;              /* length lsb */
397        *p++ = 8;               /* 8-bit precision */
398        *p++ = h >> 8;          /* height msb */
399        *p++ = h;               /* height lsb */
400        *p++ = w >> 8;          /* width msb */
401        *p++ = w;               /* wudth lsb */
402        *p++ = 3;               /* number of components */
403        *p++ = 0;               /* comp 0 */
404        if (type == 0)
405                *p++ = 0x21;    /* hsamp = 2, vsamp = 1 */
406        else
407                *p++ = 0x22;    /* hsamp = 2, vsamp = 2 */
408        *p++ = 0;               /* quant table 0 */
409        *p++ = 1;               /* comp 1 */
410        *p++ = 0x11;            /* hsamp = 1, vsamp = 1 */
411        *p++ = table_len <= 64 ? 0x00 : 0x01; //1   /* quant table 1 */
412        *p++ = 2;               /* comp 2 */
413        *p++ = 0x11;            /* hsamp = 1, vsamp = 1 */
414        *p++ = table_len <= 64 ? 0x00 : 0x01; //1;  /* quant table 1 */
415        p = MakeHuffmanHeader(p, lum_dc_codelens,
416                              sizeof(lum_dc_codelens),
417                              lum_dc_symbols,
418                              sizeof(lum_dc_symbols), 0, 0);
419        p = MakeHuffmanHeader(p, lum_ac_codelens,
420                              sizeof(lum_ac_codelens),
421                              lum_ac_symbols,
422                              sizeof(lum_ac_symbols), 0, 1);
423        p = MakeHuffmanHeader(p, chm_dc_codelens,
424                              sizeof(chm_dc_codelens),
425                              chm_dc_symbols,
426                              sizeof(chm_dc_symbols), 1, 0);
427        p = MakeHuffmanHeader(p, chm_ac_codelens,
428                              sizeof(chm_ac_codelens),
429                              chm_ac_symbols,
430                              sizeof(chm_ac_symbols), 1, 1);
431
432        *p++ = 0xff;
433        *p++ = 0xda;            /* SOS */
434        *p++ = 0;               /* length msb */
435        *p++ = 12;              /* length lsb */
436        *p++ = 3;               /* 3 components */
437        *p++ = 0;               /* comp 0 */
438        *p++ = 0;               /* huffman table 0 */
439        *p++ = 1;               /* comp 1 */
440        *p++ = 0x11;            /* huffman table 1 */
441        *p++ = 2;               /* comp 2 */
442        *p++ = 0x11;            /* huffman table 1 */
443        *p++ = 0;               /* first DCT coeff */
444        *p++ = 63;              /* last DCT coeff */
445        *p++ = 0;               /* sucessive approx. */
446
447        return (p - start);
448};
449
450
451/*
452 * Table K.1 from JPEG spec.
453 */
454static const int jpeg_luma_quantizer[64] = {
455        16, 11, 10, 16, 24, 40, 51, 61,
456        12, 12, 14, 19, 26, 58, 60, 55,
457        14, 13, 16, 24, 40, 57, 69, 56,
458        14, 17, 22, 29, 51, 87, 80, 62,
459        18, 22, 37, 56, 68, 109, 103, 77,
460        24, 35, 55, 64, 81, 104, 113, 92,
461        49, 64, 78, 87, 103, 121, 120, 101,
462        72, 92, 95, 98, 112, 100, 103, 99
463};
464
465/*
466 * Table K.2 from JPEG spec.
467 */
468static const int jpeg_chroma_quantizer[64] = {
469        17, 18, 24, 47, 99, 99, 99, 99,
470        18, 21, 26, 66, 99, 99, 99, 99,
471        24, 26, 56, 99, 99, 99, 99, 99,
472        47, 66, 99, 99, 99, 99, 99, 99,
473        99, 99, 99, 99, 99, 99, 99, 99,
474        99, 99, 99, 99, 99, 99, 99, 99,
475        99, 99, 99, 99, 99, 99, 99, 99,
476        99, 99, 99, 99, 99, 99, 99, 99
477};
478
479/*
480 * Call MakeTables with the Q factor and two u_char[64] return arrays
481 */
482static void MakeTables(int q, u_char *lqt, u_char *cqt)
483{
484        int i;
485        int factor = q;
486
487        if (q < 1) factor = 1;
488        if (q > 99) factor = 99;
489        if (q < 50)
490                q = 5000 / factor;
491        else
492                q = 200 - factor*2;
493
494        for (i=0; i < 64; i++) {
495                int lq = (jpeg_luma_quantizer[i] * q + 50) / 100;
496                int cq = (jpeg_chroma_quantizer[i] * q + 50) / 100;
497
498                /* Limit the quantizers to 1 <= q <= 255 */
499                if (lq < 1) lq = 1;
500                else if (lq > 255) lq = 255;
501                lqt[i] = lq;
502
503                if (cq < 1) cq = 1;
504                else if (cq > 255) cq = 255;
505                cqt[i] = cq;
506        }
507}
508
509static mblk_t *
510read_rfc2435_header(DecState *s,mblk_t *inm)
511{
512        if (msgdsize(inm) >= sizeof(struct jpeghdr)) {
513                struct jpeghdr *hdr = (struct jpeghdr *)inm->b_rptr;
514                uint32_t off = ntohl(*(uint32_t*)inm->b_rptr);
515                uint16_t dri=0;
516                uint16_t table_len=0;
517                int len=0;
518
519                mblk_t *headers=NULL;
520
521                inm->b_rptr += sizeof(struct jpeghdr);
522                if (hdr->type>63){
523                        struct jpeghdr_rst *rsthdr = (struct jpeghdr_rst *)inm->b_rptr;
524                        dri = ntohs(rsthdr->dri);
525                        inm->b_rptr += sizeof(struct jpeghdr_rst);
526                }
527                       
528                if (off==0){
529                        if (hdr->q>=128){
530                                inm->b_rptr++; /* MBZ */
531                                inm->b_rptr++; /* Precision */
532                                table_len = ntohs(*((uint16_t*)(inm->b_rptr)));
533                                inm->b_rptr++; /* len */
534                                inm->b_rptr++; /* len */
535                                headers = allocb(495 + table_len*2 + (dri > 0 ? 6 : 0), 0);
536                                len = MakeHeaders(headers->b_rptr, hdr->type, hdr->width, hdr->height,
537                                        inm->b_rptr, inm->b_rptr+table_len/2, table_len, dri);
538                                inm->b_rptr += table_len;
539                                headers->b_wptr += len;
540                        }else{
541                                uint8_t lqt_cqt[128];
542                                MakeTables(hdr->q, lqt_cqt, lqt_cqt+64);
543                                table_len=128;
544                                headers = allocb(495 + table_len + (dri > 0 ? 6 : 0), 0);
545                                len = MakeHeaders(headers->b_rptr, hdr->type, hdr->width, hdr->height,
546                                        lqt_cqt, lqt_cqt+64, table_len, dri);
547                                headers->b_wptr += len; 
548                        }
549                }
550
551                if (headers!=NULL)
552                {
553                        /* prepend headers to JPEG RTP data */
554                        if (mblk_get_marker_info(inm))
555                                mblk_set_marker_info(headers, TRUE);
556                        headers->b_cont=inm;
557                        msgpullup(headers, -1);
558                        return headers;
559                }
560        } else {
561                freemsg(inm);
562                inm=NULL;
563        }
564        return inm;
565}
566
567static mblk_t *get_as_yuvmsg(MSFilter *f, DecState *s, AVFrame *orig){
568        AVCodecContext *ctx=&s->av_context;
569
570        if (ctx->width==0 || ctx->height==0){
571                ms_error("%s: wrong image size provided by decoder.",f->desc->name);
572                return NULL;
573        }
574        if (orig->data[0]==NULL){
575                ms_error("%s: no image data.",f->desc->name);
576                return NULL;
577        }
578        if (s->outbuf.w!=ctx->width || s->outbuf.h!=ctx->height){
579                if (s->sws_ctx!=NULL){
580                        sws_freeContext(s->sws_ctx);
581                        s->sws_ctx=NULL;
582                }
583                s->yuv_msg=yuv_buf_alloc(&s->outbuf,ctx->width,ctx->height);
584                s->outbuf.w=ctx->width;
585                s->outbuf.h=ctx->height;
586                s->sws_ctx=sws_getContext(ctx->width,ctx->height,ctx->pix_fmt,
587                        ctx->width,ctx->height,s->output_pix_fmt,SWS_FAST_BILINEAR,
588                        NULL, NULL, NULL);
589        }
590        if (s->sws_ctx==NULL){
591                ms_error("%s: missing rescaling context.",f->desc->name);
592                return NULL;
593        }
594        if (sws_scale(s->sws_ctx,orig->data,orig->linesize, 0,
595                                        ctx->height, s->outbuf.planes, s->outbuf.strides)<0){
596                ms_error("%s: error in sws_scale().",f->desc->name);
597        }
598        return dupmsg(s->yuv_msg);
599}
600
601static void dec_process_frame(MSFilter *f, mblk_t *inm){
602        DecState *s=(DecState*)f->data;
603        AVFrame orig;
604        int got_picture;
605        /* get a picture from the input queue */
606       
607        if (f->desc->id==MS_H263_DEC_ID) inm=skip_rfc2429_header(inm);
608        else if (f->desc->id==MS_H263_OLD_DEC_ID) inm=skip_rfc2190_header(inm);
609        else if (s->codec==CODEC_ID_SNOW && s->input==NULL) inm=parse_snow_header(s,inm);
610        else if (s->codec==CODEC_ID_MJPEG && f->desc->id==MS_JPEG_DEC_ID) inm=read_rfc2435_header(s,inm);
611        if (inm){
612                /* accumulate the video packet until we have the rtp markbit*/
613                if (s->input==NULL){
614                        s->input=inm;
615                }else{
616                        concatb(s->input,inm);
617                }
618               
619                if (mblk_get_marker_info(inm)){
620                        mblk_t *frame;
621                        int remain,len;
622                        /*ms_message("got marker bit !");*/
623                        /*append some padding bytes for ffmpeg to safely
624                        read extra bytes...*/
625                        msgpullup(s->input,msgdsize(s->input)+8);
626                        frame=s->input;
627                        s->input=NULL;
628                        while ( (remain=frame->b_wptr-frame->b_rptr)> 0) {
629                                AVPacket pkt;
630                                av_init_packet(&pkt);
631                                pkt.data = frame->b_rptr;
632                                pkt.size = remain;
633                                len=avcodec_decode_video2(&s->av_context,&orig,&got_picture,&pkt);
634                                /*len=avcodec_decode_video(&s->av_context,&orig,&got_picture,(uint8_t*)frame->b_rptr,remain );*/
635                                if (len<=0) {
636                                        ms_warning("ms_AVdecoder_process: error %i.",len);
637                                        break;
638                                }
639                                if (got_picture) {
640                                        mblk_t *om = get_as_yuvmsg(f,s,&orig);
641                                        if (om!=NULL)
642                                                ms_queue_put(f->outputs[0],om);
643                                }
644                                frame->b_rptr+=len;
645                        }
646                        freemsg(frame);
647                }
648        }
649}
650
651static void dec_process(MSFilter *f){
652        mblk_t *inm;
653        while((inm=ms_queue_get(f->inputs[0]))!=0){
654                dec_process_frame(f,inm);
655        }
656}
657
658
659static MSFilterMethod methods[]={
660        {               MS_FILTER_ADD_FMTP              ,       dec_add_fmtp    },
661        {               0               ,               NULL                    }
662};
663
664#ifdef _MSC_VER
665
666MSFilterDesc ms_h263_dec_desc={
667        MS_H263_DEC_ID,
668        "MSH263Dec",
669        N_("A H.263 decoder using ffmpeg library"),
670        MS_FILTER_DECODER,
671        "H263-1998",
672        1,
673        1,
674        dec_h263_init,
675        dec_preprocess,
676        dec_process,
677        dec_postprocess,
678        dec_uninit,
679        methods
680};
681
682MSFilterDesc ms_h263_old_dec_desc={
683        MS_H263_OLD_DEC_ID,
684        "MSH263OldDec",
685        N_("A H.263 decoder using ffmpeg library"),
686        MS_FILTER_DECODER,
687        "H263",
688        1,
689        1,
690        dec_h263_init,
691        dec_preprocess,
692        dec_process,
693        dec_postprocess,
694        dec_uninit,
695        methods
696};
697
698
699MSFilterDesc ms_mpeg4_dec_desc={
700        MS_MPEG4_DEC_ID,
701        "MSMpeg4Dec",
702        N_("A MPEG4 decoder using ffmpeg library"),
703        MS_FILTER_DECODER,
704        "MP4V-ES",
705        1,
706        1,
707        dec_mpeg4_init,
708        dec_preprocess,
709        dec_process,
710        dec_postprocess,
711        dec_uninit,
712        methods
713};
714
715MSFilterDesc ms_jpeg_dec_desc={
716        MS_JPEG_DEC_ID,
717        "MSJpegDec",
718        N_("A RTP/JPEG decoder using ffmpeg library"),
719        MS_FILTER_DECODER,
720        "JPEG",
721        1,
722        1,
723        dec_mjpeg_init,
724        dec_preprocess,
725        dec_process,
726        dec_postprocess,
727        dec_uninit,
728        methods
729};
730
731MSFilterDesc ms_mjpeg_dec_desc={
732        MS_MJPEG_DEC_ID,
733        "MSMJpegDec",
734        N_("A MJPEG decoder using ffmpeg library"),
735        MS_FILTER_DECODER,
736        "MJPEG",
737        1,
738        1,
739        dec_mjpeg_init,
740        dec_preprocess,
741        dec_process,
742        dec_postprocess,
743        dec_uninit,
744        methods
745};
746
747MSFilterDesc ms_snow_dec_desc={
748        MS_SNOW_DEC_ID,
749        "MSSnowDec",
750        N_("A snow decoder using ffmpeg library"),
751        MS_FILTER_DECODER,
752        "snow",
753        1,
754        1,
755        dec_snow_init,
756        dec_preprocess,
757        dec_process,
758        dec_postprocess,
759        dec_uninit,
760        methods
761};
762
763#else
764
765MSFilterDesc ms_h263_dec_desc={
766        .id=MS_H263_DEC_ID,
767        .name="MSH263Dec",
768        .text=N_("A H.263 decoder using ffmpeg library"),
769        .category=MS_FILTER_DECODER,
770        .enc_fmt="H263-1998",
771        .ninputs=1,
772        .noutputs=1,
773        .init=dec_h263_init,
774        .preprocess=dec_preprocess,
775        .process=dec_process,
776        .postprocess=dec_postprocess,
777        .uninit=dec_uninit,
778        .methods= methods
779};
780
781MSFilterDesc ms_h263_old_dec_desc={
782        .id=MS_H263_OLD_DEC_ID,
783        .name="MSH263OldDec",
784        .text=N_("A H.263 decoder using ffmpeg library"),
785        .category=MS_FILTER_DECODER,
786        .enc_fmt="H263",
787        .ninputs=1,
788        .noutputs=1,
789        .init=dec_h263_init,
790        .preprocess=dec_preprocess,
791        .process=dec_process,
792        .postprocess=dec_postprocess,
793        .uninit=dec_uninit,
794        .methods= methods
795};
796
797
798MSFilterDesc ms_mpeg4_dec_desc={
799        .id=MS_MPEG4_DEC_ID,
800        .name="MSMpeg4Dec",
801        .text="A MPEG4 decoder using ffmpeg library",
802        .category=MS_FILTER_DECODER,
803        .enc_fmt="MP4V-ES",
804        .ninputs=1,
805        .noutputs=1,
806        .init=dec_mpeg4_init,
807        .preprocess=dec_preprocess,
808        .process=dec_process,
809        .postprocess=dec_postprocess,
810        .uninit=dec_uninit,
811        .methods= methods
812};
813
814MSFilterDesc ms_jpeg_dec_desc={
815        .id=MS_JPEG_DEC_ID,
816        .name="MSJpegDec",
817        .text="A RTP/MJEPG decoder using ffmpeg library",
818        .category=MS_FILTER_DECODER,
819        .enc_fmt="JPEG",
820        .ninputs=1,
821        .noutputs=1,
822        .init=dec_mjpeg_init,
823        .preprocess=dec_preprocess,
824        .process=dec_process,
825        .postprocess=dec_postprocess,
826        .uninit=dec_uninit,
827        .methods= methods
828};
829
830MSFilterDesc ms_mjpeg_dec_desc={
831        .id=MS_MJPEG_DEC_ID,
832        .name="MSMJpegDec",
833        .text="A MJEPG decoder using ffmpeg library",
834        .category=MS_FILTER_DECODER,
835        .enc_fmt="MJPEG",
836        .ninputs=1,
837        .noutputs=1,
838        .init=dec_mjpeg_init,
839        .preprocess=dec_preprocess,
840        .process=dec_process,
841        .postprocess=dec_postprocess,
842        .uninit=dec_uninit,
843        .methods= methods
844};
845
846MSFilterDesc ms_snow_dec_desc={
847        .id=MS_SNOW_DEC_ID,
848        .name="MSSnowDec",
849        .text="A snow decoder using ffmpeg library",
850        .category=MS_FILTER_DECODER,
851        .enc_fmt="x-snow",
852        .ninputs=1,
853        .noutputs=1,
854        .init=dec_snow_init,
855        .preprocess=dec_preprocess,
856        .process=dec_process,
857        .postprocess=dec_postprocess,
858        .uninit=dec_uninit,
859        .methods= methods
860};
861
862#endif
863
864MS_FILTER_DESC_EXPORT(ms_mpeg4_dec_desc)
865MS_FILTER_DESC_EXPORT(ms_h263_dec_desc)
866MS_FILTER_DESC_EXPORT(ms_h263_old_dec_desc)
867MS_FILTER_DESC_EXPORT(ms_snow_dec_desc)
868
869/* decode JPEG image with RTP/jpeg headers */
870MS_FILTER_DESC_EXPORT(ms_jpeg_dec_desc)
871/* decode JPEG image with jpeg headers */
872MS_FILTER_DESC_EXPORT(ms_mjpeg_dec_desc)
Note: See TracBrowser for help on using the repository browser.