As the name implies, a codec performs two jobs: first, it compresses the size of the audio or video file, and second, it decompresses it as needed. All codecs employ an algorithm to accomplish this feat of decreasing large file sizes with the ability to later retrieve and display the data with similar quality as was originally recorded. However, this does not mean there is a "default" algorithm for performing the compressing of file sizes; each codec utilizes its own technology for creating the ideal algorithm for given applications. Furthermore, each codec can choose to encode files in one of two methods: lossy or lossless compression.
Lossy compression works by removing extraneous bits of information that do not impact the overall quality of the content. For instance, an MP3 music file can use lossy compression quite effectively by extracting sounds that cannot be heard by the human ear and consequently barely affects the quality of the overall sound. Another example is a JPG image file; it too deletes excess information or converts such information in a palatable context; for instance, a true picture with a full blue sky will generate many gradients of blue but can be easily reduced to one or two shades of blue. In lossy compression, the user typically can select the extent of the file compression technique, but higher compression (resulting in smaller file sizes) produces lower-quality content; a user may resort to trial and error to determine the best compression percentage while retaining an acceptable quality of content.
Lossless compression is geared more towards preserving content quality, meaning file sizes will not be as small as under lossy compression, but no valuable data is tossed out in the compression process; instead, compression is accomplished by removing redundancy. While this sounds very much like removing data in lossy compression, it actually converts the data to logical subunits. Here is an explanation of how lossless compression can work.
Assume you have content with the following string of characters included: "aaaaaaabbbccccccddeeeeeeeee" (which works out to a string of 7 a's in a row, followed by 3 b's, then 6 c's, 2 d's, and 9 e's). Lossless compression would convert that information to "a7b3c6d2e9" by reducing the string from a size of 27 characters to just 10 characters (it describes each consecutive group of characters by the character and number of identical characters following it, so "a7" equals "aaaaaaa"). The beauty of lossless compression is that the original data can always be recovered and used when needed.
Why would someone want to reverse the compression of a large file? Professional photographers, for example, want their RAW files intact, perhaps for professional printing of an oversize picture, but also want to share a smaller, lower quality image on their social media platforms for marketing purposes. By employing lossless compression, the original work can be presented in an informal context (i.e., on Pinterest) while also available for detailed editing without losing one pixel of data.