static int init_decoder(struct viddec_state *st, const char *name) { enum AVCodecID codec_id; codec_id = avcodec_resolve_codecid(name); if (codec_id == AV_CODEC_ID_NONE) return EINVAL; st->codec = avcodec_find_decoder(codec_id); if (!st->codec) return ENOENT; #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(92<<8)+0) st->ctx = avcodec_alloc_context3(st->codec); #else st->ctx = avcodec_alloc_context(); #endif #if LIBAVUTIL_VERSION_INT >= ((52<<16)+(20<<8)+100) st->pict = av_frame_alloc(); #else st->pict = avcodec_alloc_frame(); #endif if (!st->ctx || !st->pict) return ENOMEM; #if LIBAVCODEC_VERSION_INT >= ((53<<16)+(8<<8)+0) if (avcodec_open2(st->ctx, st->codec, NULL) < 0) return ENOENT; #else if (avcodec_open(st->ctx, st->codec) < 0) return ENOENT; #endif return 0; }
int encode_update(struct videnc_state **vesp, const struct vidcodec *vc, struct videnc_param *prm, const char *fmtp, videnc_packet_h *pkth, void *arg) { struct videnc_state *st; int err = 0; if (!vesp || !vc || !prm || !pkth) return EINVAL; if (*vesp) return 0; st = mem_zalloc(sizeof(*st), destructor); if (!st) return ENOMEM; st->encprm = *prm; st->pkth = pkth; st->arg = arg; st->codec_id = avcodec_resolve_codecid(vc->name); if (st->codec_id == AV_CODEC_ID_NONE) { err = EINVAL; goto out; } st->mb = mbuf_alloc(FF_MIN_BUFFER_SIZE * 20); st->mb_frag = mbuf_alloc(1024); if (!st->mb || !st->mb_frag) { err = ENOMEM; goto out; } st->sz_max = st->mb->size; if (st->codec_id == AV_CODEC_ID_H264) { #ifndef USE_X264 err = init_encoder(st); #endif } else err = init_encoder(st); if (err) { warning("avcodec: %s: could not init encoder\n", vc->name); goto out; } if (str_isset(fmtp)) { struct pl sdp_fmtp; pl_set_str(&sdp_fmtp, fmtp); fmt_param_apply(&sdp_fmtp, param_handler, st); } debug("avcodec: video encoder %s: %d fps, %d bit/s, pktsize=%u\n", vc->name, prm->fps, prm->bitrate, prm->pktsize); out: if (err) mem_deref(st); else *vesp = st; return err; }