{"record":{"id":"5bfcea77cb395b18","repo":"Yalantis/uCrop","slug":"load-yuv-file-s-doesn-t-contain-frame-number","errorCode":null,"errorMessage":"load_yuv(): File '%s' doesn't contain frame number %u.","messagePattern":"load_yuv\\(\\): File '(.+?)' doesn't contain frame number %u\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":67221,"sourceCode":"        nfirst_frame = first_frame<last_frame?first_frame:last_frame,\n        nlast_frame = first_frame<last_frame?last_frame:first_frame,\n        nstep_frame = step_frame?step_frame:1;\n\n      if (!size_x || !size_y || size_x%cfx || size_y%cfy)\n        throw CImgArgumentException(_cimglist_instance\n                                    \"load_yuv(): Specified dimensions (%u,%u) are invalid, for file '%s'.\",\n                                    cimglist_instance,\n                                    size_x,size_y,filename?filename:\"(FILE*)\");\n\n      CImg<ucharT> YUV(size_x,size_y,1,3), UV(size_x/cfx,size_y/cfy,1,2);\n      std::FILE *const nfile = file?file:cimg::fopen(filename,\"rb\");\n      bool stop_flag = false;\n      int err;\n      if (nfirst_frame) {\n        err = cimg::fseek(nfile,(uint64T)nfirst_frame*(YUV._width*YUV._height + 2*UV._width*UV._height),SEEK_CUR);\n        if (err) {\n          if (!file) cimg::fclose(nfile);\n          throw CImgIOException(_cimglist_instance\n                                \"load_yuv(): File '%s' doesn't contain frame number %u.\",\n                                cimglist_instance,\n                                filename?filename:\"(FILE*)\",nfirst_frame);\n        }\n      }\n      unsigned int frame;\n      for (frame = nfirst_frame; !stop_flag && frame<=nlast_frame; frame+=nstep_frame) {\n        YUV.get_shared_channel(0).fill(0);\n        // *TRY* to read the luminance part, do not replace by cimg::fread!\n        err = (int)std::fread((void*)(YUV._data),1,(size_t)YUV._width*YUV._height,nfile);\n        if (err!=(int)(YUV._width*YUV._height)) {\n          stop_flag = true;\n          if (err>0)\n            cimg::warn(_cimglist_instance\n                       \"load_yuv(): File '%s' contains incomplete data or given image dimensions \"\n                       \"(%u,%u) are incorrect.\",\n                       cimglist_instance,\n                       filename?filename:\"(FILE*)\",size_x,size_y);","sourceCodeStart":67203,"sourceCodeEnd":67239,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L67203-L67239","documentation":"To start at first_frame > 0, load_yuv() seeks past (frame_size * first_frame) bytes; if fseek fails, the file is too short to contain that frame. CImg closes the file and throws CImgIOException naming the missing frame index.","triggerScenarios":"load_yuv(filename, w, h, cs, first_frame, ...) with first_frame beyond the number of frames actually present; wrong frame_size assumption (incorrect width/height/subsampling) making the computed seek offset overshoot the file; a truncated file.","commonSituations":"Hardcoding a start frame from a longer clip; using wrong dimensions so frame_size math is wrong even though the frame exists; truncated download/copy; looping over frames without knowing the file length.","solutions":["Compute the frame count as filesize / (size_x*size_y + 2*(size_x/cfx)*(size_y/cfy)) and choose first_frame < count","Verify size_x, size_y and chroma_subsampling match the actual file - wrong values shift the seek arithmetic","Re-download/re-export the file if it is truncated","Start from frame 0 to confirm the file parses, then adjust first_frame"],"exampleFix":"// before\nimgs.load_yuv(\"clip.yuv\", 640, 480, 420, 100, ~0U); // clip has 50 frames\n// after\nconst unsigned int n = fileSize(\"clip.yuv\") / (640*480 + 2*(320*240));\nunsigned int first = std::min(100u, n - 1);\nimgs.load_yuv(\"clip.yuv\", 640, 480, 420, first, ~0U);","handlingStrategy":"validation","validationCode":"uint64_t frameSize = (uint64_t)w*h + 2*((uint64_t)(w/cfx)*(h/cfy));\nuint64_t nFrames = fileSize(path) / frameSize;\nif (firstFrame >= nFrames) firstFrame = nFrames ? nFrames - 1 : 0; // clamp","typeGuard":null,"tryCatchPattern":"try { imgs.load_yuv(path, w, h, cs, first, last); }\ncatch (CImgIOException& e) { fprintf(stderr, \"frame out of range: %s\\n\", e.what()); imgs.assign(); }","preventionTips":["Derive frame count from file size before choosing frame ranges","Never hardcode frame indices across clips","Verify dimensions/subsampling first - wrong values corrupt the frame arithmetic"],"tags":["cimg","yuv","file-io","seek","video-loading"],"backgroundTag":"file-not-found","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}