00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "asterisk.h"
00023 #include "console_video.h"
00024 #include "asterisk/frame.h"
00025 #include "asterisk/utils.h"
00026
00027 struct video_out_desc;
00028 struct video_dec_desc;
00029 struct fbuf_t;
00030
00031
00032
00033
00034
00035 typedef int (*encoder_init_f)(AVCodecContext *v);
00036
00037
00038 typedef int (*encoder_encode_f)(struct video_out_desc *v);
00039
00040
00041 typedef struct ast_frame *(*encoder_encap_f)(struct fbuf_t *, int mtu,
00042 struct ast_frame **tail);
00043
00044
00045 typedef int (*decoder_init_f)(AVCodecContext *enc_ctx);
00046
00047
00048
00049
00050 typedef int (*decoder_decap_f)(struct fbuf_t *b, uint8_t *data, int len);
00051
00052
00053 typedef int (*decoder_decode_f)(struct video_dec_desc *v, struct fbuf_t *b);
00054
00055 struct video_codec_desc {
00056 const char *name;
00057 int format;
00058 encoder_init_f enc_init;
00059 encoder_encap_f enc_encap;
00060 encoder_encode_f enc_run;
00061 decoder_init_f dec_init;
00062 decoder_decap_f dec_decap;
00063 decoder_decode_f dec_run;
00064 };
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 struct video_dec_desc {
00081 struct video_codec_desc *d_callbacks;
00082 AVCodecContext *dec_ctx;
00083 AVCodec *codec;
00084 AVFrame *d_frame;
00085 AVCodecParserContext *parser;
00086 uint16_t next_seq;
00087 int discard;
00088 #define N_DEC_IN 3
00089 struct fbuf_t *dec_in_cur;
00090 struct fbuf_t *dec_in_dpy;
00091 struct fbuf_t dec_in[N_DEC_IN];
00092 struct fbuf_t dec_out;
00093 };
00094
00095 #ifdef debugging_only
00096
00097
00098
00099
00100 struct bitbuf {
00101 const uint8_t *base;
00102 int bitsize;
00103 int ofs;
00104 };
00105
00106 static struct bitbuf bitbuf_init(const uint8_t *base, int bitsize, int start_ofs)
00107 {
00108 struct bitbuf a;
00109 a.base = base;
00110 a.bitsize = bitsize;
00111 a.ofs = start_ofs;
00112 return a;
00113 }
00114
00115 static int bitbuf_left(struct bitbuf *b)
00116 {
00117 return b->bitsize - b->ofs;
00118 }
00119
00120 static uint32_t getbits(struct bitbuf *b, int n)
00121 {
00122 int i, ofs;
00123 const uint8_t *d;
00124 uint8_t mask;
00125 uint32_t retval = 0;
00126 if (n> 31) {
00127 ast_log(LOG_WARNING, "too many bits %d, max 32\n", n);
00128 return 0;
00129 }
00130 if (n + b->ofs > b->bitsize) {
00131 ast_log(LOG_WARNING, "bitbuf overflow %d of %d\n", n + b->ofs, b->bitsize);
00132 n = b->bitsize - b->ofs;
00133 }
00134 ofs = 7 - b->ofs % 8;
00135 mask = 1 << ofs;
00136 d = b->base + b->ofs / 8;
00137 for (i=0 ; i < n; i++) {
00138 retval += retval + (*d & mask ? 1 : 0);
00139 b->ofs++;
00140 mask >>= 1;
00141 if (mask == 0) {
00142 d++;
00143 mask = 0x80;
00144 }
00145 }
00146 return retval;
00147 }
00148
00149 static void check_h261(struct fbuf_t *b)
00150 {
00151 struct bitbuf a = bitbuf_init(b->data, b->used * 8, 0);
00152 uint32_t x, y;
00153
00154 x = getbits(&a, 20);
00155 if (x != 0x10) {
00156 ast_log(LOG_WARNING, "bad PSC 0x%x\n", x);
00157 return;
00158 }
00159 x = getbits(&a, 5);
00160 y = getbits(&a, 6);
00161 if (0)
00162 ast_log(LOG_WARNING, "size %d TR %d PTY spl %d doc %d freeze %d %sCIF hi %d\n",
00163 b->used,
00164 x,
00165 (y & 0x20) ? 1 : 0,
00166 (y & 0x10) ? 1 : 0,
00167 (y & 0x8) ? 1 : 0,
00168 (y & 0x4) ? "" : "Q",
00169 (y & 0x2) ? 1:0);
00170 while ( (x = getbits(&a, 1)) == 1)
00171 ast_log(LOG_WARNING, "PSPARE 0x%x\n", getbits(&a, 8));
00172
00173 while ( (x = bitbuf_left(&a)) > 0) {
00174
00175 x = getbits(&a, 16);
00176 if (x != 0x1) {
00177 ast_log(LOG_WARNING, "bad GBSC 0x%x\n", x);
00178 break;
00179 }
00180 x = getbits(&a, 4);
00181 y = getbits(&a, 5);
00182 if (x == 0) {
00183 ast_log(LOG_WARNING, " bad GN %d\n", x);
00184 break;
00185 }
00186 while ( (x = getbits(&a, 1)) == 1)
00187 ast_log(LOG_WARNING, "GSPARE 0x%x\n", getbits(&a, 8));
00188 while ( (x = bitbuf_left(&a)) > 0) {
00189 break;
00190 }
00191 }
00192 }
00193
00194 void dump_buf(struct fbuf_t *b);
00195 void dump_buf(struct fbuf_t *b)
00196 {
00197 int i, x, last2lines;
00198 char buf[80];
00199
00200 last2lines = (b->used - 16) & ~0xf;
00201 ast_log(LOG_WARNING, "buf size %d of %d\n", b->used, b->size);
00202 for (i = 0; i < b->used; i++) {
00203 x = i & 0xf;
00204 if ( x == 0) {
00205 if (i != 0)
00206 ast_log(LOG_WARNING, "%s\n", buf);
00207 memset(buf, '\0', sizeof(buf));
00208 sprintf(buf, "%04x: ", i);
00209 }
00210 sprintf(buf + 6 + x*3, "%02x ", b->data[i]);
00211 if (i > 31 && i < last2lines)
00212 i = last2lines - 1;
00213 }
00214 if (buf[0])
00215 ast_log(LOG_WARNING, "%s\n", buf);
00216 }
00217 #endif
00218
00219
00220
00221
00222
00223
00224 static struct ast_frame *create_video_frame(uint8_t *start, uint8_t *end,
00225 int format, int head, struct ast_frame *prev)
00226 {
00227 int len = end-start;
00228 uint8_t *data;
00229 struct ast_frame *f;
00230
00231 data = ast_calloc(1, len+head);
00232 f = ast_calloc(1, sizeof(*f));
00233 if (f == NULL || data == NULL) {
00234 ast_log(LOG_WARNING, "--- frame error f %p data %p len %d format %d\n",
00235 f, data, len, format);
00236 if (f)
00237 ast_free(f);
00238 if (data)
00239 ast_free(data);
00240 return NULL;
00241 }
00242 memcpy(data+head, start, len);
00243 f->data.ptr = data;
00244 f->mallocd = AST_MALLOCD_DATA | AST_MALLOCD_HDR;
00245
00246
00247 f->datalen = len+head;
00248 f->frametype = AST_FRAME_VIDEO;
00249 f->subclass = format;
00250 f->samples = 0;
00251 f->offset = 0;
00252 f->src = "Console";
00253 f->delivery.tv_sec = 0;
00254 f->delivery.tv_usec = 0;
00255 f->seqno = 0;
00256 AST_LIST_NEXT(f, frame_list) = NULL;
00257
00258 if (prev)
00259 AST_LIST_NEXT(prev, frame_list) = f;
00260
00261 return f;
00262 }
00263
00264
00265
00266
00267
00268
00269 static int fbuf_append(struct fbuf_t *b, uint8_t *src, int len,
00270 int sbit, int ebit)
00271 {
00272
00273
00274
00275
00276 int need = len + FF_INPUT_BUFFER_PADDING_SIZE;
00277 int i;
00278 uint8_t *dst, mask;
00279
00280 if (b->data == NULL) {
00281 b->size = need;
00282 b->used = 0;
00283 b->ebit = 0;
00284 b->data = ast_calloc(1, b->size);
00285 } else if (b->used + need > b->size) {
00286 b->size = b->used + need;
00287 b->data = ast_realloc(b->data, b->size);
00288 }
00289 if (b->data == NULL) {
00290 ast_log(LOG_WARNING, "alloc failure for %d, discard\n",
00291 b->size);
00292 return 1;
00293 }
00294 if (b->used == 0 && b->ebit != 0) {
00295 ast_log(LOG_WARNING, "ebit not reset at start\n");
00296 b->ebit = 0;
00297 }
00298 dst = b->data + b->used;
00299 i = b->ebit + sbit;
00300 if (i == 0) {
00301
00302 } else if (i == 8) {
00303 mask = (1 << b->ebit) - 1;
00304
00305 dst[-1] &= ~mask;
00306 dst[-1] |= (*src & mask);
00307 src += 1;
00308 len --;
00309 } else {
00310 ast_log(LOG_WARNING, "must handle shift %d %d at %d\n",
00311 b->ebit, sbit, b->used);
00312 return 1;
00313 }
00314 memcpy(dst, src, len);
00315 b->used += len;
00316 b->ebit = ebit;
00317 b->data[b->used] = 0;
00318 return 0;
00319 }
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 static int h263p_enc_init(AVCodecContext *enc_ctx)
00332 {
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342 enc_ctx->flags |=CODEC_FLAG_H263P_UMV;
00343 enc_ctx->flags |=CODEC_FLAG_AC_PRED;
00344 enc_ctx->flags |=CODEC_FLAG_H263P_SLICE_STRUCT;
00345 enc_ctx->flags |= CODEC_FLAG_H263P_AIC;
00346
00347 return 0;
00348 }
00349
00350
00351
00352
00353
00354
00355
00356 static struct ast_frame *h263p_encap(struct fbuf_t *b, int mtu,
00357 struct ast_frame **tail)
00358 {
00359 struct ast_frame *cur = NULL, *first = NULL;
00360 uint8_t *d = b->data;
00361 int len = b->used;
00362 int l = len;
00363
00364 for (;len > 0; len -= l, d += l) {
00365 uint8_t *data;
00366 struct ast_frame *f;
00367 int i, h;
00368
00369 if (len >= 3 && d[0] == 0 && d[1] == 0 && d[2] >= 0x80) {
00370
00371 for (i = 3; i < len - 3; i++) {
00372 if (d[i] == 0 && d[i+1] == 0 && d[i+2] >= 0x80) {
00373 l = i;
00374 break;
00375 }
00376 }
00377 }
00378 if (l > mtu || l > len) {
00379 l = MIN(len, mtu);
00380 }
00381 if (l < 1 || l > mtu) {
00382 ast_log(LOG_WARNING, "--- frame error l %d\n", l);
00383 break;
00384 }
00385
00386 if (d[0] == 0 && d[1] == 0) {
00387 h = 0;
00388 } else {
00389 h = 2;
00390 }
00391
00392 f = create_video_frame(d, d+l, AST_FORMAT_H263_PLUS, h, cur);
00393 if (!f)
00394 break;
00395
00396 data = f->data.ptr;
00397 if (h == 0) {
00398 data[0] |= 0x04;
00399 } else {
00400 data[0] = data[1] = 0;
00401 }
00402
00403 if (!cur)
00404 first = f;
00405 cur = f;
00406 }
00407
00408 if (cur)
00409 cur->subclass |= 1;
00410
00411 *tail = cur;
00412 return first;
00413 }
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429 static int h263p_decap(struct fbuf_t *b, uint8_t *data, int len)
00430 {
00431 int PLEN;
00432
00433 if (len < 2) {
00434 ast_log(LOG_WARNING, "invalid framesize %d\n", len);
00435 return 1;
00436 }
00437 PLEN = ( (data[0] & 1) << 5 ) | ( (data[1] & 0xf8) >> 3);
00438
00439 if (PLEN > 0) {
00440 data += PLEN;
00441 len -= PLEN;
00442 }
00443 if (data[0] & 4)
00444 data[0] = data[1] = 0;
00445 else {
00446 data += 2;
00447 len -= 2;
00448 }
00449 return fbuf_append(b, data, len, 0, 0);
00450 }
00451
00452
00453
00454
00455
00456
00457 static int ffmpeg_encode(struct video_out_desc *v)
00458 {
00459 struct fbuf_t *b = &v->enc_out;
00460 int i;
00461
00462 b->used = avcodec_encode_video(v->enc_ctx, b->data, b->size, v->enc_in_frame);
00463 i = avcodec_encode_video(v->enc_ctx, b->data + b->used, b->size - b->used, NULL);
00464 if (i > 0) {
00465 ast_log(LOG_WARNING, "have %d more bytes\n", i);
00466 b->used += i;
00467 }
00468 return 0;
00469 }
00470
00471
00472
00473
00474
00475
00476
00477
00478 static int ffmpeg_decode(struct video_dec_desc *v, struct fbuf_t *b)
00479 {
00480 uint8_t *src = b->data;
00481 int srclen = b->used;
00482 int full_frame = 0;
00483
00484 if (srclen == 0)
00485 return 0;
00486 while (srclen) {
00487 uint8_t *data;
00488 int datalen, ret;
00489 int len = av_parser_parse(v->parser, v->dec_ctx, &data, &datalen, src, srclen, 0, 0);
00490
00491 src += len;
00492 srclen -= len;
00493
00494
00495
00496 if (data == NULL || datalen == 0)
00497 continue;
00498 ret = avcodec_decode_video(v->dec_ctx, v->d_frame, &full_frame, data, datalen);
00499 if (full_frame == 1)
00500 break;
00501 if (ret < 0) {
00502 ast_log(LOG_NOTICE, "Error decoding\n");
00503 break;
00504 }
00505 }
00506 if (srclen != 0)
00507 memmove(b->data, src, srclen);
00508 b->used = srclen;
00509 b->ebit = 0;
00510 return full_frame;
00511 }
00512
00513 static struct video_codec_desc h263p_codec = {
00514 .name = "h263p",
00515 .format = AST_FORMAT_H263_PLUS,
00516 .enc_init = h263p_enc_init,
00517 .enc_encap = h263p_encap,
00518 .enc_run = ffmpeg_encode,
00519 .dec_init = NULL,
00520 .dec_decap = h263p_decap,
00521 .dec_run = ffmpeg_decode
00522 };
00523
00524
00525
00526 static int h263_enc_init(AVCodecContext *enc_ctx)
00527 {
00528
00529 enc_ctx->flags |= CODEC_FLAG_H263P_UMV;
00530 enc_ctx->flags |= CODEC_FLAG_H263P_AIC;
00531 enc_ctx->flags |= CODEC_FLAG_H263P_SLICE_STRUCT;
00532 enc_ctx->flags |= CODEC_FLAG_AC_PRED;
00533
00534 return 0;
00535 }
00536
00537
00538
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 static struct ast_frame *h263_encap(struct fbuf_t *b, int mtu,
00572 struct ast_frame **tail)
00573 {
00574 uint8_t *d = b->data;
00575 int start = 0, i, len = b->used;
00576 struct ast_frame *f, *cur = NULL, *first = NULL;
00577 const int pheader_len = 4;
00578 uint8_t h263_hdr[12];
00579 uint8_t *h = h263_hdr;
00580
00581 #define H263_MIN_LEN 6
00582 if (len < H263_MIN_LEN)
00583 return NULL;
00584
00585 memset(h263_hdr, '\0', sizeof(h263_hdr));
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598 h[1] = ( (d[4] & 0x1f) << 3 ) |
00599 ( (d[5] & 0xc0) >> 5 );
00600
00601
00602
00603
00604
00605 for (i = H263_MIN_LEN, start = 0; start < len; start = i, i += 3) {
00606
00607 for (; i < len ; i++) {
00608 uint8_t x, rpos, lpos;
00609 int rpos_i;
00610 if (d[i] != 0)
00611 continue;
00612 if (i > len - 1)
00613 break;
00614 x = d[i+1];
00615 if (x == 0)
00616 continue;
00617
00618
00619
00620
00621
00622 for (rpos = 0x80, rpos_i = 8; rpos; rpos >>= 1, rpos_i--)
00623 if (x & rpos)
00624 break;
00625 x = d[i-1];
00626 for (lpos = rpos; lpos ; lpos >>= 1)
00627 if (x & lpos)
00628 break;
00629 if (lpos)
00630 continue;
00631
00632
00633
00634 if (rpos == 0x80) {
00635 i = i - 1;
00636 } else {
00637 ast_log(LOG_WARNING, "unaligned GBSC 0x%x %d\n",
00638 rpos, rpos_i);
00639 }
00640 break;
00641 }
00642
00643
00644
00645 f = create_video_frame(d + start, d+i, AST_FORMAT_H263,
00646 pheader_len, cur);
00647
00648 if (!f)
00649 break;
00650 memmove(f->data.ptr, h, 4);
00651
00652
00653
00654 if (!cur)
00655 first = f;
00656 cur = f;
00657 }
00658
00659 if (cur)
00660 cur->subclass |= 1;
00661
00662 *tail = cur;
00663 return first;
00664 }
00665
00666
00667 static int h263_decap(struct fbuf_t *b, uint8_t *data, int len)
00668 {
00669 if (len < 4) {
00670 ast_log(LOG_WARNING, "invalid framesize %d\n", len);
00671 return 1;
00672 }
00673
00674 if ( (data[0] & 0x80) == 0) {
00675 len -= 4;
00676 data += 4;
00677 } else {
00678 ast_log(LOG_WARNING, "unsupported mode 0x%x\n",
00679 data[0]);
00680 return 1;
00681 }
00682 return fbuf_append(b, data, len, 0, 0);
00683 }
00684
00685 static struct video_codec_desc h263_codec = {
00686 .name = "h263",
00687 .format = AST_FORMAT_H263,
00688 .enc_init = h263_enc_init,
00689 .enc_encap = h263_encap,
00690 .enc_run = ffmpeg_encode,
00691 .dec_init = NULL,
00692 .dec_decap = h263_decap,
00693 .dec_run = ffmpeg_decode
00694
00695 };
00696
00697
00698 static int h261_enc_init(AVCodecContext *enc_ctx)
00699 {
00700
00701
00702
00703
00704 enc_ctx->rtp_payload_size = 0;
00705
00706 return 0;
00707 }
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725 static struct ast_frame *h261_encap(struct fbuf_t *b, int mtu,
00726 struct ast_frame **tail)
00727 {
00728 uint8_t *d = b->data;
00729 int start = 0, i, len = b->used;
00730 struct ast_frame *f, *cur = NULL, *first = NULL;
00731 const int pheader_len = 4;
00732 uint8_t h261_hdr[4];
00733 uint8_t *h = h261_hdr;
00734 int sbit = 0, ebit = 0;
00735
00736 #define H261_MIN_LEN 10
00737 if (len < H261_MIN_LEN)
00738 return NULL;
00739
00740 memset(h261_hdr, '\0', sizeof(h261_hdr));
00741
00742
00743
00744
00745
00746
00747 for (i = H261_MIN_LEN, start = 0; start < len - 1; start = i, i += 4) {
00748 #if 0
00749 i = len;
00750 #else
00751 int found = 0, found_ebit = 0;
00752 for (; i < len ; i++) {
00753 uint8_t x, rpos, lpos;
00754 if (d[i] != 0)
00755 continue;
00756 x = d[i+1];
00757 if (x == 0)
00758 continue;
00759
00760
00761
00762
00763
00764 for (rpos = 0x80, ebit = 7; rpos; ebit--, rpos >>= 1)
00765 if (x & rpos)
00766 break;
00767 x = d[i-1];
00768 for (lpos = (rpos >> 1); lpos ; lpos >>= 1)
00769 if (x & lpos)
00770 break;
00771 if (lpos)
00772 continue;
00773
00774
00775
00776 if (i - start > mtu)
00777 break;
00778 found_ebit = ebit;
00779 found = i;
00780 i += 4;
00781 }
00782 if (i >= len) {
00783 i = len;
00784 ebit = 0;
00785 }
00786 if (i - start > mtu && found) {
00787
00788 i = found;
00789 ebit = found_ebit;
00790 }
00791 #endif
00792 if (i - start < 4)
00793 continue;
00794
00795
00796
00797 f = create_video_frame(d + start, d+i, AST_FORMAT_H261,
00798 pheader_len, cur);
00799
00800 if (!f)
00801 break;
00802
00803 h[0] = ( (sbit & 7) << 5 ) | ( (ebit & 7) << 2 ) | 1;
00804 memmove(f->data.ptr, h, 4);
00805 if (ebit)
00806 i--;
00807 sbit = (8 - ebit) & 7;
00808 ebit = 0;
00809 if (!cur)
00810 first = f;
00811 cur = f;
00812 }
00813 if (cur)
00814 cur->subclass |= 1;
00815
00816 *tail = cur;
00817 return first;
00818 }
00819
00820
00821
00822
00823 static int h261_decap(struct fbuf_t *b, uint8_t *data, int len)
00824 {
00825 int ebit, sbit;
00826
00827 if (len < 8) {
00828 ast_log(LOG_WARNING, "invalid framesize %d\n", len);
00829 return 1;
00830 }
00831 sbit = (data[0] >> 5) & 7;
00832 ebit = (data[0] >> 2) & 7;
00833 len -= 4;
00834 data += 4;
00835 return fbuf_append(b, data, len, sbit, ebit);
00836 }
00837
00838 static struct video_codec_desc h261_codec = {
00839 .name = "h261",
00840 .format = AST_FORMAT_H261,
00841 .enc_init = h261_enc_init,
00842 .enc_encap = h261_encap,
00843 .enc_run = ffmpeg_encode,
00844 .dec_init = NULL,
00845 .dec_decap = h261_decap,
00846 .dec_run = ffmpeg_decode
00847 };
00848
00849
00850 static int mpeg4_enc_init(AVCodecContext *enc_ctx)
00851 {
00852 #if 0
00853
00854 enc_ctx->flags |= CODEC_FLAG_AC_PRED;
00855 enc_ctx->flags |= CODEC_FLAG_H263P_UMV;
00856 enc_ctx->flags |= CODEC_FLAG_QPEL;
00857 enc_ctx->flags |= CODEC_FLAG_4MV;
00858 enc_ctx->flags |= CODEC_FLAG_GMC;
00859 enc_ctx->flags |= CODEC_FLAG_LOOP_FILTER;
00860 enc_ctx->flags |= CODEC_FLAG_H263P_SLICE_STRUCT;
00861 #endif
00862 enc_ctx->rtp_payload_size = 0;
00863 return 0;
00864 }
00865
00866
00867 static struct ast_frame *mpeg4_encap(struct fbuf_t *b, int mtu,
00868 struct ast_frame **tail)
00869 {
00870 struct ast_frame *f, *cur = NULL, *first = NULL;
00871 uint8_t *d = b->data;
00872 uint8_t *end = d + b->used;
00873 int len;
00874
00875 for (;d < end; d += len, cur = f) {
00876 len = MIN(mtu, end - d);
00877 f = create_video_frame(d, d + len, AST_FORMAT_MP4_VIDEO, 0, cur);
00878 if (!f)
00879 break;
00880 if (!first)
00881 first = f;
00882 }
00883 if (cur)
00884 cur->subclass |= 1;
00885 *tail = cur;
00886 return first;
00887 }
00888
00889 static int mpeg4_decap(struct fbuf_t *b, uint8_t *data, int len)
00890 {
00891 return fbuf_append(b, data, len, 0, 0);
00892 }
00893
00894 static int mpeg4_decode(struct video_dec_desc *v, struct fbuf_t *b)
00895 {
00896 int full_frame = 0, datalen = b->used;
00897 int ret = avcodec_decode_video(v->dec_ctx, v->d_frame, &full_frame,
00898 b->data, datalen);
00899 if (ret < 0) {
00900 ast_log(LOG_NOTICE, "Error decoding\n");
00901 ret = datalen;
00902 }
00903 datalen -= ret;
00904 if (datalen > 0)
00905 memmove(b->data, b->data + ret, datalen);
00906 b->used = datalen;
00907 b->ebit = 0;
00908 return full_frame;
00909 }
00910
00911 static struct video_codec_desc mpeg4_codec = {
00912 .name = "mpeg4",
00913 .format = AST_FORMAT_MP4_VIDEO,
00914 .enc_init = mpeg4_enc_init,
00915 .enc_encap = mpeg4_encap,
00916 .enc_run = ffmpeg_encode,
00917 .dec_init = NULL,
00918 .dec_decap = mpeg4_decap,
00919 .dec_run = mpeg4_decode
00920 };
00921
00922 static int h264_enc_init(AVCodecContext *enc_ctx)
00923 {
00924 enc_ctx->flags |= CODEC_FLAG_TRUNCATED;
00925
00926
00927
00928 enc_ctx->rtp_mode = 0;
00929 enc_ctx->rtp_payload_size = 0;
00930 enc_ctx->bit_rate_tolerance = enc_ctx->bit_rate;
00931 return 0;
00932 }
00933
00934 static int h264_dec_init(AVCodecContext *dec_ctx)
00935 {
00936 dec_ctx->flags |= CODEC_FLAG_TRUNCATED;
00937
00938 return 0;
00939 }
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955 static struct ast_frame *h264_encap(struct fbuf_t *b, int mtu,
00956 struct ast_frame **tail)
00957 {
00958 struct ast_frame *f = NULL, *cur = NULL, *first = NULL;
00959 uint8_t *d, *start = b->data;
00960 uint8_t *end = start + b->used;
00961
00962
00963
00964
00965 #define HAVE_NAL(x) (x[-4] == 0 && x[-3] == 0 && x[-2] == 0 && x[-1] == 1)
00966 for (start += 4; start < end; start++) {
00967 int ty = start[0] & 0x1f;
00968 if (HAVE_NAL(start) && ty != 0 && ty != 31)
00969 break;
00970 }
00971
00972
00973
00974
00975
00976 for (;start < end - 4; start = d) {
00977 int size;
00978 uint8_t hdr[2];
00979 int ty = 0;
00980
00981
00982 for (d = start + 4; d < end; d++) {
00983 ty = d[0] & 0x1f;
00984 if (HAVE_NAL(d))
00985 break;
00986 }
00987
00988 if (d >= end) {
00989 d = end + 4;
00990 } else if (ty == 0 || ty == 31) {
00991 ast_log(LOG_WARNING, "skip invalid nal type %d at %d of %d\n",
00992 ty, d - (uint8_t *)b->data, b->used);
00993 continue;
00994 }
00995
00996 size = d - start - 4;
00997
00998 if (size < mtu) {
00999
01000 f = create_video_frame(start, d - 4, AST_FORMAT_H264, 0, cur);
01001 if (!f)
01002 break;
01003 if (!first)
01004 first = f;
01005
01006 cur = f;
01007 continue;
01008 }
01009
01010
01011 hdr[0] = (*start & 0xe0) | 28;
01012 hdr[1] = (*start++ & 0x1f) | 0x80 ;
01013 size--;
01014 while (size) {
01015 uint8_t *data;
01016 int frag_size = MIN(size, mtu);
01017
01018 f = create_video_frame(start, start+frag_size, AST_FORMAT_H264, 2, cur);
01019 if (!f)
01020 break;
01021 size -= frag_size;
01022 start += frag_size;
01023
01024 data = f->data.ptr;
01025 data[0] = hdr[0];
01026 data[1] = hdr[1] | (size == 0 ? 0x40 : 0);
01027 hdr[1] &= ~0x80;
01028 if (!first)
01029 first = f;
01030 cur = f;
01031 }
01032 }
01033
01034 if (cur)
01035 cur->subclass |= 1;
01036
01037 *tail = cur;
01038
01039 return first;
01040 }
01041
01042 static int h264_decap(struct fbuf_t *b, uint8_t *data, int len)
01043 {
01044
01045 uint8_t scp[] = { 0x00, 0x00, 0x00, 0x01 };
01046 int retval = 0;
01047 int type, ofs = 0;
01048
01049 if (len < 2) {
01050 ast_log(LOG_WARNING, "--- invalid len %d\n", len);
01051 return 1;
01052 }
01053
01054 if (data[0] & 0x80) {
01055 ast_log(LOG_WARNING, "--- forbidden packet; nal: %02x\n",
01056 data[0]);
01057 return 1;
01058 }
01059
01060 type = data[0] & 0x1f;
01061 switch (type) {
01062 case 0:
01063 case 31:
01064 ast_log(LOG_WARNING, "--- invalid type: %d\n", type);
01065 return 1;
01066 case 24:
01067 case 25:
01068 case 26:
01069 case 27:
01070 case 29:
01071 ast_log(LOG_WARNING, "--- encapsulation not supported : %d\n", type);
01072 return 1;
01073 case 28:
01074 if (data[1] & 0x80) {
01075 data[1] &= 0x1f;
01076 data[1] |= (data[0] & 0xe0);
01077 retval = fbuf_append(b, scp, sizeof(scp), 0, 0);
01078 ofs = 1;
01079 } else {
01080 ofs = 2;
01081 }
01082 break;
01083 default:
01084 retval = fbuf_append(b, scp, sizeof(scp), 0, 0);
01085 }
01086 if (!retval)
01087 retval = fbuf_append(b, data + ofs, len - ofs, 0, 0);
01088 if (retval)
01089 ast_log(LOG_WARNING, "result %d\n", retval);
01090 return retval;
01091 }
01092
01093 static struct video_codec_desc h264_codec = {
01094 .name = "h264",
01095 .format = AST_FORMAT_H264,
01096 .enc_init = h264_enc_init,
01097 .enc_encap = h264_encap,
01098 .enc_run = ffmpeg_encode,
01099 .dec_init = h264_dec_init,
01100 .dec_decap = h264_decap,
01101 .dec_run = ffmpeg_decode
01102 };
01103
01104
01105
01106
01107
01108
01109 struct _cm {
01110 uint32_t ast_format;
01111 enum CodecID codec;
01112 enum { CM_RD = 1, CM_WR = 2, CM_RDWR = 3 } rw;
01113
01114 };
01115
01116 static struct _cm video_formats[] = {
01117 { AST_FORMAT_H263_PLUS, CODEC_ID_H263, CM_RD },
01118 { AST_FORMAT_H263_PLUS, CODEC_ID_H263P, CM_WR },
01119 { AST_FORMAT_H263, CODEC_ID_H263, CM_RD },
01120 { AST_FORMAT_H263, CODEC_ID_H263, CM_WR },
01121 { AST_FORMAT_H261, CODEC_ID_H261, CM_RDWR },
01122 { AST_FORMAT_H264, CODEC_ID_H264, CM_RDWR },
01123 { AST_FORMAT_MP4_VIDEO, CODEC_ID_MPEG4, CM_RDWR },
01124 { 0, 0, 0 },
01125 };
01126
01127
01128
01129 static enum CodecID map_video_format(uint32_t ast_format, int rw)
01130 {
01131 struct _cm *i;
01132
01133 for (i = video_formats; i->ast_format != 0; i++)
01134 if (ast_format & i->ast_format && rw & i->rw && rw & i->rw)
01135 return i->codec;
01136 return CODEC_ID_NONE;
01137 }
01138
01139
01140 static struct video_codec_desc *supported_codecs[] = {
01141 &h263p_codec,
01142 &h264_codec,
01143 &h263_codec,
01144 &h261_codec,
01145 &mpeg4_codec,
01146 NULL
01147 };
01148
01149
01150
01151
01152
01153 static struct video_codec_desc *map_video_codec(int fmt)
01154 {
01155 int i;
01156
01157 for (i = 0; supported_codecs[i]; i++)
01158 if (fmt == supported_codecs[i]->format) {
01159 ast_log(LOG_WARNING, "using %s for format 0x%x\n",
01160 supported_codecs[i]->name, fmt);
01161 return supported_codecs[i];
01162 }
01163 return NULL;
01164 }
01165
01166
01167 static struct video_dec_desc *dec_uninit(struct video_dec_desc *v)
01168 {
01169 int i;
01170
01171 if (v == NULL)
01172 return NULL;
01173 if (v->parser) {
01174 av_parser_close(v->parser);
01175 v->parser = NULL;
01176 }
01177 if (v->dec_ctx) {
01178 avcodec_close(v->dec_ctx);
01179 av_free(v->dec_ctx);
01180 v->dec_ctx = NULL;
01181 }
01182 if (v->d_frame) {
01183 av_free(v->d_frame);
01184 v->d_frame = NULL;
01185 }
01186 v->codec = NULL;
01187 v->d_callbacks = NULL;
01188 v->discard = 1;
01189 for (i = 0; i < N_DEC_IN; i++)
01190 fbuf_free(&v->dec_in[i]);
01191 fbuf_free(&v->dec_out);
01192 ast_free(v);
01193 return NULL;
01194 }
01195
01196
01197
01198
01199 static struct video_dec_desc *dec_init(uint32_t the_ast_format)
01200 {
01201 enum CodecID codec;
01202 struct video_dec_desc *v = ast_calloc(1, sizeof(*v));
01203 if (v == NULL)
01204 return NULL;
01205
01206 v->discard = 1;
01207
01208 v->d_callbacks = map_video_codec(the_ast_format);
01209 if (v->d_callbacks == NULL) {
01210 ast_log(LOG_WARNING, "cannot find video codec, drop input 0x%x\n", the_ast_format);
01211 return dec_uninit(v);
01212 }
01213
01214 codec = map_video_format(v->d_callbacks->format, CM_RD);
01215
01216 v->codec = avcodec_find_decoder(codec);
01217 if (!v->codec) {
01218 ast_log(LOG_WARNING, "Unable to find the decoder for format %d\n", codec);
01219 return dec_uninit(v);
01220 }
01221
01222
01223
01224 v->dec_ctx = avcodec_alloc_context();
01225 if (!v->dec_ctx) {
01226 ast_log(LOG_WARNING, "Cannot allocate the decoder context\n");
01227 return dec_uninit(v);
01228 }
01229
01230 if (avcodec_open(v->dec_ctx, v->codec) < 0) {
01231 ast_log(LOG_WARNING, "Cannot open the decoder context\n");
01232 av_free(v->dec_ctx);
01233 v->dec_ctx = NULL;
01234 return dec_uninit(v);
01235 }
01236
01237 v->parser = av_parser_init(codec);
01238 if (!v->parser) {
01239 ast_log(LOG_WARNING, "Cannot initialize the decoder parser\n");
01240 return dec_uninit(v);
01241 }
01242
01243 v->d_frame = avcodec_alloc_frame();
01244 if (!v->d_frame) {
01245 ast_log(LOG_WARNING, "Cannot allocate decoding video frame\n");
01246 return dec_uninit(v);
01247 }
01248 v->dec_in_cur = &v->dec_in[0];
01249 v->dec_in_dpy = NULL;
01250
01251 return v;
01252 }
01253