{"record":{"id":"9cd26e4ddbeb2e00","repo":"pion/webrtc","slug":"data-is-not-a-h264-bitstream","errorCode":null,"errorMessage":"data is not a H264 bitstream","messagePattern":"data is not a H264 bitstream","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/media/h264reader/h264reader.go","lineNumber":26,"sourceCode":"\t\"bytes\"\n\t\"errors\"\n\t\"io\"\n)\n\n// H264Reader reads data from stream and constructs h264 nal units.\ntype H264Reader struct {\n\tstream                      io.Reader\n\tnalBuffer                   []byte\n\tcountOfConsecutiveZeroBytes int\n\tnalPrefixParsed             bool\n\treadBuffer                  []byte\n\ttmpReadBuf                  []byte\n\tincludeSEI                  bool\n}\n\nvar (\n\terrNilReader           = errors.New(\"stream is nil\")\n\terrDataIsNotH264Stream = errors.New(\"data is not a H264 bitstream\")\n)\n\n// NewReader creates new H264Reader.\nfunc NewReader(in io.Reader) (*H264Reader, error) {\n\tif in == nil {\n\t\treturn nil, errNilReader\n\t}\n\n\treader := &H264Reader{\n\t\tstream:          in,\n\t\tnalBuffer:       make([]byte, 0),\n\t\tnalPrefixParsed: false,\n\t\treadBuffer:      make([]byte, 0),\n\t\ttmpReadBuf:      make([]byte, 4096),\n\t\tincludeSEI:      false,\n\t}\n\n\treturn reader, nil","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/pion/webrtc/blob/8c25dc09fa9e7c09aac4309ead88093d760432b0/pkg/media/h264reader/h264reader.go#L8-L44","documentation":"Returned when the first bytes read from the stream do not look like an H264 Annex-B bitstream: fewer than 3 bytes were read, or no valid start-code prefix (00 00 01 / 00 00 00 01) is found at the head of the data. The reader validates the bitstream prefix before parsing NAL units.","triggerScenarios":"Calling ParseNextClosure/NextNAL on a stream whose first bytes lack an H264 start code, or a stream shorter than 3 bytes; bitStreamStartsWithH264Prefix fails.","commonSituations":"Feeding an H265/HEVC file to the H264 reader, feeding raw NAL units without Annex-B start codes, empty or truncated files, MP4 containers (length-prefixed, no start codes) passed directly.","solutions":["Verify the input is actually H264 Annex-B (starts with 00 00 00 01) before handing it to the reader.","If the source is HEVC, use h265reader instead; if it's MP4/matroska, remux or extract an Annex-B elementary stream first.","Check the file isn't empty or truncated (size < 4 bytes)."],"exampleFix":"// before\nrdr, _ := h264reader.NewReader(hevcFile) // wrong codec\n// after\nif !bytes.HasPrefix(head, []byte{0, 0, 0, 1}) {\n    return fmt.Errorf(\"input is not H264 Annex-B bitstream\")\n}\nrdr, err := h264reader.NewReader(h264File)","handlingStrategy":"validation","validationCode":"head := make([]byte, 4)\nn, _ := io.ReadFull(stream, head)\nif n < 4 || !bytes.Equal(head[3:], []byte{1}) || head[0] != 0 || head[1] != 0 {\n    return errors.New(\"not an H264 Annex-B bitstream\")\n}\nstream.Seek(0, io.SeekStart)","typeGuard":null,"tryCatchPattern":"_, err := reader.NextNAL()\nif err == errDataIsNotH264Stream {\n    return fmt.Errorf(\"input is not H264 Annex-B: %w\", err)\n}","preventionTips":["Confirm the codec of your input before choosing h264reader vs h265reader.","Convert container formats (MP4/MKV) to Annex-B elementary streams first.","Never feed length-prefixed NAL data directly to this reader."],"tags":["go","media","h264","bitstream-format"],"backgroundTag":"invalid-bitstream-format","analyzedSha":"8c25dc09fa9e7c09aac4309ead88093d760432b0","analyzedAt":"2026-09-03T21:56:56.182Z","contentChangedAt":"2026-09-03T21:56:56.182Z","schemaVersion":2},"datasetVersion":"2026-09-11T07:07:21.782Z"}