--- encoder.c 2010/12/18 16:02:00 1.134 +++ encoder.c 2011/04/07 19:07:36 1.135.2.6 @@ -21,7 +21,7 @@ * along with this program ; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * $Id: encoder.c,v 1.134 2010/12/18 16:02:00 Isibaar Exp $ + * $Id: encoder.c,v 1.135.2.6 2011/04/07 19:07:36 Isibaar Exp $ * ****************************************************************************/ @@ -449,7 +449,11 @@ /* multithreaded stuff */ if (create->num_threads > 0) { +#ifndef HAVE_PTHREAD + int t = MAX(1, create->num_threads); +#else int t = MIN(create->num_threads, (int) (pEnc->mbParam.mb_height>>1)); /* at least two rows per thread */ +#endif int threads_per_slice = MAX(1, (t / pEnc->num_slices)); int rows_per_thread = (pEnc->mbParam.mb_height + threads_per_slice - 1) / threads_per_slice; @@ -1609,7 +1613,8 @@ memcpy((void *)((ptr_t)bs->start + pos), (void *)((ptr_t)pEnc->smpData[k].bs->start), len); - current->length = pos += len; + current->length += len; + pos += len; /* collect stats */ current->sStat.iTextBits += pEnc->smpData[k].sStat->iTextBits; @@ -1645,9 +1650,10 @@ int bound = 0, num_slices = pEnc->num_slices; int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices)); int slices_per_thread = (num_slices*1024 / num_threads); - int mb_width = pEnc->mbParam.mb_width; int mb_height = pEnc->mbParam.mb_height; +#ifdef HAVE_PTHREAD void * status = NULL; +#endif uint16_t k; pEnc->mbParam.m_rounding_type = 1; @@ -1687,19 +1693,23 @@ } pEnc->smpData[0].bs = bs; pEnc->smpData[0].sStat = &pEnc->current->sStat; - + +#ifdef HAVE_PTHREAD /* create threads */ for (k = 1; k < num_threads; k++) { pthread_create(&pEnc->smpData[k].handle, NULL, (void*)SliceCodeI, (void*)&pEnc->smpData[k]); } +#endif SliceCodeI(&pEnc->smpData[0]); +#ifdef HAVE_PTHREAD /* wait until all threads are finished */ for (k = 1; k < num_threads; k++) { pthread_join(pEnc->smpData[k].handle, &status); } +#endif pEnc->current->length = BitstreamLength(bs) - (bits/8); @@ -1755,7 +1765,6 @@ FRAMEINFO *const current = pEnc->current; FRAMEINFO *const reference = pEnc->reference; MBParam * const pParam = &pEnc->mbParam; - IMAGE *pRef = &reference->image; int mb_width = pParam->mb_width; int mb_height = pParam->mb_height; @@ -1855,7 +1864,9 @@ iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x, pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x, pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant); - if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) { + if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant || ((bound > 1) && + ((y*mb_width+x == bound) || (y*mb_width+x == bound+1)))) { /* Some third-party decoders have problems with coloc skip MB before or after + resync marker in BVOP. We avoid any ambiguity and force no skip at slice boundary */ bSkip = 0; /* could not SKIP */ if (pParam->vol_flags & XVID_VOL_QUARTERPEL) { VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, bound, x, y, 0); @@ -1907,16 +1918,18 @@ int k = 0, bound = 0, num_slices = pEnc->num_slices; int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices)); +#ifdef HAVE_PTHREAD void * status = NULL; - int slices_per_thread = (num_slices*1024 / num_threads); int threads_per_slice = (pEnc->num_threads*1024 / num_threads); +#endif + int slices_per_thread = (num_slices*1024 / num_threads); IMAGE *pRef = &reference->image; if (!reference->is_edged) { start_timer(); image_setedges(pRef, pParam->edged_width, pParam->edged_height, - pParam->width, pParam->height, 0); + pParam->width, pParam->height, XVID_BS_VERSION); stop_edges_timer(); reference->is_edged = 1; } @@ -2004,6 +2017,7 @@ } } +#ifdef HAVE_PTHREAD if (pEnc->num_threads > 0) { /* multithreaded motion estimation - dispatch threads */ @@ -2058,7 +2072,9 @@ current->fcode = pEnc->smpData[k].minfcode; } - } else { + } else +#endif + { /* regular ME */ @@ -2102,18 +2118,22 @@ pEnc->smpData[0].bs = bs; pEnc->smpData[0].sStat = ¤t->sStat; +#ifdef HAVE_PTHREAD /* create threads */ for (k = 1; k < num_threads; k++) { pthread_create(&pEnc->smpData[k].handle, NULL, (void*)SliceCodeP, (void*)&pEnc->smpData[k]); } +#endif SliceCodeP(&pEnc->smpData[0]); +#ifdef HAVE_PTHREAD /* wait until all threads are finished */ for (k = 1; k < num_threads; k++) { pthread_join(pEnc->smpData[k].handle, &status); } +#endif current->length = BitstreamLength(bs) - (bits/8); @@ -2129,7 +2149,8 @@ if (current->sStat.kblks + current->sStat.mblks < (pParam->frame_drop_ratio * mb_width * mb_height) / 100 && - ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) ) + ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) && + (current->coding_type == P_VOP) ) { current->sStat.kblks = current->sStat.mblks = current->sStat.iTextBits = 0; current->sStat.ublks = mb_width * mb_height; @@ -2206,19 +2227,15 @@ int num_slices = pEnc->num_slices; if (data->start_y > 0) { /* write resync marker */ - write_video_packet_header(bs, pParam, frame, bound); + write_video_packet_header(bs, pParam, frame, bound+1); } - for (y = data->start_y; y < data->stop_y; y++) { + for (y = data->start_y; y < MIN(data->stop_y+1, mb_height); y++) { int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices); + int stop_x = (y == data->stop_y) ? 1 : mb_width; + int start_x = (y == data->start_y && y > 0) ? 1 : 0; - if (new_bound > bound) { - bound = new_bound; - BitstreamPadAlways(bs); - write_video_packet_header(bs, pParam, frame, bound); - } - - for (x = 0; x < mb_width; x++) { + for (x = start_x; x < stop_x; x++) { MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ @@ -2230,6 +2247,12 @@ continue; } + if (new_bound > bound && x > 0) { + bound = new_bound; + BitstreamPadAlways(bs); + write_video_packet_header(bs, pParam, frame, y*mb_width+x); + } + mb->quant = frame->quant; if (mb->cbp != 0 || pParam->plugin_flags & XVID_REQORIGINAL) { @@ -2273,15 +2296,16 @@ int bits = BitstreamPos(bs); int k = 0, bound = 0, num_slices = pEnc->num_slices; int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices)); +#ifdef HAVE_PTHREAD void * status = NULL; - int slices_per_thread = (num_slices*1024 / num_threads); int threads_per_slice = (pEnc->num_threads*1024 / num_threads); +#endif + int slices_per_thread = (num_slices*1024 / num_threads); IMAGE *f_ref = &pEnc->reference->image; IMAGE *b_ref = &pEnc->current->image; MBParam * const pParam = &pEnc->mbParam; - int mb_width = pParam->mb_width; int mb_height = pParam->mb_height; #ifdef BFRAMES_DEC_DEBUG @@ -2300,8 +2324,8 @@ if (!pEnc->reference->is_edged) { image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, - pEnc->mbParam.height, 0); - pEnc->current->is_edged = 1; + pEnc->mbParam.height, XVID_BS_VERSION); + pEnc->reference->is_edged = 1; } if (pEnc->reference->is_interpolated != 0) { @@ -2317,7 +2341,7 @@ if (!pEnc->current->is_edged) { image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, - pEnc->mbParam.height, 0); + pEnc->mbParam.height, XVID_BS_VERSION); pEnc->current->is_edged = 1; } @@ -2332,8 +2356,8 @@ frame->coding_type = B_VOP; - if (pEnc->current->vop_flags & XVID_VOP_RD_PSNRHVSM) { - image_block_variance(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->current->mbs, + if ((frame->vop_flags & XVID_VOP_RD_PSNRHVSM) && (frame->vop_flags & XVID_VOP_RD_BVOP)) { + image_block_variance(&frame->image, pEnc->mbParam.edged_width, frame->mbs, pEnc->mbParam.mb_width, pEnc->mbParam.mb_height); } @@ -2343,6 +2367,7 @@ start_timer(); +#ifdef HAVE_PTHREAD if (pEnc->num_threads > 0) { /* multithreaded motion estimation - dispatch threads */ @@ -2398,7 +2423,9 @@ if (pEnc->smpData[k].minbcode > frame->bcode) frame->bcode = pEnc->smpData[k].minbcode; } - } else { + } else +#endif + { MotionEstimationBVOP(&pEnc->mbParam, frame, ((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ @@ -2406,7 +2433,8 @@ pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, pEnc->current, b_ref, &pEnc->vInterH, - &pEnc->vInterV, &pEnc->vInterHV); + &pEnc->vInterV, &pEnc->vInterHV, + pEnc->num_slices); } stop_motion_timer(); @@ -2447,18 +2475,22 @@ } } +#ifdef HAVE_PTHREAD for (k = 1; k < num_threads; k++) { pthread_create(&pEnc->smpData[k].handle, NULL, (void*)SliceCodeB, (void*)&pEnc->smpData[k]); } +#endif pEnc->smpData[0].bs = bs; pEnc->smpData[0].sStat = &frame->sStat; SliceCodeB(&pEnc->smpData[0]); +#ifdef HAVE_PTHREAD for (k = 1; k < num_threads; k++) { pthread_join(pEnc->smpData[k].handle, &status); } +#endif frame->length = BitstreamLength(bs) - (bits/8);