my first attempts at libsdl

It’s late, which means two things — first, I should be in bed, and second, I’m writing the coolest code evar.

With my little DVD toolkit programs I’ve been writing to learn C, I’ve had the idea bounce around in my head a lot to write a tiny (tiny, super tiny, seriously tiny) DVD player. For no other reason than to learn more C. I’m not interested in features in the least (right now), since I don’t even *use* a DVD player to debug ripping and archiving all my DVDs. Well, that’s not true. I’ll use VLC if I don’t want to patiently track down the valid track on a poisoned DVD layout. But that’s rare, and it’s just for gathering information.

There’s a couple of other ideas I’d thought about with doing direct DVD to some kind of manipulation, and each time, the idea is either shut down as in it’s stupid or it’s just completely unnecessary and something I’d never use. One was to write “dvd_mkv” which would just extract the data off a track, and dump it directly into a Matroska container. That’s a cool idea and all, but I’d never need it. In fact, I went the opposite direction and added the feature to dvd_copy to print to stdout, and I just use avconv to dump the stream straight to a MKV myself. It’s simple, and I can select the streams. Perfect.

Here’s how that’d work, by the way:

$ dvd_copy /dev/sr0 | avconv -i – -codec copy dvd_longest_track.mkv

That program’s already in my tree. I need to roll that out to distros though. That’s another post though.

So, getting raw data off isn’t a big problem (although I can’t use that method to get vobsubs off, booooooo), and there’s no real reason to hack on something.

I have tried, though, in the past, and it’s been really frustrating. I’ve tried a lot with the libav libraries, and I just can’t get it completely figured out. I’m not going to rail on their code or applications or libraries in the slightest, because that’s certainly not the case, there is *nothing* wrong on their end. There’s a high learning curve (which I’m sure few people would disagree with me), and I’m an absolute novice at C, so, realistically, the goal is pretty much out of reach at the time. Combine that with the fact I’d never use it, and I kind of meh out. (I should note though that I did get some code written so I could parse enough data on a DVD directly to get some video info. A small amount of metadata, but still, it’s cool as an accomplishment. I just noticed it’s in my tree as well. Go crazy.)

Moving on, though, if the barrier to entry was much smaller, I might be more interested in tackling it.

Enter first, libmpeg2.

The libmpeg2 project is amazing, and the code is really simple to use (as in, I can read it and partly understand it), and it’s what libav* uses for decoding anyway. I managed to write a program I use as a test case, dvd_extract_mpeg2, which copies the raw video straight off of the DVD — unencrypted of course, and it filters out the garbage in there as well. I’ll probably pull it from the repo at some point or another, but for now you can see the code here. I should make it really, really clear that I *did not* write the MPEG2 decoding in there, it’s wholesale lifted from libmpeg2’s code (open source free software for the win, yo).

*That* one was a lot of fun to write, because it was the first time I was accessing the data directly and writing straight to a file. This was way before dvd_copy was a thing, so it was pretty exciting at the time. The code works, and I actually *do* use this one a lot even though it was never really intended as anything more than a fun tool to build, and it comes in handy when I just want to quickly look at the video of a source DVD. I don’t really need anything to extract the audio, because I either in the past would re-encode the AC3 audio, or do what I do now which is copy it straight to the target stream, so I never bothered writing anything. I probably never will.

I was playing with libmpeg2 for some reason this week … probably debugging some video … and I realized that mpeg2dec has an option to playback video directly. Hmm. That gave me an idea. Then I saw that it could use SDL as an output option. Hmmmmm. That gave me more ideas. I started thinking to myself, “If there was a simpler way, or at least, an alternate way to display the video, maybe I should look at that?”

So I started looking at libsdl. SDL (Simple DirectMedia Library) really is that … it’s really simple. I started reading through the documentation, and it’s super easy to understand. It’s one of those things you look through it, and ask yourself, “is that it?” thinking you missed a major part somewhere, as if there was more to it you were completely unaware of. It’s really simple. I loves it.

I’ve slowly been walking through the API documentation and poking at their samples and writing tiny little snippets to see if can understand it, and I’ve already made remarkable progress. I can’t remember when I started, but it’s only been a few days and I already figured out how to get it display a bitmap image on the screen. That may sound simple, but consider that I had *never* imagined in my life that I’d be writing anything related to a GUI since it seemed so complex and overwhelming to me, and the fact that I could even get one tiny little image displayed in a window with so little effort has completely blown me away. SDL2 also for the win, yo.

I’ve been tinkering with the code, believing that I’m understanding it right, and I got it to display one bitmap, then load another one, and switch to that. This is huge progress, by the way.

So for fun tonight, I exported the opening sequence to Teen Titans into bitmaps, and put it in a loaded sequence of my little window program. It’s pretty awesome. :D I’ve got a screenshot here, which is only a screenshot and not the sequential video, but for me it’s a little milestone marker for me. I’m having a lot of fun. (Note the incredibly short list of headers there, by the way … holy cats!)

Screenshot_2018-05-21_01-53-51

There’s already some cool stuff I’m learning (aside from libsdl and C), which is video and image formats in general.

One of the reasons I wanted to learn C (and likewise put it off for so long) was to take something that seemed so complex and nebulous and see if I could figure it out. For me, anything written in C was just amazing. I’d pop open the code, be completely confused, understand a tiny bit, and then completely get lost again. It was just overwhelming how hard it looked, and assumed I’d never be able to really understand it.

Interestingly enough, what finally got me to learn it was having a purpose to write applications in C — namely, stuff to access DVDs. Without that, the only reason I would have looked into it would have been to play around with it a bit. The novelty quickly wears off though, especially with something so difficult.

The same effect is happening with me trying to understand audio and video though. Again, it seems like this massive *thing* that’s just *out there* that is only observable, but never able to quite understand. It’s like the X-Files of code.

I’m already learning though, that things aren’t as scary and complex as that. The big hurdle is actually getting me to process new ideas and new ways that things *can be*. I would go into more detail about what I mean there, but I can’t even put it into my words myself. All I can say is that my understanding is growing.

So, I’m having fun. Will I ever get a tiny little DVD player written that goes into my tree? Probably. Will I ever *use* it? Probably not. It’ll most likely end up to be a hobby project, something I can be proud of and play around with it when I feel like it. Those are the best kinds of programs though, the ones where I learn the most.

Final wrap-up, I want to throw a shout out to dxr3player, which also gave me the inspiration to do this … it also uses libmpeg2 and a52dec to decode the video and audio directly, something I’d never thought of doing before. The ogle DVD player got me started thinking about alternate ways to do playback as well. Thanks guys!

A link to my DVDs wiki is probably in order as well.

Advertisements

c in the land of dvds

So, I started learning C lately, and my life satisfaction has gone way up.  In retrospect, I started thinking about all the things within the past three years that I’ve started doing — and they are all things that I’ve wanted to do since I was like 12 — and how much absolutely fun life has gotten since then.

We have:

  • learning C
  • subsequently, hacking on libdvdread and writing my own DVD programs in C
  • learning how to play and be a Dungeon Master for Advanced Dungeons & Dragons (2nd edition baby, yah! nothing but!)
  • skateboarding
  • reading Star Trek: The Next Generation novels
  • switching to paper plates

(I could go on for a while .. )

If I go back a few years before that, then we have me going back to school and studying psychology, which has also been a major life event.  And going back a few more than that (so we’re at about 8 now), it would be getting involved doing community service.

So, if I had some words of wisdom to pass down to a lot of people at once, this is what I would say: you can do all your grocery shopping in the frozen foods aisle.

Working on C has been a huge amount of fun for me.  When it comes to working with computers and/or development, I love doing things the absolutely most complex and difficult way possible (a la Gentoo).  Why?  It’s because if there’s some knob I can turn, or some option that I’m given, I want to customize it.  Originally it used to be because … well, I dunno why … but I’ve definitely mellowed out a lot over the years.  I’ve always dreamed of learning C, though, mostly, because it’s just *hard*.

I mean, the language only has a few data types, and it’s super easy to screw up your memory, your life, your grocery store, you name it.  Things just got real.  Which means things just got fun.

At the risk of sounding like a snotty kid who wants everyone to know how awesome he is, it occurred to me late last year, that of all the things computer-wise I know about a lot, it’s probably DVDs.  Having a moderately-sized library (a few hundred or so … or a few hundred more than that few hundred …), I’ve ripped and accessed all of them, and because of that, I’ve found lots of tweaks and bugs and ways to get certain information out.  It kind of clicked with me that, I had a huge wealth of knowledge in my head about one subject, and it may just possibly be something someone else could use.  Since I doubt anyone would give me an honorary Ph.D., I had to do the next best thing — slap together a half-finished wiki of my braindumps on the matter.

Honestly, I’m doing it more in a sense of the posterity of knowledge than anything else.  Going through my notes, I have a lot of stuff that I just dumped on there.  There’s really not *much* content there, but what it does carry behind it is years of working with DVDs on Linux … so when I say “use Handbrake,” I have months of research of saying why that is, or something similar.  Like I said, I’m a user and I’ve run into every situation imaginable, so it’s good to be able to kind of share that.  And let everyone know how detailed I can get.  Because people love that kind of thing.  Yahp.

However, learning C has taken me to the next level, where I can do two things: apply patches to the main DVD libraries, and write my own C programs.  I would be the *last* person to claim that this stuff is elegant code or wonderful programs, but it does, for me, have the goal of driving towards the point of providing data that users need.  Whatever that phrase means.  I think I’m trying to say that I like writing stuff that is for *intermediate* users.  I used to work on the Gentoo wiki a lot, and I noticed I really liked writing documents that go beyond a basic “this is this and that is that” explanation, and instead dives into the perspective of “let’s assume you know the basics, and want to do something that’s a little more detailed.”  Yah.  That.

I don’t know where I was going with all this, but there’s two things I’m sure of.  No, one.  I am out of frozen pizza.

giving thanks

Something I find interesting lately, is that with the Playstation 3 coming out, I haven’t seen hardly any positive press about how this thing freely lets you run other operating systems on it. Geeks will get all up in arms about DRM and lockdowns (which is a good thing), but nobody seems to really give say too much when someone actually does open up a platform. I must be reading the wrong blogs, or something.

Well, I think it’s cool. They even have a page about how to get started hacking your playstation. I’m not much of a gamer, and certainly not a console one, but just the fact that you can freely screw around with it makes me think about getting one someday.

ivtv-0.8.1 released

Just another quick Gentoo public service announcement. Hans released a new version of ivtv, v0.8.1, and this comes with a lot of cool updates. It’s already been bumped in portage as well, so for those of you using Hauppage PVR cards, give it a whirl. Note that you’ll have to install it with a 2.6.18 kernel because of the new MPEG2 API.

I’ve been crazy busy lately, but I installed mine and managed to record and watch about 15 seconds of TV just fine with MPlayer’s new PVR functionality, so it seems like it’s okay. I’ve been rebuilding my server all weekend, and hopefully I’ll get around to finishing up with mythtv and testing it out again. With luck, my squelchy audio issues will go away too. :)

bash

I wrote my first real (more than 3 lines) bash script at work today. It isn’t that complex, but I wanted to write it in bash instead of PHP, because I really need to learn it.

So far, color me impressed. I’m amazed at how easy it is to do stuff like string manipulation. I want to get good at it since ebuilds are written in bash, and I need the practice. My big goal is to write “dvd2mkv” in bash as well. The bonus is that it will remove one major dependency: PHP 5.1. That, and it will be hard as crap. Trial by fire, says me.

kde++

There are some things I don’t want to be doing at 11:13 at night when I’m tired and cranky and just want something to work. Calculating cron jobs is one of them. Once again, KDE comes to the rescue.

Now, I don’t use KDE as my desktop, but their tools are absolutely amazingly nice, practical, and easy to use. I just searched the portage tree for cron, and kcron came up in the list. Thanks to Gentoo’s split KDE ebuilds, I got to emerge just that file, which took 2 minutes and 47 seconds.

Then, I ran the little sucker and instead of using my tired head to figure out my freaky cron job schedule, I just used the nice little friendly GUI and it did everything for me. It even works with fcron, which webmin can’t do (seems to only work correctly with vixie-cron).

KDE rocks.

tv tuners

I haven’t been able to sleep tonight, and one thing that’s been keeping me up tonight is that the AV on my myth recordings tonight are mysteriously out of sync. I’m not the one to blame this time, though, I really haven’t touched anything in a good while. I am curious what’s throwing it off, though.

Well, I started playing with my TV tuner card in my desktop (a Lifeview Flyvideo 3000), and recorded a few 30-second spots on the same channel to see if it was still doing it. Well, everything seemed fine, so I guess it was just a fluke or those shows.

Well, I was recording the TV using MEncoder instead of Myth because I’ve been having problems with my tuner card and MythTV not recording the audio. That idea lead me to start thinking why I don’t use my TV tuner card instead of my Plextor ConvertX. Now, I love my little USB MPEG4 hardware recorder, but it does have its drawbacks. The USB2 requirement is one, which really isn’t a big deal unless you want to stick it in an older computer. Even then, you’d buy a USB2 PCI card, which is easy enough to do, but if you’re running low on PCI slots it becomes a hassle. The second thing is that it *only* works with MythTv. That of course is noone’s fault, it’s just that myth is the only project that has written software that will interface with the device.

The problem is that it gets frustrating sometimes with myth being my *only* option. If I was using a TV tuner card I could easily switch between mythtv and freevo and mencoder plus a cron job and tweak and play around with it much easier and have far more options when something is going wrong.

So now I’m starting to think about swapping out my TV tuner card to be my main recording device. I think the main reason I got the Plextor was to take the recording load off of the computer. Well, with an amd64 box, the CPU usage is not even noticable. It runs between 15 to 55 percent depending on how high a quality I’m recording to and the resolution. A thirty-second clip of using mencoder to record to MPEG4 at 2200 video bitrate, and MP3 VBR audio with 720×578 resolution (the highest my card supports) hovers at around 40 percent. That’s really good. Especially considering you can record this stuff at 320×240 for much lower CPU usage (about 13%) and that will still be really good quality for watching it on a standard TV display.

I gotta say, too, that the video quality on this card rocks. It’s the best I’ve ever seen. I daresay it’s even better than the Plextor, even though they both use the same Philips tuner … dunno why, just seems that way. Plus, my card records in stereo. And I don’t need a stupid audio cable from my TV tuner card to my audio input to record sound. Not to mention I bought this thing years ago, and it *still* runs like a champ.

Sometimes I wonder why I make things so complex.