ref: 1b2e47b2c6bc6906ee896ceb9fddf9fa7e04046e
dir: /decoder_h264.c/
#include <decoder/core/inc/decoder.h> #include <decoder/core/inc/manage_dec_ref.h> #include <thread.h> #include "frame.h" #include "stream.h" #include "decoder.h" #include "misc.h" #pragma lib "../openh264/codec/libopenh264.$M.a" typedef struct Aux Aux; struct Aux { SWelsDecoderContext ctx; SLogContext logctx; SBufferInfo info; SWelsLastDecPicInfo pic; SVlcTable vlctbl; SDecoderStatistics stat; uint8_t *data[3]; /* reordering */ SPictInfo pics[16]; int npics; int lastgopremain; int lastwritten; int lbufpicind; int minpoc; int picind; }; static void logfun(void *ctx, const int32_t level, const char *fmt, va_list argv) { USED(ctx); if(debug >= level){ fprint(2, "h264: "); vfprint(2, fmt, argv); fprint(2, "\n"); } } static uvlong reorder(Aux *a, uvlong ts) { int i, firstvalid; if(a->npics > 0){ if(a->ctx.pLastDecPicInfo->pPreviousDecodedPictureInDpb && a->ctx.pLastDecPicInfo->pPreviousDecodedPictureInDpb->bNewSeqBegin){ a->lastgopremain = a->npics; for(i = 0; i <= a->lbufpicind; i++) a->pics[i].bLastGOP = a->pics[i].iPOC > IMinInt32; }else{ for(i = 0; i <= a->lbufpicind; i++){ if(a->pics[i].iPOC == a->ctx.pSliceHeader->iPicOrderCntLsb){ a->lastgopremain = a->npics; for(i = 0; i <= a->lbufpicind; i++) a->pics[i].bLastGOP = a->pics[i].iPOC > IMinInt32; break; } } } } for(i = 0; i < nelem(a->pics); i++){ if(a->pics[i].iPOC == IMinInt32){ memmove(&a->pics[i].sBufferInfo, &a->info, sizeof(a->info)); a->pics[i].iPOC = a->ctx.pSliceHeader->iPicOrderCntLsb; a->pics[i].iPicBuffIdx = a->ctx.pLastDecPicInfo->pPreviousDecodedPictureInDpb->iPicBuffIdx; a->pics[i].uiDecodingTimeStamp = ts; a->ctx.pLastDecPicInfo->pPreviousDecodedPictureInDpb->iRefCount++; a->pics[i].bLastGOP = false; a->info.iBufferStatus = 0; a->npics++; if(i > a->lbufpicind) a->lbufpicind = i; break; } } PPicBuff picbuf = a->ctx.pPicBuff; if(a->lastgopremain > 0){ a->minpoc = IMinInt32; firstvalid = -1; for(i = 0; i <= a->lbufpicind; i++){ if(a->minpoc == IMinInt32 && a->pics[i].iPOC > IMinInt32 && a->pics[i].bLastGOP){ a->minpoc = a->pics[i].iPOC; a->picind = i; firstvalid = i; break; } } for(i = 0; i <= a->lbufpicind; i++){ if(i == firstvalid) continue; if(a->pics[i].iPOC > IMinInt32 && a->pics[i].iPOC < a->minpoc && a->pics[i].bLastGOP){ a->minpoc = a->pics[i].iPOC; a->picind = i; } } a->lastwritten = a->minpoc; memmove(&a->info, &a->pics[a->picind].sBufferInfo, sizeof(a->info)); a->data[0] = a->info.pDst[0]; a->data[1] = a->info.pDst[1]; a->data[2] = a->info.pDst[2]; a->pics[a->picind].iPOC = IMinInt32; ts = a->pics[a->picind].uiDecodingTimeStamp; picbuf->ppPic[a->pics[a->picind].iPicBuffIdx]->iRefCount--; a->pics[a->picind].bLastGOP = false; a->minpoc = IMinInt32; a->npics--; a->lastgopremain--; if(a->lastgopremain == 0) a->lastwritten = IMinInt32; return ts; } if(a->npics > 0){ a->minpoc = IMinInt32; firstvalid = -1; for(i = 0; i <= a->lbufpicind; i++){ if(a->minpoc == IMinInt32 && a->pics[i].iPOC > IMinInt32){ a->minpoc = a->pics[i].iPOC; a->picind = i; firstvalid = i; break; } } for(i = 0; i <= a->lbufpicind; i++){ if(i == firstvalid) continue; if(a->pics[i].iPOC > IMinInt32 && a->pics[i].iPOC < a->minpoc){ a->minpoc = a->pics[i].iPOC; a->picind = i; } } } if(a->minpoc > IMinInt32 && ((a->lastwritten > IMinInt32 && a->minpoc - a->lastwritten <= 1) || a->minpoc < a->ctx.pSliceHeader->iPicOrderCntLsb)){ a->lastwritten = a->minpoc; memmove(&a->info, &a->pics[a->picind].sBufferInfo, sizeof(a->info)); a->data[0] = a->info.pDst[0]; a->data[1] = a->info.pDst[1]; a->data[2] = a->info.pDst[2]; ts = a->pics[a->picind].uiDecodingTimeStamp; a->pics[a->picind].iPOC = IMinInt32; picbuf->ppPic[a->pics[a->picind].iPicBuffIdx]->iRefCount--; a->pics[a->picind].bLastGOP = false; a->minpoc = IMinInt32; a->npics--; } return ts; } static void resetreorder(Aux *a, int full) { int i; a->npics = 0; a->lastgopremain = 0; a->lastwritten = IMinInt32; a->minpoc = IMinInt32; a->picind = 0; for(i = 0; i < (full ? nelem(a->pics) : (a->lbufpicind+1)); i++){ a->pics[i].bLastGOP = false; a->pics[i].iPOC = IMinInt32; } a->lbufpicind = 0; } static char * err2s(int err) { static char t[256]; char *s, *e; t[0] = t[1] = 0; s = t; e = t+sizeof(t); if(err & dsFramePending) s = seprint(s, e, "|FramePending"); if(err & dsRefLost) s = seprint(s, e, "|RefLost"); if(err & dsBitstreamError) s = seprint(s, e, "|BitstreamError"); if(err & dsDepLayerLost) s = seprint(s, e, "|DepLayerLost"); if(err & dsNoParamSets) s = seprint(s, e, "|NoParamSets"); if(err & dsDataErrorConcealed) s = seprint(s, e, "|DataErrorConcealed"); if(err & dsRefListNullPtrs) s = seprint(s, e, "|RefListNullPtrs"); if(err & dsInvalidArgument) s = seprint(s, e, "|InvalidArgument"); if(err & dsInitialOptExpected) s = seprint(s, e, "|InitialOptExpected"); if(err & dsOutOfMemory) s = seprint(s, e, "|OutOfMemory"); if(err & dsDstBufNeedExpan) s = seprint(s, e, "|DstBufNeedExpan"); USED(s); return t+1; } static int one(Aux *a, Streamframe *sf) { int res; if(sf->buf == nil){ a->ctx.bEndOfStreamFlag = true; a->ctx.bInstantDecFlag = true; }else{ a->ctx.bEndOfStreamFlag = false; } a->ctx.iErrorCode = dsErrorFree; a->ctx.iFeedbackVclNalInAu = FEEDBACK_UNKNOWN_NAL; a->ctx.uiTimeStamp = a->info.uiInBsTimeStamp; memset(&a->info, 0, sizeof(a->info)); memset(a->data, 0, sizeof(a->data)); a->ctx.bReferenceLostAtT0Flag = false; a->ctx.bCurAuContainLtrMarkSeFlag = false; a->ctx.iFrameNumOfAuMarkedLtr = 0; a->ctx.iFrameNum = -1; a->ctx.iFeedbackTidInAu = -1; a->ctx.iFeedbackNalRefIdc = -1; if((res = WelsDecodeBs(&a->ctx, sf->buf, sf->sz, a->data, &a->info, nil)) != 0){ if(a->ctx.iErrorCode & dsOutOfMemory) return 0; werrstr("%s: %.*H", err2s(res), MIN(32, sf->sz), sf->buf); return -1; } if(a->info.iBufferStatus != 0 && a->ctx.pSps->uiProfileIdc != 66 && a->ctx.pSps->uiProfileIdc != 83){ /* non-baseline needs reordering */ memset(a->data, 0, sizeof(a->data)); sf->timestamp = reorder(a, sf->timestamp); } return 0; } static void decode(void *x) { uvlong lasttimestamp; Decoder *d; Channel *c; Frame *f; Streamframe sf; Aux *a; int res, w, h, *stride; uvlong start, framenum; d = x; a = d->aux; lasttimestamp = 0; start = nanosec(); for(res = 0, framenum = 0; res >= 0 && (res = Sread(d->s, &sf)) == 0 && sf.sz > 0; framenum++){ if(one(a, &sf) != 0) goto failed; tryagain: if(a->data[0] != nil && sf.timestamp >= lasttimestamp){ d->decodetime = nanosec() - start; w = a->info.UsrData.sSystemBuffer.iWidth; h = a->info.UsrData.sSystemBuffer.iHeight; stride = a->info.UsrData.sSystemBuffer.iStride; if((f = newframe(w, h, a->data, stride[0], stride[1])) != nil){ f->crop.left = a->ctx.sFrameCrop.iLeftOffset; f->crop.top = a->ctx.sFrameCrop.iTopOffset; f->crop.right = w - a->ctx.sFrameCrop.iRightOffset; f->crop.bottom = h - a->ctx.sFrameCrop.iBottomOffset; f->dt = (sf.timestamp - lasttimestamp) * d->timebase * 1000000000ULL; lasttimestamp = sf.timestamp; if(sendp(d->frames, f) < 0){ free(f); goto done; } } start = nanosec(); } if(sf.buf != nil){ sf.buf = nil; sf.sz = 0; if(one(a, &sf) != 0){ failed: werrstr("frame %llud: %r", framenum); break; } goto tryagain; } } if(res != 0) fprint(2, "h264: %r\n"); /* FIXME the frames are finished but there might still be left-overs */ resetreorder(a, 0); WelsResetRefPic(&a->ctx); done: WelsEndDecoder(&a->ctx); free(a); c = d->finished; sendp(c, res == 0 ? nil : "error"); chanclose(c); threadexits(nil); } static int h264open(Decoder *d) { Aux *a; int res; a = calloc(1, sizeof(*a)); a->ctx.pLastDecPicInfo = &a->pic; a->ctx.pVlcTable = &a->vlctbl; a->ctx.pDecoderStatistics = &a->stat; a->ctx.pMemAlign = &cMemoryAlign; a->logctx.pfLog = logfun; a->ctx.sLogCtx = a->logctx; a->ctx.pParam = calloc(1, sizeof(SDecodingParam)); a->ctx.pParam->sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT; resetreorder(a, 1); if((res = WelsInitDecoder(&a->ctx, &a->logctx)) != 0){ werrstr("WelsInitDecoder: %s", err2s(res)); free(a); return -1; } d->aux = a; proccreate(decode, d, 65536); return res; } static void h264close(Decoder *d) { USED(d); } Decoderops h264ops = { .open = h264open, .close = h264close, };