media: make sure the buffer is writable before modifying it