2 * Copyright 2008, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Alexandre Deckner <alex@zappotek.com>
9 #include "VideoFileTexture.h"
13 #include <MediaFile.h>
14 #include <MediaTrack.h>
22 VideoFileTexture::VideoFileTexture(const char* fileName
)
33 VideoFileTexture::~VideoFileTexture()
37 if (fMediaFile
!= NULL
) {
38 fMediaFile
->ReleaseAllTracks();
39 fMediaFile
->CloseFile();
46 VideoFileTexture::_Load(const char* fileName
)
48 BEntry
entry(fileName
);
52 fMediaFile
= new BMediaFile(&ref
);
53 status_t err
= fMediaFile
->InitCheck();
55 printf("cannot contruct BMediaFile object -- %s\n", strerror(err
));
59 int32 trackCount
= fMediaFile
->CountTracks();
61 for (int32 i
= 0; i
< trackCount
; i
++) {
62 BMediaTrack
* track
= fMediaFile
->TrackAt(i
);
64 printf("cannot contruct BMediaTrack object\n");
68 // get the encoded format
70 err
= track
->EncodedFormat(&format
);
72 printf("BMediaTrack::EncodedFormat error -- %s\n", strerror(err
));
76 if (format
.type
== B_MEDIA_ENCODED_VIDEO
) {
78 // allocate a bitmap large enough to contain the decoded frame.
79 BRect
bounds(0.0, 0.0,
80 format
.u
.encoded_video
.output
.display
.line_width
- 1.0,
81 format
.u
.encoded_video
.output
.display
.line_count
- 1.0);
82 fVideoBitmap
= new BBitmap(bounds
, B_RGB32
);
84 // specifiy the decoded format. we derive this information from
85 // the encoded format.
86 memset(&format
, 0, sizeof(media_format
));
87 format
.u
.raw_video
.last_active
= (int32
) (bounds
.Height() - 1.0);
88 format
.u
.raw_video
.orientation
= B_VIDEO_TOP_LEFT_RIGHT
;
89 format
.u
.raw_video
.pixel_width_aspect
= 1;
90 format
.u
.raw_video
.pixel_height_aspect
= 3;
91 format
.u
.raw_video
.display
.format
= fVideoBitmap
->ColorSpace();
92 format
.u
.raw_video
.display
.line_width
= (int32
) bounds
.Width();
93 format
.u
.raw_video
.display
.line_count
= (int32
) bounds
.Height();
94 format
.u
.raw_video
.display
.bytes_per_row
95 = fVideoBitmap
->BytesPerRow();
97 err
= fVideoTrack
->DecodedFormat(&format
);
99 printf("error with BMediaTrack::DecodedFormat() -- %s\n",
105 glGenTextures(1, &fId
);
106 glBindTexture(GL_TEXTURE_2D
, fId
);
107 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
108 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
109 glTexImage2D(GL_TEXTURE_2D
, 0, 4,
110 (int) fVideoBitmap
->Bounds().Width() + 1,
111 (int) fVideoBitmap
->Bounds().Height() + 1,
112 0, GL_BGRA
, GL_UNSIGNED_BYTE
, fVideoBitmap
->Bits());
119 VideoFileTexture::Update(float /*dt*/) {
121 int64 frameCount
= 0;
124 = fVideoTrack
->ReadFrames(fVideoBitmap
->Bits(), &frameCount
, &mh
);
126 printf("BMediaTrack::ReadFrames error -- %s\n", strerror(err
));
130 glBindTexture(GL_TEXTURE_2D
, fId
);
131 glTexSubImage2D(GL_TEXTURE_2D
, 0, 0, 0,
132 (int)fVideoBitmap
->Bounds().Width() + 1,
133 (int)fVideoBitmap
->Bounds().Height() + 1,
134 GL_BGRA
, GL_UNSIGNED_BYTE
, fVideoBitmap
->Bits());