2 * iaxclient: a cross-platform IAX softphone library
5 * Copyright (C) 2003-2006, Horizon Wimba, Inc.
6 * Copyright (C) 2007, Wimba, Inc.
9 * Steve Kann <stevek@stevek.com>
10 * Peter Grayson <jpgrayson@gmail.com>
12 * This program is free software, distributed under the terms of
13 * the GNU Lesser (Library) General Public License.
15 * A video codec using the ffmpeg library.
17 * TODO: this code still uses its own slicing mechanism
18 * It should be converted to use the API provided in slice.[ch]
23 #include "codec_ffmpeg.h"
24 #include "iaxclient_lib.h"
27 #include "libavcodec/avcodec.h"
29 #include <ffmpeg/avcodec.h>
34 unsigned char version;
35 unsigned short source_id;
36 unsigned char frame_index;
37 unsigned char slice_index;
38 unsigned char num_slices;
43 AVCodecContext * avctx;
46 struct slice_header_t slice_header;
48 unsigned char *frame_buf;
54 AVCodecContext * avctx;
57 struct slice_header_t slice_header;
60 unsigned char * frame_buf;
64 static struct slice_set_t * g_slice_set = 0;
66 static enum CodecID map_iaxc_codec_to_avcodec(int format)
70 case IAXC_FORMAT_H261:
73 case IAXC_FORMAT_H263:
76 case IAXC_FORMAT_H263_PLUS:
77 return CODEC_ID_H263P;
79 case IAXC_FORMAT_MPEG4:
80 return CODEC_ID_MPEG4;
82 case IAXC_FORMAT_H264:
85 case IAXC_FORMAT_THEORA:
86 return CODEC_ID_THEORA;
93 static void destroy(struct iaxc_video_codec *c)
97 struct encoder_ctx *e = (struct encoder_ctx *) c->encstate;
98 struct decoder_ctx *d = (struct decoder_ctx *) c->decstate;
103 av_freep(&e->picture);
112 av_freep(&d->picture);
122 static void reset_decoder_frame_state(struct decoder_ctx * d)
124 memset(d->frame_buf, 0, d->frame_buf_len);
126 d->slice_header.slice_index = 0;
129 static int frame_to_frame_xlate(AVCodecContext * avctx, AVFrame * picture,
130 int * outlen, char * out)
134 *outlen = avctx->width * avctx->height * 6 / 4;
136 for ( line = 0; line < avctx->height / 2; ++line )
139 memcpy(out + avctx->width * (2 * line + 0),
140 picture->data[0] + (2 * line + 0) * picture->linesize[0],
144 memcpy(out + avctx->width * (2 * line + 1),
145 picture->data[0] + (2 * line + 1) * picture->linesize[0],
149 memcpy(out + avctx->width * avctx->height
150 + line * avctx->width / 2,
151 picture->data[1] + line * picture->linesize[1],
154 memcpy(out + avctx->width * avctx->height * 5 / 4
155 + line * avctx->width / 2,
156 picture->data[2] + line * picture->linesize[2],
163 static int pass_frame_to_decoder(AVCodecContext * avctx, AVFrame * picture,
164 int inlen, unsigned char * in, int * outlen, char * out)
169 bytes_decoded = avcodec_decode_video(avctx, picture, &got_picture,
172 if ( bytes_decoded != inlen )
175 "codec_ffmpeg: decode: failed to decode whole frame %d / %d\n",
176 bytes_decoded, inlen);
183 "codec_ffmpeg: decode: failed to get picture\n");
187 frame_to_frame_xlate(avctx, picture, outlen, out);
192 static char *parse_slice_header(char * in, struct slice_header_t * slice_header)
194 slice_header->version = in[0];
195 slice_header->source_id = (in[1] << 8) | in[2];
196 slice_header->frame_index = in[3];
197 slice_header->slice_index = in[4];
198 slice_header->num_slices = in[5];
200 if ( slice_header->version != 0 )
203 "codec_ffmpeg: decode: unknown slice header version %d\n",
204 slice_header->version);
211 static int decode_iaxc_slice(struct iaxc_video_codec * c, int inlen,
212 char * in, int * outlen, char * out)
214 struct decoder_ctx *d = (struct decoder_ctx *) c->decstate;
215 struct slice_header_t * sh_saved = &d->slice_header;
216 struct slice_header_t sh_this;
220 inp = parse_slice_header(in, &sh_this);
227 if ( sh_this.source_id == sh_saved->source_id )
229 unsigned char frame_delta;
231 frame_delta = sh_this.frame_index - sh_saved->frame_index;
233 if ( frame_delta > 20 )
235 /* This is an old slice. It's too late, we ignore it. */
238 else if ( frame_delta > 0 )
240 /* This slice belongs to a future frame */
241 if ( sh_saved->slice_index > 0 )
243 /* We have received slices for a previous
244 * frame (e.g. the one we were previously
245 * working on), so we go ahead and send this
246 * partial frame to the decoder and get setup
250 ret = pass_frame_to_decoder(d->avctx, d->picture,
251 d->frame_size, d->frame_buf,
254 reset_decoder_frame_state(d);
260 sh_saved->frame_index = sh_this.frame_index;
265 sh_saved->source_id = sh_this.source_id;
266 sh_saved->frame_index = sh_this.frame_index;
267 sh_saved->slice_index = 0;
271 if ( c->fragsize * sh_this.slice_index + inlen > d->frame_buf_len )
274 "codec_ffmpeg: decode: slice overflows decoder frame buffer\n");
278 memcpy(d->frame_buf + c->fragsize * sh_this.slice_index,
280 sh_saved->slice_index++;
281 d->frame_size = c->fragsize * sh_this.slice_index + inlen;
283 if ( sh_saved->slice_index < sh_this.num_slices )
285 /* Do not decode yet, there are more slices coming for
291 ret = pass_frame_to_decoder(d->avctx, d->picture, d->frame_size,
292 d->frame_buf, outlen, out);
294 reset_decoder_frame_state(d);
302 static int decode_rtp_slice(struct iaxc_video_codec * c,
303 int inlen, char * in, int * outlen, char * out)
305 struct decoder_ctx *d = (struct decoder_ctx *) c->decstate;
313 bytes_decoded = avcodec_decode_video(d->avctx, d->picture,
314 &got_picture, (unsigned char *)in, inlen);
316 if ( bytes_decoded < 0 )
319 "codec_ffmpeg: decode: error decoding frame\n");
323 inlen -= bytes_decoded;
326 if ( got_picture && ret == 0)
329 "codec_ffmpeg: decode: unexpected second frame\n");
335 frame_to_frame_xlate(d->avctx, d->picture, outlen, out);
343 static void slice_encoded_frame(struct slice_header_t * sh,
344 struct slice_set_t * slice_set,
345 unsigned char * in, int inlen, int fragsize)
347 sh->num_slices = slice_set->num_slices = (inlen - 1) / fragsize + 1;
349 for (sh->slice_index = 0; sh->slice_index < sh->num_slices;
352 int slice_size = (sh->slice_index == sh->num_slices - 1) ?
353 inlen % fragsize : fragsize;
355 slice_set->size[sh->slice_index] = slice_size + 6;
356 slice_set->data[sh->slice_index][0] = sh->version;
357 slice_set->data[sh->slice_index][1] = sh->source_id >> 8;
358 slice_set->data[sh->slice_index][2] = sh->source_id & 0xff;
359 slice_set->data[sh->slice_index][3] = sh->frame_index;
360 slice_set->data[sh->slice_index][4] = sh->slice_index;
361 slice_set->data[sh->slice_index][5] = sh->num_slices;
363 memcpy(&slice_set->data[sh->slice_index][6], in, slice_size);
371 static int encode(struct iaxc_video_codec *c,
372 int inlen, char * in, struct slice_set_t * slice_set)
374 struct encoder_ctx *e = (struct encoder_ctx *) c->encstate;
377 avcodec_get_frame_defaults(e->picture);
379 e->picture->data[0] = (unsigned char *)in;
380 e->picture->data[1] = (unsigned char *)in
381 + e->avctx->width * e->avctx->height;
382 e->picture->data[2] = (unsigned char *)in
383 + e->avctx->width * e->avctx->height * 5 / 4;
385 e->picture->linesize[0] = e->avctx->width;
386 e->picture->linesize[1] = e->avctx->width / 2;
387 e->picture->linesize[2] = e->avctx->width / 2;
389 /* TODO: investigate setting a real pts value */
390 e->picture->pts = AV_NOPTS_VALUE;
392 /* TODO: investigate quality */
393 e->picture->quality = 10;
395 g_slice_set = slice_set;
396 slice_set->num_slices = 0;
398 encoded_size = avcodec_encode_video(e->avctx,
399 e->frame_buf, e->frame_buf_len, e->picture);
403 fprintf(stderr, "codec_ffmpeg: encode failed\n");
407 slice_set->key_frame = e->avctx->coded_frame->key_frame;
409 /* This is paranoia, of course. */
412 /* We are in one of two modes here.
414 * The first possibility is that the codec supports rtp
415 * packetization. In this case, the slice_set has already been
416 * filled via encode_rtp_callback() calls made during the call
417 * to avcodec_encode_video().
419 * The second possibility is that we have one big encoded frame
420 * that we need to slice-up ourselves.
423 if (!e->avctx->rtp_payload_size)
424 slice_encoded_frame(&e->slice_header, slice_set,
425 e->frame_buf, encoded_size, c->fragsize);
430 void encode_rtp_callback(struct AVCodecContext *avctx, void *data, int size,
433 memcpy(&g_slice_set->data[g_slice_set->num_slices], data, size);
434 g_slice_set->size[g_slice_set->num_slices] = size;
435 g_slice_set->num_slices++;
438 struct iaxc_video_codec *codec_video_ffmpeg_new(int format, int w, int h,
439 int framerate, int bitrate,
442 struct encoder_ctx *e;
443 struct decoder_ctx *d;
445 int ff_enc_id, ff_dec_id;
448 struct iaxc_video_codec *c = calloc(sizeof(struct iaxc_video_codec), 1);
453 "codec_ffmpeg: failed to allocate video context\n");
458 avcodec_register_all();
463 c->framerate = framerate;
464 c->bitrate = bitrate;
465 /* TODO: Is a fragsize of zero valid? If so, there's a divide
466 * by zero error to contend with.
468 c->fragsize = fragsize;
471 c->decode = decode_iaxc_slice;
472 c->destroy = destroy;
474 c->encstate = calloc(sizeof(struct encoder_ctx), 1);
478 e->avctx = avcodec_alloc_context();
481 e->picture = avcodec_alloc_frame();
484 /* The idea here is that the encoded frame that will land in this
485 * buffer will be no larger than the size of an uncompressed 32-bit
488 * TODO: Is this assumption really valid?
490 e->frame_buf_len = w * h * 4;
491 e->frame_buf = malloc(e->frame_buf_len);
495 c->decstate = calloc(sizeof(struct decoder_ctx), 1);
499 d->avctx = avcodec_alloc_context();
502 d->picture = avcodec_alloc_frame();
505 d->frame_buf_len = e->frame_buf_len;
506 d->frame_buf = malloc(d->frame_buf_len);
510 e->slice_header.version = 0;
512 e->slice_header.source_id = random() & 0xffff;
514 e->avctx->time_base.num = 1;
515 e->avctx->time_base.den = framerate;
518 e->avctx->height = h;
520 e->avctx->bit_rate = bitrate;
522 /* This determines how often i-frames are sent */
523 e->avctx->gop_size = framerate * 3;
524 e->avctx->pix_fmt = PIX_FMT_YUV420P;
525 e->avctx->has_b_frames = 0;
527 e->avctx->mb_qmin = e->avctx->qmin = 10;
528 e->avctx->mb_qmax = e->avctx->qmax = 10;
530 e->avctx->lmin = 2 * FF_QP2LAMBDA;
531 e->avctx->lmax = 10 * FF_QP2LAMBDA;
532 e->avctx->global_quality = FF_QP2LAMBDA * 2;
533 e->avctx->qblur = 0.5;
534 e->avctx->global_quality = 10;
536 e->avctx->flags |= CODEC_FLAG_PSNR;
537 e->avctx->flags |= CODEC_FLAG_QSCALE;
539 e->avctx->mb_decision = FF_MB_DECISION_SIMPLE;
541 ff_enc_id = ff_dec_id = map_iaxc_codec_to_avcodec(format);
543 /* Note, when fragsize is used (non-zero) ffmpeg will use a "best
544 * effort" strategy: the fragment size will be fragsize +/- 20%
550 case IAXC_FORMAT_H261:
551 /* TODO: H261 only works with specific resolutions. */
555 case IAXC_FORMAT_H263:
556 /* TODO: H263 only works with specific resolutions. */
558 e->avctx->flags |= CODEC_FLAG_AC_PRED;
561 c->decode = decode_rtp_slice;
562 e->avctx->rtp_payload_size = fragsize;
564 CODEC_FLAG_TRUNCATED | CODEC_FLAG2_STRICT_GOP;
565 e->avctx->rtp_callback = encode_rtp_callback;
566 d->avctx->flags |= CODEC_FLAG_TRUNCATED;
570 case IAXC_FORMAT_H263_PLUS:
571 /* Although the encoder is CODEC_ID_H263P, the decoder
572 * is the regular h.263, so we handle this special case
575 ff_dec_id = CODEC_ID_H263;
577 e->avctx->flags |= CODEC_FLAG_AC_PRED;
580 c->decode = decode_rtp_slice;
581 e->avctx->rtp_payload_size = fragsize;
583 CODEC_FLAG_TRUNCATED |
584 CODEC_FLAG_H263P_SLICE_STRUCT |
585 CODEC_FLAG2_STRICT_GOP |
586 CODEC_FLAG2_LOCAL_HEADER;
587 e->avctx->rtp_callback = encode_rtp_callback;
588 d->avctx->flags |= CODEC_FLAG_TRUNCATED;
592 case IAXC_FORMAT_MPEG4:
594 c->decode = decode_rtp_slice;
595 e->avctx->rtp_payload_size = fragsize;
596 e->avctx->rtp_callback = encode_rtp_callback;
598 CODEC_FLAG_TRUNCATED |
599 CODEC_FLAG_H263P_SLICE_STRUCT |
600 CODEC_FLAG2_STRICT_GOP |
601 CODEC_FLAG2_LOCAL_HEADER;
603 d->avctx->flags |= CODEC_FLAG_TRUNCATED;
606 case IAXC_FORMAT_H264:
613 /* Headers are not repeated */
614 /* e->avctx->flags |= CODEC_FLAG_GLOBAL_HEADER; */
616 /* Slower, less blocky */
617 /* e->avctx->flags |= CODEC_FLAG_LOOP_FILTER; */
619 e->avctx->flags |= CODEC_FLAG_PASS1;
620 /* e->avctx->flags |= CODEC_FLAG_PASS2; */
622 /* Compute psnr values at encode-time (avctx->error[]) */
623 /* e->avctx->flags |= CODEC_FLAG_PSNR; */
625 /* e->avctx->flags2 |= CODEC_FLAG2_8X8DCT; */
627 /* Access Unit Delimiters */
628 e->avctx->flags2 |= CODEC_FLAG2_AUD;
630 /* Allow b-frames to be used as reference */
631 /* e->avctx->flags2 |= CODEC_FLAG2_BPYRAMID; */
633 /* b-frame rate distortion optimization */
634 /* e->avctx->flags2 |= CODEC_FLAG2_BRDO; */
636 /* e->avctx->flags2 |= CODEC_FLAG2_FASTPSKIP; */
638 /* Multiple references per partition */
639 /* e->avctx->flags2 |= CODEC_FLAG2_MIXED_REFS; */
641 /* Weighted biprediction for b-frames */
642 /* e->avctx->flags2 |= CODEC_FLAG2_WPRED; */
648 /* Do not draw edges */
649 /* d->avctx->flags |= CODEC_FLAG_EMU_EDGE; */
651 /* Decode grayscale only */
652 /* d->avctx->flags |= CODEC_FLAG_GRAY; */
654 /* d->avctx->flags |= CODEC_FLAG_LOW_DELAY; */
656 /* Allow input bitstream to be randomly truncated */
657 /* d->avctx->flags |= CODEC_FLAG_TRUNCATED; */
659 /* Allow out-of-spec speed tricks */
660 /* d->avctx->flags2 |= CODEC_FLAG2_FAST; */
663 case IAXC_FORMAT_THEORA:
664 /* TODO: ffmpeg only has a theora decoder. Until it has
665 * an encoder also, we cannot use ffmpeg for theora.
671 fprintf(stderr, "codec_ffmpeg: unsupported format (0x%08x)\n",
676 strcpy(c->name, "ffmpeg-");
677 strncat(c->name, name, sizeof(c->name));
680 codec = avcodec_find_encoder(ff_enc_id);
683 iaxci_usermsg(IAXC_TEXT_TYPE_ERROR,
684 "codec_ffmpeg: cannot find encoder %d\n",
689 if (avcodec_open(e->avctx, codec))
691 iaxci_usermsg(IAXC_TEXT_TYPE_ERROR,
692 "codec_ffmpeg: cannot open encoder %s\n", name);
696 codec = avcodec_find_decoder(ff_dec_id);
699 iaxci_usermsg(IAXC_TEXT_TYPE_ERROR,
700 "codec_ffmpeg: cannot find decoder %d\n",
704 if (avcodec_open(d->avctx, codec))
706 iaxci_usermsg(IAXC_TEXT_TYPE_ERROR,
707 "codec_ffmpeg: cannot open decoder %s\n", name);
712 enum PixelFormat fmts[] = { PIX_FMT_YUV420P, -1 };
713 if (d->avctx->get_format(d->avctx, fmts) != PIX_FMT_YUV420P)
715 iaxci_usermsg(IAXC_TEXT_TYPE_ERROR,
716 "codec_ffmpeg: cannot set decode format to YUV420P\n");
728 int codec_video_ffmpeg_check_codec(int format)
731 enum CodecID codec_id;
733 /* These functions are idempotent, so it is okay that we
734 * may call them elsewhere at a different time.
737 avcodec_register_all();
739 codec_id = map_iaxc_codec_to_avcodec(format);
741 if (codec_id == CODEC_ID_NONE)
744 codec = avcodec_find_encoder(codec_id);
746 return codec ? 1 : 0;