0

I have written some C code that takes an mp4 file with h264-encoded video and AAC-encoded audio and writes it to segmented .ts files.

The code can be seen here: http://pastebin.com/JVdgjM9G

The problem is that the code chokes on audio packets. Because I am converting from h264, I have to use the "h264_mp4toannexb" which I finally got working for video frames. However, as soon as the program reaches the first audio packet (stream 1 below) it crashes.

Sample output:

Output #0, mpegts, to 'testvideo':
    Stream #0.0: Video: libx264, yuv420p, 1280x720, q=2-31, 1416 kb/s, 90k tbn, 23.98 tbc
    Stream #0.1: Audio: libfaac, 48000 Hz, stereo, 127 kb/s
First chunk: testvideo-00001.ts
Read frame, keyframe: 1, index: 0
Did bitfilter fun!
Read frame, keyframe: 0, index: 0
Did bitfilter fun!
(...this repeats several more times, truncated for space...)
Did bitfilter fun!
Read frame, keyframe: 0, index: 0
Did bitfilter fun!
Read frame, keyframe: 1, index: 1
base(54516) malloc: *** error for object 0x7fd2db404520: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

I tried changing the code to also run the filter on the audio stream (using audio_stream->codec instead of video_stream->codec), but that simply just gives an error from the filter.

The problem happens when I try to call av_interleaved_write_frame(output_context, &packet);- for the filtered video packets, there is no problem but the audio packet it completely chokes on. I am kind of stumped on why though, so any help is appreciated.

Christian P.
  • 4,784
  • 7
  • 53
  • 70
  • So the error happens at line 297? What is object 0x7fd2db404520? – idoby Jun 11 '13 at 12:25
  • That's the packet object, as far as I can tell. I tried debugging it, but got into something where there was no symbols (black hole) and could not follow it. But packet is the only thing that changes between each iteration for that function, so I cannot imagine it being anything else. – Christian P. Jun 11 '13 at 12:40
  • This is quite a lot of code to debug by eyeball. Can you post this as a buildable project, including any libraries, so people can stick it in a VM, build and debug it? – idoby Jun 11 '13 at 13:53
  • This is the only part of the code I have written - the rest is from libav.org, which is practically undocumented :( – Christian P. Jun 11 '13 at 14:17
  • What version of the libraries are you using? – Anton Khirnov Jun 12 '13 at 14:02
  • 1
    My guess is that somehow you're passing an already freed video packet to the muxer, but since it waits until it has at least on packet for each stream before interleaving, it doesn't crash until it gets an audio packet. Are you absolutely sure it's the release and not a later git version? I can imagine why this would happen with git, but not the release. – Anton Khirnov Jun 12 '13 at 14:36
  • That makes sense. I just assumed that it was "bad" audio packets, but if it buffers the packets until there is actually something to interleave I need to take a closer look at my packet allocation in general. There's a good chance that it is MY code (and not libav) that is misbehaving. – Christian P. Jun 12 '13 at 14:41
  • Turns out it was a premature `av_free_packet`, and it fixed the problem (at least allowing for it to be written). There are other problems, but not in the same category. If you want credit, simply post as an answer. – Christian P. Jun 12 '13 at 14:47

1 Answers1

0

It turns out the av_free_packet call after the bitfilter manipulation was actually releasing the video packets. Removing that call caused the code to run correctly!

Christian P.
  • 4,784
  • 7
  • 53
  • 70