Wed Jan 27 20:02:16 2016

Asterisk developer's documentation


vcodecs.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright 2007-2008, Sergio Fadda, Luigi Rizzo
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*
00018  * Video codecs support for console_video.c
00019  * $Revision: 369001 $
00020  */
00021 
00022 /*** MODULEINFO
00023    <support_level>extended</support_level>
00024  ***/
00025 
00026 #include "asterisk.h"
00027 #include "console_video.h"
00028 #include "asterisk/frame.h"
00029 #include "asterisk/utils.h"   /* ast_calloc() */
00030 
00031 struct video_out_desc;
00032 struct video_dec_desc;
00033 struct fbuf_t;
00034 
00035 /*
00036  * Each codec is defined by a number of callbacks
00037  */
00038 /*! \brief initialize the encoder */
00039 typedef int (*encoder_init_f)(AVCodecContext *v);
00040 
00041 /*! \brief actually call the encoder */
00042 typedef int (*encoder_encode_f)(struct video_out_desc *v);
00043 
00044 /*! \brief encapsulate the bistream in RTP frames */
00045 typedef struct ast_frame *(*encoder_encap_f)(struct fbuf_t *, int mtu,
00046       struct ast_frame **tail);
00047 
00048 /*! \brief inizialize the decoder */
00049 typedef int (*decoder_init_f)(AVCodecContext *enc_ctx);
00050 
00051 /*! \brief extract the bitstream from RTP frames and store in the fbuf.
00052  * return 0 if ok, 1 on error
00053  */
00054 typedef int (*decoder_decap_f)(struct fbuf_t *b, uint8_t *data, int len);
00055 
00056 /*! \brief actually call the decoder */
00057 typedef int (*decoder_decode_f)(struct video_dec_desc *v, struct fbuf_t *b);
00058 
00059 struct video_codec_desc {
00060    const char     *name;      /* format name */
00061    int         format;     /* AST_FORMAT_* */
00062    encoder_init_f    enc_init;
00063    encoder_encap_f      enc_encap;
00064    encoder_encode_f  enc_run;
00065    decoder_init_f    dec_init;
00066    decoder_decap_f      dec_decap;
00067    decoder_decode_f  dec_run;
00068 };
00069 
00070 /*
00071  * Descriptor for the incoming stream, with multiple buffers for the bitstream
00072  * extracted from the RTP packets, RTP reassembly info, and a frame buffer
00073  * for the decoded frame (buf).
00074  * The descriptor is allocated as the first frame comes in.
00075  *
00076  * Incoming payload is stored in one of the dec_in[] buffers, which are
00077  * emptied by the video thread. These buffers are organized in a circular
00078  * queue, with dec_in_cur being the buffer in use by the incoming stream,
00079  * and dec_in_dpy is the one being displayed. When the pointers need to
00080  * be changed, we synchronize the access to them with dec_lock.
00081  * When the list is full dec_in_cur = NULL (we cannot store new data),
00082  * when the list is empty dec_in_dpy = NULL (we cannot display frames).
00083  */
00084 struct video_dec_desc {
00085    struct video_codec_desc *d_callbacks;  /* decoder callbacks */
00086    AVCodecContext          *dec_ctx;   /* information about the codec in the stream */
00087    AVCodec                 *codec;     /* reference to the codec */
00088    AVFrame                 *d_frame;   /* place to store the decoded frame */
00089    AVCodecParserContext    *parser;
00090    uint16_t       next_seq;   /* must be 16 bit */
00091    int                     discard; /* flag for discard status */
00092 #define N_DEC_IN  3  /* number of incoming buffers */
00093    struct fbuf_t     *dec_in_cur;   /* buffer being filled in */
00094    struct fbuf_t     *dec_in_dpy;   /* buffer to display */
00095    struct fbuf_t dec_in[N_DEC_IN];  /* incoming bitstream, allocated/extended in fbuf_append() */
00096    struct fbuf_t dec_out;  /* decoded frame, no buffer (data is in AVFrame) */
00097 };
00098 
00099 #ifdef debugging_only
00100 
00101 /* some debugging code to check the bitstream:
00102  * declare a bit buffer, initialize it, and fetch data from it.
00103  */
00104 struct bitbuf {
00105    const uint8_t *base;
00106    int   bitsize; /* total size in bits */
00107    int   ofs;  /* next bit to read */
00108 };
00109 
00110 static struct bitbuf bitbuf_init(const uint8_t *base, int bitsize, int start_ofs)
00111 {
00112    struct bitbuf a;
00113    a.base = base;
00114    a.bitsize = bitsize;
00115    a.ofs = start_ofs;
00116    return a;
00117 }
00118 
00119 static int bitbuf_left(struct bitbuf *b)
00120 {
00121    return b->bitsize - b->ofs;
00122 }
00123 
00124 static uint32_t getbits(struct bitbuf *b, int n)
00125 {
00126    int i, ofs;
00127    const uint8_t *d;
00128    uint8_t mask;
00129    uint32_t retval = 0;
00130    if (n> 31) {
00131       ast_log(LOG_WARNING, "too many bits %d, max 32\n", n);
00132       return 0;
00133    }
00134    if (n + b->ofs > b->bitsize) {
00135       ast_log(LOG_WARNING, "bitbuf overflow %d of %d\n", n + b->ofs, b->bitsize);
00136       n = b->bitsize - b->ofs;
00137    }
00138    ofs = 7 - b->ofs % 8;   /* start from msb */
00139    mask = 1 << ofs;
00140    d = b->base + b->ofs / 8;  /* current byte */
00141    for (i=0 ; i < n; i++) {
00142       retval += retval + (*d & mask ? 1 : 0);   /* shift in new byte */
00143       b->ofs++;
00144       mask >>= 1;
00145       if (mask == 0) {
00146          d++;
00147          mask = 0x80;
00148       }
00149    }
00150    return retval;
00151 }
00152 
00153 static void check_h261(struct fbuf_t *b)
00154 {
00155    struct bitbuf a = bitbuf_init(b->data, b->used * 8, 0);
00156    uint32_t x, y;
00157    
00158    x = getbits(&a, 20); /* PSC, 0000 0000 0000 0001 0000 */
00159    if (x != 0x10) {
00160       ast_log(LOG_WARNING, "bad PSC 0x%x\n", x);
00161       return;
00162    }
00163    x = getbits(&a, 5);  /* temporal reference */
00164    y = getbits(&a, 6);  /* ptype */
00165    if (0)
00166    ast_log(LOG_WARNING, "size %d TR %d PTY spl %d doc %d freeze %d %sCIF hi %d\n",
00167       b->used,
00168       x,
00169       (y & 0x20) ? 1 : 0,
00170       (y & 0x10) ? 1 : 0,
00171       (y & 0x8) ? 1 : 0,
00172       (y & 0x4) ? "" : "Q",
00173       (y & 0x2) ? 1:0);
00174    while ( (x = getbits(&a, 1)) == 1)
00175       ast_log(LOG_WARNING, "PSPARE 0x%x\n", getbits(&a, 8));
00176    // ast_log(LOG_WARNING, "PSPARE 0 - start GOB LAYER\n");
00177    while ( (x = bitbuf_left(&a)) > 0) {
00178       // ast_log(LOG_WARNING, "GBSC %d bits left\n", x);
00179       x = getbits(&a, 16); /* GBSC 0000 0000 0000 0001 */
00180       if (x != 0x1) {
00181          ast_log(LOG_WARNING, "bad GBSC 0x%x\n", x);
00182          break;
00183       }
00184       x = getbits(&a, 4);  /* group number */
00185       y = getbits(&a, 5);  /* gquant */
00186       if (x == 0) {
00187          ast_log(LOG_WARNING, "  bad GN %d\n", x);
00188          break;
00189       }
00190       while ( (x = getbits(&a, 1)) == 1)
00191          ast_log(LOG_WARNING, "GSPARE 0x%x\n", getbits(&a, 8));
00192       while ( (x = bitbuf_left(&a)) > 0) { /* MB layer */
00193          break;
00194       }
00195    }
00196 }
00197 
00198 void dump_buf(struct fbuf_t *b);
00199 void dump_buf(struct fbuf_t *b)
00200 {
00201    int i, x, last2lines;
00202    char buf[80];
00203 
00204    last2lines = (b->used - 16) & ~0xf;
00205    ast_log(LOG_WARNING, "buf size %d of %d\n", b->used, b->size);
00206    for (i = 0; i < b->used; i++) {
00207       x = i & 0xf;
00208       if ( x == 0) { /* new line */
00209          if (i != 0)
00210             ast_log(LOG_WARNING, "%s\n", buf);
00211          memset(buf, '\0', sizeof(buf));
00212          sprintf(buf, "%04x: ", i);
00213       }
00214       sprintf(buf + 6 + x*3, "%02x ", b->data[i]);
00215       if (i > 31 && i < last2lines)
00216          i = last2lines - 1;
00217    }
00218    if (buf[0])
00219       ast_log(LOG_WARNING, "%s\n", buf);
00220 }
00221 #endif /* debugging_only */
00222 
00223 /*!
00224  * Build an ast_frame for a given chunk of data, and link it into
00225  * the queue, with possibly 'head' bytes at the beginning to
00226  * fill in some fields later.
00227  */
00228 static struct ast_frame *create_video_frame(uint8_t *start, uint8_t *end,
00229                   int format, int head, struct ast_frame *prev)
00230 {
00231    int len = end-start;
00232    uint8_t *data;
00233    struct ast_frame *f;
00234 
00235    data = ast_calloc(1, len+head);
00236    f = ast_calloc(1, sizeof(*f));
00237    if (f == NULL || data == NULL) {
00238       ast_log(LOG_WARNING, "--- frame error f %p data %p len %d format %d\n",
00239             f, data, len, format);
00240       if (f)
00241          ast_free(f);
00242       if (data)
00243          ast_free(data);
00244       return NULL;
00245    }
00246    memcpy(data+head, start, len);
00247    f->data.ptr = data;
00248    f->mallocd = AST_MALLOCD_DATA | AST_MALLOCD_HDR;
00249    //f->has_timing_info = 1;
00250    //f->ts = ast_tvdiff_ms(ast_tvnow(), out->ts);
00251    f->datalen = len+head;
00252    f->frametype = AST_FRAME_VIDEO;
00253    f->subclass = format;
00254    f->samples = 0;
00255    f->offset = 0;
00256    f->src = "Console";
00257    f->delivery.tv_sec = 0;
00258    f->delivery.tv_usec = 0;
00259    f->seqno = 0;
00260    AST_LIST_NEXT(f, frame_list) = NULL;
00261 
00262    if (prev)
00263            AST_LIST_NEXT(prev, frame_list) = f;
00264 
00265    return f;
00266 }
00267 
00268 
00269 /*
00270  * Append a chunk of data to a buffer taking care of bit alignment
00271  * Return 0 on success, != 0 on failure
00272  */
00273 static int fbuf_append(struct fbuf_t *b, uint8_t *src, int len,
00274    int sbit, int ebit)
00275 {
00276    /*
00277     * Allocate buffer. ffmpeg wants an extra FF_INPUT_BUFFER_PADDING_SIZE,
00278     * and also wants 0 as a buffer terminator to prevent trouble.
00279     */
00280    int need = len + FF_INPUT_BUFFER_PADDING_SIZE;
00281    int i;
00282    uint8_t *dst, mask;
00283 
00284    if (b->data == NULL) {
00285       b->size = need;
00286       b->used = 0;
00287       b->ebit = 0;
00288       b->data = ast_calloc(1, b->size);
00289    } else if (b->used + need > b->size) {
00290       b->size = b->used + need;
00291       b->data = ast_realloc(b->data, b->size);
00292    }
00293    if (b->data == NULL) {
00294       ast_log(LOG_WARNING, "alloc failure for %d, discard\n",
00295          b->size);
00296       return 1;
00297    }
00298    if (b->used == 0 && b->ebit != 0) {
00299       ast_log(LOG_WARNING, "ebit not reset at start\n");
00300       b->ebit = 0;
00301    }
00302    dst = b->data + b->used;
00303    i = b->ebit + sbit;  /* bits to ignore around */
00304    if (i == 0) {  /* easy case, just append */
00305       /* do everything in the common block */
00306    } else if (i == 8) { /* easy too, just handle the overlap byte */
00307       mask = (1 << b->ebit) - 1;
00308       /* update the last byte in the buffer */
00309       dst[-1] &= ~mask; /* clear bits to ignore */
00310       dst[-1] |= (*src & mask);  /* append new bits */
00311       src += 1;   /* skip and prepare for common block */
00312       len --;
00313    } else { /* must shift the new block, not done yet */
00314       ast_log(LOG_WARNING, "must handle shift %d %d at %d\n",
00315          b->ebit, sbit, b->used);
00316       return 1;
00317    }
00318    memcpy(dst, src, len);
00319    b->used += len;
00320    b->ebit = ebit;
00321    b->data[b->used] = 0;   /* padding */
00322    return 0;
00323 }
00324 
00325 /*
00326  * Here starts the glue code for the various supported video codecs.
00327  * For each of them, we need to provide routines for initialization,
00328  * calling the encoder, encapsulating the bitstream in ast_frames,
00329  * extracting payload from ast_frames, and calling the decoder.
00330  */
00331 
00332 /*--- h263+ support --- */
00333 
00334 /*! \brief initialization of h263p */
00335 static int h263p_enc_init(AVCodecContext *enc_ctx)
00336 {
00337    /* modes supported are
00338    - Unrestricted Motion Vector (annex D)
00339    - Advanced Prediction (annex F)
00340    - Advanced Intra Coding (annex I)
00341    - Deblocking Filter (annex J)
00342    - Slice Structure (annex K)
00343    - Alternative Inter VLC (annex S)
00344    - Modified Quantization (annex T)
00345    */
00346    enc_ctx->flags |=CODEC_FLAG_H263P_UMV; /* annex D */
00347    enc_ctx->flags |=CODEC_FLAG_AC_PRED; /* annex f ? */
00348    enc_ctx->flags |=CODEC_FLAG_H263P_SLICE_STRUCT; /* annex k */
00349    enc_ctx->flags |= CODEC_FLAG_H263P_AIC; /* annex I */
00350 
00351    return 0;
00352 }
00353 
00354 
00355 /*
00356  * Create RTP/H.263 fragments to avoid IP fragmentation. We fragment on a
00357  * PSC or a GBSC, but if we don't find a suitable place just break somewhere.
00358  * Everything is byte-aligned.
00359  */
00360 static struct ast_frame *h263p_encap(struct fbuf_t *b, int mtu,
00361    struct ast_frame **tail)
00362 {
00363    struct ast_frame *cur = NULL, *first = NULL;
00364    uint8_t *d = b->data;
00365    int len = b->used;
00366    int l = len; /* size of the current fragment. If 0, must look for a psc */
00367 
00368    for (;len > 0; len -= l, d += l) {
00369       uint8_t *data;
00370       struct ast_frame *f;
00371       int i, h;
00372 
00373       if (len >= 3 && d[0] == 0 && d[1] == 0 && d[2] >= 0x80) {
00374          /* we are starting a new block, so look for a PSC. */
00375          for (i = 3; i < len - 3; i++) {
00376             if (d[i] == 0 && d[i+1] == 0 && d[i+2] >= 0x80) {
00377                l = i;
00378                break;
00379             }
00380          }
00381       }
00382       if (l > mtu || l > len) { /* psc not found, split */
00383          l = MIN(len, mtu);
00384       }
00385       if (l < 1 || l > mtu) {
00386          ast_log(LOG_WARNING, "--- frame error l %d\n", l);
00387          break;
00388       }
00389       
00390       if (d[0] == 0 && d[1] == 0) { /* we start with a psc */
00391          h = 0;
00392       } else { /* no psc, create a header */
00393          h = 2;
00394       }
00395 
00396       f = create_video_frame(d, d+l, AST_FORMAT_H263_PLUS, h, cur);
00397       if (!f)
00398          break;
00399 
00400       data = f->data.ptr;
00401       if (h == 0) {  /* we start with a psc */
00402          data[0] |= 0x04;  // set P == 1, and we are done
00403       } else { /* no psc, create a header */
00404          data[0] = data[1] = 0;  // P == 0
00405       }
00406 
00407       if (!cur)
00408          first = f;
00409       cur = f;
00410    }
00411 
00412    if (cur)
00413       cur->subclass |= 1; // RTP Marker
00414 
00415    *tail = cur;   /* end of the list */
00416    return first;
00417 }
00418 
00419 /*! \brief extract the bitstreem from the RTP payload.
00420  * This is format dependent.
00421  * For h263+, the format is defined in RFC 2429
00422  * and basically has a fixed 2-byte header as follows:
00423  * 5 bits   RR reserved, shall be 0
00424  * 1 bit P  indicate a start/end condition,
00425  *       in which case the payload should be prepended
00426  *       by two zero-valued bytes.
00427  * 1 bit V  there is an additional VRC header after this header
00428  * 6 bits   PLEN  length in bytes of extra picture header
00429  * 3 bits   PEBIT how many bits to be ignored in the last byte
00430  *
00431  * XXX the code below is not complete.
00432  */
00433 static int h263p_decap(struct fbuf_t *b, uint8_t *data, int len)
00434 {
00435    int PLEN;
00436 
00437    if (len < 2) {
00438       ast_log(LOG_WARNING, "invalid framesize %d\n", len);
00439       return 1;
00440    }
00441    PLEN = ( (data[0] & 1) << 5 ) | ( (data[1] & 0xf8) >> 3);
00442 
00443    if (PLEN > 0) {
00444       data += PLEN;
00445       len -= PLEN;
00446    }
00447    if (data[0] & 4)  /* bit P */
00448       data[0] = data[1] = 0;
00449    else {
00450       data += 2;
00451       len -= 2;
00452    }
00453    return fbuf_append(b, data, len, 0, 0);   /* ignore trail bits */
00454 }
00455 
00456 
00457 /*
00458  * generic encoder, used by the various protocols supported here.
00459  * We assume that the buffer is empty at the beginning.
00460  */
00461 static int ffmpeg_encode(struct video_out_desc *v)
00462 {
00463    struct fbuf_t *b = &v->enc_out;
00464    int i;
00465 
00466    b->used = avcodec_encode_video(v->enc_ctx, b->data, b->size, v->enc_in_frame);
00467    i = avcodec_encode_video(v->enc_ctx, b->data + b->used, b->size - b->used, NULL); /* delayed frames ? */
00468    if (i > 0) {
00469       ast_log(LOG_WARNING, "have %d more bytes\n", i);
00470       b->used += i;
00471    }
00472    return 0;
00473 }
00474 
00475 /*
00476  * Generic decoder, which is used by h263p, h263 and h261 as it simply
00477  * invokes ffmpeg's decoder.
00478  * av_parser_parse should merge a randomly chopped up stream into
00479  * proper frames. After that, if we have a valid frame, we decode it
00480  * until the entire frame is processed.
00481  */
00482 static int ffmpeg_decode(struct video_dec_desc *v, struct fbuf_t *b)
00483 {
00484    uint8_t *src = b->data;
00485    int srclen = b->used;
00486    int full_frame = 0;
00487 
00488    if (srclen == 0)  /* no data */
00489       return 0;
00490    while (srclen) {
00491       uint8_t *data;
00492       int datalen, ret;
00493       int len = av_parser_parse(v->parser, v->dec_ctx, &data, &datalen, src, srclen, 0, 0);
00494 
00495       src += len;
00496       srclen -= len;
00497       /* The parser might return something it cannot decode, so it skips
00498        * the block returning no data
00499        */
00500       if (data == NULL || datalen == 0)
00501          continue;
00502       ret = avcodec_decode_video(v->dec_ctx, v->d_frame, &full_frame, data, datalen);
00503       if (full_frame == 1) /* full frame */
00504          break;
00505       if (ret < 0) {
00506          ast_log(LOG_NOTICE, "Error decoding\n");
00507          break;
00508       }
00509    }
00510    if (srclen != 0)  /* update b with leftover data */
00511       memmove(b->data, src, srclen);
00512    b->used = srclen;
00513    b->ebit = 0;
00514    return full_frame;
00515 }
00516 
00517 static struct video_codec_desc h263p_codec = {
00518    .name = "h263p",
00519    .format = AST_FORMAT_H263_PLUS,
00520    .enc_init = h263p_enc_init,
00521    .enc_encap = h263p_encap,
00522    .enc_run = ffmpeg_encode,
00523    .dec_init = NULL,
00524    .dec_decap = h263p_decap,
00525    .dec_run = ffmpeg_decode
00526 };
00527 
00528 /*--- Plain h263 support --------*/
00529 
00530 static int h263_enc_init(AVCodecContext *enc_ctx)
00531 {
00532    /* XXX check whether these are supported */
00533    enc_ctx->flags |= CODEC_FLAG_H263P_UMV;
00534    enc_ctx->flags |= CODEC_FLAG_H263P_AIC;
00535    enc_ctx->flags |= CODEC_FLAG_H263P_SLICE_STRUCT;
00536    enc_ctx->flags |= CODEC_FLAG_AC_PRED;
00537 
00538    return 0;
00539 }
00540 
00541 /*
00542  * h263 encapsulation is specified in RFC2190. There are three modes
00543  * defined (A, B, C), with 4, 8 and 12 bytes of header, respectively.
00544  * The header is made as follows
00545  *     0.....................|.......................|.............|....31
00546  * F:1 P:1 SBIT:3 EBIT:3 SRC:3 I:1 U:1 S:1 A:1 R:4 DBQ:2 TRB:3 TR:8
00547  * FP = 0- mode A, (only one word of header)
00548  * FP = 10 mode B, and also means this is an I or P frame
00549  * FP = 11 mode C, and also means this is a PB frame.
00550  * SBIT, EBIT nuber of bits to ignore at beginning (msbits) and end (lsbits)
00551  * SRC  bits 6,7,8 from the h263 PTYPE field
00552  * I = 0 intra-coded, 1 = inter-coded (bit 9 from PTYPE)
00553  * U = 1 for Unrestricted Motion Vector (bit 10 from PTYPE)
00554  * S = 1 for Syntax Based Arith coding (bit 11 from PTYPE)
00555  * A = 1 for Advanced Prediction (bit 12 from PTYPE)
00556  * R = reserved, must be 0
00557  * DBQ = differential quantization, DBQUANT from h263, 0 unless we are using
00558  * PB frames
00559  * TRB = temporal reference for bframes, also 0 unless this is a PB frame
00560  * TR = temporal reference for P frames, also 0 unless PB frame.
00561  *
00562  * Mode B and mode C description omitted.
00563  *
00564  * An RTP frame can start with a PSC 0000 0000 0000 0000 1000 0
00565  * or with a GBSC, which also has the first 17 bits as a PSC.
00566  * Note - PSC are byte-aligned, GOB not necessarily. PSC start with
00567  * PSC:22 0000 0000 0000 0000 1000 00  picture start code
00568  * TR:8   .... ....        temporal reference
00569  *      PTYPE:13 or more         ptype...
00570  * If we don't fragment a GOB SBIT and EBIT = 0.
00571  * reference, 8 bit) 
00572  * 
00573  * The assumption below is that we start with a PSC.
00574  */
00575 static struct ast_frame *h263_encap(struct fbuf_t *b, int mtu,
00576       struct ast_frame **tail)
00577 {
00578    uint8_t *d = b->data;
00579    int start = 0, i, len = b->used;
00580    struct ast_frame *f, *cur = NULL, *first = NULL;
00581    const int pheader_len = 4; /* Use RFC-2190 Mode A */
00582    uint8_t h263_hdr[12];   /* worst case, room for a type c header */
00583    uint8_t *h = h263_hdr;  /* shorthand */
00584 
00585 #define H263_MIN_LEN 6
00586    if (len < H263_MIN_LEN) /* unreasonably small */
00587       return NULL;
00588 
00589    memset(h263_hdr, '\0', sizeof(h263_hdr));
00590    /* Now set the header bytes. Only type A by now,
00591     * and h[0] = h[2] = h[3] = 0 by default.
00592     * PTYPE starts 30 bits in the picture, so the first useful
00593     * bit for us is bit 36 i.e. within d[4] (0 is the msbit).
00594     * SRC = d[4] & 0x1c goes into data[1] & 0xe0
00595     * I   = d[4] & 0x02 goes into data[1] & 0x10
00596     * U   = d[4] & 0x01 goes into data[1] & 0x08
00597     * S   = d[5] & 0x80 goes into data[1] & 0x04
00598     * A   = d[5] & 0x40 goes into data[1] & 0x02
00599     * R   = 0           goes into data[1] & 0x01
00600     * Optimizing it, we have
00601     */
00602    h[1] = ( (d[4] & 0x1f) << 3 ) |  /* SRC, I, U */
00603       ( (d[5] & 0xc0) >> 5 );    /* S, A, R */
00604 
00605    /* now look for the next PSC or GOB header. First try to hit
00606     * a '0' byte then look around for the 0000 0000 0000 0000 1 pattern
00607     * which is both in the PSC and the GBSC.
00608     */
00609    for (i = H263_MIN_LEN, start = 0; start < len; start = i, i += 3) {
00610       //ast_log(LOG_WARNING, "search at %d of %d/%d\n", i, start, len);
00611       for (; i < len ; i++) {
00612          uint8_t x, rpos, lpos;
00613          int rpos_i; /* index corresponding to rpos */
00614          if (d[i] != 0)    /* cannot be in a GBSC */
00615             continue;
00616          if (i > len - 1)
00617             break;
00618          x = d[i+1];
00619          if (x == 0) /* next is equally good */
00620             continue;
00621          /* see if around us we can make 16 '0' bits for the GBSC.
00622           * Look for the first bit set on the right, and then
00623           * see if we have enough 0 on the left.
00624           * We are guaranteed to end before rpos == 0
00625           */
00626          for (rpos = 0x80, rpos_i = 8; rpos; rpos >>= 1, rpos_i--)
00627             if (x & rpos)  /* found the '1' bit in GBSC */
00628                break;
00629          x = d[i-1];    /* now look behind */
00630          for (lpos = rpos; lpos ; lpos >>= 1)
00631             if (x & lpos)  /* too early, not a GBSC */
00632                break;
00633          if (lpos)      /* as i said... */
00634             continue;
00635          /* now we have a GBSC starting somewhere in d[i-1],
00636           * but it might be not byte-aligned
00637           */
00638          if (rpos == 0x80) {  /* lucky case */
00639             i = i - 1;
00640          } else { /* XXX to be completed */
00641             ast_log(LOG_WARNING, "unaligned GBSC 0x%x %d\n",
00642                rpos, rpos_i);
00643          }
00644          break;
00645       }
00646       /* This frame is up to offset i (not inclusive).
00647        * We do not split it yet even if larger than MTU.
00648        */
00649       f = create_video_frame(d + start, d+i, AST_FORMAT_H263,
00650             pheader_len, cur);
00651 
00652       if (!f)
00653          break;
00654       memmove(f->data.ptr, h, 4);   /* copy the h263 header */
00655       /* XXX to do: if not aligned, fix sbit and ebit,
00656        * then move i back by 1 for the next frame
00657        */
00658       if (!cur)
00659          first = f;
00660       cur = f;
00661    }
00662 
00663    if (cur)
00664       cur->subclass |= 1;  // RTP Marker
00665 
00666    *tail = cur;
00667    return first;
00668 }
00669 
00670 /* XXX We only drop the header here, but maybe we need more. */
00671 static int h263_decap(struct fbuf_t *b, uint8_t *data, int len)
00672 {
00673    if (len < 4) {
00674       ast_log(LOG_WARNING, "invalid framesize %d\n", len);
00675       return 1;   /* error */
00676    }
00677 
00678    if ( (data[0] & 0x80) == 0) {
00679       len -= 4;
00680       data += 4;
00681    } else {
00682       ast_log(LOG_WARNING, "unsupported mode 0x%x\n",
00683          data[0]);
00684       return 1;
00685    }
00686    return fbuf_append(b, data, len, 0, 0);   /* XXX no bit alignment support yet */
00687 }
00688 
00689 static struct video_codec_desc h263_codec = {
00690    .name = "h263",
00691    .format = AST_FORMAT_H263,
00692    .enc_init = h263_enc_init,
00693    .enc_encap = h263_encap,
00694    .enc_run = ffmpeg_encode,
00695    .dec_init = NULL,
00696    .dec_decap = h263_decap,
00697    .dec_run = ffmpeg_decode
00698                   
00699 };
00700 
00701 /*---- h261 support -----*/
00702 static int h261_enc_init(AVCodecContext *enc_ctx)
00703 {
00704    /* It is important to set rtp_payload_size = 0, otherwise
00705     * ffmpeg in h261 mode will produce output that it cannot parse.
00706     * Also try to send I frames more frequently than with other codecs.
00707     */
00708    enc_ctx->rtp_payload_size = 0; /* important - ffmpeg fails otherwise */
00709 
00710    return 0;
00711 }
00712 
00713 /*
00714  * The encapsulation of H261 is defined in RFC4587 which obsoletes RFC2032
00715  * The bitstream is preceded by a 32-bit header word:
00716  *  SBIT:3 EBIT:3 I:1 V:1 GOBN:4 MBAP:5 QUANT:5 HMVD:5 VMVD:5
00717  * SBIT and EBIT are the bits to be ignored at beginning and end,
00718  * I=1 if the stream has only INTRA frames - cannot change during the stream.
00719  * V=0 if motion vector is not used. Cannot change.
00720  * GOBN is the GOB number in effect at the start of packet, 0 if we
00721  * start with a GOB header
00722  * QUANT is the quantizer in effect, 0 if we start with GOB header
00723  * HMVD  reference horizontal motion vector. 10000 is forbidden
00724  * VMVD  reference vertical motion vector, as above.
00725  * Packetization should occur at GOB boundaries, and if not possible
00726  * with MacroBlock fragmentation. However it is likely that blocks
00727  * are not bit-aligned so we must take care of this.
00728  */
00729 static struct ast_frame *h261_encap(struct fbuf_t *b, int mtu,
00730       struct ast_frame **tail)
00731 {
00732    uint8_t *d = b->data;
00733    int start = 0, i, len = b->used;
00734    struct ast_frame *f, *cur = NULL, *first = NULL;
00735    const int pheader_len = 4;
00736    uint8_t h261_hdr[4];
00737    uint8_t *h = h261_hdr;  /* shorthand */
00738    int sbit = 0, ebit = 0;
00739 
00740 #define H261_MIN_LEN 10
00741    if (len < H261_MIN_LEN) /* unreasonably small */
00742       return NULL;
00743 
00744    memset(h261_hdr, '\0', sizeof(h261_hdr));
00745 
00746    /* Similar to the code in h263_encap, but the marker there is longer.
00747     * Start a few bytes within the bitstream to avoid hitting the marker
00748     * twice. Note we might access the buffer at len, but this is ok because
00749     * the caller has it oversized.
00750     */
00751    for (i = H261_MIN_LEN, start = 0; start < len - 1; start = i, i += 4) {
00752 #if 0 /* test - disable packetization */
00753       i = len; /* wrong... */
00754 #else
00755       int found = 0, found_ebit = 0;   /* last GBSC position found */
00756       for (; i < len ; i++) {
00757          uint8_t x, rpos, lpos;
00758          if (d[i] != 0)    /* cannot be in a GBSC */
00759             continue;
00760          x = d[i+1];
00761          if (x == 0) /* next is equally good */
00762             continue;
00763          /* See if around us we find 15 '0' bits for the GBSC.
00764           * Look for the first bit set on the right, and then
00765           * see if we have enough 0 on the left.
00766           * We are guaranteed to end before rpos == 0
00767           */
00768          for (rpos = 0x80, ebit = 7; rpos; ebit--, rpos >>= 1)
00769             if (x & rpos)  /* found the '1' bit in GBSC */
00770                break;
00771          x = d[i-1];    /* now look behind */
00772          for (lpos = (rpos >> 1); lpos ; lpos >>= 1)
00773             if (x & lpos)  /* too early, not a GBSC */
00774                break;
00775          if (lpos)      /* as i said... */
00776             continue;
00777          /* now we have a GBSC starting somewhere in d[i-1],
00778           * but it might be not byte-aligned. Just remember it.
00779           */
00780          if (i - start > mtu) /* too large, stop now */
00781             break;
00782          found_ebit = ebit;
00783          found = i;
00784          i += 4;  /* continue forward */
00785       }
00786       if (i >= len) {   /* trim if we went too forward */
00787          i = len;
00788          ebit = 0;   /* hopefully... should ask the bitstream ? */
00789       }
00790       if (i - start > mtu && found) {
00791          /* use the previous GBSC, hope is within the mtu */
00792          i = found;
00793          ebit = found_ebit;
00794       }
00795 #endif /* test */
00796       if (i - start < 4)   /* XXX too short ? */
00797          continue;
00798       /* This frame is up to offset i (not inclusive).
00799        * We do not split it yet even if larger than MTU.
00800        */
00801       f = create_video_frame(d + start, d+i, AST_FORMAT_H261,
00802             pheader_len, cur);
00803 
00804       if (!f)
00805          break;
00806       /* recompute header with I=0, V=1 */
00807       h[0] = ( (sbit & 7) << 5 ) | ( (ebit & 7) << 2 ) | 1;
00808       memmove(f->data.ptr, h, 4);   /* copy the h261 header */
00809       if (ebit)   /* not aligned, restart from previous byte */
00810          i--;
00811       sbit = (8 - ebit) & 7;
00812       ebit = 0;
00813       if (!cur)
00814          first = f;
00815       cur = f;
00816    }
00817    if (cur)
00818       cur->subclass |= 1;  // RTP Marker
00819 
00820    *tail = cur;
00821    return first;
00822 }
00823 
00824 /*
00825  * Pieces might be unaligned so we really need to put them together.
00826  */
00827 static int h261_decap(struct fbuf_t *b, uint8_t *data, int len)
00828 {
00829    int ebit, sbit;
00830 
00831    if (len < 8) {
00832       ast_log(LOG_WARNING, "invalid framesize %d\n", len);
00833       return 1;
00834    }
00835    sbit = (data[0] >> 5) & 7;
00836    ebit = (data[0] >> 2) & 7;
00837    len -= 4;
00838    data += 4;
00839    return fbuf_append(b, data, len, sbit, ebit);
00840 }
00841 
00842 static struct video_codec_desc h261_codec = {
00843    .name = "h261",
00844    .format = AST_FORMAT_H261,
00845    .enc_init = h261_enc_init,
00846    .enc_encap = h261_encap,
00847    .enc_run = ffmpeg_encode,
00848    .dec_init = NULL,
00849    .dec_decap = h261_decap,
00850    .dec_run = ffmpeg_decode
00851 };
00852 
00853 /* mpeg4 support */
00854 static int mpeg4_enc_init(AVCodecContext *enc_ctx)
00855 {
00856 #if 0
00857    //enc_ctx->flags |= CODEC_FLAG_LOW_DELAY; /*don't use b frames ?*/
00858    enc_ctx->flags |= CODEC_FLAG_AC_PRED;
00859    enc_ctx->flags |= CODEC_FLAG_H263P_UMV;
00860    enc_ctx->flags |= CODEC_FLAG_QPEL;
00861    enc_ctx->flags |= CODEC_FLAG_4MV;
00862    enc_ctx->flags |= CODEC_FLAG_GMC;
00863    enc_ctx->flags |= CODEC_FLAG_LOOP_FILTER;
00864    enc_ctx->flags |= CODEC_FLAG_H263P_SLICE_STRUCT;
00865 #endif
00866    enc_ctx->rtp_payload_size = 0; /* important - ffmpeg fails otherwise */
00867    return 0;
00868 }
00869 
00870 /* simplistic encapsulation - just split frames in mtu-size units */
00871 static struct ast_frame *mpeg4_encap(struct fbuf_t *b, int mtu,
00872    struct ast_frame **tail)
00873 {
00874    struct ast_frame *f, *cur = NULL, *first = NULL;
00875    uint8_t *d = b->data;
00876    uint8_t *end = d + b->used;
00877    int len;
00878 
00879    for (;d < end; d += len, cur = f) {
00880       len = MIN(mtu, end - d);
00881       f = create_video_frame(d, d + len, AST_FORMAT_MP4_VIDEO, 0, cur);
00882       if (!f)
00883          break;
00884       if (!first)
00885          first = f;
00886    }
00887    if (cur)
00888       cur->subclass |= 1;
00889    *tail = cur;
00890    return first;
00891 }
00892 
00893 static int mpeg4_decap(struct fbuf_t *b, uint8_t *data, int len)
00894 {
00895    return fbuf_append(b, data, len, 0, 0);
00896 }
00897 
00898 static int mpeg4_decode(struct video_dec_desc *v, struct fbuf_t *b)
00899 {
00900    int full_frame = 0, datalen = b->used;
00901    int ret = avcodec_decode_video(v->dec_ctx, v->d_frame, &full_frame,
00902       b->data, datalen);
00903    if (ret < 0) {
00904       ast_log(LOG_NOTICE, "Error decoding\n");
00905       ret = datalen; /* assume we used everything. */
00906    }
00907    datalen -= ret;
00908    if (datalen > 0)  /* update b with leftover bytes */
00909       memmove(b->data, b->data + ret, datalen);
00910    b->used = datalen;
00911    b->ebit = 0;
00912    return full_frame;
00913 }
00914 
00915 static struct video_codec_desc mpeg4_codec = {
00916    .name = "mpeg4",
00917    .format = AST_FORMAT_MP4_VIDEO,
00918    .enc_init = mpeg4_enc_init,
00919    .enc_encap = mpeg4_encap,
00920    .enc_run = ffmpeg_encode,
00921    .dec_init = NULL,
00922    .dec_decap = mpeg4_decap,
00923    .dec_run = mpeg4_decode
00924 };
00925 
00926 static int h264_enc_init(AVCodecContext *enc_ctx)
00927 {
00928    enc_ctx->flags |= CODEC_FLAG_TRUNCATED;
00929    //enc_ctx->flags |= CODEC_FLAG_GLOBAL_HEADER;
00930    //enc_ctx->flags2 |= CODEC_FLAG2_FASTPSKIP;
00931    /* TODO: Maybe we need to add some other flags */
00932    enc_ctx->rtp_mode = 0;
00933    enc_ctx->rtp_payload_size = 0;
00934    enc_ctx->bit_rate_tolerance = enc_ctx->bit_rate;
00935    return 0;
00936 }
00937 
00938 static int h264_dec_init(AVCodecContext *dec_ctx)
00939 {
00940    dec_ctx->flags |= CODEC_FLAG_TRUNCATED;
00941 
00942    return 0;
00943 }
00944 
00945 /*
00946  * The structure of a generic H.264 stream is:
00947  * - 0..n 0-byte(s), unused, optional. one zero-byte is always present
00948  *   in the first NAL before the start code prefix.
00949  * - start code prefix (3 bytes): 0x000001
00950  *   (the first bytestream has a 
00951  *   like these 0x00000001!)
00952  * - NAL header byte ( F[1] | NRI[2] | Type[5] ) where type != 0
00953  * - byte-stream
00954  * - 0..n 0-byte(s) (padding, unused).
00955  * Segmentation in RTP only needs to be done on start code prefixes.
00956  * If fragments are too long... we don't support it yet.
00957  * - encapsulate (or fragment) the byte-stream (with NAL header included)
00958  */
00959 static struct ast_frame *h264_encap(struct fbuf_t *b, int mtu,
00960    struct ast_frame **tail)
00961 {
00962    struct ast_frame *f = NULL, *cur = NULL, *first = NULL;
00963    uint8_t *d, *start = b->data;
00964    uint8_t *end = start + b->used;
00965 
00966    /* Search the first start code prefix - ITU-T H.264 sec. B.2,
00967     * and move start right after that, on the NAL header byte.
00968     */
00969 #define HAVE_NAL(x) (x[-4] == 0 && x[-3] == 0 && x[-2] == 0 && x[-1] == 1)
00970    for (start += 4; start < end; start++) {
00971       int ty = start[0] & 0x1f;
00972       if (HAVE_NAL(start) && ty != 0 && ty != 31)
00973          break;
00974    }
00975    /* if not found, or too short, we just skip the next loop and are done. */
00976 
00977    /* Here follows the main loop to create frames. Search subsequent start
00978     * codes, and then possibly fragment the unit into smaller fragments.
00979     */
00980    for (;start < end - 4; start = d) {
00981    int size;      /* size of current block */
00982    uint8_t hdr[2];      /* add-on header when fragmenting */
00983    int ty = 0;
00984 
00985    /* now search next nal */
00986    for (d = start + 4; d < end; d++) {
00987       ty = d[0] & 0x1f;
00988       if (HAVE_NAL(d))
00989          break;   /* found NAL */
00990    }
00991    /* have a block to send. d past the start code unless we overflow */
00992    if (d >= end) {   /* NAL not found */
00993       d = end + 4;
00994    } else if (ty == 0 || ty == 31) { /* found but invalid type, skip */
00995       ast_log(LOG_WARNING, "skip invalid nal type %d at %d of %d\n",
00996          ty, d - (uint8_t *)b->data, b->used);
00997       continue;
00998    }
00999 
01000    size = d - start - 4;   /* don't count the end */
01001 
01002    if (size < mtu) { // test - don't fragment
01003       // Single NAL Unit
01004       f = create_video_frame(start, d - 4, AST_FORMAT_H264, 0, cur);
01005       if (!f)
01006          break;
01007       if (!first)
01008          first = f;
01009 
01010       cur = f;
01011       continue;
01012    }
01013 
01014    // Fragmented Unit (Mode A: no DON, very weak)
01015    hdr[0] = (*start & 0xe0) | 28;   /* mark as a fragmentation unit */
01016    hdr[1] = (*start++ & 0x1f) | 0x80 ; /* keep type and set START bit */
01017    size--;     /* skip the NAL header */
01018    while (size) {
01019       uint8_t *data;
01020       int frag_size = MIN(size, mtu);
01021 
01022       f = create_video_frame(start, start+frag_size, AST_FORMAT_H264, 2, cur);
01023       if (!f)
01024          break;
01025       size -= frag_size;   /* skip this data block */
01026       start += frag_size;
01027 
01028       data = f->data.ptr;
01029       data[0] = hdr[0];
01030       data[1] = hdr[1] | (size == 0 ? 0x40 : 0);   /* end bit if we are done */
01031       hdr[1] &= ~0x80;  /* clear start bit for subsequent frames */
01032       if (!first)
01033          first = f;
01034       cur = f;
01035    }
01036     }
01037 
01038    if (cur)
01039       cur->subclass |= 1;     // RTP Marker
01040 
01041    *tail = cur;
01042 
01043    return first;
01044 }
01045 
01046 static int h264_decap(struct fbuf_t *b, uint8_t *data, int len)
01047 {
01048    /* Start Code Prefix (Annex B in specification) */
01049    uint8_t scp[] = { 0x00, 0x00, 0x00, 0x01 };
01050    int retval = 0;
01051    int type, ofs = 0;
01052 
01053    if (len < 2) {
01054       ast_log(LOG_WARNING, "--- invalid len %d\n", len);
01055       return 1;
01056    }
01057    /* first of all, check if the packet has F == 0 */
01058    if (data[0] & 0x80) {
01059       ast_log(LOG_WARNING, "--- forbidden packet; nal: %02x\n",
01060          data[0]);
01061       return 1;
01062    }
01063 
01064    type = data[0] & 0x1f;
01065    switch (type) {
01066    case 0:
01067    case 31:
01068       ast_log(LOG_WARNING, "--- invalid type: %d\n", type);
01069       return 1;
01070    case 24:
01071    case 25:
01072    case 26:
01073    case 27:
01074    case 29:
01075       ast_log(LOG_WARNING, "--- encapsulation not supported : %d\n", type);
01076       return 1;
01077    case 28: /* FU-A Unit */
01078       if (data[1] & 0x80) { // S == 1, import F and NRI from next
01079          data[1] &= 0x1f;  /* preserve type */
01080          data[1] |= (data[0] & 0xe0);  /* import F & NRI */
01081          retval = fbuf_append(b, scp, sizeof(scp), 0, 0);
01082          ofs = 1;
01083       } else {
01084          ofs = 2;
01085       }
01086       break;
01087    default: /* From 1 to 23 (Single NAL Unit) */
01088       retval = fbuf_append(b, scp, sizeof(scp), 0, 0);
01089    }
01090    if (!retval)
01091       retval = fbuf_append(b, data + ofs, len - ofs, 0, 0);
01092    if (retval)
01093       ast_log(LOG_WARNING, "result %d\n", retval);
01094    return retval;
01095 }
01096 
01097 static struct video_codec_desc h264_codec = {
01098    .name = "h264",
01099    .format = AST_FORMAT_H264,
01100    .enc_init = h264_enc_init,
01101    .enc_encap = h264_encap,
01102    .enc_run = ffmpeg_encode,
01103    .dec_init = h264_dec_init,
01104    .dec_decap = h264_decap,
01105    .dec_run = ffmpeg_decode
01106 };
01107 
01108 /*
01109  * Table of translation between asterisk and ffmpeg formats.
01110  * We need also a field for read and write (encoding and decoding), because
01111  * e.g. H263+ uses different codec IDs in ffmpeg when encoding or decoding.
01112  */
01113 struct _cm {    /* map ffmpeg codec types to asterisk formats */
01114    uint32_t ast_format; /* 0 is a terminator */
01115    enum CodecID   codec;
01116    enum { CM_RD = 1, CM_WR = 2, CM_RDWR = 3 } rw;  /* read or write or both ? */
01117    //struct video_codec_desc *codec_desc;
01118 };
01119 
01120 static const struct _cm video_formats[] = {
01121         { AST_FORMAT_H263_PLUS, CODEC_ID_H263,  CM_RD }, /* incoming H263P ? */
01122         { AST_FORMAT_H263_PLUS, CODEC_ID_H263P, CM_WR },
01123         { AST_FORMAT_H263,      CODEC_ID_H263,  CM_RD },
01124         { AST_FORMAT_H263,      CODEC_ID_H263,  CM_WR },
01125         { AST_FORMAT_H261,      CODEC_ID_H261,  CM_RDWR },
01126         { AST_FORMAT_H264,      CODEC_ID_H264,  CM_RDWR },
01127         { AST_FORMAT_MP4_VIDEO, CODEC_ID_MPEG4, CM_RDWR },
01128         { 0,                    0, 0 },
01129 };
01130                 
01131 
01132 /*! \brief map an asterisk format into an ffmpeg one */
01133 static enum CodecID map_video_format(uint32_t ast_format, int rw)
01134 {
01135    struct _cm *i;
01136 
01137    for (i = video_formats; i->ast_format != 0; i++)
01138       if (ast_format & i->ast_format && rw & i->rw && rw & i->rw)
01139          return i->codec;
01140    return CODEC_ID_NONE;
01141 }
01142 
01143 /* pointers to supported codecs. We assume the first one to be non null. */
01144 static const struct video_codec_desc *supported_codecs[] = {
01145    &h263p_codec,
01146    &h264_codec,
01147    &h263_codec,
01148    &h261_codec,
01149    &mpeg4_codec,
01150    NULL
01151 };
01152 
01153 /*
01154  * Map the AST_FORMAT to the library. If not recognised, fail.
01155  * This is useful in the input path where we get frames.
01156  */
01157 static struct video_codec_desc *map_video_codec(int fmt)
01158 {
01159    int i;
01160 
01161    for (i = 0; supported_codecs[i]; i++)
01162       if (fmt == supported_codecs[i]->format) {
01163          ast_log(LOG_WARNING, "using %s for format 0x%x\n",
01164             supported_codecs[i]->name, fmt);
01165          return supported_codecs[i];
01166       }
01167    return NULL;
01168 }
01169 
01170 /*! \brief uninitialize the descriptor for remote video stream */
01171 static struct video_dec_desc *dec_uninit(struct video_dec_desc *v)
01172 {
01173    int i;
01174 
01175    if (v == NULL)    /* not initialized yet */
01176       return NULL;
01177    if (v->parser) {
01178       av_parser_close(v->parser);
01179       v->parser = NULL;
01180    }
01181    if (v->dec_ctx) {
01182       avcodec_close(v->dec_ctx);
01183       av_free(v->dec_ctx);
01184       v->dec_ctx = NULL;
01185    }
01186    if (v->d_frame) {
01187       av_free(v->d_frame);
01188       v->d_frame = NULL;
01189    }
01190    v->codec = NULL;  /* only a reference */
01191    v->d_callbacks = NULL;     /* forget the decoder */
01192    v->discard = 1;      /* start in discard mode */
01193    for (i = 0; i < N_DEC_IN; i++)
01194       fbuf_free(&v->dec_in[i]);
01195    fbuf_free(&v->dec_out);
01196    ast_free(v);
01197    return NULL;   /* error, in case someone cares */
01198 }
01199 
01200 /*
01201  * initialize ffmpeg resources used for decoding frames from the network.
01202  */
01203 static struct video_dec_desc *dec_init(uint32_t the_ast_format)
01204 {
01205    enum CodecID codec;
01206    struct video_dec_desc *v = ast_calloc(1, sizeof(*v));
01207    if (v == NULL)
01208       return NULL;
01209 
01210    v->discard = 1;
01211 
01212    v->d_callbacks = map_video_codec(the_ast_format);
01213    if (v->d_callbacks == NULL) {
01214       ast_log(LOG_WARNING, "cannot find video codec, drop input 0x%x\n", the_ast_format);
01215       return dec_uninit(v);
01216    }
01217 
01218    codec = map_video_format(v->d_callbacks->format, CM_RD);
01219 
01220    v->codec = avcodec_find_decoder(codec);
01221    if (!v->codec) {
01222       ast_log(LOG_WARNING, "Unable to find the decoder for format %d\n", codec);
01223       return dec_uninit(v);
01224    }
01225    /*
01226     * Initialize the codec context.
01227     */
01228    v->dec_ctx = avcodec_alloc_context();
01229    if (!v->dec_ctx) {
01230       ast_log(LOG_WARNING, "Cannot allocate the decoder context\n");
01231       return dec_uninit(v);
01232    }
01233    /* XXX call dec_init() ? */
01234    if (avcodec_open(v->dec_ctx, v->codec) < 0) {
01235       ast_log(LOG_WARNING, "Cannot open the decoder context\n");
01236       av_free(v->dec_ctx);
01237       v->dec_ctx = NULL;
01238       return dec_uninit(v);
01239    }
01240 
01241    v->parser = av_parser_init(codec);
01242    if (!v->parser) {
01243       ast_log(LOG_WARNING, "Cannot initialize the decoder parser\n");
01244       return dec_uninit(v);
01245    }
01246 
01247    v->d_frame = avcodec_alloc_frame();
01248    if (!v->d_frame) {
01249       ast_log(LOG_WARNING, "Cannot allocate decoding video frame\n");
01250       return dec_uninit(v);
01251    }
01252         v->dec_in_cur = &v->dec_in[0]; /* buffer for incoming frames */
01253         v->dec_in_dpy = NULL;      /* nothing to display */
01254 
01255    return v;   /* ok */
01256 }
01257 /*------ end codec specific code -----*/

Generated on 27 Jan 2016 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1