00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "asterisk.h"
00027 #include "console_video.h"
00028 #include "asterisk/frame.h"
00029 #include "asterisk/utils.h"
00030
00031 struct video_out_desc;
00032 struct video_dec_desc;
00033 struct fbuf_t;
00034
00035
00036
00037
00038
00039 typedef int (*encoder_init_f)(AVCodecContext *v);
00040
00041
00042 typedef int (*encoder_encode_f)(struct video_out_desc *v);
00043
00044
00045 typedef struct ast_frame *(*encoder_encap_f)(struct fbuf_t *, int mtu,
00046 struct ast_frame **tail);
00047
00048
00049 typedef int (*decoder_init_f)(AVCodecContext *enc_ctx);
00050
00051
00052
00053
00054 typedef int (*decoder_decap_f)(struct fbuf_t *b, uint8_t *data, int len);
00055
00056
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;
00061 int 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
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 struct video_dec_desc {
00085 struct video_codec_desc *d_callbacks;
00086 AVCodecContext *dec_ctx;
00087 AVCodec *codec;
00088 AVFrame *d_frame;
00089 AVCodecParserContext *parser;
00090 uint16_t next_seq;
00091 int discard;
00092 #define N_DEC_IN 3
00093 struct fbuf_t *dec_in_cur;
00094 struct fbuf_t *dec_in_dpy;
00095 struct fbuf_t dec_in[N_DEC_IN];
00096 struct fbuf_t dec_out;
00097 };
00098
00099 #ifdef debugging_only
00100
00101
00102
00103
00104 struct bitbuf {
00105 const uint8_t *base;
00106 int bitsize;
00107 int ofs;
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;
00139 mask = 1 << ofs;
00140 d = b->base + b->ofs / 8;
00141 for (i=0 ; i < n; i++) {
00142 retval += retval + (*d & mask ? 1 : 0);
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);
00159 if (x != 0x10) {
00160 ast_log(LOG_WARNING, "bad PSC 0x%x\n", x);
00161 return;
00162 }
00163 x = getbits(&a, 5);
00164 y = getbits(&a, 6);
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
00177 while ( (x = bitbuf_left(&a)) > 0) {
00178
00179 x = getbits(&a, 16);
00180 if (x != 0x1) {
00181 ast_log(LOG_WARNING, "bad GBSC 0x%x\n", x);
00182 break;
00183 }
00184 x = getbits(&a, 4);
00185 y = getbits(&a, 5);
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) {
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) {
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
00222
00223
00224
00225
00226
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
00250
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
00271
00272
00273 static int fbuf_append(struct fbuf_t *b, uint8_t *src, int len,
00274 int sbit, int ebit)
00275 {
00276
00277
00278
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;
00304 if (i == 0) {
00305
00306 } else if (i == 8) {
00307 mask = (1 << b->ebit) - 1;
00308
00309 dst[-1] &= ~mask;
00310 dst[-1] |= (*src & mask);
00311 src += 1;
00312 len --;
00313 } else {
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;
00322 return 0;
00323 }
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335 static int h263p_enc_init(AVCodecContext *enc_ctx)
00336 {
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 enc_ctx->flags |=CODEC_FLAG_H263P_UMV;
00347 enc_ctx->flags |=CODEC_FLAG_AC_PRED;
00348 enc_ctx->flags |=CODEC_FLAG_H263P_SLICE_STRUCT;
00349 enc_ctx->flags |= CODEC_FLAG_H263P_AIC;
00350
00351 return 0;
00352 }
00353
00354
00355
00356
00357
00358
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;
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
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) {
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) {
00391 h = 0;
00392 } else {
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) {
00402 data[0] |= 0x04;
00403 } else {
00404 data[0] = data[1] = 0;
00405 }
00406
00407 if (!cur)
00408 first = f;
00409 cur = f;
00410 }
00411
00412 if (cur)
00413 cur->subclass |= 1;
00414
00415 *tail = cur;
00416 return first;
00417 }
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
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)
00448 data[0] = data[1] = 0;
00449 else {
00450 data += 2;
00451 len -= 2;
00452 }
00453 return fbuf_append(b, data, len, 0, 0);
00454 }
00455
00456
00457
00458
00459
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);
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
00477
00478
00479
00480
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)
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
00498
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)
00504 break;
00505 if (ret < 0) {
00506 ast_log(LOG_NOTICE, "Error decoding\n");
00507 break;
00508 }
00509 }
00510 if (srclen != 0)
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
00529
00530 static int h263_enc_init(AVCodecContext *enc_ctx)
00531 {
00532
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
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
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;
00582 uint8_t h263_hdr[12];
00583 uint8_t *h = h263_hdr;
00584
00585 #define H263_MIN_LEN 6
00586 if (len < H263_MIN_LEN)
00587 return NULL;
00588
00589 memset(h263_hdr, '\0', sizeof(h263_hdr));
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602 h[1] = ( (d[4] & 0x1f) << 3 ) |
00603 ( (d[5] & 0xc0) >> 5 );
00604
00605
00606
00607
00608
00609 for (i = H263_MIN_LEN, start = 0; start < len; start = i, i += 3) {
00610
00611 for (; i < len ; i++) {
00612 uint8_t x, rpos, lpos;
00613 int rpos_i;
00614 if (d[i] != 0)
00615 continue;
00616 if (i > len - 1)
00617 break;
00618 x = d[i+1];
00619 if (x == 0)
00620 continue;
00621
00622
00623
00624
00625
00626 for (rpos = 0x80, rpos_i = 8; rpos; rpos >>= 1, rpos_i--)
00627 if (x & rpos)
00628 break;
00629 x = d[i-1];
00630 for (lpos = rpos; lpos ; lpos >>= 1)
00631 if (x & lpos)
00632 break;
00633 if (lpos)
00634 continue;
00635
00636
00637
00638 if (rpos == 0x80) {
00639 i = i - 1;
00640 } else {
00641 ast_log(LOG_WARNING, "unaligned GBSC 0x%x %d\n",
00642 rpos, rpos_i);
00643 }
00644 break;
00645 }
00646
00647
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);
00655
00656
00657
00658 if (!cur)
00659 first = f;
00660 cur = f;
00661 }
00662
00663 if (cur)
00664 cur->subclass |= 1;
00665
00666 *tail = cur;
00667 return first;
00668 }
00669
00670
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;
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);
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
00702 static int h261_enc_init(AVCodecContext *enc_ctx)
00703 {
00704
00705
00706
00707
00708 enc_ctx->rtp_payload_size = 0;
00709
00710 return 0;
00711 }
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
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;
00738 int sbit = 0, ebit = 0;
00739
00740 #define H261_MIN_LEN 10
00741 if (len < H261_MIN_LEN)
00742 return NULL;
00743
00744 memset(h261_hdr, '\0', sizeof(h261_hdr));
00745
00746
00747
00748
00749
00750
00751 for (i = H261_MIN_LEN, start = 0; start < len - 1; start = i, i += 4) {
00752 #if 0
00753 i = len;
00754 #else
00755 int found = 0, found_ebit = 0;
00756 for (; i < len ; i++) {
00757 uint8_t x, rpos, lpos;
00758 if (d[i] != 0)
00759 continue;
00760 x = d[i+1];
00761 if (x == 0)
00762 continue;
00763
00764
00765
00766
00767
00768 for (rpos = 0x80, ebit = 7; rpos; ebit--, rpos >>= 1)
00769 if (x & rpos)
00770 break;
00771 x = d[i-1];
00772 for (lpos = (rpos >> 1); lpos ; lpos >>= 1)
00773 if (x & lpos)
00774 break;
00775 if (lpos)
00776 continue;
00777
00778
00779
00780 if (i - start > mtu)
00781 break;
00782 found_ebit = ebit;
00783 found = i;
00784 i += 4;
00785 }
00786 if (i >= len) {
00787 i = len;
00788 ebit = 0;
00789 }
00790 if (i - start > mtu && found) {
00791
00792 i = found;
00793 ebit = found_ebit;
00794 }
00795 #endif
00796 if (i - start < 4)
00797 continue;
00798
00799
00800
00801 f = create_video_frame(d + start, d+i, AST_FORMAT_H261,
00802 pheader_len, cur);
00803
00804 if (!f)
00805 break;
00806
00807 h[0] = ( (sbit & 7) << 5 ) | ( (ebit & 7) << 2 ) | 1;
00808 memmove(f->data.ptr, h, 4);
00809 if (ebit)
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;
00819
00820 *tail = cur;
00821 return first;
00822 }
00823
00824
00825
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
00854 static int mpeg4_enc_init(AVCodecContext *enc_ctx)
00855 {
00856 #if 0
00857
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;
00867 return 0;
00868 }
00869
00870
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;
00906 }
00907 datalen -= ret;
00908 if (datalen > 0)
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
00930
00931
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
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
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
00967
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
00976
00977
00978
00979
00980 for (;start < end - 4; start = d) {
00981 int size;
00982 uint8_t hdr[2];
00983 int ty = 0;
00984
00985
00986 for (d = start + 4; d < end; d++) {
00987 ty = d[0] & 0x1f;
00988 if (HAVE_NAL(d))
00989 break;
00990 }
00991
00992 if (d >= end) {
00993 d = end + 4;
00994 } else if (ty == 0 || ty == 31) {
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;
01001
01002 if (size < mtu) {
01003
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
01015 hdr[0] = (*start & 0xe0) | 28;
01016 hdr[1] = (*start++ & 0x1f) | 0x80 ;
01017 size--;
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;
01026 start += frag_size;
01027
01028 data = f->data.ptr;
01029 data[0] = hdr[0];
01030 data[1] = hdr[1] | (size == 0 ? 0x40 : 0);
01031 hdr[1] &= ~0x80;
01032 if (!first)
01033 first = f;
01034 cur = f;
01035 }
01036 }
01037
01038 if (cur)
01039 cur->subclass |= 1;
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
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
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:
01078 if (data[1] & 0x80) {
01079 data[1] &= 0x1f;
01080 data[1] |= (data[0] & 0xe0);
01081 retval = fbuf_append(b, scp, sizeof(scp), 0, 0);
01082 ofs = 1;
01083 } else {
01084 ofs = 2;
01085 }
01086 break;
01087 default:
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
01110
01111
01112
01113 struct _cm {
01114 uint32_t ast_format;
01115 enum CodecID codec;
01116 enum { CM_RD = 1, CM_WR = 2, CM_RDWR = 3 } rw;
01117
01118 };
01119
01120 static const struct _cm video_formats[] = {
01121 { AST_FORMAT_H263_PLUS, CODEC_ID_H263, CM_RD },
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
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
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
01155
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
01171 static struct video_dec_desc *dec_uninit(struct video_dec_desc *v)
01172 {
01173 int i;
01174
01175 if (v == NULL)
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;
01191 v->d_callbacks = NULL;
01192 v->discard = 1;
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;
01198 }
01199
01200
01201
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
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
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];
01253 v->dec_in_dpy = NULL;
01254
01255 return v;
01256 }
01257