A method for encoding data comprising: receiving a frame of data comprising a plurality of subunits, wherein the frame of data comprises data associated with a plurality of pixels; setting a bit budget for the frame of data, the bit budget being a number of bits available for encoding the frame; initializing a remaining bit budget to be equal to the bit budget for the frame of data; setting a lossless codec as a current codec, wherein the lossless codec has a lossless codec bitrate; and sequentially encoding the subunits of the frame of data, wherein the encoding of the sub-units comprises: encoding a first subunit of the subunits using the current codec, set to the lossless codec, to compute a first encoded subunit; subtracting a number of bits used to encode the first encoded subunit from the remaining bit budget; determining a first lossy number of bits, wherein the first lossy number of bits is a number of bits required to encode a plurality of remaining subunits of the frame of data using a first lossy codec, the first lossy codec having a first lossy codec bitrate lower than the lossless codec bitrate; determining whether the remaining bit budget exceeds the first lossy number of bits; in response to determining that the remaining bit budget is not greater than the first lossy number of bits, setting the first lossy codec as the current codec; in response to determining that the remaining bit budget is greater than the first lossy number of bits, setting the lossless codec as the current codec; and encoding a second subunit of the subunits using the current codec, to compute a second encoded subunit, wherein the bit budget comprises W - lossyBpp - 1 extra margin bits, where W is the length of a longest binary codeword of a symbol in the lossless codec, and where lossyBpp is the first lossy codec bitrate. receiving a frame of data comprising a plurality of subunits, wherein the frame of data comprises data associated with a plurality of pixels; setting a bit budget for the frame of data, the bit budget being a number of bits available for encoding the frame; initializing a remaining bit budget to be equal to the bit budget for the frame of data; setting a lossless codec as a current codec, wherein the lossless codec has a lossless codec bitrate; and sequentially encoding the subunits of the frame of data, wherein the encoding of the sub-units comprises: encoding a first subunit of the subunits using the current codec, set to the lossless codec, to compute a first encoded subunit; subtracting a number of bits used to encode the first encoded subunit from the remaining bit budget; determining a first lossy number of bits, wherein the first lossy number of bits is a number of bits required to encode a plurality of remaining subunits of the frame of data using a first lossy codec, the first lossy codec having a first lossy codec bitrate lower than the lossless codec bitrate; determining whether the remaining bit budget exceeds the first lossy number of bits; in response to determining that the remaining bit budget is not greater than the first lossy number of bits, setting the first lossy codec as the current codec; in response to determining that the remaining bit budget is greater than the first lossy number of bits, setting the lossless codec as the current codec; and encoding a second subunit of the subunits using the current codec, to encoding a first subunit of the subunits using the current codec, set to the lossless codec, to compute a first encoded subunit; subtracting a number of bits used to encode the first encoded subunit from the remaining bit budget; determining a first lossy number of bits, wherein the first lossy number of bits is a number of bits required to encode a plurality of remaining subunits of the frame of data using a first lossy codec, the first lossy codec having a first lossy codec bitrate lower than the lossless codec bitrate; determining whether the remaining bit budget exceeds the first lossy number of bits; in response to determining that the remaining bit budget is not greater than the first lossy number of bits, setting the first lossy codec as the current codec; in response to determining that the remaining bit budget is greater than the first lossy number of bits, setting the lossless codec as the current codec; and encoding a second subunit of the subunits using the current codec, to compute a second encoded subunit,